Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/peermanager_tests.cpp
Show First 20 Lines • Show All 456 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(node_binding_reorg) { | ||||
ProofBuilder pb(0, 0, CKey::MakeCompressedKey()); | ProofBuilder pb(0, 0, CKey::MakeCompressedKey()); | ||||
auto key = CKey::MakeCompressedKey(); | auto key = CKey::MakeCompressedKey(); | ||||
const CScript script = GetScriptForDestination(PKHash(key.GetPubKey())); | const CScript script = GetScriptForDestination(PKHash(key.GetPubKey())); | ||||
COutPoint utxo(TxId(GetRandHash()), 0); | COutPoint utxo(TxId(GetRandHash()), 0); | ||||
Amount amount = 1 * COIN; | Amount amount = 1 * COIN; | ||||
const int height = 1234; | const int height = 1234; | ||||
BOOST_CHECK(pb.addUTXO(utxo, amount, height, false, key)); | BOOST_CHECK(pb.addUTXO(utxo, amount, height, false, key)); | ||||
auto proof = std::make_shared<Proof>(pb.build()); | auto proof = pb.build(); | ||||
const ProofId &proofid = proof->getId(); | const ProofId &proofid = proof->getId(); | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); | CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); | ||||
coins.AddCoin(utxo, Coin(CTxOut(amount, script), height, false), false); | coins.AddCoin(utxo, Coin(CTxOut(amount, script), height, false), false); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(proof_conflict) { | ||||
avalanche::PeerManager pm; | avalanche::PeerManager pm; | ||||
CKey masterKey = CKey::MakeCompressedKey(); | CKey masterKey = CKey::MakeCompressedKey(); | ||||
const auto getPeerId = [&](const std::vector<COutPoint> &outpoints) { | const auto getPeerId = [&](const std::vector<COutPoint> &outpoints) { | ||||
ProofBuilder pb(0, 0, masterKey); | ProofBuilder pb(0, 0, masterKey); | ||||
for (const auto &o : outpoints) { | for (const auto &o : outpoints) { | ||||
BOOST_CHECK(pb.addUTXO(o, v, height, false, key)); | BOOST_CHECK(pb.addUTXO(o, v, height, false, key)); | ||||
} | } | ||||
return pm.getPeerId(std::make_shared<Proof>(pb.build())); | return pm.getPeerId(pb.build()); | ||||
}; | }; | ||||
// Add one peer. | // Add one peer. | ||||
const PeerId peer1 = getPeerId({COutPoint(txid1, 0)}); | const PeerId peer1 = getPeerId({COutPoint(txid1, 0)}); | ||||
BOOST_CHECK(peer1 != NO_PEER); | BOOST_CHECK(peer1 != NO_PEER); | ||||
// Same proof, same peer. | // Same proof, same peer. | ||||
BOOST_CHECK_EQUAL(getPeerId({COutPoint(txid1, 0)}), peer1); | BOOST_CHECK_EQUAL(getPeerId({COutPoint(txid1, 0)}), peer1); | ||||
Show All 13 Lines | BOOST_AUTO_TEST_CASE(proof_conflict) { | ||||
const PeerId peer4 = getPeerId({COutPoint(txid1, 2), COutPoint(txid2, 2)}); | const PeerId peer4 = getPeerId({COutPoint(txid1, 2), COutPoint(txid2, 2)}); | ||||
BOOST_CHECK(peer4 != NO_PEER && peer4 != peer1); | BOOST_CHECK(peer4 != NO_PEER && peer4 != peer1); | ||||
// Duplicated input. | // Duplicated input. | ||||
{ | { | ||||
ProofBuilder pb(0, 0, CKey::MakeCompressedKey()); | ProofBuilder pb(0, 0, CKey::MakeCompressedKey()); | ||||
COutPoint o(txid1, 3); | COutPoint o(txid1, 3); | ||||
BOOST_CHECK(pb.addUTXO(o, v, height, false, key)); | BOOST_CHECK(pb.addUTXO(o, v, height, false, key)); | ||||
PeerId peerid = pm.getPeerId(std::make_shared<Proof>( | PeerId peerid = | ||||
TestProofBuilder::buildDuplicatedStakes(pb))); | pm.getPeerId(TestProofBuilder::buildDuplicatedStakes(pb)); | ||||
BOOST_CHECK_EQUAL(peerid, NO_PEER); | BOOST_CHECK_EQUAL(peerid, NO_PEER); | ||||
} | } | ||||
// Multiple inputs, collision on first input. | // Multiple inputs, collision on first input. | ||||
BOOST_CHECK_EQUAL(getPeerId({COutPoint(txid1, 0), COutPoint(txid2, 4)}), | BOOST_CHECK_EQUAL(getPeerId({COutPoint(txid1, 0), COutPoint(txid2, 4)}), | ||||
NO_PEER); | NO_PEER); | ||||
// Mutliple inputs, collision on second input. | // Mutliple inputs, collision on second input. | ||||
Show All 17 Lines | BOOST_AUTO_TEST_CASE(orphan_proofs) { | ||||
const Amount v = 5 * COIN; | const Amount v = 5 * COIN; | ||||
const int height = 1234; | const int height = 1234; | ||||
const int wrongHeight = 12345; | const int wrongHeight = 12345; | ||||
const auto makeProof = [&](const COutPoint &outpoint, const int h) { | const auto makeProof = [&](const COutPoint &outpoint, const int h) { | ||||
ProofBuilder pb(0, 0, CKey::MakeCompressedKey()); | ProofBuilder pb(0, 0, CKey::MakeCompressedKey()); | ||||
BOOST_CHECK(pb.addUTXO(outpoint, v, h, false, key)); | BOOST_CHECK(pb.addUTXO(outpoint, v, h, false, key)); | ||||
return std::make_shared<Proof>(pb.build()); | return pb.build(); | ||||
}; | }; | ||||
auto proof1 = makeProof(outpoint1, height); | auto proof1 = makeProof(outpoint1, height); | ||||
auto proof2 = makeProof(outpoint2, height); | auto proof2 = makeProof(outpoint2, height); | ||||
auto proof3 = makeProof(outpoint3, wrongHeight); | auto proof3 = makeProof(outpoint3, wrongHeight); | ||||
const Coin coin = Coin(CTxOut(v, script), height, false); | const Coin coin = Coin(CTxOut(v, script), height, false); | ||||
▲ Show 20 Lines • Show All 185 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(conflicting_proof_rescan) { | ||||
ProofRef proofToInvalidate; | ProofRef proofToInvalidate; | ||||
{ | { | ||||
ProofBuilder pb(0, 0, key); | ProofBuilder pb(0, 0, key); | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
pb.addUTXO(conflictingOutpoint, amount, height, is_coinbase, key)); | pb.addUTXO(conflictingOutpoint, amount, height, is_coinbase, key)); | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
pb.addUTXO(outpointToSend, amount, height, is_coinbase, key)); | pb.addUTXO(outpointToSend, amount, height, is_coinbase, key)); | ||||
proofToInvalidate = std::make_shared<Proof>(pb.build()); | proofToInvalidate = pb.build(); | ||||
} | } | ||||
BOOST_CHECK(pm.registerProof(proofToInvalidate)); | BOOST_CHECK(pm.registerProof(proofToInvalidate)); | ||||
ProofRef conflictingProof; | ProofRef conflictingProof; | ||||
{ | { | ||||
ProofBuilder pb(0, 0, key); | ProofBuilder pb(0, 0, key); | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
pb.addUTXO(conflictingOutpoint, amount, height, is_coinbase, key)); | pb.addUTXO(conflictingOutpoint, amount, height, is_coinbase, key)); | ||||
BOOST_CHECK(pb.addUTXO(addCoin(), amount, height, is_coinbase, key)); | BOOST_CHECK(pb.addUTXO(addCoin(), amount, height, is_coinbase, key)); | ||||
conflictingProof = std::make_shared<Proof>(pb.build()); | conflictingProof = pb.build(); | ||||
} | } | ||||
BOOST_CHECK(!pm.registerProof(conflictingProof)); | BOOST_CHECK(!pm.registerProof(conflictingProof)); | ||||
// The conflicting proof is orphaned | // The conflicting proof is orphaned | ||||
BOOST_CHECK(pm.isOrphan(conflictingProof->getId())); | BOOST_CHECK(pm.isOrphan(conflictingProof->getId())); | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
Show All 14 Lines |