diff --git a/src/avalanche/test/CMakeLists.txt b/src/avalanche/test/CMakeLists.txt --- a/src/avalanche/test/CMakeLists.txt +++ b/src/avalanche/test/CMakeLists.txt @@ -20,6 +20,7 @@ proof_tests.cpp proofcomparator_tests.cpp proofpool_tests.cpp + voterecord_tests.cpp ) target_link_libraries(test-avalanche server testutil) diff --git a/src/avalanche/test/processor_tests.cpp b/src/avalanche/test/processor_tests.cpp --- a/src/avalanche/test/processor_tests.cpp +++ b/src/avalanche/test/processor_tests.cpp @@ -314,112 +314,6 @@ // FIXME A std::tuple can be used instead of boost::mpl::list after boost 1.67 using VoteItemProviders = boost::mpl::list; -#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) { CBlockIndex index; CBlockIndex *pindex = &index; diff --git a/src/avalanche/test/voterecord_tests.cpp b/src/avalanche/test/voterecord_tests.cpp new file mode 100644 --- /dev/null +++ b/src/avalanche/test/voterecord_tests.cpp @@ -0,0 +1,121 @@ +// Copyright (c) 2018-2022 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include + +#include + +using namespace avalanche; + +BOOST_FIXTURE_TEST_SUITE(voterecord_tests, TestingSetup) + +#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_SUITE_END()