Skip to content

Commit

Permalink
Add hash only votes, which can be batched (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower authored and rkeene committed Aug 12, 2018
1 parent e30ab8d commit 595d514
Show file tree
Hide file tree
Showing 15 changed files with 631 additions and 385 deletions.
58 changes: 3 additions & 55 deletions rai/core_test/conflicts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ TEST (conflicts, start_stop)
ASSERT_NE (node1.active.roots.end (), existing1);
auto votes1 (existing1->election);
ASSERT_NE (nullptr, votes1);
ASSERT_EQ (1, votes1->votes.rep_votes.size ());
ASSERT_EQ (1, votes1->last_votes.size ());
}

TEST (conflicts, add_existing)
Expand All @@ -40,8 +40,8 @@ TEST (conflicts, add_existing)
ASSERT_EQ (1, node1.active.roots.size ());
auto votes1 (node1.active.roots.find (send2->root ())->election);
ASSERT_NE (nullptr, votes1);
ASSERT_EQ (2, votes1->votes.rep_votes.size ());
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (key2.pub));
ASSERT_EQ (2, votes1->last_votes.size ());
ASSERT_NE (votes1->last_votes.end (), votes1->last_votes.find (key2.pub));
}

TEST (conflicts, add_two)
Expand All @@ -60,55 +60,3 @@ TEST (conflicts, add_two)
node1.active.start (send2);
ASSERT_EQ (2, node1.active.roots.size ());
}

TEST (votes, contested)
{
rai::genesis genesis;
auto block1 (std::make_shared<rai::state_block> (rai::test_genesis_key.pub, genesis.hash (), rai::test_genesis_key.pub, rai::genesis_amount - rai::Gxrb_ratio, rai::test_genesis_key.pub, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto block2 (std::make_shared<rai::state_block> (rai::test_genesis_key.pub, genesis.hash (), rai::test_genesis_key.pub, rai::genesis_amount - 2 * rai::Gxrb_ratio, rai::test_genesis_key.pub, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
ASSERT_FALSE (*block1 == *block2);
rai::votes votes (block1);
ASSERT_TRUE (votes.uncontested ());
votes.rep_votes[rai::test_genesis_key.pub] = block2;
ASSERT_FALSE (votes.uncontested ());
}

TEST (conflicts, rollback_source)
{
rai::system system (24000, 2);
auto & node1 (*system.nodes[0]);
auto & node2 (*system.nodes[1]);
rai::genesis genesis;
rai::keypair key1;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
auto send1 (std::make_shared<rai::state_block> (rai::test_genesis_key.pub, genesis.hash (), rai::test_genesis_key.pub, rai::genesis_amount - rai::Gxrb_ratio, key1.pub, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
ASSERT_EQ (rai::process_result::progress, node2.process (*send1).code);
ASSERT_FALSE (node2.active.start (send1));
ASSERT_EQ (rai::process_result::progress, node1.process (*send1).code);
ASSERT_FALSE (node1.active.start (send1));
auto send2 (std::make_shared<rai::state_block> (rai::test_genesis_key.pub, send1->hash (), rai::test_genesis_key.pub, rai::genesis_amount - 2 * rai::Gxrb_ratio, key1.pub, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (send1->hash ())));
ASSERT_EQ (rai::process_result::progress, node2.process (*send2).code);
ASSERT_FALSE (node2.active.start (send2));
auto send3 (std::make_shared<rai::state_block> (key1.pub, 0, key1.pub, rai::Gxrb_ratio, send1->hash (), key1.prv, key1.pub, system.work.generate (key1.pub)));
ASSERT_EQ (rai::process_result::progress, node2.process (*send3).code);
ASSERT_FALSE (node2.active.start (send3));
auto send4 (std::make_shared<rai::state_block> (key1.pub, send3->hash (), key1.pub, 2 * rai::Gxrb_ratio, send2->hash (), key1.prv, key1.pub, system.work.generate (send3->hash ())));
ASSERT_EQ (rai::process_result::progress, node2.process (*send4).code);
ASSERT_FALSE (node2.active.start (send4));
auto send5 (std::make_shared<rai::state_block> (rai::test_genesis_key.pub, send1->hash (), rai::test_genesis_key.pub, rai::genesis_amount - 3 * rai::Gxrb_ratio, key1.pub, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (send1->hash ())));
ASSERT_EQ (rai::process_result::progress, node1.process (*send5).code);
ASSERT_FALSE (node1.active.start (send5));
auto iterations (0);
while (!node2.active.active (*send4))
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
while (node2.active.active (*send4))
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
}
9 changes: 8 additions & 1 deletion rai/core_test/gap_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,17 @@ TEST (gap_cache, gap_bootstrap)
ASSERT_EQ (rai::genesis_amount, system.nodes[1]->balance (rai::genesis_account));
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (0)->insert_adhoc (key.prv);
system.wallet (0)->send_action (rai::test_genesis_key.pub, key.pub, 100);
auto latest_block (system.wallet (0)->send_action (rai::test_genesis_key.pub, key.pub, 100));
ASSERT_NE (nullptr, latest_block);
ASSERT_EQ (rai::genesis_amount - 200, system.nodes[0]->balance (rai::genesis_account));
ASSERT_EQ (rai::genesis_amount, system.nodes[1]->balance (rai::genesis_account));
system.deadline_set (10s);
{
// The separate publish and vote system doesn't work very well here because it's instantly confirmed.
// We help it get the block and vote out here.
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
system.nodes[0]->network.republish_block (transaction, latest_block);
}
while (system.nodes[1]->balance (rai::genesis_account) != rai::genesis_amount - 200)
{
ASSERT_NO_ERROR (system.poll ());
Expand Down
100 changes: 51 additions & 49 deletions rai/core_test/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,13 +785,13 @@ TEST (votes, check_signature)
auto node_l (system.nodes[0]);
node1.active.start (send1);
auto votes1 (node1.active.roots.find (send1->root ())->election);
ASSERT_EQ (1, votes1->votes.rep_votes.size ());
ASSERT_EQ (1, votes1->last_votes.size ());
auto vote1 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send1));
vote1->signature.bytes[0] ^= 1;
ASSERT_EQ (rai::vote_code::invalid, node1.vote_processor.vote_blocking (transaction, vote1, rai::endpoint ()));
ASSERT_EQ (rai::vote_code::invalid, node1.vote_processor.vote_blocking (transaction, vote1, rai::endpoint (boost::asio::ip::address_v6 (), 0)));
vote1->signature.bytes[0] ^= 1;
ASSERT_EQ (rai::vote_code::vote, node1.vote_processor.vote_blocking (transaction, vote1, rai::endpoint ()));
ASSERT_EQ (rai::vote_code::replay, node1.vote_processor.vote_blocking (transaction, vote1, rai::endpoint ()));
ASSERT_EQ (rai::vote_code::vote, node1.vote_processor.vote_blocking (transaction, vote1, rai::endpoint (boost::asio::ip::address_v6 (), 0)));
ASSERT_EQ (rai::vote_code::replay, node1.vote_processor.vote_blocking (transaction, vote1, rai::endpoint (boost::asio::ip::address_v6 (), 0)));
}

TEST (votes, add_one)
Expand All @@ -808,17 +808,17 @@ TEST (votes, add_one)
auto node_l (system.nodes[0]);
node1.active.start (send1);
auto votes1 (node1.active.roots.find (send1->root ())->election);
ASSERT_EQ (1, votes1->votes.rep_votes.size ());
ASSERT_EQ (1, votes1->last_votes.size ());
auto vote1 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send1));
votes1->vote (vote1);
ASSERT_FALSE (node1.active.vote (vote1));
auto vote2 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 2, send1));
votes1->vote (vote1);
ASSERT_EQ (2, votes1->votes.rep_votes.size ());
auto existing1 (votes1->votes.rep_votes.find (rai::test_genesis_key.pub));
ASSERT_NE (votes1->votes.rep_votes.end (), existing1);
ASSERT_EQ (*send1, *existing1->second);
ASSERT_FALSE (node1.active.vote (vote2));
ASSERT_EQ (2, votes1->last_votes.size ());
auto existing1 (votes1->last_votes.find (rai::test_genesis_key.pub));
ASSERT_NE (votes1->last_votes.end (), existing1);
ASSERT_EQ (send1->hash (), existing1->second.hash);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto winner (node1.ledger.winner (transaction, votes1->votes));
auto winner (*votes1->tally (transaction).begin ());
ASSERT_EQ (*send1, *winner.second);
ASSERT_EQ (rai::genesis_amount - 100, winner.first);
}
Expand All @@ -838,18 +838,18 @@ TEST (votes, add_two)
node1.active.start (send1);
auto votes1 (node1.active.roots.find (send1->root ())->election);
auto vote1 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send1));
votes1->vote (vote1);
ASSERT_FALSE (node1.active.vote (vote1));
rai::keypair key2;
auto send2 (std::make_shared<rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote2 (std::make_shared<rai::vote> (key2.pub, key2.prv, 1, send2));
votes1->vote (vote2);
ASSERT_EQ (3, votes1->votes.rep_votes.size ());
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (*send1, *votes1->votes.rep_votes[rai::test_genesis_key.pub]);
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (key2.pub));
ASSERT_EQ (*send2, *votes1->votes.rep_votes[key2.pub]);
ASSERT_FALSE (node1.active.vote (vote2));
ASSERT_EQ (3, votes1->last_votes.size ());
ASSERT_NE (votes1->last_votes.end (), votes1->last_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (send1->hash (), votes1->last_votes[rai::test_genesis_key.pub].hash);
ASSERT_NE (votes1->last_votes.end (), votes1->last_votes.find (key2.pub));
ASSERT_EQ (send2->hash (), votes1->last_votes[key2.pub].hash);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto winner (node1.ledger.winner (transaction, votes1->votes));
auto winner (*votes1->tally (transaction).begin ());
ASSERT_EQ (*send1, *winner.second);
}

Expand All @@ -860,7 +860,7 @@ TEST (votes, add_existing)
auto & node1 (*system.nodes[0]);
rai::genesis genesis;
rai::keypair key1;
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key1.pub, rai::genesis_amount - rai::Gxrb_ratio, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
{
rai::transaction transaction (node1.store.environment, nullptr, true);
ASSERT_EQ (rai::process_result::progress, node1.ledger.process (transaction, *send1).code);
Expand All @@ -869,24 +869,26 @@ TEST (votes, add_existing)
node1.active.start (send1);
auto votes1 (node1.active.roots.find (send1->root ())->election);
auto vote1 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send1));
votes1->vote (vote1);
ASSERT_FALSE (node1.active.vote (vote1));
ASSERT_FALSE (node1.active.publish (send1));
ASSERT_EQ (1, votes1->last_votes[rai::test_genesis_key.pub].sequence);
rai::keypair key2;
auto send2 (std::make_shared<rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send2 (std::make_shared<rai::send_block> (genesis.hash (), key2.pub, rai::genesis_amount - rai::Gxrb_ratio, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote2 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 2, send2));
// Pretend we've waited the timeout
votes1->last_votes[rai::test_genesis_key.pub].time = std::chrono::steady_clock::now () - std::chrono::seconds (20);
votes1->vote (vote2);
ASSERT_FALSE (node1.active.vote (vote2));
ASSERT_FALSE (node1.active.publish (send2));
ASSERT_EQ (2, votes1->last_votes[rai::test_genesis_key.pub].sequence);
// Also resend the old vote, and see if we respect the sequence number
votes1->last_votes[rai::test_genesis_key.pub].time = std::chrono::steady_clock::now () - std::chrono::seconds (20);
votes1->vote (vote1);
ASSERT_TRUE (node1.active.vote (vote1));
ASSERT_EQ (2, votes1->last_votes[rai::test_genesis_key.pub].sequence);
ASSERT_EQ (2, votes1->votes.rep_votes.size ());
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (*send2, *votes1->votes.rep_votes[rai::test_genesis_key.pub]);
ASSERT_EQ (2, votes1->last_votes.size ());
ASSERT_NE (votes1->last_votes.end (), votes1->last_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (send2->hash (), votes1->last_votes[rai::test_genesis_key.pub].hash);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto winner (node1.ledger.winner (transaction, votes1->votes));
auto winner (*votes1->tally (transaction).begin ());
ASSERT_EQ (*send2, *winner.second);
}

Expand All @@ -910,10 +912,10 @@ TEST (votes, add_old)
auto vote2 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send2));
votes1->last_votes[rai::test_genesis_key.pub].time = std::chrono::steady_clock::now () - std::chrono::seconds (20);
node1.vote_processor.vote_blocking (transaction, vote2, rai::endpoint ());
ASSERT_EQ (2, votes1->votes.rep_votes.size ());
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (*send1, *votes1->votes.rep_votes[rai::test_genesis_key.pub]);
auto winner (node1.ledger.winner (transaction, votes1->votes));
ASSERT_EQ (2, votes1->last_votes.size ());
ASSERT_NE (votes1->last_votes.end (), votes1->last_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (send1->hash (), votes1->last_votes[rai::test_genesis_key.pub].hash);
auto winner (*votes1->tally (transaction).begin ());
ASSERT_EQ (*send1, *winner.second);
}

Expand All @@ -936,26 +938,26 @@ TEST (votes, add_old_different_account)
node1.active.start (send2);
auto votes1 (node1.active.roots.find (send1->root ())->election);
auto votes2 (node1.active.roots.find (send2->root ())->election);
ASSERT_EQ (1, votes1->votes.rep_votes.size ());
ASSERT_EQ (1, votes2->votes.rep_votes.size ());
ASSERT_EQ (1, votes1->last_votes.size ());
ASSERT_EQ (1, votes2->last_votes.size ());
auto vote1 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 2, send1));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto vote_result1 (node1.vote_processor.vote_blocking (transaction, vote1, rai::endpoint ()));
ASSERT_EQ (rai::vote_code::vote, vote_result1);
ASSERT_EQ (2, votes1->votes.rep_votes.size ());
ASSERT_EQ (1, votes2->votes.rep_votes.size ());
ASSERT_EQ (2, votes1->last_votes.size ());
ASSERT_EQ (1, votes2->last_votes.size ());
auto vote2 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send2));
auto vote_result2 (node1.vote_processor.vote_blocking (transaction, vote2, rai::endpoint ()));
ASSERT_EQ (rai::vote_code::vote, vote_result2);
ASSERT_EQ (2, votes1->votes.rep_votes.size ());
ASSERT_EQ (2, votes2->votes.rep_votes.size ());
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (rai::test_genesis_key.pub));
ASSERT_NE (votes2->votes.rep_votes.end (), votes2->votes.rep_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (*send1, *votes1->votes.rep_votes[rai::test_genesis_key.pub]);
ASSERT_EQ (*send2, *votes2->votes.rep_votes[rai::test_genesis_key.pub]);
auto winner1 (node1.ledger.winner (transaction, votes1->votes));
ASSERT_EQ (2, votes1->last_votes.size ());
ASSERT_EQ (2, votes2->last_votes.size ());
ASSERT_NE (votes1->last_votes.end (), votes1->last_votes.find (rai::test_genesis_key.pub));
ASSERT_NE (votes2->last_votes.end (), votes2->last_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (send1->hash (), votes1->last_votes[rai::test_genesis_key.pub].hash);
ASSERT_EQ (send2->hash (), votes2->last_votes[rai::test_genesis_key.pub].hash);
auto winner1 (*votes1->tally (transaction).begin ());
ASSERT_EQ (*send1, *winner1.second);
auto winner2 (node1.ledger.winner (transaction, votes2->votes));
auto winner2 (*votes2->tally (transaction).begin ());
ASSERT_EQ (*send2, *winner2.second);
}

Expand All @@ -978,10 +980,10 @@ TEST (votes, add_cooldown)
auto send2 (std::make_shared<rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote2 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 2, send2));
node1.vote_processor.vote_blocking (transaction, vote2, rai::endpoint ());
ASSERT_EQ (2, votes1->votes.rep_votes.size ());
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (*send1, *votes1->votes.rep_votes[rai::test_genesis_key.pub]);
auto winner (node1.ledger.winner (transaction, votes1->votes));
ASSERT_EQ (2, votes1->last_votes.size ());
ASSERT_NE (votes1->last_votes.end (), votes1->last_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (send1->hash (), votes1->last_votes[rai::test_genesis_key.pub].hash);
auto winner (*votes1->tally (transaction).begin ());
ASSERT_EQ (*send1, *winner.second);
}

Expand Down
Loading

0 comments on commit 595d514

Please sign in to comment.