Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/peermanager_tests.cpp
Show All 25 Lines | struct TestPeerManager { | ||||
} | } | ||||
static bool isNodePending(const PeerManager &pm, NodeId nodeid) { | static bool isNodePending(const PeerManager &pm, NodeId nodeid) { | ||||
auto &pendingNodesView = pm.pendingNodes.get<by_nodeid>(); | auto &pendingNodesView = pm.pendingNodes.get<by_nodeid>(); | ||||
return pendingNodesView.find(nodeid) != pendingNodesView.end(); | return pendingNodesView.find(nodeid) != pendingNodesView.end(); | ||||
} | } | ||||
}; | }; | ||||
} // namespace | } // namespace | ||||
class TestConflictingProofHandler | |||||
: public PeerManager::ConflictingProofHandler { | |||||
public: | |||||
std::shared_ptr<Proof> lastProof; | |||||
void onConflictingProof(const std::shared_ptr<Proof> &proof, | |||||
bool accepted) override { | |||||
lastProof = proof; | |||||
} | |||||
}; | |||||
} // namespace avalanche | } // namespace avalanche | ||||
BOOST_FIXTURE_TEST_SUITE(peermanager_tests, TestingSetup) | BOOST_FIXTURE_TEST_SUITE(peermanager_tests, TestingSetup) | ||||
BOOST_AUTO_TEST_CASE(select_peer_linear) { | BOOST_AUTO_TEST_CASE(select_peer_linear) { | ||||
// No peers. | // No peers. | ||||
BOOST_CHECK_EQUAL(selectPeerImpl({}, 0, 0), NO_PEER); | BOOST_CHECK_EQUAL(selectPeerImpl({}, 0, 0), NO_PEER); | ||||
BOOST_CHECK_EQUAL(selectPeerImpl({}, 1, 3), NO_PEER); | BOOST_CHECK_EQUAL(selectPeerImpl({}, 1, 3), NO_PEER); | ||||
▲ Show 20 Lines • Show All 748 Lines • ▼ Show 20 Lines | const std::string badProofHex( | ||||
"3e95b02cacfd357b64e4fb6c92e92dd00"); | "3e95b02cacfd357b64e4fb6c92e92dd00"); | ||||
bilingual_str error; | bilingual_str error; | ||||
Proof badProof; | Proof badProof; | ||||
BOOST_CHECK(Proof::FromHex(badProof, badProofHex, error)); | BOOST_CHECK(Proof::FromHex(badProof, badProofHex, error)); | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
!pm.registerProof(std::make_shared<Proof>(std::move(badProof)))); | !pm.registerProof(std::make_shared<Proof>(std::move(badProof)))); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(conflicting_proof_handler) { | |||||
avalanche::PeerManager pm; | |||||
auto conflictingProofHandler = | |||||
std::make_shared<TestConflictingProofHandler>(); | |||||
auto handler = pm.handleConflictingProof(conflictingProofHandler); | |||||
const CKey key = CKey::MakeCompressedKey(); | |||||
const COutPoint outpoint(TxId(GetRandHash()), 0); | |||||
const Amount amount(10 * COIN); | |||||
const uint32_t height = 100; | |||||
const bool is_coinbase = false; | |||||
CScript script = GetScriptForDestination(PKHash(key.GetPubKey())); | |||||
{ | |||||
LOCK(cs_main); | |||||
CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); | |||||
coins.AddCoin(outpoint, | |||||
Coin(CTxOut(amount, script), height, is_coinbase), false); | |||||
} | |||||
auto buildProofWithSequence = [&](uint64_t sequence) { | |||||
ProofBuilder pb(sequence, GetRandInt(std::numeric_limits<int>::max()), | |||||
key); | |||||
BOOST_CHECK(pb.addUTXO(outpoint, amount, height, is_coinbase, key)); | |||||
return std::make_shared<Proof>(pb.build()); | |||||
}; | |||||
auto proof_base = buildProofWithSequence(0); | |||||
BOOST_CHECK_NE(pm.getPeerId(proof_base), NO_PEER); | |||||
BOOST_CHECK(!conflictingProofHandler->lastProof); | |||||
for (size_t i = 1; i < 100; i++) { | |||||
auto proof_conflicting = buildProofWithSequence(i); | |||||
BOOST_CHECK_EQUAL(pm.getPeerId(proof_conflicting), NO_PEER); | |||||
BOOST_CHECK_EQUAL(conflictingProofHandler->lastProof->getId(), | |||||
proof_conflicting->getId()); | |||||
} | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |