Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/processor_tests.cpp
Show First 20 Lines • Show All 308 Lines • ▼ Show 20 Lines | |||||
} // namespace | } // namespace | ||||
BOOST_FIXTURE_TEST_SUITE(processor_tests, AvalancheTestingSetup) | BOOST_FIXTURE_TEST_SUITE(processor_tests, AvalancheTestingSetup) | ||||
// FIXME A std::tuple can be used instead of boost::mpl::list after boost 1.67 | // FIXME A std::tuple can be used instead of boost::mpl::list after boost 1.67 | ||||
using VoteItemProviders = boost::mpl::list<BlockProvider, ProofProvider>; | using VoteItemProviders = boost::mpl::list<BlockProvider, ProofProvider>; | ||||
#define REGISTER_VOTE_AND_CHECK(vr, vote, state, finalized, confidence) \ | |||||
vr.registerVote(NO_NODE, vote); \ | |||||
BOOST_CHECK_EQUAL(vr.isAccepted(), state); \ | |||||
BOOST_CHECK_EQUAL(vr.hasFinalized(), finalized); \ | |||||
BOOST_CHECK_EQUAL(vr.getConfidence(), confidence); | |||||
BOOST_AUTO_TEST_CASE(vote_record) { | |||||
VoteRecord vraccepted(true); | |||||
// Check initial state. | |||||
BOOST_CHECK_EQUAL(vraccepted.isAccepted(), true); | |||||
BOOST_CHECK_EQUAL(vraccepted.hasFinalized(), false); | |||||
BOOST_CHECK_EQUAL(vraccepted.getConfidence(), 0); | |||||
VoteRecord vr(false); | |||||
// Check initial state. | |||||
BOOST_CHECK_EQUAL(vr.isAccepted(), false); | |||||
BOOST_CHECK_EQUAL(vr.hasFinalized(), false); | |||||
BOOST_CHECK_EQUAL(vr.getConfidence(), 0); | |||||
// We need to register 6 positive votes before we start counting. | |||||
for (int i = 0; i < 6; i++) { | |||||
REGISTER_VOTE_AND_CHECK(vr, 0, false, false, 0); | |||||
} | |||||
// Next vote will flip state, and confidence will increase as long as we | |||||
// vote yes. | |||||
REGISTER_VOTE_AND_CHECK(vr, 0, true, false, 0); | |||||
// A single neutral vote do not change anything. | |||||
REGISTER_VOTE_AND_CHECK(vr, -1, true, false, 1); | |||||
for (int i = 2; i < 8; i++) { | |||||
REGISTER_VOTE_AND_CHECK(vr, 0, true, false, i); | |||||
} | |||||
// Two neutral votes will stall progress. | |||||
REGISTER_VOTE_AND_CHECK(vr, -1, true, false, 7); | |||||
REGISTER_VOTE_AND_CHECK(vr, -1, true, false, 7); | |||||
for (int i = 2; i < 8; i++) { | |||||
REGISTER_VOTE_AND_CHECK(vr, 0, true, false, 7); | |||||
} | |||||
// Now confidence will increase as long as we vote yes. | |||||
for (int i = 8; i < AVALANCHE_FINALIZATION_SCORE; i++) { | |||||
REGISTER_VOTE_AND_CHECK(vr, 0, true, false, i); | |||||
} | |||||
// The next vote will finalize the decision. | |||||
REGISTER_VOTE_AND_CHECK(vr, 1, true, true, AVALANCHE_FINALIZATION_SCORE); | |||||
// Now that we have two no votes, confidence stop increasing. | |||||
for (int i = 0; i < 5; i++) { | |||||
REGISTER_VOTE_AND_CHECK(vr, 1, true, true, | |||||
AVALANCHE_FINALIZATION_SCORE); | |||||
} | |||||
// Next vote will flip state, and confidence will increase as long as we | |||||
// vote no. | |||||
REGISTER_VOTE_AND_CHECK(vr, 1, false, false, 0); | |||||
// A single neutral vote do not change anything. | |||||
REGISTER_VOTE_AND_CHECK(vr, -1, false, false, 1); | |||||
for (int i = 2; i < 8; i++) { | |||||
REGISTER_VOTE_AND_CHECK(vr, 1, false, false, i); | |||||
} | |||||
// Two neutral votes will stall progress. | |||||
REGISTER_VOTE_AND_CHECK(vr, -1, false, false, 7); | |||||
REGISTER_VOTE_AND_CHECK(vr, -1, false, false, 7); | |||||
for (int i = 2; i < 8; i++) { | |||||
REGISTER_VOTE_AND_CHECK(vr, 1, false, false, 7); | |||||
} | |||||
// Now confidence will increase as long as we vote no. | |||||
for (int i = 8; i < AVALANCHE_FINALIZATION_SCORE; i++) { | |||||
REGISTER_VOTE_AND_CHECK(vr, 1, false, false, i); | |||||
} | |||||
// The next vote will finalize the decision. | |||||
REGISTER_VOTE_AND_CHECK(vr, 0, false, true, AVALANCHE_FINALIZATION_SCORE); | |||||
// Check that inflight accounting work as expected. | |||||
VoteRecord vrinflight(false); | |||||
for (int i = 0; i < 2 * AVALANCHE_MAX_INFLIGHT_POLL; i++) { | |||||
bool shouldPoll = vrinflight.shouldPoll(); | |||||
BOOST_CHECK_EQUAL(shouldPoll, i < AVALANCHE_MAX_INFLIGHT_POLL); | |||||
BOOST_CHECK_EQUAL(vrinflight.registerPoll(), shouldPoll); | |||||
} | |||||
// Clear various number of inflight requests and check everything behaves as | |||||
// expected. | |||||
for (int i = 1; i < AVALANCHE_MAX_INFLIGHT_POLL; i++) { | |||||
vrinflight.clearInflightRequest(i); | |||||
BOOST_CHECK(vrinflight.shouldPoll()); | |||||
for (int j = 1; j < i; j++) { | |||||
BOOST_CHECK(vrinflight.registerPoll()); | |||||
BOOST_CHECK(vrinflight.shouldPoll()); | |||||
} | |||||
BOOST_CHECK(vrinflight.registerPoll()); | |||||
BOOST_CHECK(!vrinflight.shouldPoll()); | |||||
} | |||||
} | |||||
BOOST_AUTO_TEST_CASE(block_update) { | BOOST_AUTO_TEST_CASE(block_update) { | ||||
CBlockIndex index; | CBlockIndex index; | ||||
CBlockIndex *pindex = &index; | CBlockIndex *pindex = &index; | ||||
std::set<VoteStatus> status{ | std::set<VoteStatus> status{ | ||||
VoteStatus::Invalid, | VoteStatus::Invalid, | ||||
VoteStatus::Rejected, | VoteStatus::Rejected, | ||||
VoteStatus::Accepted, | VoteStatus::Accepted, | ||||
▲ Show 20 Lines • Show All 973 Lines • Show Last 20 Lines |