15 #include <test/util/setup_common.h>
17 #include <boost/test/unit_test.hpp>
30 for (
size_t i = 0; i < 10; i++) {
33 ProofPool::AddProofStatus::SUCCEED);
34 proofIds.insert(proof->getId());
40 for (
auto proofid : proofIds) {
50 std::vector<ProofRef> proofs;
51 for (
size_t i = 0; i < 10; i++) {
55 ProofPool::AddProofStatus::SUCCEED);
60 for (
size_t j = 0; j < 10; j++) {
62 ProofPool::AddProofStatus::DUPLICATED);
66 proofs.push_back(std::move(proof));
72 auto buildProofWithSequence = [&](uint64_t sequence) {
75 pb.
addUTXO(conflictingOutpoint, 10 *
COIN, 123456,
false, key));
79 auto proof_seq10 = buildProofWithSequence(10);
81 ProofPool::AddProofStatus::SUCCEED);
84 proofs.push_back(std::move(proof_seq10));
86 auto proof_seq20 = buildProofWithSequence(20);
88 ProofPool::AddProofStatus::REJECTED);
93 for (
size_t i = 0; i < 10; i++) {
99 for (
auto proof : proofs) {
118 bool hasPeer =
false;
124 std::set<ProofRef, ProofRefComparatorByAddress> poolProofs;
125 for (
size_t i = 0; i < 10; i++) {
128 ProofPool::AddProofStatus::SUCCEED);
129 poolProofs.insert(std::move(proof));
137 std::set<ProofRef, ProofRefComparatorByAddress> pmProofs;
139 BOOST_CHECK_EQUAL_COLLECTIONS(poolProofs.begin(), poolProofs.end(),
140 pmProofs.begin(), pmProofs.end());
153 auto buildProofWithSequenceAndOutpoints =
154 [&](uint64_t sequence,
const std::vector<COutPoint> &outpoints) {
156 for (
const COutPoint &outpoint : outpoints) {
158 pb.
addUTXO(outpoint, 10 *
COIN, 123456,
false, key));
168 auto proof_seq10 = buildProofWithSequenceAndOutpoints(10, {outpoint1});
169 auto proof_seq20 = buildProofWithSequenceAndOutpoints(20, {outpoint2});
170 auto proof_seq30 = buildProofWithSequenceAndOutpoints(30, {outpoint3});
173 ProofPool::AddProofStatus::SUCCEED);
179 ProofPool::AddProofStatus::SUCCEED);
185 ProofPool::AddProofStatus::SUCCEED);
191 auto proof_seq123 = buildProofWithSequenceAndOutpoints(
192 123, {outpoint1, outpoint2, outpoint3});
194 proof_seq10, proof_seq20, proof_seq30};
201 ProofPool::AddProofStatus::REJECTED);
202 BOOST_CHECK_EQUAL_COLLECTIONS(
203 conflictingProofs.begin(), conflictingProofs.end(),
204 expectedConflictingProofs.begin(), expectedConflictingProofs.end());
213 conflictingProofs.clear();
216 ProofPool::AddProofStatus::SUCCEED);
217 BOOST_CHECK_EQUAL_COLLECTIONS(
218 conflictingProofs.begin(), conflictingProofs.end(),
219 expectedConflictingProofs.begin(), expectedConflictingProofs.end());
234 auto buildProofWithSequence = [&](uint64_t sequence) {
237 pb.
addUTXO(conflictingOutpoint, 10 *
COIN, 123456,
false, key));
241 auto proofSeq10 = buildProofWithSequence(10);
242 auto proofSeq20 = buildProofWithSequence(20);
243 auto proofSeq30 = buildProofWithSequence(30);
246 ProofPool::AddProofStatus::SUCCEED);
250 auto getRandomConflictingProofSet = [&active_chainstate]() {
258 auto checkConflictingProofs =
261 BOOST_CHECK_EQUAL_COLLECTIONS(conflictingProofs.begin(),
262 conflictingProofs.end(),
263 expectedConflictingProofs.begin(),
264 expectedConflictingProofs.end());
269 auto conflictingProofs = getRandomConflictingProofSet();
272 ProofPool::AddProofStatus::DUPLICATED);
273 checkConflictingProofs(conflictingProofs, {});
278 auto conflictingProofs = getRandomConflictingProofSet();
281 ProofPool::AddProofStatus::DUPLICATED);
282 checkConflictingProofs(conflictingProofs, {});
287 auto conflictingProofs = getRandomConflictingProofSet();
290 ProofPool::AddProofStatus::REJECTED);
291 checkConflictingProofs(conflictingProofs, {proofSeq20});
296 auto conflictingProofs = getRandomConflictingProofSet();
299 ProofPool::AddProofStatus::REJECTED);
300 checkConflictingProofs(conflictingProofs, {proofSeq20});
305 auto conflictingProofs = getRandomConflictingProofSet();
308 ProofPool::AddProofStatus::REJECTED);
309 checkConflictingProofs(conflictingProofs, {proofSeq20});
314 auto conflictingProofs = getRandomConflictingProofSet();
317 ProofPool::AddProofStatus::SUCCEED);
318 checkConflictingProofs(conflictingProofs, {proofSeq20});
325 for (
size_t i = 0; i < 10; i++) {
331 for (
size_t i = 0; i < 10; i++) {
334 ProofPool::AddProofStatus::SUCCEED);
336 auto retrievedProof = testPool.
getProof(proof->getId());
337 BOOST_CHECK_NE(retrievedProof,
nullptr);
347 auto buildProofWithRandomOutpoints = [&](uint32_t score) {
348 int numOutpoints = InsecureRand32() % 10 + 1;
350 for (
int i = 0; i < numOutpoints; i++) {
352 if (i == numOutpoints - 1) {
355 (int64_t(score) *
COIN) / 100 - (numOutpoints - 1) *
COIN;
364 for (
int i = 9; i >= 0; i--) {
367 ProofPool::AddProofStatus::SUCCEED);
379 for (
size_t i = 1; i < 10; i++) {
382 ProofPool::AddProofStatus::SUCCEED);
387 lowestScoreProof->getId());
393 for (
int scoreCount = 1; scoreCount < 10; scoreCount++) {
394 for (
size_t i = 0; i < 2; i++) {
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.
An outpoint - a combination of a transaction hash and an index n into its vout.
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)
const ProofId & getId() const
uint32_t getScore() 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)
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_AUTO_TEST_SUITE_END()
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
BOOST_AUTO_TEST_CASE(get_proof_ids)
uint256 GetRandHash() noexcept
A TxId is the identifier of a transaction.