Skip to content

Commit

Permalink
Fix conflicts.add_two unit test (#3545)
Browse files Browse the repository at this point in the history
* Fix conflicts.add_two unit test

* Style fixes

* Address code review
  • Loading branch information
theohax authored Nov 5, 2021
1 parent a063100 commit cbe19d8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
93 changes: 93 additions & 0 deletions nano/core_test/conflicts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,99 @@ TEST (conflicts, DISABLED_add_two)
ASSERT_EQ (2, node1.active.size ());
}

TEST (conflicts, add_two)
{
nano::system system{};
auto const & node = system.add_node ();

// define a functor that sends from given account to given destination,
// optionally force-confirming the send blocks *and* receiving on the destination account;
// the functor returns a pair of the send and receive blocks created or nullptrs if something failed
//
auto const do_send = [&node] (auto const & previous, auto const & from, auto const & to, bool forceConfirm = true)
-> std::pair<std::shared_ptr<nano::block>, std::shared_ptr<nano::block>> {
auto send = std::make_shared<nano::send_block> (previous->hash (), to.pub, 0, from.prv, from.pub, 0);
node->work_generate_blocking (*send);

if (nano::process_result::progress != node->process (*send).code)
{
send.reset ();
return std::make_pair (std::move (send), std::move (send));
}

if (forceConfirm)
{
node->block_confirm (send);
node->active.election (send->qualified_root ())->force_confirm ();

auto receive = std::make_shared<nano::open_block> (send->hash (), to.pub, to.pub, to.prv, to.pub, 0);
node->work_generate_blocking (*receive);

if (nano::process_result::progress != node->process (*receive).code)
{
return std::make_pair (nullptr, nullptr);
}

node->block_confirm (receive);
node->active.election (receive->qualified_root ())->force_confirm ();

return std::make_pair (std::move (send), std::move (receive));
}

return std::make_pair (std::move (send), nullptr);
};

system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);

// send from genesis to account1 and receive it on account1
//
nano::keypair account1{};
auto const [send1, receive1] = do_send (nano::dev::genesis, nano::dev::genesis_key, account1);
ASSERT_TRUE (send1 && receive1);
// both blocks having been fully confirmed, we expect 1 (genesis) + 2 (send/receive) = 3 cemented blocks
//
ASSERT_TIMELY (3s, 3 == node->ledger.cache.cemented_count);

// send from genesis to account2 and receive it on account2
//
nano::keypair account2{};
auto const [send2, receive2] = do_send (send1, nano::dev::genesis_key, account2);
ASSERT_TRUE (send2 && receive2);
ASSERT_TIMELY (3s, 5 == node->ledger.cache.cemented_count);

// send from account1 to account3 but do not receive it on account3 and do not force confirm the send block
//
nano::keypair account3{};
{
auto const [send3, _] = do_send (receive1, account1, account3, false);
ASSERT_TRUE (send3);
// expect the number of cemented blocks not to have changed since the last operation
//
ASSERT_TIMELY (3s, 5 == node->ledger.cache.cemented_count);
}

// send from account1 to account3 but do not receive it on account3 and do not force confirm the send block
//
{
auto const [send4, _] = do_send (receive2, account2, account3, false);
ASSERT_TRUE (send4);
// expect the number of cemented blocks not to have changed since the last operation
//
ASSERT_TIMELY (3s, 5 == node->ledger.cache.cemented_count);
}

// activate elections for the previous two send blocks (to account3) that we did not forcefully confirm
//
node->scheduler.activate (account3.pub, node->store.tx_begin_read ());
node->scheduler.flush ();

// wait 3s before asserting just to make sure there would be enough time
// for the Active Elections Container to evict both elections in case they would wrongfully get confirmed
//
std::this_thread::sleep_for (3s);
ASSERT_EQ (2, node->active.size ());
}

TEST (vote_uniquer, null)
{
nano::block_uniquer block_uniquer;
Expand Down
2 changes: 1 addition & 1 deletion nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2986,7 +2986,7 @@ TEST (rpc, accounts_representatives)
accounts.push_back (std::make_pair ("", entry));
request.add_child ("accounts", accounts);
auto response (wait_response (system, rpc, request));
auto response_representative (response.get_child("representatives").get<std::string>(nano::dev::genesis->account ().to_account ()));
auto response_representative (response.get_child ("representatives").get<std::string> (nano::dev::genesis->account ().to_account ()));
ASSERT_EQ (response_representative, nano::dev::genesis->account ().to_account ());
}

Expand Down

0 comments on commit cbe19d8

Please sign in to comment.