Changeset View
Changeset View
Standalone View
Standalone View
src/test/avalanche_tests.cpp
// Copyright (c) 2010 The Bitcoin developers | // Copyright (c) 2010 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "avalanche.h" | #include "avalanche.h" | ||||
#include "scheduler.h" | #include "scheduler.h" | ||||
#include "test/test_bitcoin.h" | #include "test/test_bitcoin.h" | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
struct AvalancheTest { | |||||
static std::vector<CInv> getInvsForNextPoll(const AvalancheProcessor &p) { | |||||
return p.getInvsForNextPoll(); | |||||
} | |||||
}; | |||||
BOOST_FIXTURE_TEST_SUITE(avalanche_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(avalanche_tests, BasicTestingSetup) | ||||
#define REGISTER_VOTE_AND_CHECK(vr, vote, state, finalized, confidence) \ | #define REGISTER_VOTE_AND_CHECK(vr, vote, state, finalized, confidence) \ | ||||
vr.registerVote(vote); \ | vr.registerVote(vote); \ | ||||
BOOST_CHECK_EQUAL(vr.isValid(), state); \ | BOOST_CHECK_EQUAL(vr.isValid(), state); \ | ||||
BOOST_CHECK_EQUAL(vr.hasFinalized(), finalized); \ | BOOST_CHECK_EQUAL(vr.hasFinalized(), finalized); \ | ||||
BOOST_CHECK_EQUAL(vr.getConfidence(), confidence); | BOOST_CHECK_EQUAL(vr.getConfidence(), confidence); | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | REGISTER_VOTE_AND_CHECK(vr, true, false, true, | ||||
AVALANCHE_FINALIZATION_SCORE); | AVALANCHE_FINALIZATION_SCORE); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(block_register) { | BOOST_AUTO_TEST_CASE(block_register) { | ||||
AvalancheProcessor p; | AvalancheProcessor p; | ||||
CBlockIndex index; | CBlockIndex index; | ||||
// Make sure the block has a hash. | // Make sure the block has a hash. | ||||
const uint256 zeroHash; | static const uint256 blockHash(uint256S( | ||||
index.phashBlock = &zeroHash; | "abcdef0000000000000000000000000000000000000000000000000000000001")); | ||||
index.phashBlock = &blockHash; | |||||
// Querying for random block returns false. | // Querying for random block returns false. | ||||
BOOST_CHECK(!p.isAccepted(&index)); | BOOST_CHECK(!p.isAccepted(&index)); | ||||
BOOST_CHECK(!p.hasFinalized(&index)); | BOOST_CHECK(!p.hasFinalized(&index)); | ||||
// Newly added blocks are also considered rejected. | // Add a new block. Check it is added to the polls. | ||||
BOOST_CHECK(p.addBlockToReconcile(&index)); | BOOST_CHECK(p.addBlockToReconcile(&index)); | ||||
auto invs = AvalancheTest::getInvsForNextPoll(p); | |||||
BOOST_CHECK_EQUAL(invs.size(), 1); | |||||
BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | |||||
BOOST_CHECK(invs[0].hash == blockHash); | |||||
// Newly added blocks are also considered rejected. | |||||
BOOST_CHECK(!p.isAccepted(&index)); | BOOST_CHECK(!p.isAccepted(&index)); | ||||
BOOST_CHECK(!p.hasFinalized(&index)); | BOOST_CHECK(!p.hasFinalized(&index)); | ||||
// Let's vote for this block a few times. | // Let's vote for this block a few times. | ||||
AvalancheResponse resp{{AvalancheVote(zeroHash, 0)}}; | AvalancheResponse resp{{AvalancheVote(blockHash, 0)}}; | ||||
for (int i = 0; i < 5; i++) { | for (int i = 0; i < 5; i++) { | ||||
p.registerVotes(resp); | p.registerVotes(resp); | ||||
BOOST_CHECK(!p.isAccepted(&index)); | BOOST_CHECK(!p.isAccepted(&index)); | ||||
BOOST_CHECK(!p.hasFinalized(&index)); | BOOST_CHECK(!p.hasFinalized(&index)); | ||||
} | } | ||||
// Now it is accepeted, but we can vote for it numerous times. | // Now it is accepeted, but we can vote for it numerous times. | ||||
for (int i = 0; i < AVALANCHE_FINALIZATION_SCORE; i++) { | for (int i = 0; i < AVALANCHE_FINALIZATION_SCORE; i++) { | ||||
p.registerVotes(resp); | p.registerVotes(resp); | ||||
BOOST_CHECK(p.isAccepted(&index)); | BOOST_CHECK(p.isAccepted(&index)); | ||||
BOOST_CHECK(!p.hasFinalized(&index)); | BOOST_CHECK(!p.hasFinalized(&index)); | ||||
} | } | ||||
// As long as it is not finalized, we poll. | |||||
invs = AvalancheTest::getInvsForNextPoll(p); | |||||
BOOST_CHECK_EQUAL(invs.size(), 1); | |||||
BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | |||||
BOOST_CHECK(invs[0].hash == blockHash); | |||||
// Now finalize the decision. | // Now finalize the decision. | ||||
resp = {{AvalancheVote(zeroHash, 1)}}; | resp = {{AvalancheVote(blockHash, 1)}}; | ||||
p.registerVotes(resp); | p.registerVotes(resp); | ||||
BOOST_CHECK(p.isAccepted(&index)); | BOOST_CHECK(p.isAccepted(&index)); | ||||
BOOST_CHECK(p.hasFinalized(&index)); | BOOST_CHECK(p.hasFinalized(&index)); | ||||
// Once the decision is finalized, there is no poll for it. | |||||
invs = AvalancheTest::getInvsForNextPoll(p); | |||||
BOOST_CHECK_EQUAL(invs.size(), 0); | |||||
// Now let's undo this and finalize rejection. | // Now let's undo this and finalize rejection. | ||||
for (int i = 0; i < 5; i++) { | for (int i = 0; i < 5; i++) { | ||||
p.registerVotes(resp); | p.registerVotes(resp); | ||||
BOOST_CHECK(p.isAccepted(&index)); | BOOST_CHECK(p.isAccepted(&index)); | ||||
BOOST_CHECK(p.hasFinalized(&index)); | BOOST_CHECK(p.hasFinalized(&index)); | ||||
} | } | ||||
// Now it is rejected, but we can vote for it numerous times. | // Now it is rejected, but we can vote for it numerous times. | ||||
for (int i = 0; i < AVALANCHE_FINALIZATION_SCORE; i++) { | for (int i = 0; i < AVALANCHE_FINALIZATION_SCORE; i++) { | ||||
p.registerVotes(resp); | p.registerVotes(resp); | ||||
BOOST_CHECK(!p.isAccepted(&index)); | BOOST_CHECK(!p.isAccepted(&index)); | ||||
BOOST_CHECK(!p.hasFinalized(&index)); | BOOST_CHECK(!p.hasFinalized(&index)); | ||||
} | } | ||||
// As long as it is not finalized, we poll. | |||||
invs = AvalancheTest::getInvsForNextPoll(p); | |||||
BOOST_CHECK_EQUAL(invs.size(), 1); | |||||
BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | |||||
BOOST_CHECK(invs[0].hash == blockHash); | |||||
// Now finalize the decision. | // Now finalize the decision. | ||||
p.registerVotes(resp); | p.registerVotes(resp); | ||||
BOOST_CHECK(!p.isAccepted(&index)); | BOOST_CHECK(!p.isAccepted(&index)); | ||||
BOOST_CHECK(p.hasFinalized(&index)); | BOOST_CHECK(p.hasFinalized(&index)); | ||||
// Once the decision is finalized, there is no poll for it. | |||||
invs = AvalancheTest::getInvsForNextPoll(p); | |||||
BOOST_CHECK_EQUAL(invs.size(), 0); | |||||
// Adding the block twice does nothing. | // Adding the block twice does nothing. | ||||
BOOST_CHECK(!p.addBlockToReconcile(&index)); | BOOST_CHECK(!p.addBlockToReconcile(&index)); | ||||
BOOST_CHECK(!p.isAccepted(&index)); | BOOST_CHECK(!p.isAccepted(&index)); | ||||
BOOST_CHECK(p.hasFinalized(&index)); | BOOST_CHECK(p.hasFinalized(&index)); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(event_loop) { | BOOST_AUTO_TEST_CASE(event_loop) { | ||||
AvalancheProcessor p; | AvalancheProcessor p; | ||||
▲ Show 20 Lines • Show All 53 Lines • Show Last 20 Lines |