Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Dec 21, 2024
1 parent 64ef214 commit f762d19
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 45 deletions.
86 changes: 45 additions & 41 deletions nano/core_test/rep_crawler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,53 +128,56 @@ TEST (rep_crawler, rep_remove)
nano::keypair keys_rep2; // Principal representative 2
nano::block_builder builder;

auto const rep_weight = nano::test::minimum_principal_weight () * 2;

// Send enough nanos to Rep1 to make it a principal representative
std::shared_ptr<nano::block> send_to_rep1 = builder
.state ()
.account (nano::dev::genesis_key.pub)
.previous (nano::dev::genesis->hash ())
.representative (nano::dev::genesis_key.pub)
.balance (nano::dev::constants.genesis_amount - searching_node.minimum_principal_weight () * 2)
.link (keys_rep1.pub)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*system.work.generate (nano::dev::genesis->hash ()))
.build ();
auto send_to_rep1 = builder
.state ()
.account (nano::dev::genesis_key.pub)
.previous (nano::dev::genesis->hash ())
.representative (nano::dev::genesis_key.pub)
.balance (nano::dev::constants.genesis_amount - rep_weight)
.link (keys_rep1.pub)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*system.work.generate (nano::dev::genesis->hash ()))
.build ();

// Receive by Rep1
std::shared_ptr<nano::block> receive_rep1 = builder
.state ()
.account (keys_rep1.pub)
.previous (0)
.representative (keys_rep1.pub)
.balance (searching_node.minimum_principal_weight () * 2)
.link (send_to_rep1->hash ())
.sign (keys_rep1.prv, keys_rep1.pub)
.work (*system.work.generate (keys_rep1.pub))
.build ();
auto receive_rep1 = builder
.state ()
.account (keys_rep1.pub)
.previous (0)
.representative (keys_rep1.pub)
.balance (rep_weight)
.link (send_to_rep1->hash ())
.sign (keys_rep1.prv, keys_rep1.pub)
.work (*system.work.generate (keys_rep1.pub))
.build ();

// Send enough nanos to Rep2 to make it a principal representative
std::shared_ptr<nano::block> send_to_rep2 = builder
.state ()
.account (nano::dev::genesis_key.pub)
.previous (send_to_rep1->hash ())
.representative (nano::dev::genesis_key.pub)
.balance (nano::dev::constants.genesis_amount - searching_node.minimum_principal_weight () * 4)
.link (keys_rep2.pub)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*system.work.generate (send_to_rep1->hash ()))
.build ();
auto send_to_rep2 = builder
.state ()
.account (nano::dev::genesis_key.pub)
.previous (send_to_rep1->hash ())
.representative (nano::dev::genesis_key.pub)
.balance (nano::dev::constants.genesis_amount - rep_weight * 2)
.link (keys_rep2.pub)
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub)
.work (*system.work.generate (send_to_rep1->hash ()))
.build ();

// Receive by Rep2
std::shared_ptr<nano::block> receive_rep2 = builder
.state ()
.account (keys_rep2.pub)
.previous (0)
.representative (keys_rep2.pub)
.balance (searching_node.minimum_principal_weight () * 2)
.link (send_to_rep2->hash ())
.sign (keys_rep2.prv, keys_rep2.pub)
.work (*system.work.generate (keys_rep2.pub))
.build ();
auto receive_rep2 = builder
.state ()
.account (keys_rep2.pub)
.previous (0)
.representative (keys_rep2.pub)
.balance (rep_weight)
.link (send_to_rep2->hash ())
.sign (keys_rep2.prv, keys_rep2.pub)
.work (*system.work.generate (keys_rep2.pub))
.build ();

{
auto transaction = searching_node.ledger.tx_begin_write ();
ASSERT_EQ (nano::block_status::progress, searching_node.ledger.process (transaction, send_to_rep1));
Expand All @@ -188,11 +191,12 @@ TEST (rep_crawler, rep_remove)

// Ensure Rep1 is found by the rep_crawler after receiving a vote from it
auto vote_rep1 = std::make_shared<nano::vote> (keys_rep1.pub, keys_rep1.prv, 0, 0, std::vector<nano::block_hash>{ nano::dev::genesis->hash () });
ASSERT_LE (searching_node.minimum_principal_weight (), rep_weight);
searching_node.rep_crawler.force_process (vote_rep1, channel_rep1);
ASSERT_TIMELY_EQ (5s, searching_node.rep_crawler.representative_count (), 1);
auto reps (searching_node.rep_crawler.representatives (1));
ASSERT_EQ (1, reps.size ());
ASSERT_EQ (searching_node.minimum_principal_weight () * 2, searching_node.ledger.weight (reps[0].account));
ASSERT_LE (searching_node.minimum_principal_weight (), searching_node.ledger.weight (reps[0].account));
ASSERT_EQ (keys_rep1.pub, reps[0].account);
ASSERT_EQ (channel_rep1, reps[0].channel);

Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/vote_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ TEST (vote_processor, weights)
auto & node (*system.nodes[0]);

// Create representatives of different weight levels
auto const stake = node.balance (nano::dev::genesis_key.pub);
auto const stake = node.config.online_weight_minimum.number ();
auto const level0 = stake / 5000; // 0.02%
auto const level1 = stake / 500; // 0.2%
auto const level2 = stake / 50; // 2%
Expand Down
8 changes: 5 additions & 3 deletions nano/node/repcrawler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,18 @@ void nano::rep_crawler::validate_and_process (nano::unique_lock<nano::mutex> & l
if (channel->get_type () == nano::transport::transport_type::loopback)
{
logger.debug (nano::log::type::rep_crawler, "Ignoring vote from loopback channel: {}", channel->to_string ());
continue;

continue; // Skip this vote
}

nano::uint128_t const rep_weight = node.ledger.weight (vote->account);
if (rep_weight < minimum)
{
logger.debug (nano::log::type::rep_crawler, "Ignoring vote from account: {} with too little voting weight: {}",
vote->account.to_account (),
nano::util::to_str (rep_weight));
continue;
fmt::streamed (rep_weight));

continue; // Skip this vote
}

// temporary data used for logging after dropping the lock
Expand Down
5 changes: 5 additions & 0 deletions nano/test_common/testutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,8 @@ std::vector<std::shared_ptr<nano::block>> nano::test::all_blocks (nano::node & n
}
return result;
}

nano::uint128_t nano::test::minimum_principal_weight ()
{
return nano::dev::genesis->balance ().number () / nano::dev::network_params.network.principal_weight_factor;
}
2 changes: 2 additions & 0 deletions nano/test_common/testutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,5 +432,7 @@ namespace test
* Returns all blocks in the ledger
*/
std::vector<std::shared_ptr<nano::block>> all_blocks (nano::node &);

nano::uint128_t minimum_principal_weight ();
}
}

0 comments on commit f762d19

Please sign in to comment.