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 2 - Store representative weights in database #4483

Merged
merged 7 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 25 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,6 +11,7 @@
#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>

Expand Down Expand Up @@ -657,15 +660,36 @@ 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, 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
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
4 changes: 2 additions & 2 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ nano::node::node (boost::asio::io_context & io_ctx_a, std::filesystem::path cons

if (!is_initialized && !flags.read_only)
{
auto const transaction (store.tx_begin_write ({ tables::accounts, tables::blocks, tables::confirmation_height, tables::frontiers }));
auto const transaction (store.tx_begin_write ({ tables::accounts, tables::blocks, tables::confirmation_height, tables::frontiers, tables::rep_weights }));
// Store was empty meaning we just created it, add the genesis block
store.initialize (transaction, ledger.cache, ledger.constants);
}
Expand Down Expand Up @@ -558,7 +558,7 @@ void nano::node::process_active (std::shared_ptr<nano::block> const & incoming)

nano::block_status nano::node::process (std::shared_ptr<nano::block> block)
{
auto const transaction = store.tx_begin_write ({ tables::accounts, tables::blocks, tables::frontiers, tables::pending });
auto const transaction = store.tx_begin_write ({ tables::accounts, tables::blocks, tables::frontiers, tables::pending, tables::rep_weights });
return process (transaction, block);
}

Expand Down
2 changes: 2 additions & 0 deletions nano/secure/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ add_library(
network_filter.cpp
pending_info.hpp
pending_info.cpp
rep_weights.hpp
rep_weights.cpp
utility.hpp
utility.cpp
vote.hpp
Expand Down
33 changes: 33 additions & 0 deletions nano/secure/account_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,36 @@ nano::epoch nano::account_info::epoch () const
{
return epoch_m;
}

size_t nano::account_info_v22::db_size () const
{
debug_assert (reinterpret_cast<uint8_t const *> (this) == reinterpret_cast<uint8_t const *> (&head));
debug_assert (reinterpret_cast<uint8_t const *> (&head) + sizeof (head) == reinterpret_cast<uint8_t const *> (&representative));
debug_assert (reinterpret_cast<uint8_t const *> (&representative) + sizeof (representative) == reinterpret_cast<uint8_t const *> (&open_block));
debug_assert (reinterpret_cast<uint8_t const *> (&open_block) + sizeof (open_block) == reinterpret_cast<uint8_t const *> (&balance));
debug_assert (reinterpret_cast<uint8_t const *> (&balance) + sizeof (balance) == reinterpret_cast<uint8_t const *> (&modified));
debug_assert (reinterpret_cast<uint8_t const *> (&modified) + sizeof (modified) == reinterpret_cast<uint8_t const *> (&block_count));
debug_assert (reinterpret_cast<uint8_t const *> (&block_count) + sizeof (block_count) == reinterpret_cast<uint8_t const *> (&epoch_m));
return sizeof (head) + sizeof (representative) + sizeof (open_block) + sizeof (balance) + sizeof (modified) + sizeof (block_count) + sizeof (epoch_m);
}

bool nano::account_info_v22::deserialize (nano::stream & stream_a)
{
auto error (false);
try
{
nano::read (stream_a, head.bytes);
nano::read (stream_a, representative.bytes);
nano::read (stream_a, open_block.bytes);
nano::read (stream_a, balance.bytes);
nano::read (stream_a, modified);
nano::read (stream_a, block_count);
nano::read (stream_a, epoch_m);
}
catch (std::runtime_error const &)
{
error = true;
}

return error;
}
Loading
Loading