Changeset View
Changeset View
Standalone View
Standalone View
src/test/avalanche_tests.cpp
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(vote_record) { | ||||
VoteRecord vr(false); | VoteRecord vr(false); | ||||
// Check initial state. | // Check initial state. | ||||
BOOST_CHECK_EQUAL(vr.isAccepted(), false); | BOOST_CHECK_EQUAL(vr.isAccepted(), false); | ||||
BOOST_CHECK_EQUAL(vr.hasFinalized(), false); | BOOST_CHECK_EQUAL(vr.hasFinalized(), false); | ||||
BOOST_CHECK_EQUAL(vr.getConfidence(), 0); | BOOST_CHECK_EQUAL(vr.getConfidence(), 0); | ||||
// We register one vote for, which keep things at 4/4. | // We need to register 6 positive votes before we start counting. | ||||
REGISTER_VOTE_AND_CHECK(vr, true, false, false, 0); | for (int i = 0; i < 6; i++) { | ||||
REGISTER_VOTE_AND_CHECK(vr, 0, false, false, 0); | |||||
// One more and we are at 5/3. | } | ||||
REGISTER_VOTE_AND_CHECK(vr, true, false, false, 0); | |||||
// One more and we are at 5/3. | // Next vote will flip state, and confidence will increase as long as we | ||||
REGISTER_VOTE_AND_CHECK(vr, true, false, false, 0); | // vote yes. | ||||
REGISTER_VOTE_AND_CHECK(vr, 0, true, false, 0); | |||||
// One more and we are at 6/2. | // A single neutral vote do not change anything. | ||||
REGISTER_VOTE_AND_CHECK(vr, true, false, false, 0); | 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); | |||||
} | |||||
// One more and we are at 6/2. | // Two neutral votes will stall progress. | ||||
REGISTER_VOTE_AND_CHECK(vr, true, false, false, 0); | 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); | |||||
} | |||||
// Next vote will flip state, and confidence will increase as long as we | // Now confidence will increase as long as we vote yes. | ||||
// vote yes. | for (int i = 8; i < AVALANCHE_FINALIZATION_SCORE; i++) { | ||||
for (int i = 0; i < AVALANCHE_FINALIZATION_SCORE; i++) { | REGISTER_VOTE_AND_CHECK(vr, 0, true, false, i); | ||||
REGISTER_VOTE_AND_CHECK(vr, true, true, false, i); | |||||
} | } | ||||
// The next vote will finalize the decision. | // The next vote will finalize the decision. | ||||
REGISTER_VOTE_AND_CHECK(vr, false, true, true, | REGISTER_VOTE_AND_CHECK(vr, 1, true, true, AVALANCHE_FINALIZATION_SCORE); | ||||
AVALANCHE_FINALIZATION_SCORE); | |||||
// Now that we have two no votes, confidence stop increasing. | // Now that we have two no votes, confidence stop increasing. | ||||
for (int i = 0; i < 5; i++) { | for (int i = 0; i < 5; i++) { | ||||
REGISTER_VOTE_AND_CHECK(vr, false, true, true, | REGISTER_VOTE_AND_CHECK(vr, 1, true, true, | ||||
AVALANCHE_FINALIZATION_SCORE); | AVALANCHE_FINALIZATION_SCORE); | ||||
} | } | ||||
// Next vote will flip state, and confidence will increase as long as we | // Next vote will flip state, and confidence will increase as long as we | ||||
// vote no. | // vote no. | ||||
for (int i = 0; i < AVALANCHE_FINALIZATION_SCORE; i++) { | REGISTER_VOTE_AND_CHECK(vr, 1, false, false, 0); | ||||
REGISTER_VOTE_AND_CHECK(vr, false, false, false, i); | |||||
// 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. | // The next vote will finalize the decision. | ||||
REGISTER_VOTE_AND_CHECK(vr, true, false, true, | REGISTER_VOTE_AND_CHECK(vr, 0, false, true, AVALANCHE_FINALIZATION_SCORE); | ||||
AVALANCHE_FINALIZATION_SCORE); | |||||
} | } | ||||
BOOST_AUTO_TEST_CASE(block_update) { | BOOST_AUTO_TEST_CASE(block_update) { | ||||
CBlockIndex index; | CBlockIndex index; | ||||
CBlockIndex *pindex = &index; | CBlockIndex *pindex = &index; | ||||
std::set<AvalancheBlockUpdate::Status> status{ | std::set<AvalancheBlockUpdate::Status> status{ | ||||
AvalancheBlockUpdate::Status::Invalid, | AvalancheBlockUpdate::Status::Invalid, | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(block_register) { | ||||
BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | ||||
BOOST_CHECK(invs[0].hash == blockHash); | BOOST_CHECK(invs[0].hash == blockHash); | ||||
// Newly added blocks' state reflect the blockchain. | // Newly added blocks' state reflect the blockchain. | ||||
BOOST_CHECK(p.isAccepted(pindex)); | BOOST_CHECK(p.isAccepted(pindex)); | ||||
// Let's vote for this block a few times. | // Let's vote for this block a few times. | ||||
AvalancheResponse resp{0, 0, {AvalancheVote(0, blockHash)}}; | AvalancheResponse resp{0, 0, {AvalancheVote(0, blockHash)}}; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 6; i++) { | ||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | ||||
BOOST_CHECK(p.isAccepted(pindex)); | BOOST_CHECK(p.isAccepted(pindex)); | ||||
BOOST_CHECK_EQUAL(p.getConfidence(pindex), 0); | |||||
BOOST_CHECK_EQUAL(updates.size(), 0); | |||||
} | |||||
// A single neutral vote do not change anything. | |||||
resp = {AvalancheTest::getRound(p), 0, {AvalancheVote(-1, blockHash)}}; | |||||
AvalancheTest::runEventLoop(p); | |||||
BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | |||||
BOOST_CHECK(p.isAccepted(pindex)); | |||||
BOOST_CHECK_EQUAL(p.getConfidence(pindex), 0); | |||||
BOOST_CHECK_EQUAL(updates.size(), 0); | |||||
resp = {AvalancheTest::getRound(p), 0, {AvalancheVote(0, blockHash)}}; | |||||
for (int i = 1; i < 7; i++) { | |||||
AvalancheTest::runEventLoop(p); | |||||
BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | |||||
BOOST_CHECK(p.isAccepted(pindex)); | |||||
BOOST_CHECK_EQUAL(p.getConfidence(pindex), i); | |||||
BOOST_CHECK_EQUAL(updates.size(), 0); | |||||
} | |||||
// Two neutral votes will stall progress. | |||||
resp = {AvalancheTest::getRound(p), 0, {AvalancheVote(-1, blockHash)}}; | |||||
AvalancheTest::runEventLoop(p); | |||||
BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | |||||
BOOST_CHECK(p.isAccepted(pindex)); | |||||
BOOST_CHECK_EQUAL(p.getConfidence(pindex), 6); | |||||
BOOST_CHECK_EQUAL(updates.size(), 0); | |||||
AvalancheTest::runEventLoop(p); | |||||
BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | |||||
BOOST_CHECK(p.isAccepted(pindex)); | |||||
BOOST_CHECK_EQUAL(p.getConfidence(pindex), 6); | |||||
BOOST_CHECK_EQUAL(updates.size(), 0); | |||||
resp = {AvalancheTest::getRound(p), 0, {AvalancheVote(0, blockHash)}}; | |||||
for (int i = 2; i < 8; i++) { | |||||
AvalancheTest::runEventLoop(p); | |||||
BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | |||||
BOOST_CHECK(p.isAccepted(pindex)); | |||||
BOOST_CHECK_EQUAL(p.getConfidence(pindex), 6); | |||||
BOOST_CHECK_EQUAL(updates.size(), 0); | BOOST_CHECK_EQUAL(updates.size(), 0); | ||||
} | } | ||||
// We vote for it numerous times to finalize it. | // We vote for it numerous times to finalize it. | ||||
for (int i = 0; i < AVALANCHE_FINALIZATION_SCORE; i++) { | for (int i = 7; i < AVALANCHE_FINALIZATION_SCORE; i++) { | ||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | ||||
BOOST_CHECK(p.isAccepted(pindex)); | BOOST_CHECK(p.isAccepted(pindex)); | ||||
BOOST_CHECK_EQUAL(p.getConfidence(pindex), i); | |||||
BOOST_CHECK_EQUAL(updates.size(), 0); | BOOST_CHECK_EQUAL(updates.size(), 0); | ||||
} | } | ||||
// As long as it is not finalized, we poll. | // As long as it is not finalized, we poll. | ||||
invs = AvalancheTest::getInvsForNextPoll(p); | invs = AvalancheTest::getInvsForNextPoll(p); | ||||
BOOST_CHECK_EQUAL(invs.size(), 1); | BOOST_CHECK_EQUAL(invs.size(), 1); | ||||
BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | ||||
BOOST_CHECK(invs[0].hash == blockHash); | BOOST_CHECK(invs[0].hash == blockHash); | ||||
Show All 14 Lines | BOOST_AUTO_TEST_CASE(block_register) { | ||||
// Now let's undo this and finalize rejection. | // Now let's undo this and finalize rejection. | ||||
BOOST_CHECK(p.addBlockToReconcile(pindex)); | BOOST_CHECK(p.addBlockToReconcile(pindex)); | ||||
invs = AvalancheTest::getInvsForNextPoll(p); | invs = AvalancheTest::getInvsForNextPoll(p); | ||||
BOOST_CHECK_EQUAL(invs.size(), 1); | BOOST_CHECK_EQUAL(invs.size(), 1); | ||||
BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | ||||
BOOST_CHECK(invs[0].hash == blockHash); | BOOST_CHECK(invs[0].hash == blockHash); | ||||
resp = {AvalancheTest::getRound(p), 0, {AvalancheVote(1, blockHash)}}; | resp = {AvalancheTest::getRound(p), 0, {AvalancheVote(1, blockHash)}}; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 6; i++) { | ||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | ||||
BOOST_CHECK(p.isAccepted(pindex)); | BOOST_CHECK(p.isAccepted(pindex)); | ||||
BOOST_CHECK_EQUAL(updates.size(), 0); | BOOST_CHECK_EQUAL(updates.size(), 0); | ||||
} | } | ||||
// Now the state will flip. | // Now the state will flip. | ||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(multi_block_register) { | ||||
// Ensure B comes before A because it has accumulated more PoW. | // Ensure B comes before A because it has accumulated more PoW. | ||||
BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | ||||
BOOST_CHECK(invs[0].hash == blockHashB); | BOOST_CHECK(invs[0].hash == blockHashB); | ||||
BOOST_CHECK_EQUAL(invs[1].type, MSG_BLOCK); | BOOST_CHECK_EQUAL(invs[1].type, MSG_BLOCK); | ||||
BOOST_CHECK(invs[1].hash == blockHashA); | BOOST_CHECK(invs[1].hash == blockHashA); | ||||
// Let's vote for these blocks a few times. | // Let's vote for these blocks a few times. | ||||
for (int i = 0; i < 3; i++) { | for (int i = 0; i < 4; i++) { | ||||
NodeId nodeid = AvalancheTest::getSuitableNodeToQuery(p); | NodeId nodeid = AvalancheTest::getSuitableNodeToQuery(p); | ||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | BOOST_CHECK(p.registerVotes(nodeid, next(resp), updates)); | ||||
BOOST_CHECK_EQUAL(updates.size(), 0); | BOOST_CHECK_EQUAL(updates.size(), 0); | ||||
} | } | ||||
// Now it is accepted, but we can vote for it numerous times. | // Now it is accepted, 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++) { | ||||
▲ Show 20 Lines • Show All 306 Lines • Show Last 20 Lines |