13#include <validation.h>
16#include <test/util/random.h>
17#include <test/util/setup_common.h>
19#include <boost/test/unit_test.hpp>
23BOOST_FIXTURE_TEST_SUITE(proofpool_tests, TestChain100Setup)
33 auto buildProof = [&](uint64_t sequence,
34 const std::vector<COutPoint> &outpoints) {
36 for (
const COutPoint &outpoint : outpoints) {
42 auto proofA = buildProof(1, {outpointA});
43 auto proofB = buildProof(1, {outpointB});
44 auto proofAB = buildProof(2, {outpointA, outpointB});
45 auto proofOther = buildProof(1, {otherOutpoint});
51 ProofPool::AddProofStatus::SUCCEED);
63 ProofPool::AddProofStatus::SUCCEED);
79 for (
size_t i = 0; i < 10; i++) {
82 ProofPool::AddProofStatus::SUCCEED);
83 proofIds.insert(proof->getId());
89 for (
auto proofid : proofIds) {
99 std::vector<ProofRef> proofs;
100 for (
size_t i = 0; i < 10; i++) {
104 ProofPool::AddProofStatus::SUCCEED);
109 for (
size_t j = 0; j < 10; j++) {
111 ProofPool::AddProofStatus::DUPLICATED);
115 proofs.push_back(std::move(proof));
121 auto buildProofWithSequence = [&](uint64_t sequence) {
124 pb.
addUTXO(conflictingOutpoint, 10 *
COIN, 123456,
false, key));
128 auto proof_seq10 = buildProofWithSequence(10);
130 ProofPool::AddProofStatus::SUCCEED);
133 proofs.push_back(std::move(proof_seq10));
135 auto proof_seq20 = buildProofWithSequence(20);
137 ProofPool::AddProofStatus::REJECTED);
142 for (
size_t i = 0; i < 10; i++) {
148 for (
auto proof : proofs) {
167 bool hasPeer =
false;
173 std::set<ProofRef, ProofRefComparatorByAddress> poolProofs;
174 for (
size_t i = 0; i < 10; i++) {
177 ProofPool::AddProofStatus::SUCCEED);
178 poolProofs.insert(std::move(proof));
186 std::set<ProofRef, ProofRefComparatorByAddress> pmProofs;
188 BOOST_CHECK_EQUAL_COLLECTIONS(poolProofs.begin(), poolProofs.end(),
189 pmProofs.begin(), pmProofs.end());
202 auto buildProofWithSequenceAndOutpoints =
203 [&](uint64_t sequence,
const std::vector<COutPoint> &outpoints) {
205 for (
const COutPoint &outpoint : outpoints) {
207 pb.
addUTXO(outpoint, 10 *
COIN, 123456,
false, key));
217 auto proof_seq10 = buildProofWithSequenceAndOutpoints(10, {outpoint1});
218 auto proof_seq20 = buildProofWithSequenceAndOutpoints(20, {outpoint2});
219 auto proof_seq30 = buildProofWithSequenceAndOutpoints(30, {outpoint3});
222 ProofPool::AddProofStatus::SUCCEED);
228 ProofPool::AddProofStatus::SUCCEED);
234 ProofPool::AddProofStatus::SUCCEED);
240 auto proof_seq123 = buildProofWithSequenceAndOutpoints(
241 123, {outpoint1, outpoint2, outpoint3});
243 proof_seq10, proof_seq20, proof_seq30};
250 ProofPool::AddProofStatus::REJECTED);
251 BOOST_CHECK_EQUAL_COLLECTIONS(
252 conflictingProofs.begin(), conflictingProofs.end(),
253 expectedConflictingProofs.begin(), expectedConflictingProofs.end());
262 conflictingProofs.clear();
265 ProofPool::AddProofStatus::SUCCEED);
266 BOOST_CHECK_EQUAL_COLLECTIONS(
267 conflictingProofs.begin(), conflictingProofs.end(),
268 expectedConflictingProofs.begin(), expectedConflictingProofs.end());
283 auto buildProofWithSequence = [&](uint64_t sequence) {
286 pb.
addUTXO(conflictingOutpoint, 10 *
COIN, 123456,
false, key));
290 auto proofSeq10 = buildProofWithSequence(10);
291 auto proofSeq20 = buildProofWithSequence(20);
292 auto proofSeq30 = buildProofWithSequence(30);
295 ProofPool::AddProofStatus::SUCCEED);
299 auto getRandomConflictingProofSet = [&active_chainstate]() {
307 auto checkConflictingProofs =
310 BOOST_CHECK_EQUAL_COLLECTIONS(conflictingProofs.begin(),
311 conflictingProofs.end(),
312 expectedConflictingProofs.begin(),
313 expectedConflictingProofs.end());
318 auto conflictingProofs = getRandomConflictingProofSet();
321 ProofPool::AddProofStatus::DUPLICATED);
322 checkConflictingProofs(conflictingProofs, {});
327 auto conflictingProofs = getRandomConflictingProofSet();
330 ProofPool::AddProofStatus::DUPLICATED);
331 checkConflictingProofs(conflictingProofs, {});
336 auto conflictingProofs = getRandomConflictingProofSet();
339 ProofPool::AddProofStatus::REJECTED);
340 checkConflictingProofs(conflictingProofs, {proofSeq20});
345 auto conflictingProofs = getRandomConflictingProofSet();
348 ProofPool::AddProofStatus::REJECTED);
349 checkConflictingProofs(conflictingProofs, {proofSeq20});
354 auto conflictingProofs = getRandomConflictingProofSet();
357 ProofPool::AddProofStatus::REJECTED);
358 checkConflictingProofs(conflictingProofs, {proofSeq20});
363 auto conflictingProofs = getRandomConflictingProofSet();
366 ProofPool::AddProofStatus::SUCCEED);
367 checkConflictingProofs(conflictingProofs, {proofSeq20});
374 for (
size_t i = 0; i < 10; i++) {
380 for (
size_t i = 0; i < 10; i++) {
383 ProofPool::AddProofStatus::SUCCEED);
385 auto retrievedProof = testPool.
getProof(proof->getId());
386 BOOST_CHECK_NE(retrievedProof,
nullptr);
396 auto buildProofWithRandomOutpoints = [&](uint32_t score) {
397 int numOutpoints = m_rng.rand32() % 10 + 1;
399 for (
int i = 0; i < numOutpoints; i++) {
401 if (i == numOutpoints - 1) {
404 (int64_t(score) *
COIN) / 100 - (numOutpoints - 1) *
COIN;
413 for (
int i = 9; i >= 0; i--) {
416 ProofPool::AddProofStatus::SUCCEED);
428 for (
size_t i = 1; i < 10; i++) {
431 ProofPool::AddProofStatus::SUCCEED);
436 lowestScoreProof->getId());
442 for (
int scoreCount = 1; scoreCount < 10; scoreCount++) {
443 for (
size_t i = 0; i < 2; i++) {
458BOOST_AUTO_TEST_SUITE_END()
static constexpr Amount COIN
#define Assert(val)
Identity function.
void ForceSetArg(const std::string &strArg, const std::string &strValue)
void ClearForcedArg(const std::string &strArg)
Remove a forced arg setting, used only in testing.
An encapsulated secp256k1 private key.
static CKey MakeCompressedKey()
Produce a valid compressed key.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
void forEachPeer(Callable &&func) const
bool addUTXO(COutPoint utxo, Amount amount, uint32_t height, bool is_coinbase, CKey key)
uint32_t getScore() const
const ProofId & getId() const
Map a proof to each utxo.
AddProofStatus addProofIfPreferred(const ProofRef &proof, ConflictingProofSet &conflictingProofs)
Attempt to add a proof to the pool.
AddProofStatus addProofIfNoConflict(const ProofRef &proof, ConflictingProofSet &conflictingProofs)
Attempt to add a proof to the pool, and fail if there is a conflict on any UTXO.
size_t countProofs() const
bool removeProof(ProofId proofid)
ProofRef getProof(const ProofId &proofid) const
std::set< ProofRef, ConflictingProofComparator > ConflictingProofSet
ProofRef getLowestScoreProof() const
std::unordered_set< ProofRef, SaltedProofHasher > rescan(PeerManager &peerManager)
ConflictingProofSet getConflicts(const ProofRef &proof) const
Return proofs in this pool that share any UTXO with proof.
ProofIdSet getProofIds() const
static constexpr Amount PROOF_DUST_THRESHOLD
Minimum amount per utxo.
const CScript UNSPENDABLE_ECREG_PAYOUT_SCRIPT
ProofRef buildRandomProof(Chainstate &active_chainstate, uint32_t score, int height, const CKey &masterKey)
constexpr uint32_t MIN_VALID_PROOF_SCORE
std::unordered_set< ProofId, SaltedProofIdHasher > ProofIdSet
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
BOOST_AUTO_TEST_CASE(get_conflicts)
uint256 GetRandHash() noexcept
========== CONVENIENCE FUNCTIONS FOR COMMONLY USED RANDOMNESS ==========
A TxId is the identifier of a transaction.