Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rep_weights part 3 - Add mininum weight to representative cache #4485

Merged
merged 10 commits into from
Mar 22, 2024
53 changes: 48 additions & 5 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <nano/lib/work.hpp>
#include <nano/node/common.hpp>
#include <nano/node/make_store.hpp>
#include <nano/secure/common.hpp>
#include <nano/secure/ledger.hpp>
#include <nano/secure/utility.hpp>
#include <nano/store/account.hpp>
Expand Down Expand Up @@ -379,7 +380,7 @@ TEST (block_store, genesis)
nano::logger logger;
auto store = nano::make_store (logger, nano::unique_path (), nano::dev::constants);
ASSERT_TRUE (!store->init_error ());
nano::ledger_cache ledger_cache;
nano::ledger_cache ledger_cache{ store->rep_weight };
auto transaction (store->tx_begin_write ());
store->initialize (transaction, ledger_cache, nano::dev::constants);
nano::account_info info;
Expand Down Expand Up @@ -901,7 +902,7 @@ TEST (block_store, cemented_count_cache)
auto store = nano::make_store (logger, nano::unique_path (), nano::dev::constants);
ASSERT_TRUE (!store->init_error ());
auto transaction (store->tx_begin_write ());
nano::ledger_cache ledger_cache;
nano::ledger_cache ledger_cache{ store->rep_weight };
store->initialize (transaction, ledger_cache, nano::dev::constants);
ASSERT_EQ (1, ledger_cache.cemented_count);
}
Expand All @@ -911,7 +912,7 @@ TEST (block_store, block_random)
nano::logger logger;
auto store = nano::make_store (logger, nano::unique_path (), nano::dev::constants);
{
nano::ledger_cache ledger_cache;
nano::ledger_cache ledger_cache{ store->rep_weight };
auto transaction (store->tx_begin_write ());
store->initialize (transaction, ledger_cache, nano::dev::constants);
}
Expand All @@ -938,7 +939,7 @@ TEST (block_store, pruned_random)
block->sideband_set ({});
auto hash1 (block->hash ());
{
nano::ledger_cache ledger_cache;
nano::ledger_cache ledger_cache{ store->rep_weight };
auto transaction (store->tx_begin_write ());
store->initialize (transaction, ledger_cache, nano::dev::constants);
store->pruned.put (transaction, hash1);
Expand Down Expand Up @@ -968,7 +969,7 @@ TEST (block_store, state_block)

block1->sideband_set ({});
{
nano::ledger_cache ledger_cache;
nano::ledger_cache ledger_cache{ store->rep_weight };
auto transaction (store->tx_begin_write ());
store->initialize (transaction, ledger_cache, nano::dev::constants);
ASSERT_EQ (nano::block_type::state, block1->type ());
Expand Down Expand Up @@ -1445,6 +1446,48 @@ TEST (rocksdb_block_store, upgrade_v21_v22)
}
}

// Tests that the new rep_weight table gets filled with all
// existing representatives
TEST (mdb_block_store, upgrade_v22_to_v23)
{
nano::logger logger;
auto const path = nano::unique_path ();
nano::account rep_a{ 123 };
nano::account rep_b{ 456 };
// Setting the database to its 22nd version state
{
auto store{ nano::make_store (logger, path, nano::dev::constants) };
auto txn{ store->tx_begin_write () };

// Add three accounts referencing two representatives
nano::account_info info1{};
info1.representative = rep_a;
info1.balance = 1000;
store->account.put (txn, 1, info1);

nano::account_info info2{};
info2.representative = rep_a;
info2.balance = 500;
store->account.put (txn, 2, info2);

nano::account_info info3{};
info3.representative = rep_b;
info3.balance = 42;
store->account.put (txn, 3, info3);

store->version.put (txn, 22);
}

// Testing the upgrade code worked
auto store{ nano::make_store (logger, path, nano::dev::constants) };
auto txn (store->tx_begin_read ());
ASSERT_EQ (store->version.get (txn), store->version_current);

// The rep_weight table should contain all reps now
ASSERT_EQ (1500, store->rep_weight.get (txn, rep_a));
ASSERT_EQ (42, store->rep_weight.get (txn, rep_b));
}

TEST (mdb_block_store, upgrade_backup)
{
if (nano::rocksdb_config::using_rocksdb_in_tests ())
Expand Down
56 changes: 55 additions & 1 deletion nano/core_test/ledger.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "nano/lib/numbers.hpp"

#include <nano/lib/blocks.hpp>
#include <nano/lib/logging.hpp>
#include <nano/lib/stats.hpp>
Expand All @@ -9,11 +11,14 @@
#include <nano/node/transport/inproc.hpp>
#include <nano/store/rocksdb/rocksdb.hpp>
#include <nano/test_common/ledger.hpp>
#include <nano/test_common/make_store.hpp>
#include <nano/test_common/system.hpp>
#include <nano/test_common/testutil.hpp>

#include <gtest/gtest.h>

#include <limits>

using namespace std::chrono_literals;

// Init returns an error if it can't open files at the path
Expand Down Expand Up @@ -657,15 +662,64 @@ TEST (ledger, open_fork)

TEST (ledger, representation_changes)
{
auto store{ nano::test::make_store () };
nano::keypair key1;
nano::rep_weights rep_weights;
nano::rep_weights rep_weights{ store->rep_weight };
ASSERT_EQ (0, rep_weights.representation_get (key1.pub));
rep_weights.representation_put (key1.pub, 1);
ASSERT_EQ (1, rep_weights.representation_get (key1.pub));
rep_weights.representation_put (key1.pub, 2);
ASSERT_EQ (2, rep_weights.representation_get (key1.pub));
}

TEST (ledger, delete_rep_weight_of_zero)
{
auto store{ nano::test::make_store () };
nano::rep_weights rep_weights{ store->rep_weight };
auto txn{ store->tx_begin_write () };
rep_weights.representation_add (txn, 1, 100);
rep_weights.representation_add_dual (txn, 2, 100, 3, 100);
ASSERT_EQ (3, rep_weights.size ());
ASSERT_EQ (3, store->rep_weight.count (txn));

// set rep weights to 0
rep_weights.representation_add (txn, 1, nano::uint128_t{ 0 } - 100);
ASSERT_EQ (2, rep_weights.size ());
ASSERT_EQ (2, store->rep_weight.count (txn));

rep_weights.representation_add_dual (txn, 2, nano::uint128_t{ 0 } - 100, 3, nano::uint128_t{ 0 } - 100);
ASSERT_EQ (0, rep_weights.size ());
ASSERT_EQ (0, store->rep_weight.count (txn));
}

TEST (ledger, rep_cache_min_weight)
{
auto store{ nano::test::make_store () };
nano::uint128_t min_weight{ 10 };
nano::rep_weights rep_weights{ store->rep_weight, min_weight };
auto txn{ store->tx_begin_write () };

// one below min weight
rep_weights.representation_add (txn, 1, 9);
ASSERT_EQ (0, rep_weights.size ());
ASSERT_EQ (1, store->rep_weight.count (txn));

// exactly min weight
rep_weights.representation_add (txn, 1, 1);
ASSERT_EQ (1, rep_weights.size ());
ASSERT_EQ (1, store->rep_weight.count (txn));

// above min weight
rep_weights.representation_add (txn, 1, 1);
ASSERT_EQ (1, rep_weights.size ());
ASSERT_EQ (1, store->rep_weight.count (txn));

// fall blow min weight
rep_weights.representation_add (txn, 1, nano::uint128_t{ 0 } - 5);
ASSERT_EQ (0, rep_weights.size ());
ASSERT_EQ (1, store->rep_weight.count (txn));
}

TEST (ledger, representation)
{
auto ctx = nano::test::context::ledger_empty ();
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2906,7 +2906,7 @@ TEST (node, dont_write_lock_node)
nano::logger logger;
auto store = nano::make_store (logger, path, nano::dev::constants, false, true);
{
nano::ledger_cache ledger_cache;
nano::ledger_cache ledger_cache{ store->rep_weight };
auto transaction (store->tx_begin_write ());
store->initialize (transaction, ledger_cache, nano::dev::constants);
}
Expand Down
3 changes: 3 additions & 0 deletions nano/core_test/toml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ TEST (toml, daemon_config_deserialize_defaults)
ASSERT_EQ (conf.node.background_threads, defaults.node.background_threads);
ASSERT_EQ (conf.node.secondary_work_peers, defaults.node.secondary_work_peers);
ASSERT_EQ (conf.node.online_weight_minimum, defaults.node.online_weight_minimum);
ASSERT_EQ (conf.node.representative_vote_weight_minimum, defaults.node.representative_vote_weight_minimum);
ASSERT_EQ (conf.node.rep_crawler_weight_minimum, defaults.node.rep_crawler_weight_minimum);
ASSERT_EQ (conf.node.password_fanout, defaults.node.password_fanout);
ASSERT_EQ (conf.node.peering_port, defaults.node.peering_port);
Expand Down Expand Up @@ -404,6 +405,7 @@ TEST (toml, daemon_config_deserialize_no_defaults)
network_threads = 999
background_threads = 999
online_weight_minimum = "999"
representative_vote_weight_minimum = "999"
rep_crawler_weight_minimum = "999"
password_fanout = 999
peering_port = 999
Expand Down Expand Up @@ -597,6 +599,7 @@ TEST (toml, daemon_config_deserialize_no_defaults)
ASSERT_NE (conf.node.max_pruning_age, defaults.node.max_pruning_age);
ASSERT_NE (conf.node.max_pruning_depth, defaults.node.max_pruning_depth);
ASSERT_NE (conf.node.online_weight_minimum, defaults.node.online_weight_minimum);
ASSERT_NE (conf.node.representative_vote_weight_minimum, defaults.node.representative_vote_weight_minimum);
ASSERT_NE (conf.node.rep_crawler_weight_minimum, defaults.node.rep_crawler_weight_minimum);
ASSERT_NE (conf.node.password_fanout, defaults.node.password_fanout);
ASSERT_NE (conf.node.peering_port, defaults.node.peering_port);
Expand Down
4 changes: 4 additions & 0 deletions nano/core_test/vote_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ TEST (vote_processor, no_broadcast_local)
nano::node_flags flags;
flags.disable_request_loop = true;
nano::node_config config1, config2;
config1.representative_vote_weight_minimum = 0;
config1.frontiers_confirmation = nano::frontiers_confirmation_mode::disabled;
auto & node (*system.add_node (config1, flags));
config2.representative_vote_weight_minimum = 0;
config2.frontiers_confirmation = nano::frontiers_confirmation_mode::disabled;
config2.peering_port = system.get_available_port ();
system.add_node (config2, flags);
Expand Down Expand Up @@ -229,8 +231,10 @@ TEST (vote_processor, local_broadcast_without_a_representative)
nano::node_flags flags;
flags.disable_request_loop = true;
nano::node_config config1, config2;
config1.representative_vote_weight_minimum = 0;
config1.frontiers_confirmation = nano::frontiers_confirmation_mode::disabled;
auto & node (*system.add_node (config1, flags));
config2.representative_vote_weight_minimum = 0;
config2.frontiers_confirmation = nano::frontiers_confirmation_mode::disabled;
config2.peering_port = system.get_available_port ();
system.add_node (config2, flags);
Expand Down
2 changes: 0 additions & 2 deletions nano/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ add_library(
rate_limiting.hpp
rate_limiting.cpp
relaxed_atomic.hpp
rep_weights.hpp
rep_weights.cpp
rocksdbconfig.hpp
rocksdbconfig.cpp
rpc_handler_interface.hpp
Expand Down
96 changes: 0 additions & 96 deletions nano/lib/rep_weights.cpp

This file was deleted.

37 changes: 0 additions & 37 deletions nano/lib/rep_weights.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion nano/node/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ auto nano::block_processor::process_batch (nano::unique_lock<nano::mutex> & lock
processed_batch_t processed;

auto scoped_write_guard = write_database_queue.wait (nano::writer::process_batch);
auto transaction (node.store.tx_begin_write ({ tables::accounts, tables::blocks, tables::frontiers, tables::pending }));
auto transaction (node.store.tx_begin_write ({ tables::accounts, tables::blocks, tables::frontiers, tables::pending, tables::rep_weights }));
nano::timer<std::chrono::milliseconds> timer_l;

lock_a.lock ();
Expand Down
Loading
Loading