15#include <test/util/random.h>
16#include <test/util/setup_common.h>
18#include <boost/test/unit_test.hpp>
22BOOST_FIXTURE_TEST_SUITE(proofpool_tests, TestChain100Setup)
31 for (
size_t i = 0; i < 10; i++) {
34 ProofPool::AddProofStatus::SUCCEED);
35 proofIds.insert(proof->getId());
41 for (
auto proofid : proofIds) {
51 std::vector<ProofRef> proofs;
52 for (
size_t i = 0; i < 10; i++) {
56 ProofPool::AddProofStatus::SUCCEED);
61 for (
size_t j = 0; j < 10; j++) {
63 ProofPool::AddProofStatus::DUPLICATED);
67 proofs.push_back(std::move(proof));
73 auto buildProofWithSequence = [&](uint64_t sequence) {
76 pb.
addUTXO(conflictingOutpoint, 10 *
COIN, 123456,
false, key));
80 auto proof_seq10 = buildProofWithSequence(10);
82 ProofPool::AddProofStatus::SUCCEED);
85 proofs.push_back(std::move(proof_seq10));
87 auto proof_seq20 = buildProofWithSequence(20);
89 ProofPool::AddProofStatus::REJECTED);
94 for (
size_t i = 0; i < 10; i++) {
100 for (
auto proof : proofs) {
119 bool hasPeer =
false;
125 std::set<ProofRef, ProofRefComparatorByAddress> poolProofs;
126 for (
size_t i = 0; i < 10; i++) {
129 ProofPool::AddProofStatus::SUCCEED);
130 poolProofs.insert(std::move(proof));
138 std::set<ProofRef, ProofRefComparatorByAddress> pmProofs;
140 BOOST_CHECK_EQUAL_COLLECTIONS(poolProofs.begin(), poolProofs.end(),
141 pmProofs.begin(), pmProofs.end());
154 auto buildProofWithSequenceAndOutpoints =
155 [&](uint64_t sequence,
const std::vector<COutPoint> &outpoints) {
157 for (
const COutPoint &outpoint : outpoints) {
159 pb.
addUTXO(outpoint, 10 *
COIN, 123456,
false, key));
169 auto proof_seq10 = buildProofWithSequenceAndOutpoints(10, {outpoint1});
170 auto proof_seq20 = buildProofWithSequenceAndOutpoints(20, {outpoint2});
171 auto proof_seq30 = buildProofWithSequenceAndOutpoints(30, {outpoint3});
174 ProofPool::AddProofStatus::SUCCEED);
180 ProofPool::AddProofStatus::SUCCEED);
186 ProofPool::AddProofStatus::SUCCEED);
192 auto proof_seq123 = buildProofWithSequenceAndOutpoints(
193 123, {outpoint1, outpoint2, outpoint3});
195 proof_seq10, proof_seq20, proof_seq30};
202 ProofPool::AddProofStatus::REJECTED);
203 BOOST_CHECK_EQUAL_COLLECTIONS(
204 conflictingProofs.begin(), conflictingProofs.end(),
205 expectedConflictingProofs.begin(), expectedConflictingProofs.end());
214 conflictingProofs.clear();
217 ProofPool::AddProofStatus::SUCCEED);
218 BOOST_CHECK_EQUAL_COLLECTIONS(
219 conflictingProofs.begin(), conflictingProofs.end(),
220 expectedConflictingProofs.begin(), expectedConflictingProofs.end());
235 auto buildProofWithSequence = [&](uint64_t sequence) {
238 pb.
addUTXO(conflictingOutpoint, 10 *
COIN, 123456,
false, key));
242 auto proofSeq10 = buildProofWithSequence(10);
243 auto proofSeq20 = buildProofWithSequence(20);
244 auto proofSeq30 = buildProofWithSequence(30);
247 ProofPool::AddProofStatus::SUCCEED);
251 auto getRandomConflictingProofSet = [&active_chainstate]() {
259 auto checkConflictingProofs =
262 BOOST_CHECK_EQUAL_COLLECTIONS(conflictingProofs.begin(),
263 conflictingProofs.end(),
264 expectedConflictingProofs.begin(),
265 expectedConflictingProofs.end());
270 auto conflictingProofs = getRandomConflictingProofSet();
273 ProofPool::AddProofStatus::DUPLICATED);
274 checkConflictingProofs(conflictingProofs, {});
279 auto conflictingProofs = getRandomConflictingProofSet();
282 ProofPool::AddProofStatus::DUPLICATED);
283 checkConflictingProofs(conflictingProofs, {});
288 auto conflictingProofs = getRandomConflictingProofSet();
291 ProofPool::AddProofStatus::REJECTED);
292 checkConflictingProofs(conflictingProofs, {proofSeq20});
297 auto conflictingProofs = getRandomConflictingProofSet();
300 ProofPool::AddProofStatus::REJECTED);
301 checkConflictingProofs(conflictingProofs, {proofSeq20});
306 auto conflictingProofs = getRandomConflictingProofSet();
309 ProofPool::AddProofStatus::REJECTED);
310 checkConflictingProofs(conflictingProofs, {proofSeq20});
315 auto conflictingProofs = getRandomConflictingProofSet();
318 ProofPool::AddProofStatus::SUCCEED);
319 checkConflictingProofs(conflictingProofs, {proofSeq20});
326 for (
size_t i = 0; i < 10; i++) {
332 for (
size_t i = 0; i < 10; i++) {
335 ProofPool::AddProofStatus::SUCCEED);
337 auto retrievedProof = testPool.
getProof(proof->getId());
338 BOOST_CHECK_NE(retrievedProof,
nullptr);
348 auto buildProofWithRandomOutpoints = [&](uint32_t score) {
349 int numOutpoints = InsecureRand32() % 10 + 1;
351 for (
int i = 0; i < numOutpoints; i++) {
353 if (i == numOutpoints - 1) {
356 (int64_t(score) *
COIN) / 100 - (numOutpoints - 1) *
COIN;
365 for (
int i = 9; i >= 0; i--) {
368 ProofPool::AddProofStatus::SUCCEED);
380 for (
size_t i = 1; i < 10; i++) {
383 ProofPool::AddProofStatus::SUCCEED);
388 lowestScoreProof->getId());
394 for (
int scoreCount = 1; scoreCount < 10; scoreCount++) {
395 for (
size_t i = 0; i < 2; i++) {
410BOOST_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)
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_proof_ids)
uint256 GetRandHash() noexcept
A TxId is the identifier of a transaction.