Cache rep weights (#2217)

* Cache rep weights

* Review comments

* Use rep_block not head

* Remove left over code from testing

* Review comments

* Formatting

* Actually use new flag in ledger weights caching
This commit is contained in:
Wesley Shillingford 2019-08-28 13:04:58 +01:00 committed by GitHub
commit 2422230bc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 841 additions and 932 deletions

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -70,8 +70,7 @@ TEST (network, send_node_id_handshake)
{
nano::system system (24000, 1);
ASSERT_EQ (0, system.nodes[0]->network.size ());
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
node1->start ();
system.nodes.push_back (node1);
auto initial (system.nodes[0]->stats.count (nano::stat::type::message, nano::stat::detail::node_id_handshake, nano::stat::dir::in));
@ -105,8 +104,7 @@ TEST (network, send_node_id_handshake_tcp)
{
nano::system system (24000, 1);
ASSERT_EQ (0, system.nodes[0]->network.size ());
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
node1->start ();
system.nodes.push_back (node1);
auto initial (system.nodes[0]->stats.count (nano::stat::type::message, nano::stat::detail::node_id_handshake, nano::stat::dir::in));
@ -161,8 +159,7 @@ TEST (network, last_contacted)
{
nano::system system (24000, 1);
ASSERT_EQ (0, system.nodes[0]->network.size ());
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
node1->start ();
system.nodes.push_back (node1);
auto channel1 (std::make_shared<nano::transport::channel_udp> (node1->network.udp_channels, nano::endpoint (boost::asio::ip::address_v6::loopback (), 24000), node1->network_params.protocol.protocol_version));
@ -196,9 +193,8 @@ TEST (network, multi_keepalive)
{
nano::system system (24000, 1);
ASSERT_EQ (0, system.nodes[0]->network.size ());
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
node1->start ();
system.nodes.push_back (node1);
ASSERT_EQ (0, node1->network.size ());
@ -211,9 +207,8 @@ TEST (network, multi_keepalive)
{
ASSERT_NO_ERROR (system.poll ());
}
nano::node_init init2;
auto node2 (std::make_shared<nano::node> (init2, system.io_ctx, 24002, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init2.error ());
auto node2 (std::make_shared<nano::node> (system.io_ctx, 24002, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node2->init_error ());
node2->start ();
system.nodes.push_back (node2);
auto channel2 (std::make_shared<nano::transport::channel_udp> (node2->network.udp_channels, system.nodes[0]->network.endpoint (), node2->network_params.protocol.protocol_version));
@ -635,9 +630,8 @@ TEST (bulk_pull, count_limit)
TEST (bootstrap_processor, DISABLED_process_none)
{
nano::system system (24000, 1);
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
auto done (false);
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
while (!done)
@ -653,8 +647,7 @@ TEST (bootstrap_processor, process_one)
nano::system system (24000, 1);
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (nano::test_genesis_key.pub, nano::test_genesis_key.pub, 100));
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
nano::block_hash hash1 (system.nodes[0]->latest (nano::test_genesis_key.pub));
nano::block_hash hash2 (node1->latest (nano::test_genesis_key.pub));
ASSERT_NE (hash1, hash2);
@ -681,9 +674,8 @@ TEST (bootstrap_processor, process_two)
ASSERT_NE (hash1, hash2);
ASSERT_NE (hash1, hash3);
ASSERT_NE (hash2, hash3);
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
ASSERT_NE (node1->latest (nano::test_genesis_key.pub), system.nodes[0]->latest (nano::test_genesis_key.pub));
system.deadline_set (10s);
@ -707,8 +699,7 @@ TEST (bootstrap_processor, process_state)
node0->work_generate_blocking (*block2);
node0->process (*block1);
node0->process (*block2);
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_EQ (node0->latest (nano::test_genesis_key.pub), block2->hash ());
ASSERT_NE (node1->latest (nano::test_genesis_key.pub), block2->hash ());
node1->bootstrap_initiator.bootstrap (node0->network.endpoint ());
@ -736,9 +727,8 @@ TEST (bootstrap_processor, process_new)
}
nano::uint128_t balance1 (system.nodes[0]->balance (nano::test_genesis_key.pub));
nano::uint128_t balance2 (system.nodes[0]->balance (key2.pub));
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24002, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24002, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
system.deadline_set (10s);
while (node1->balance (key2.pub) != balance2)
@ -761,9 +751,8 @@ TEST (bootstrap_processor, pull_diamond)
ASSERT_EQ (nano::process_result::progress, system.nodes[0]->process (*send2).code);
auto receive (std::make_shared<nano::receive_block> (send1->hash (), send2->hash (), nano::test_genesis_key.prv, nano::test_genesis_key.pub, system.work.generate (send1->hash ())));
ASSERT_EQ (nano::process_result::progress, system.nodes[0]->process (*receive).code);
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24002, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24002, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
system.deadline_set (10s);
while (node1->balance (nano::test_genesis_key.pub) != 100)
@ -778,9 +767,8 @@ TEST (bootstrap_processor, push_diamond)
{
nano::system system (24000, 1);
nano::keypair key;
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24002, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24002, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
auto wallet1 (node1->wallets.create (100));
wallet1->insert_adhoc (nano::test_genesis_key.prv);
wallet1->insert_adhoc (key.prv);
@ -805,9 +793,8 @@ TEST (bootstrap_processor, push_diamond)
TEST (bootstrap_processor, push_one)
{
nano::system system (24000, 1);
nano::node_init init1;
nano::keypair key1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto wallet (node1->wallets.create (nano::uint256_union ()));
ASSERT_NE (nullptr, wallet);
wallet->insert_adhoc (nano::test_genesis_key.prv);
@ -826,7 +813,6 @@ TEST (bootstrap_processor, push_one)
TEST (bootstrap_processor, lazy_hash)
{
nano::system system (24000, 1);
nano::node_init init1;
nano::genesis genesis;
nano::keypair key1;
nano::keypair key2;
@ -842,7 +828,7 @@ TEST (bootstrap_processor, lazy_hash)
system.nodes[0]->block_processor.add (receive2);
system.nodes[0]->block_processor.flush ();
// Start lazy bootstrap with last block in chain known
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
node1->network.udp_channels.insert (system.nodes[0]->network.endpoint (), node1->network_params.protocol.protocol_version);
node1->bootstrap_initiator.bootstrap_lazy (receive2->hash ());
// Check processed blocks
@ -857,7 +843,6 @@ TEST (bootstrap_processor, lazy_hash)
TEST (bootstrap_processor, lazy_max_pull_count)
{
nano::system system (24000, 1);
nano::node_init init1;
nano::genesis genesis;
nano::keypair key1;
nano::keypair key2;
@ -879,7 +864,7 @@ TEST (bootstrap_processor, lazy_max_pull_count)
system.nodes[0]->block_processor.add (change3);
system.nodes[0]->block_processor.flush ();
// Start lazy bootstrap with last block in chain known
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
node1->network.udp_channels.insert (system.nodes[0]->network.endpoint (), node1->network_params.protocol.protocol_version);
node1->bootstrap_initiator.bootstrap_lazy (change3->hash ());
// Check processed blocks
@ -894,7 +879,6 @@ TEST (bootstrap_processor, lazy_max_pull_count)
TEST (bootstrap_processor, wallet_lazy_frontier)
{
nano::system system (24000, 1);
nano::node_init init1;
nano::genesis genesis;
nano::keypair key1;
nano::keypair key2;
@ -910,7 +894,7 @@ TEST (bootstrap_processor, wallet_lazy_frontier)
system.nodes[0]->block_processor.add (receive2);
system.nodes[0]->block_processor.flush ();
// Start wallet lazy bootstrap
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
node1->network.udp_channels.insert (system.nodes[0]->network.endpoint (), node1->network_params.protocol.protocol_version);
auto wallet (node1->wallets.create (nano::uint256_union ()));
ASSERT_NE (nullptr, wallet);
@ -928,7 +912,6 @@ TEST (bootstrap_processor, wallet_lazy_frontier)
TEST (bootstrap_processor, wallet_lazy_pending)
{
nano::system system (24000, 1);
nano::node_init init1;
nano::genesis genesis;
nano::keypair key1;
nano::keypair key2;
@ -942,7 +925,7 @@ TEST (bootstrap_processor, wallet_lazy_pending)
system.nodes[0]->block_processor.add (send2);
system.nodes[0]->block_processor.flush ();
// Start wallet lazy bootstrap
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
node1->network.udp_channels.insert (system.nodes[0]->network.endpoint (), node1->network_params.protocol.protocol_version);
auto wallet (node1->wallets.create (nano::uint256_union ()));
ASSERT_NE (nullptr, wallet);
@ -1079,9 +1062,8 @@ TEST (bulk, genesis)
{
nano::system system (24000, 1);
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
nano::block_hash latest1 (system.nodes[0]->latest (nano::test_genesis_key.pub));
nano::block_hash latest2 (node1->latest (nano::test_genesis_key.pub));
ASSERT_EQ (latest1, latest2);
@ -1103,9 +1085,8 @@ TEST (bulk, offline_send)
{
nano::system system (24000, 1);
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
node1->start ();
system.nodes.push_back (node1);
nano::keypair key2;
@ -2331,8 +2312,7 @@ TEST (network, replace_port)
{
nano::system system (24000, 1);
ASSERT_EQ (0, system.nodes[0]->network.size ());
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
node1->start ();
system.nodes.push_back (node1);
{

View file

@ -29,21 +29,19 @@ TEST (node, stop)
TEST (node, block_store_path_failure)
{
nano::node_init init;
auto service (boost::make_shared<boost::asio::io_context> ());
nano::alarm alarm (*service);
auto path (nano::unique_path ());
nano::logging logging;
logging.init (path);
nano::work_pool work (std::numeric_limits<unsigned>::max ());
auto node (std::make_shared<nano::node> (init, *service, 24000, path, alarm, logging, work));
auto node (std::make_shared<nano::node> (*service, 24000, path, alarm, logging, work));
ASSERT_TRUE (node->wallets.items.empty ());
node->stop ();
}
TEST (node, password_fanout)
{
nano::node_init init;
auto service (boost::make_shared<boost::asio::io_context> ());
nano::alarm alarm (*service);
auto path (nano::unique_path ());
@ -52,7 +50,7 @@ TEST (node, password_fanout)
config.logging.init (path);
nano::work_pool work (std::numeric_limits<unsigned>::max ());
config.password_fanout = 10;
auto node (std::make_shared<nano::node> (init, *service, path, alarm, config, work));
auto node (std::make_shared<nano::node> (*service, path, alarm, config, work));
auto wallet (node->wallets.create (100));
ASSERT_EQ (10, wallet->store.password.values.size ());
node->stop ();
@ -235,9 +233,8 @@ TEST (node, auto_bootstrap)
{
ASSERT_NO_ERROR (system.poll ());
}
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
auto channel (std::make_shared<nano::transport::channel_udp> (node1->network.udp_channels, system.nodes[0]->network.endpoint (), node1->network_params.protocol.protocol_version));
node1->network.send_keepalive (channel);
node1->start ();
@ -272,9 +269,8 @@ TEST (node, auto_bootstrap_reverse)
nano::keypair key2;
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
system.wallet (0)->insert_adhoc (key2.prv);
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
ASSERT_NE (nullptr, system.wallet (0)->send_action (nano::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
auto channel (std::make_shared<nano::transport::channel_udp> (system.nodes[0]->network.udp_channels, node1->network.endpoint (), system.nodes[0]->network_params.protocol.protocol_version));
system.nodes[0]->network.send_keepalive (channel);
@ -452,8 +448,7 @@ TEST (node, unlock_search)
TEST (node, connect_after_junk)
{
nano::system system (24000, 1);
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto junk_buffer (std::make_shared<std::vector<uint8_t>> ());
junk_buffer->push_back (0);
auto channel1 (std::make_shared<nano::transport::channel_udp> (node1->network.udp_channels, system.nodes[0]->network.endpoint (), node1->network_params.protocol.protocol_version));
@ -1085,8 +1080,7 @@ TEST (node_flags, disable_udp)
auto node1 = system.nodes[0];
nano::node_flags node_flags;
node_flags.disable_udp = true;
nano::node_init init;
auto node2 (std::make_shared<nano::node> (init, system.io_ctx, nano::unique_path (), system.alarm, nano::node_config (24001, system.logging), system.work, node_flags));
auto node2 (std::make_shared<nano::node> (system.io_ctx, nano::unique_path (), system.alarm, nano::node_config (24001, system.logging), system.work, node_flags));
system.nodes.push_back (node2);
node2->start ();
// Send UDP message
@ -2016,8 +2010,7 @@ TEST (node, rep_remove)
node.rep_crawler.response (channel1, nano::test_genesis_key.pub, nano::genesis_amount);
ASSERT_EQ (2, node.rep_crawler.representative_count ());
// Add inactive TCP representative channel
nano::node_init init;
auto node2 (std::make_shared<nano::node> (init, system.io_ctx, nano::unique_path (), system.alarm, nano::node_config (24003, system.logging), system.work));
auto node2 (std::make_shared<nano::node> (system.io_ctx, nano::unique_path (), system.alarm, nano::node_config (24003, system.logging), system.work));
std::atomic<bool> done{ false };
std::weak_ptr<nano::node> node_w (node.shared ());
node.network.tcp_channels.start_tcp (node2->network.endpoint (), [node_w, &done](std::shared_ptr<nano::transport::channel> channel2) {
@ -2904,8 +2897,7 @@ TEST (node, peers)
nano::system system (24000, 1);
ASSERT_TRUE (system.nodes.front ()->network.empty ());
nano::node_init init;
auto node (std::make_shared<nano::node> (init, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
system.nodes.push_back (node);
auto endpoint = system.nodes.front ()->network.endpoint ();
@ -2968,8 +2960,7 @@ TEST (node, peer_cache_restart)
nano::endpoint_key endpoint_key{ endpoint.address ().to_v6 ().to_bytes (), endpoint.port () };
auto path (nano::unique_path ());
{
nano::node_init init;
auto node (std::make_shared<nano::node> (init, system.io_ctx, 24001, path, system.alarm, system.logging, system.work));
auto node (std::make_shared<nano::node> (system.io_ctx, 24001, path, system.alarm, system.logging, system.work));
system.nodes.push_back (node);
auto & store = node->store;
{
@ -2991,10 +2982,9 @@ TEST (node, peer_cache_restart)
}
// Restart node
{
nano::node_init init;
nano::node_flags node_flags;
node_flags.read_only = true;
auto node (std::make_shared<nano::node> (init, system.io_ctx, 24002, path, system.alarm, system.logging, system.work, node_flags));
auto node (std::make_shared<nano::node> (system.io_ctx, 24002, path, system.alarm, system.logging, system.work, node_flags));
system.nodes.push_back (node);
// Check cached peers after restart
node->network.start ();
@ -3059,12 +3049,12 @@ TEST (node, dont_write_lock_node)
// clang-format off
std::thread ([&path, &write_lock_held_promise, &finished_promise]() {
nano::logger_mt logger;
bool init (false);
auto store = nano::make_store (init, logger, path, false, true);
nano::genesis genesis;
auto store = nano::make_store (logger, path, false, true);
{
nano::genesis genesis;
nano::rep_weights rep_weights;
auto transaction (store->tx_begin_write ());
store->initialize (transaction, genesis);
store->initialize (transaction, genesis, rep_weights);
}
// Hold write lock open until main thread is done needing it

View file

@ -11,14 +11,13 @@
TEST (processor_service, bad_send_signature)
{
nano::logger_mt logger;
bool init (false);
auto store = nano::make_store (init, logger, nano::unique_path ());
ASSERT_FALSE (init);
auto store = nano::make_store (logger, nano::unique_path ());
ASSERT_FALSE (store->init_error ());
nano::stat stats;
nano::ledger ledger (*store, stats);
nano::genesis genesis;
auto transaction (store->tx_begin_write ());
store->initialize (transaction, genesis);
store->initialize (transaction, genesis, ledger.rep_weights);
nano::work_pool pool (std::numeric_limits<unsigned>::max ());
nano::account_info info1;
ASSERT_FALSE (store->account_get (transaction, nano::test_genesis_key.pub, info1));
@ -31,14 +30,13 @@ TEST (processor_service, bad_send_signature)
TEST (processor_service, bad_receive_signature)
{
nano::logger_mt logger;
bool init (false);
auto store = nano::make_store (init, logger, nano::unique_path ());
ASSERT_FALSE (init);
auto store = nano::make_store (logger, nano::unique_path ());
ASSERT_FALSE (store->init_error ());
nano::stat stats;
nano::ledger ledger (*store, stats);
nano::genesis genesis;
auto transaction (store->tx_begin_write ());
store->initialize (transaction, genesis);
store->initialize (transaction, genesis, ledger.rep_weights);
nano::work_pool pool (std::numeric_limits<unsigned>::max ());
nano::account_info info1;
ASSERT_FALSE (store->account_get (transaction, nano::test_genesis_key.pub, info1));

View file

@ -13,9 +13,8 @@ TEST (versioning, account_info_v1)
nano::account_info_v1 v1 (open.hash (), open.hash (), 3, 4);
{
nano::logger_mt logger;
auto error (false);
nano::mdb_store store (error, logger, file);
ASSERT_FALSE (error);
nano::mdb_store store (logger, file);
ASSERT_FALSE (store.init_error ());
auto transaction (store.tx_begin_write ());
nano::block_sideband sideband (nano::block_type::open, 0, 0, 0, 0, 0);
store.block_put (transaction, open.hash (), open, sideband);
@ -25,9 +24,8 @@ TEST (versioning, account_info_v1)
}
nano::logger_mt logger;
auto error (false);
nano::mdb_store store (error, logger, file);
ASSERT_FALSE (error);
nano::mdb_store store (logger, file);
ASSERT_FALSE (store.init_error ());
auto transaction (store.tx_begin_read ());
nano::account_info v_latest;
ASSERT_FALSE (store.account_get (transaction, account, v_latest));
@ -51,9 +49,8 @@ TEST (versioning, account_info_v5)
nano::account_info_v5 v5 (open.hash (), open.hash (), open.hash (), 3, 4);
{
nano::logger_mt logger;
auto error (false);
nano::mdb_store store (error, logger, file);
ASSERT_FALSE (error);
nano::mdb_store store (logger, file);
ASSERT_FALSE (store.init_error ());
auto transaction (store.tx_begin_write ());
nano::block_sideband sideband (nano::block_type::open, 0, 0, 0, 0, 0);
store.block_put (transaction, open.hash (), open, sideband);
@ -63,9 +60,8 @@ TEST (versioning, account_info_v5)
}
nano::logger_mt logger;
auto error (false);
nano::mdb_store store (error, logger, file);
ASSERT_FALSE (error);
nano::mdb_store store (logger, file);
ASSERT_FALSE (store.init_error ());
auto transaction (store.tx_begin_read ());
nano::account_info v_latest;
ASSERT_FALSE (store.account_get (transaction, account, v_latest));
@ -89,9 +85,8 @@ TEST (versioning, account_info_v13)
nano::account_info_v13 v13 (open.hash (), open.hash (), open.hash (), 3, 4, 10, nano::epoch::epoch_0);
{
nano::logger_mt logger;
auto error (false);
nano::mdb_store store (error, logger, file);
ASSERT_FALSE (error);
nano::mdb_store store (logger, file);
ASSERT_FALSE (store.init_error ());
auto transaction (store.tx_begin_write ());
nano::block_sideband sideband (nano::block_type::open, 0, 0, 0, 0, 0);
store.block_put (transaction, open.hash (), open, sideband);
@ -101,9 +96,8 @@ TEST (versioning, account_info_v13)
}
nano::logger_mt logger;
auto error (false);
nano::mdb_store store (error, logger, file);
ASSERT_FALSE (error);
nano::mdb_store store (logger, file);
ASSERT_FALSE (store.init_error ());
auto transaction (store.tx_begin_read ());
nano::account_info v_latest;
ASSERT_FALSE (store.account_get (transaction, account, v_latest));

View file

@ -886,9 +886,8 @@ TEST (wallet, upgrade_backup)
nano::keypair id;
{
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, dir, system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, dir, system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
auto wallet (node1->wallets.create (id.pub));
ASSERT_NE (nullptr, wallet);
auto transaction (node1->wallets.tx_begin_write ());
@ -898,13 +897,12 @@ TEST (wallet, upgrade_backup)
// Check with config backup_before_upgrade = false
{
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, dir, system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, dir, system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
auto wallet (node1->wallets.open (id.pub));
ASSERT_NE (nullptr, wallet);
auto transaction (node1->wallets.tx_begin_write ());
ASSERT_LT (3, wallet->store.version (transaction));
ASSERT_LT (3u, wallet->store.version (transaction));
wallet->store.version_put (transaction, 3);
}
ASSERT_EQ (get_backup_path ().string (), dir.string ());
@ -913,13 +911,12 @@ TEST (wallet, upgrade_backup)
{
nano::node_config node_config (24001, system.logging);
node_config.backup_before_upgrade = true;
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, dir, system.alarm, node_config, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, dir, system.alarm, node_config, system.work));
ASSERT_FALSE (node1->init_error ());
auto wallet (node1->wallets.open (id.pub));
ASSERT_NE (nullptr, wallet);
auto transaction (node1->wallets.tx_begin_read ());
ASSERT_LT (3, wallet->store.version (transaction));
ASSERT_LT (3u, wallet->store.version (transaction));
}
ASSERT_NE (get_backup_path ().string (), dir.string ());
}

View file

@ -85,9 +85,8 @@ TEST (wallets, upgrade)
auto path (nano::unique_path ());
nano::keypair id;
{
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, path, system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, path, system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
bool error (false);
nano::wallets wallets (error, *node1);
wallets.create (id.pub);
@ -106,8 +105,7 @@ TEST (wallets, upgrade)
(void)status;
assert (status == 0);
}
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, path, system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, path, system.alarm, system.logging, system.work));
ASSERT_EQ (1, node1->wallets.items.size ());
ASSERT_EQ (id.pub, node1->wallets.items.begin ()->first);
auto transaction_new (node1->wallets.env.tx_begin_write ());

View file

@ -82,13 +82,12 @@ boost::optional<std::string> websocket_test_call (std::string host, std::string
TEST (websocket, subscription_edge)
{
nano::system system (24000, 1);
nano::node_init init1;
nano::node_config config;
nano::node_flags node_flags;
config.websocket_config.enabled = true;
config.websocket_config.port = 24078;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
auto node1 (std::make_shared<nano::node> (system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
node1->start ();
system.nodes.push_back (node1);
@ -161,13 +160,12 @@ TEST (websocket, subscription_edge)
TEST (websocket, active_difficulty)
{
nano::system system (24000, 1);
nano::node_init init1;
nano::node_config config;
nano::node_flags node_flags;
config.websocket_config.enabled = true;
config.websocket_config.port = 24078;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
auto node1 (std::make_shared<nano::node> (system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
node1->start ();
system.nodes.push_back (node1);
@ -230,13 +228,12 @@ TEST (websocket, active_difficulty)
TEST (websocket, confirmation)
{
nano::system system (24000, 1);
nano::node_init init1;
nano::node_config config;
nano::node_flags node_flags;
config.websocket_config.enabled = true;
config.websocket_config.port = 24078;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
auto node1 (std::make_shared<nano::node> (system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
nano::uint256_union wallet;
nano::random_pool::generate_block (wallet.bytes.data (), wallet.bytes.size ());
node1->wallets.create (wallet);
@ -341,13 +338,12 @@ TEST (websocket, confirmation)
TEST (websocket, stopped_election)
{
nano::system system (24000, 1);
nano::node_init init1;
nano::node_config config;
nano::node_flags node_flags;
config.websocket_config.enabled = true;
config.websocket_config.port = 24078;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
auto node1 (std::make_shared<nano::node> (system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
nano::uint256_union wallet;
nano::random_pool::generate_block (wallet.bytes.data (), wallet.bytes.size ());
node1->wallets.create (wallet);
@ -404,13 +400,12 @@ TEST (websocket, stopped_election)
TEST (websocket, confirmation_options)
{
nano::system system (24000, 1);
nano::node_init init1;
nano::node_config config;
nano::node_flags node_flags;
config.websocket_config.enabled = true;
config.websocket_config.port = 24078;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
auto node1 (std::make_shared<nano::node> (system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
nano::uint256_union wallet;
nano::random_pool::generate_block (wallet.bytes.data (), wallet.bytes.size ());
node1->wallets.create (wallet);
@ -554,13 +549,12 @@ TEST (websocket, confirmation_options)
TEST (websocket, vote)
{
nano::system system (24000, 1);
nano::node_init init1;
nano::node_config config;
nano::node_flags node_flags;
config.websocket_config.enabled = true;
config.websocket_config.port = 24078;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
auto node1 (std::make_shared<nano::node> (system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
nano::uint256_union wallet;
nano::random_pool::generate_block (wallet.bytes.data (), wallet.bytes.size ());
node1->wallets.create (wallet);
@ -618,13 +612,12 @@ TEST (websocket, vote)
TEST (websocket, vote_options)
{
nano::system system (24000, 1);
nano::node_init init1;
nano::node_config config;
nano::node_flags node_flags;
config.websocket_config.enabled = true;
config.websocket_config.port = 24078;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
auto node1 (std::make_shared<nano::node> (system.io_ctx, nano::unique_path (), system.alarm, config, system.work, node_flags));
nano::uint256_union wallet;
nano::random_pool::generate_block (wallet.bytes.data (), wallet.bytes.size ());
node1->wallets.create (wallet);

View file

@ -36,6 +36,8 @@ add_library (nano_lib
memory.cpp
numbers.hpp
numbers.cpp
rep_weights.hpp
rep_weights.cpp
rpc_handler_interface.hpp
rpcconfig.hpp
rpcconfig.cpp

69
nano/lib/rep_weights.cpp Normal file
View file

@ -0,0 +1,69 @@
#include <nano/lib/rep_weights.hpp>
#include <nano/secure/blockstore.hpp>
void nano::rep_weights::representation_add (nano::account const & source_rep, nano::uint128_t const & amount_a)
{
std::lock_guard<std::mutex> guard (mutex);
auto source_previous (get (source_rep));
put (source_rep, source_previous + amount_a);
}
void nano::rep_weights::representation_put (nano::account const & account_a, nano::uint128_union const & representation_a)
{
std::lock_guard<std::mutex> guard (mutex);
put (account_a, representation_a);
}
nano::uint128_t nano::rep_weights::representation_get (nano::account const & account_a)
{
std::lock_guard<std::mutex> lk (mutex);
return get (account_a);
}
/** Makes a copy */
std::unordered_map<nano::account, nano::uint128_t> nano::rep_weights::get_rep_amounts ()
{
std::lock_guard<std::mutex> guard (mutex);
return rep_amounts;
}
void nano::rep_weights::put (nano::account const & account_a, nano::uint128_union const & representation_a)
{
auto it = rep_amounts.find (account_a);
auto amount = representation_a.number ();
if (it != rep_amounts.end ())
{
it->second = amount;
}
else
{
rep_amounts.emplace (account_a, amount);
}
}
nano::uint128_t nano::rep_weights::get (nano::account const & account_a)
{
auto it = rep_amounts.find (account_a);
if (it != rep_amounts.end ())
{
return it->second;
}
else
{
return nano::uint128_t{ 0 };
}
}
std::unique_ptr<nano::seq_con_info_component> nano::collect_seq_con_info (nano::rep_weights & rep_weights, const std::string & name)
{
size_t rep_amounts_count = 0;
{
std::lock_guard<std::mutex> guard (rep_weights.mutex);
rep_amounts_count = rep_weights.rep_amounts.size ();
}
auto sizeof_element = sizeof (decltype (rep_weights.rep_amounts)::value_type);
auto composite = std::make_unique<nano::seq_con_info_composite> (name);
composite->add_component (std::make_unique<nano::seq_con_info_leaf> (seq_con_info{ "rep_amounts", rep_amounts_count, sizeof_element }));
return composite;
}

33
nano/lib/rep_weights.hpp Normal file
View file

@ -0,0 +1,33 @@
#pragma once
#include <nano/lib/numbers.hpp>
#include <nano/lib/utility.hpp>
#include <memory>
#include <mutex>
#include <unordered_map>
namespace nano
{
class block_store;
class transaction;
class rep_weights
{
public:
void representation_add (nano::account const & source_a, nano::uint128_t const & amount_a);
nano::uint128_t representation_get (nano::account const & account_a);
void representation_put (nano::account const & account_a, nano::uint128_union const & representation_a);
std::unordered_map<nano::account, nano::uint128_t> get_rep_amounts ();
private:
std::mutex mutex;
std::unordered_map<nano::account, nano::uint128_t> rep_amounts;
void put (nano::account const & account_a, nano::uint128_union const & representation_a);
nano::uint128_t get (nano::account const & account_a);
friend std::unique_ptr<seq_con_info_component> collect_seq_con_info (rep_weights &, const std::string &);
};
std::unique_ptr<seq_con_info_component> collect_seq_con_info (rep_weights &, const std::string &);
}

View file

@ -54,11 +54,10 @@ void nano_daemon::daemon::run (boost::filesystem::path const & data_path, nano::
}
: std::function<boost::optional<uint64_t> (nano::uint256_union const &, uint64_t, std::atomic<int> &)> (nullptr));
nano::alarm alarm (io_ctx);
nano::node_init init;
try
{
auto node (std::make_shared<nano::node> (init, io_ctx, data_path, alarm, config.node, opencl_work, flags));
if (!init.error ())
auto node (std::make_shared<nano::node> (io_ctx, data_path, alarm, config.node, opencl_work, flags));
if (!node->init_error ())
{
auto network_label = node->network_params.network.get_current_network_as_string ();
std::cout << "Network: " << network_label << ", version: " << NANO_VERSION_STRING << "\n"

View file

@ -270,29 +270,15 @@ int main (int argc, char * const * argv)
}
else if (vm.count ("debug_dump_representatives"))
{
nano::inactive_node node (data_path);
nano::inactive_node node (data_path, 24000, true);
auto transaction (node.node->store.tx_begin_read ());
nano::uint128_t total;
for (auto i (node.node->store.representation_begin (transaction)), n (node.node->store.representation_end ()); i != n; ++i)
auto rep_amounts = node.node->ledger.rep_weights.get_rep_amounts ();
std::map<nano::account, nano::uint128_t> ordered_reps (rep_amounts.begin (), rep_amounts.end ());
for (auto const & rep : ordered_reps)
{
nano::account const & account (i->first);
auto amount (node.node->store.representation_get (transaction, account));
total += amount;
std::cout << boost::str (boost::format ("%1% %2% %3%\n") % account.to_account () % amount.convert_to<std::string> () % total.convert_to<std::string> ());
}
std::map<nano::account, nano::uint128_t> calculated;
for (auto i (node.node->store.latest_begin (transaction)), n (node.node->store.latest_end ()); i != n; ++i)
{
nano::account_info const & info (i->second);
nano::block_hash rep_block (node.node->ledger.representative_calculated (transaction, info.head));
auto block (node.node->store.block_get (transaction, rep_block));
calculated[block->representative ()] += info.balance.number ();
}
total = 0;
for (auto i (calculated.begin ()), n (calculated.end ()); i != n; ++i)
{
total += i->second;
std::cout << boost::str (boost::format ("%1% %2% %3%\n") % i->first.to_account () % i->second.convert_to<std::string> () % total.convert_to<std::string> ());
total += rep.second;
std::cout << boost::str (boost::format ("%1% %2% %3%\n") % rep.first.to_account () % rep.second.convert_to<std::string> () % total.convert_to<std::string> ());
}
}
else if (vm.count ("debug_dump_frontier_unchecked_dependents"))
@ -565,12 +551,11 @@ int main (int argc, char * const * argv)
size_t max_blocks (2 * num_accounts * num_interations + num_accounts * 2); // 1,000,000 + 2* 100,000 = 1,200,000 blocks
std::cerr << boost::str (boost::format ("Starting pregenerating %1% blocks\n") % max_blocks);
nano::system system (24000, 1);
nano::node_init init;
nano::work_pool work (std::numeric_limits<unsigned>::max ());
nano::logging logging;
auto path (nano::unique_path ());
logging.init (path);
auto node (std::make_shared<nano::node> (init, system.io_ctx, 24001, path, system.alarm, logging, work));
auto node (std::make_shared<nano::node> (system.io_ctx, 24001, path, system.alarm, logging, work));
nano::block_hash genesis_latest (node->latest (test_params.ledger.test_genesis_key.pub));
nano::uint128_t genesis_balance (std::numeric_limits<nano::uint128_t>::max ());
// Generating keys
@ -677,12 +662,11 @@ int main (int argc, char * const * argv)
size_t max_votes (num_elections * num_representatives); // 40,000 * 25 = 1,000,000 votes
std::cerr << boost::str (boost::format ("Starting pregenerating %1% votes\n") % max_votes);
nano::system system (24000, 1);
nano::node_init init;
nano::work_pool work (std::numeric_limits<unsigned>::max ());
nano::logging logging;
auto path (nano::unique_path ());
logging.init (path);
auto node (std::make_shared<nano::node> (init, system.io_ctx, 24001, path, system.alarm, logging, work));
auto node (std::make_shared<nano::node> (system.io_ctx, 24001, path, system.alarm, logging, work));
nano::block_hash genesis_latest (node->latest (test_params.ledger.test_genesis_key.pub));
nano::uint128_t genesis_balance (std::numeric_limits<nano::uint128_t>::max ());
// Generating keys

View file

@ -85,11 +85,10 @@ int run_wallet (QApplication & application, int argc, char * const * argv, boost
}
: std::function<boost::optional<uint64_t> (nano::uint256_union const &, uint64_t, std::atomic<int> &)> (nullptr));
nano::alarm alarm (io_ctx);
nano::node_init init;
nano::node_flags flags;
node = std::make_shared<nano::node> (init, io_ctx, data_path, alarm, config.node, work, flags);
if (!init.error ())
node = std::make_shared<nano::node> (io_ctx, data_path, alarm, config.node, work, flags);
if (!node->init_error ())
{
auto wallet (node->wallets.open (wallet_config.wallet));
if (wallet == nullptr)

View file

@ -84,7 +84,7 @@ bool copy_database (boost::filesystem::path const & data_path, boost::program_op
bool needs_to_write = vm.count ("unchecked_clear") || vm.count ("clear_send_ids") || vm.count ("online_weight_clear") || vm.count ("peer_clear") || vm.count ("confirmation_height_clear");
nano::inactive_node node (data_path, 24000, !needs_to_write);
if (!node.init.error ())
if (!node.node->init_error ())
{
if (vm.count ("unchecked_clear"))
{
@ -291,7 +291,7 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
{
boost::filesystem::path data_path = vm.count ("data_path") ? boost::filesystem::path (vm["data_path"].as<std::string> ()) : nano::working_path ();
inactive_node node (data_path, 24000, false);
if (!node.init.error ())
if (!node.node->init_error ())
{
auto transaction (node.node->store.tx_begin_write ());
node.node->store.unchecked_clear (transaction);
@ -306,7 +306,7 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
{
boost::filesystem::path data_path = vm.count ("data_path") ? boost::filesystem::path (vm["data_path"].as<std::string> ()) : nano::working_path ();
inactive_node node (data_path, 24000, false);
if (!node.init.error ())
if (!node.node->init_error ())
{
auto transaction (node.node->wallets.tx_begin_write ());
node.node->wallets.clear_send_ids (transaction);
@ -321,7 +321,7 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
{
boost::filesystem::path data_path = vm.count ("data_path") ? boost::filesystem::path (vm["data_path"].as<std::string> ()) : nano::working_path ();
inactive_node node (data_path, 24000, false);
if (!node.init.error ())
if (!node.node->init_error ())
{
auto transaction (node.node->store.tx_begin_write ());
node.node->store.online_weight_clear (transaction);
@ -336,7 +336,7 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
{
boost::filesystem::path data_path = vm.count ("data_path") ? boost::filesystem::path (vm["data_path"].as<std::string> ()) : nano::working_path ();
inactive_node node (data_path, 24000, false);
if (!node.init.error ())
if (!node.node->init_error ())
{
auto transaction (node.node->store.tx_begin_write ());
node.node->store.peer_clear (transaction);
@ -351,7 +351,7 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
{
boost::filesystem::path data_path = vm.count ("data_path") ? boost::filesystem::path (vm["data_path"].as<std::string> ()) : nano::working_path ();
inactive_node node (data_path, 24000, false);
if (!node.init.error ())
if (!node.node->init_error ())
{
auto account_it = vm.find ("account");
if (account_it != vm.cend ())

View file

@ -1799,7 +1799,7 @@ void nano::json_handler::confirmation_info ()
if (i->second->hash () == ii->second.hash)
{
nano::account const & representative (ii->first);
auto amount (node.store.representation_get (transaction, representative));
auto amount (node.ledger.rep_weights.representation_get (representative));
representatives.emplace (std::move (amount), representative);
}
}
@ -3131,30 +3131,37 @@ void nano::json_handler::representatives ()
{
const bool sorting = request.get<bool> ("sorting", false);
boost::property_tree::ptree representatives;
auto transaction (node.store.tx_begin_read ());
auto rep_amounts = node.ledger.rep_weights.get_rep_amounts ();
if (!sorting) // Simple
{
for (auto i (node.store.representation_begin (transaction)), n (node.store.representation_end ()); i != n && representatives.size () < count; ++i)
std::map<nano::account, nano::uint128_t> ordered (rep_amounts.begin (), rep_amounts.end ());
for (auto & rep_amount : rep_amounts)
{
nano::account const & account (i->first);
auto amount (node.store.representation_get (transaction, account));
auto const & account (rep_amount.first);
auto const & amount (rep_amount.second);
representatives.put (account.to_account (), amount.convert_to<std::string> ());
if (representatives.size () > count)
{
break;
}
}
}
else // Sorting
{
std::vector<std::pair<nano::uint128_union, std::string>> representation;
for (auto i (node.store.representation_begin (transaction)), n (node.store.representation_end ()); i != n; ++i)
std::vector<std::pair<nano::uint128_t, std::string>> representation;
for (auto & rep_amount : rep_amounts)
{
nano::account const & account (i->first);
auto amount (node.store.representation_get (transaction, account));
representation.push_back (std::make_pair (amount, account.to_account ()));
auto const & account (rep_amount.first);
auto const & amount (rep_amount.second);
representation.emplace_back (amount, account.to_account ());
}
std::sort (representation.begin (), representation.end ());
std::reverse (representation.begin (), representation.end ());
for (auto i (representation.begin ()), n (representation.end ()); i != n && representatives.size () < count; ++i)
{
representatives.put (i->second, (i->first).number ().convert_to<std::string> ());
representatives.put (i->second, (i->first).convert_to<std::string> ());
}
}
response_l.add_child ("representatives", representatives);

View file

@ -39,13 +39,13 @@ void mdb_val::convert_buffer_to_value ()
}
}
nano::mdb_store::mdb_store (bool & error_a, nano::logger_mt & logger_a, boost::filesystem::path const & path_a, nano::txn_tracking_config const & txn_tracking_config_a, std::chrono::milliseconds block_processor_batch_max_time_a, int lmdb_max_dbs, bool drop_unchecked, size_t const batch_size, bool backup_before_upgrade) :
nano::mdb_store::mdb_store (nano::logger_mt & logger_a, boost::filesystem::path const & path_a, nano::txn_tracking_config const & txn_tracking_config_a, std::chrono::milliseconds block_processor_batch_max_time_a, int lmdb_max_dbs, bool drop_unchecked, size_t const batch_size, bool backup_before_upgrade) :
logger (logger_a),
env (error_a, path_a, lmdb_max_dbs, true),
env (error, path_a, lmdb_max_dbs, true),
mdb_txn_tracker (logger_a, txn_tracking_config_a, block_processor_batch_max_time_a),
txn_tracking_enabled (txn_tracking_config_a.enable)
{
if (!error_a)
if (!error)
{
auto is_fully_upgraded (false);
{
@ -68,19 +68,19 @@ txn_tracking_enabled (txn_tracking_config_a.enable)
create_backup_file (env, path_a, logger_a);
}
auto transaction (tx_begin_write ());
open_databases (error_a, transaction, MDB_CREATE);
if (!error_a)
open_databases (error, transaction, MDB_CREATE);
if (!error)
{
error_a |= do_upgrades (transaction, batch_size);
error |= do_upgrades (transaction, batch_size);
}
}
else
{
auto transaction (tx_begin_read ());
open_databases (error_a, transaction, 0);
open_databases (error, transaction, 0);
}
if (!error_a && drop_unchecked)
if (!error && drop_unchecked)
{
auto transaction (tx_begin_write ({ nano::tables::cached_counts, tables::unchecked }));
unchecked_clear (transaction);
@ -133,7 +133,6 @@ void nano::mdb_store::open_databases (bool & error_a, nano::transaction const &
error_a |= mdb_dbi_open (env.tx (transaction_a), "state_v1", flags, &state_blocks_v1) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "pending", flags, &pending_v0) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "pending_v1", flags, &pending_v1) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "representation", flags, &representation) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "unchecked", flags, &unchecked) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "vote", flags, &vote) != 0;
error_a |= mdb_dbi_open (env.tx (transaction_a), "online_weight", flags, &online_weight) != 0;
@ -237,7 +236,6 @@ void nano::mdb_store::upgrade_v2_to_v3 (nano::write_transaction const & transact
info.rep_block = visitor.result;
auto impl (boost::polymorphic_downcast<nano::mdb_iterator<nano::account, nano::account_info_v5> *> (i.get ()));
mdb_cursor_put (impl->cursor, nano::mdb_val (account_l), nano::mdb_val (sizeof (info), &info), MDB_CURRENT);
representation_add (transaction_a, visitor.result, info.balance.number ());
}
}
@ -497,6 +495,14 @@ void nano::mdb_store::upgrade_v14_to_v15 (nano::write_transaction const & transa
{
account_put (transaction_a, account_info.first, account_info.second);
}
// Representation table is no longer used
if (representation != 0)
{
auto status (mdb_drop (env.tx (transaction_a), representation, 1));
release_assert (status == MDB_SUCCESS);
representation = 0;
}
}
/** Takes a filepath, appends '_backup_<timestamp>' to the end (but before any extension) and saves that file in the same directory */
@ -645,8 +651,6 @@ MDB_dbi nano::mdb_store::table_to_dbi (tables table_a) const
return pending_v1;
case tables::blocks_info:
return blocks_info;
case tables::representation:
return representation;
case tables::unchecked:
return unchecked;
case tables::vote:
@ -683,4 +687,9 @@ int nano::mdb_store::status_code_not_found () const
bool nano::mdb_store::copy_db (boost::filesystem::path const & destination_file)
{
return !mdb_env_copy2 (env.environment, destination_file.string ().c_str (), MDB_CP_COMPACT);
}
}
bool nano::mdb_store::init_error () const
{
return error;
}

View file

@ -32,7 +32,7 @@ public:
using block_store_partial::block_exists;
using block_store_partial::unchecked_put;
mdb_store (bool &, nano::logger_mt &, boost::filesystem::path const &, nano::txn_tracking_config const & txn_tracking_config_a = nano::txn_tracking_config{}, std::chrono::milliseconds block_processor_batch_max_time_a = std::chrono::milliseconds (5000), int lmdb_max_dbs = 128, bool drop_unchecked = false, size_t batch_size = 512, bool backup_before_upgrade = false);
mdb_store (nano::logger_mt &, boost::filesystem::path const &, nano::txn_tracking_config const & txn_tracking_config_a = nano::txn_tracking_config{}, std::chrono::milliseconds block_processor_batch_max_time_a = std::chrono::milliseconds (5000), int lmdb_max_dbs = 128, bool drop_unchecked = false, size_t batch_size = 512, bool backup_before_upgrade = false);
nano::write_transaction tx_begin_write (std::vector<nano::tables> const & tables_requiring_lock = {}, std::vector<nano::tables> const & tables_no_lock = {}) override;
nano::read_transaction tx_begin_read () override;
@ -44,8 +44,11 @@ public:
static void create_backup_file (nano::mdb_env &, boost::filesystem::path const &, nano::logger_mt &);
private:
nano::logger_mt & logger;
bool error{ false };
public:
nano::mdb_env env;
/**
@ -115,13 +118,13 @@ public:
MDB_dbi pending_v1{ 0 };
/**
* Maps block hash to account and balance.
* Maps block hash to account and balance. (Removed)
* block_hash -> nano::account, nano::amount
*/
MDB_dbi blocks_info{ 0 };
/**
* Representative weights.
* Representative weights. (Removed)
* nano::account -> nano::uint128_t
*/
MDB_dbi representation{ 0 };
@ -194,6 +197,8 @@ public:
return nano::store_iterator<Key, Value> (std::make_unique<nano::mdb_merge_iterator<Key, Value>> (transaction_a, table_to_dbi (table1_a), table_to_dbi (table2_a)));
}
bool init_error () const override;
private:
bool do_upgrades (nano::write_transaction &, size_t);
void upgrade_v1_to_v2 (nano::write_transaction const &);

View file

@ -64,11 +64,6 @@ void nano::node::keepalive (std::string const & address_a, uint16_t port_a)
});
}
bool nano::node_init::error () const
{
return block_store_init || wallets_store_init;
}
namespace nano
{
std::unique_ptr<seq_con_info_component> collect_seq_con_info (rep_crawler & rep_crawler, const std::string & name)
@ -113,12 +108,12 @@ std::unique_ptr<seq_con_info_component> collect_seq_con_info (block_processor &
}
}
nano::node::node (nano::node_init & init_a, boost::asio::io_context & io_ctx_a, uint16_t peering_port_a, boost::filesystem::path const & application_path_a, nano::alarm & alarm_a, nano::logging const & logging_a, nano::work_pool & work_a, nano::node_flags flags_a) :
node (init_a, io_ctx_a, application_path_a, alarm_a, nano::node_config (peering_port_a, logging_a), work_a, flags_a)
nano::node::node (boost::asio::io_context & io_ctx_a, uint16_t peering_port_a, boost::filesystem::path const & application_path_a, nano::alarm & alarm_a, nano::logging const & logging_a, nano::work_pool & work_a, nano::node_flags flags_a) :
node (io_ctx_a, application_path_a, alarm_a, nano::node_config (peering_port_a, logging_a), work_a, flags_a)
{
}
nano::node::node (nano::node_init & init_a, boost::asio::io_context & io_ctx_a, boost::filesystem::path const & application_path_a, nano::alarm & alarm_a, nano::node_config const & config_a, nano::work_pool & work_a, nano::node_flags flags_a) :
nano::node::node (boost::asio::io_context & io_ctx_a, boost::filesystem::path const & application_path_a, nano::alarm & alarm_a, nano::node_config const & config_a, nano::work_pool & work_a, nano::node_flags flags_a) :
io_ctx (io_ctx_a),
node_initialized_latch (1),
config (config_a),
@ -127,12 +122,12 @@ flags (flags_a),
alarm (alarm_a),
work (work_a),
logger (config_a.logging.min_time_between_log_output),
store_impl (nano::make_store (init_a.block_store_init, logger, application_path_a, flags.read_only, true, config_a.diagnostics_config.txn_tracking, config_a.block_processor_batch_max_time, config_a.lmdb_max_dbs, !flags.disable_unchecked_drop, flags.sideband_batch_size, config_a.backup_before_upgrade)),
store_impl (nano::make_store (logger, application_path_a, flags.read_only, true, config_a.diagnostics_config.txn_tracking, config_a.block_processor_batch_max_time, config_a.lmdb_max_dbs, !flags.disable_unchecked_drop, flags.sideband_batch_size, config_a.backup_before_upgrade)),
store (*store_impl),
wallets_store_impl (std::make_unique<nano::mdb_wallets_store> (init_a.wallets_store_init, application_path_a / "wallets.ldb", config_a.lmdb_max_dbs)),
wallets_store_impl (std::make_unique<nano::mdb_wallets_store> (application_path_a / "wallets.ldb", config_a.lmdb_max_dbs)),
wallets_store (*wallets_store_impl),
gap_cache (*this),
ledger (store, stats, config.epoch_block_link, config.epoch_block_signer),
ledger (store, stats, config.epoch_block_link, config.epoch_block_signer, flags_a.cache_representative_weights_from_frontiers),
checker (config.signature_checker_threads),
network (*this, config.peering_port),
bootstrap_initiator (*this),
@ -147,15 +142,15 @@ block_processor_thread ([this]() {
nano::thread_role::set (nano::thread_role::name::block_processing);
this->block_processor.process_blocks ();
}),
online_reps (*this, config.online_weight_minimum.number (), init_a.block_store_init),
online_reps (*this, config.online_weight_minimum.number ()),
vote_uniquer (block_uniquer),
active (*this),
confirmation_height_processor (pending_confirmation_height, store, ledger.stats, active, ledger.epoch_link, write_database_queue, config.conf_height_processor_batch_min_time, logger),
payment_observer_processor (observers.blocks),
wallets (init_a.wallets_store_init, *this),
wallets (wallets_store.init_error (), *this),
startup_time (std::chrono::steady_clock::now ())
{
if (!init_a.error ())
if (!init_error ())
{
if (config.websocket_config.enabled)
{
@ -396,7 +391,7 @@ startup_time (std::chrono::steady_clock::now ())
release_assert (!flags.read_only);
auto transaction (store.tx_begin_write ());
// Store was empty meaning we just created it, add the genesis block
store.initialize (transaction, genesis);
store.initialize (transaction, genesis, ledger.rep_weights);
}
auto transaction (store.tx_begin_read ());
@ -1639,7 +1634,12 @@ int nano::node::store_version ()
return store.version_get (transaction);
}
nano::inactive_node::inactive_node (boost::filesystem::path const & path_a, uint16_t peering_port_a, bool read_only_a) :
bool nano::node::init_error () const
{
return store.init_error () || wallets_store.init_error ();
}
nano::inactive_node::inactive_node (boost::filesystem::path const & path_a, uint16_t peering_port_a, bool read_only_a, bool cache_reps_a) :
path (path_a),
io_context (std::make_shared<boost::asio::io_context> ()),
alarm (*io_context),
@ -1655,9 +1655,10 @@ peering_port (peering_port_a)
nano::set_secure_perm_directory (path, error_chmod);
logging.max_size = std::numeric_limits<std::uintmax_t>::max ();
logging.init (path);
auto node_flags = nano::node_flags ();
nano::node_flags node_flags;
node_flags.read_only = read_only_a;
node = std::make_shared<nano::node> (init, *io_context, peering_port, path, alarm, logging, work, node_flags);
node_flags.cache_representative_weights_from_frontiers = cache_reps_a;
node = std::make_shared<nano::node> (*io_context, peering_port, path, alarm, logging, work, node_flags);
node->active.stop ();
}
@ -1666,11 +1667,11 @@ nano::inactive_node::~inactive_node ()
node->stop ();
}
std::unique_ptr<nano::block_store> nano::make_store (bool & init, nano::logger_mt & logger, boost::filesystem::path const & path, bool read_only, bool add_db_postfix, nano::txn_tracking_config const & txn_tracking_config_a, std::chrono::milliseconds block_processor_batch_max_time_a, int lmdb_max_dbs, bool drop_unchecked, size_t batch_size, bool backup_before_upgrade)
std::unique_ptr<nano::block_store> nano::make_store (nano::logger_mt & logger, boost::filesystem::path const & path, bool read_only, bool add_db_postfix, nano::txn_tracking_config const & txn_tracking_config_a, std::chrono::milliseconds block_processor_batch_max_time_a, int lmdb_max_dbs, bool drop_unchecked, size_t batch_size, bool backup_before_upgrade)
{
#if NANO_ROCKSDB
return std::make_unique<nano::rocksdb_store> (init, logger, add_db_postfix ? path / "rocksdb" : path, drop_unchecked, read_only);
return std::make_unique<nano::rocksdb_store> (logger, add_db_postfix ? path / "rocksdb" : path, drop_unchecked, read_only);
#else
return std::make_unique<nano::mdb_store> (init, logger, add_db_postfix ? path / "data.ldb" : path, txn_tracking_config_a, block_processor_batch_max_time_a, lmdb_max_dbs, drop_unchecked, batch_size, backup_before_upgrade);
return std::make_unique<nano::mdb_store> (logger, add_db_postfix ? path / "data.ldb" : path, txn_tracking_config_a, block_processor_batch_max_time_a, lmdb_max_dbs, drop_unchecked, batch_size, backup_before_upgrade);
#endif
}

View file

@ -2,6 +2,7 @@
#include <nano/boost/asio.hpp>
#include <nano/lib/alarm.hpp>
#include <nano/lib/rep_weights.hpp>
#include <nano/lib/stats.hpp>
#include <nano/lib/work.hpp>
#include <nano/node/active_transactions.hpp>
@ -72,22 +73,14 @@ public:
std::unique_ptr<seq_con_info_component> collect_seq_con_info (block_arrival & block_arrival, const std::string & name);
class node_init final
{
public:
bool error () const;
bool block_store_init{ false };
bool wallets_store_init{ false };
};
std::unique_ptr<seq_con_info_component> collect_seq_con_info (rep_crawler & rep_crawler, const std::string & name);
std::unique_ptr<seq_con_info_component> collect_seq_con_info (block_processor & block_processor, const std::string & name);
class node final : public std::enable_shared_from_this<nano::node>
{
public:
node (nano::node_init &, boost::asio::io_context &, uint16_t, boost::filesystem::path const &, nano::alarm &, nano::logging const &, nano::work_pool &, nano::node_flags = nano::node_flags ());
node (nano::node_init &, boost::asio::io_context &, boost::filesystem::path const &, nano::alarm &, nano::node_config const &, nano::work_pool &, nano::node_flags = nano::node_flags ());
node (boost::asio::io_context &, uint16_t, boost::filesystem::path const &, nano::alarm &, nano::logging const &, nano::work_pool &, nano::node_flags = nano::node_flags ());
node (boost::asio::io_context &, boost::filesystem::path const &, nano::alarm &, nano::node_config const &, nano::work_pool &, nano::node_flags = nano::node_flags ());
~node ();
template <typename T>
void background (T action_a)
@ -141,6 +134,7 @@ public:
void ongoing_online_weight_calculation ();
void ongoing_online_weight_calculation_queue ();
bool online () const;
bool init_error () const;
nano::worker worker;
nano::write_database_queue write_database_queue;
boost::asio::io_context & io_ctx;
@ -195,13 +189,12 @@ std::unique_ptr<seq_con_info_component> collect_seq_con_info (node & node, const
class inactive_node final
{
public:
inactive_node (boost::filesystem::path const & path = nano::working_path (), uint16_t = 24000, bool read_only_a = true);
inactive_node (boost::filesystem::path const & path = nano::working_path (), uint16_t = 24000, bool = true, bool = false);
~inactive_node ();
boost::filesystem::path path;
std::shared_ptr<boost::asio::io_context> io_context;
nano::alarm alarm;
nano::logging logging;
nano::node_init init;
nano::work_pool work;
uint16_t peering_port;
std::shared_ptr<nano::node> node;

View file

@ -117,6 +117,8 @@ public:
bool disable_unchecked_drop{ true };
bool fast_bootstrap{ false };
bool read_only{ false };
/** Whether to read all frontiers and construct the representative weights */
bool cache_representative_weights_from_frontiers{ true };
size_t sideband_batch_size{ 512 };
size_t block_processor_batch_size{ 0 };
size_t block_processor_full_size{ 65536 };

View file

@ -3,11 +3,11 @@
#include <cassert>
nano::online_reps::online_reps (nano::node & node_a, nano::uint128_t minimum_a, bool block_store_error_a) :
nano::online_reps::online_reps (nano::node & node_a, nano::uint128_t minimum_a) :
node (node_a),
minimum (minimum_a)
{
if (!block_store_error_a)
if (!node.ledger.store.init_error ())
{
auto transaction (node.ledger.store.tx_begin_read ());
online = trend (transaction);

View file

@ -16,7 +16,7 @@ class transaction;
class online_reps final
{
public:
online_reps (nano::node &, nano::uint128_t, bool);
online_reps (nano::node &, nano::uint128_t);
/** Add voting account \p rep_account to the set of online representatives */
void observe (nano::account const & rep_account);

View file

@ -40,15 +40,15 @@ void rocksdb_val::convert_buffer_to_value ()
}
}
nano::rocksdb_store::rocksdb_store (bool & error_a, nano::logger_mt & logger_a, boost::filesystem::path const & path_a, bool drop_unchecked_a, bool open_read_only_a) :
nano::rocksdb_store::rocksdb_store (nano::logger_mt & logger_a, boost::filesystem::path const & path_a, bool drop_unchecked_a, bool open_read_only_a) :
logger (logger_a)
{
boost::system::error_code error_mkdir, error_chmod;
boost::filesystem::create_directories (path_a, error_mkdir);
nano::set_secure_perm_directory (path_a, error_chmod);
error_a |= static_cast<bool> (error_mkdir);
error = static_cast<bool> (error_mkdir);
if (!error_a)
if (!error)
{
auto table_options = get_table_options ();
table_factory.reset (rocksdb::NewBlockBasedTableFactory (table_options));
@ -56,10 +56,10 @@ logger (logger_a)
{
construct_column_family_mutexes ();
}
open (error_a, path_a, open_read_only_a);
open (error, path_a, open_read_only_a);
}
if (!error_a && !open_read_only_a && drop_unchecked_a)
if (!error && !open_read_only_a && drop_unchecked_a)
{
auto transaction (tx_begin_write ({ nano::tables::cached_counts, tables::unchecked }));
unchecked_clear (transaction);
@ -608,9 +608,13 @@ bool nano::rocksdb_store::copy_db (boost::filesystem::path const & destination_p
// Open it so that it flushes all WAL files
if (status.ok ())
{
auto error{ false };
nano::rocksdb_store rocksdb_store (error, logger, destination_path.string (), false, false);
return !error;
nano::rocksdb_store rocksdb_store (logger, destination_path.string (), false, false);
return !rocksdb_store.init_error ();
}
return false;
}
bool nano::rocksdb_store::init_error () const
{
return error;
}

View file

@ -24,7 +24,7 @@ class logging_mt;
class rocksdb_store : public block_store_partial<rocksdb::Slice, rocksdb_store>
{
public:
rocksdb_store (bool &, nano::logger_mt &, boost::filesystem::path const &, bool drop_unchecked = false, bool open_read_only = false);
rocksdb_store (nano::logger_mt &, boost::filesystem::path const &, bool drop_unchecked = false, bool open_read_only = false);
~rocksdb_store ();
nano::write_transaction tx_begin_write (std::vector<nano::tables> const & tables_requiring_lock = {}, std::vector<nano::tables> const & tables_no_lock = {}) override;
nano::read_transaction tx_begin_read () override;
@ -69,7 +69,10 @@ public:
return nano::store_iterator<Key, Value> (std::make_unique<nano::rocksdb_merge_iterator<Key, Value>> (db, transaction_a, table_to_column_family (table1_a), table_to_column_family (table2_a)));
}
bool init_error () const override;
private:
bool error{ false };
nano::logger_mt & logger;
std::vector<rocksdb::ColumnFamilyHandle *> handles;
// Optimistic transactions are used in write mode

View file

@ -25,9 +25,8 @@ std::string nano::error_system_messages::message (int ev) const
/** Returns the node added. */
std::shared_ptr<nano::node> nano::system::add_node (nano::node_config const & node_config_a, nano::node_flags node_flags_a, nano::transport::transport_type type_a)
{
nano::node_init init;
auto node (std::make_shared<nano::node> (init, io_ctx, nano::unique_path (), alarm, node_config_a, work, node_flags_a));
assert (!init.error ());
auto node (std::make_shared<nano::node> (io_ctx, nano::unique_path (), alarm, node_config_a, work, node_flags_a));
assert (!node->init_error ());
node->start ();
nano::uint256_union wallet;
nano::random_pool::generate_block (wallet.bytes.data (), wallet.bytes.size ());

View file

@ -263,9 +263,10 @@ void nano::vote_processor::calculate_weights ()
representatives_3.clear ();
auto supply (node.online_reps.online_stake ());
auto transaction (node.store.tx_begin_read ());
for (auto i (node.store.representation_begin (transaction)), n (node.store.representation_end ()); i != n; ++i)
auto rep_amounts = node.ledger.rep_weights.get_rep_amounts ();
for (auto const & rep_amount : rep_amounts)
{
nano::account const & representative (i->first);
nano::account const & representative (rep_amount.first);
auto weight (node.ledger.weight (transaction, representative));
if (weight > supply / 1000) // 0.1% or above (level 1)
{

View file

@ -1995,10 +1995,16 @@ nano::store_iterator<nano::uint256_union, nano::wallet_value> nano::wallet_store
{
return nano::store_iterator<nano::uint256_union, nano::wallet_value> (nullptr);
}
nano::mdb_wallets_store::mdb_wallets_store (bool & error_a, boost::filesystem::path const & path_a, int lmdb_max_dbs) :
environment (error_a, path_a, lmdb_max_dbs, false, 1ULL * 1024 * 1024 * 1024)
nano::mdb_wallets_store::mdb_wallets_store (boost::filesystem::path const & path_a, int lmdb_max_dbs) :
environment (error, path_a, lmdb_max_dbs, false, 1ULL * 1024 * 1024 * 1024)
{
}
bool nano::mdb_wallets_store::init_error () const
{
return error;
}
MDB_txn * nano::wallet_store::tx (nano::transaction const & transaction_a) const
{
return static_cast<MDB_txn *> (transaction_a.get_handle ());
@ -2020,7 +2026,7 @@ std::unique_ptr<seq_con_info_component> collect_seq_con_info (wallets & wallets,
auto sizeof_item_element = sizeof (decltype (wallets.items)::value_type);
auto sizeof_actions_element = sizeof (decltype (wallets.actions)::value_type);
composite->add_component (std::make_unique<seq_con_info_leaf> (seq_con_info{ "items", items_count, sizeof_item_element }));
composite->add_component (std::make_unique<seq_con_info_leaf> (seq_con_info{ "actions_count", actions_count, sizeof_actions_element }));
composite->add_component (std::make_unique<seq_con_info_leaf> (seq_con_info{ "actions", actions_count, sizeof_actions_element }));
return composite;
}
}

View file

@ -236,11 +236,14 @@ class wallets_store
{
public:
virtual ~wallets_store () = default;
virtual bool init_error () const = 0;
};
class mdb_wallets_store final : public wallets_store
{
public:
mdb_wallets_store (bool &, boost::filesystem::path const &, int lmdb_max_dbs = 128);
mdb_wallets_store (boost::filesystem::path const &, int lmdb_max_dbs = 128);
nano::mdb_env environment;
bool init_error () const override;
bool error{ false };
};
}

View file

@ -465,7 +465,6 @@ TEST (wallet, create_change)
TEST (history, short_text)
{
bool init (false);
nano_qt::eventloop_processor processor;
nano::keypair key;
nano::system system (24000, 1);
@ -476,13 +475,13 @@ TEST (history, short_text)
account = system.account (transaction, 0);
}
auto wallet (std::make_shared<nano_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
nano::mdb_store store (init, system.nodes[0]->logger, nano::unique_path ());
ASSERT_TRUE (!init);
nano::mdb_store store (system.nodes[0]->logger, nano::unique_path ());
ASSERT_TRUE (!store.init_error ());
nano::genesis genesis;
nano::ledger ledger (store, system.nodes[0]->stats);
{
auto transaction (store.tx_begin_write ());
store.initialize (transaction, genesis);
store.initialize (transaction, genesis, ledger.rep_weights);
nano::keypair key;
auto latest (ledger.latest (transaction, nano::test_genesis_key.pub));
nano::send_block send (latest, nano::test_genesis_key.pub, 0, nano::test_genesis_key.prv, nano::test_genesis_key.pub, system.work.generate (latest));

View file

@ -1970,8 +1970,7 @@ TEST (rpc, process_subtype_receive)
TEST (rpc, keepalive)
{
nano::system system (24000, 1);
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
node1->start ();
system.nodes.push_back (node1);
auto node = system.nodes.front ();
@ -6108,7 +6107,6 @@ TEST (rpc, block_confirm_absent)
TEST (rpc, block_confirm_confirmed)
{
nano::system system (24000, 1);
nano::node_init init;
auto path (nano::unique_path ());
nano::node_config config;
config.peering_port = 24001;
@ -6116,7 +6114,7 @@ TEST (rpc, block_confirm_confirmed)
config.callback_port = 24002;
config.callback_target = "/";
config.logging.init (path);
auto node (std::make_shared<nano::node> (init, system.io_ctx, path, system.alarm, config, system.work));
auto node (std::make_shared<nano::node> (system.io_ctx, path, system.alarm, config, system.work));
node->start ();
system.nodes.push_back (node);
nano::genesis genesis;

View file

@ -565,6 +565,8 @@ private:
std::unique_ptr<nano::write_transaction_impl> impl;
};
class rep_weights;
/**
* Manages block storage and iteration
*/
@ -572,7 +574,7 @@ class block_store
{
public:
virtual ~block_store () = default;
virtual void initialize (nano::write_transaction const &, nano::genesis const &) = 0;
virtual void initialize (nano::write_transaction const &, nano::genesis const &, nano::rep_weights &) = 0;
virtual void block_put (nano::write_transaction const &, nano::block_hash const &, nano::block const &, nano::block_sideband const &, nano::epoch version = nano::epoch::epoch_0) = 0;
virtual nano::block_hash block_successor (nano::transaction const &, nano::block_hash const &) const = 0;
virtual void block_successor_clear (nano::write_transaction const &, nano::block_hash const &) = 0;
@ -627,12 +629,6 @@ public:
virtual nano::uint128_t block_balance_calculated (std::shared_ptr<nano::block>, nano::block_sideband const &) const = 0;
virtual nano::epoch block_version (nano::transaction const &, nano::block_hash const &) = 0;
virtual nano::uint128_t representation_get (nano::transaction const &, nano::account const &) = 0;
virtual void representation_put (nano::write_transaction const &, nano::account const &, nano::uint128_union const &) = 0;
virtual void representation_add (nano::write_transaction const &, nano::account const &, nano::uint128_t const &) = 0;
virtual nano::store_iterator<nano::account, nano::uint128_union> representation_begin (nano::transaction const &) = 0;
virtual nano::store_iterator<nano::account, nano::uint128_union> representation_end () = 0;
virtual void unchecked_clear (nano::write_transaction const &) = 0;
virtual void unchecked_put (nano::write_transaction const &, nano::unchecked_key const &, nano::unchecked_info const &) = 0;
virtual void unchecked_put (nano::write_transaction const &, nano::block_hash const &, std::shared_ptr<nano::block> const &) = 0;
@ -691,6 +687,8 @@ public:
/** Not applicable to all sub-classes */
virtual void serialize_mdb_tracker (boost::property_tree::ptree &, std::chrono::milliseconds, std::chrono::milliseconds) = 0;
virtual bool init_error () const = 0;
/** Start read-write transaction */
virtual nano::write_transaction tx_begin_write (std::vector<nano::tables> const & tables_to_lock = {}, std::vector<nano::tables> const & tables_no_lock = {}) = 0;
@ -698,7 +696,7 @@ public:
virtual nano::read_transaction tx_begin_read () = 0;
};
std::unique_ptr<nano::block_store> make_store (bool & init, nano::logger_mt & logger, boost::filesystem::path const & path, bool open_read_only = false, bool add_db_postfix = false, nano::txn_tracking_config const & txn_tracking_config_a = nano::txn_tracking_config{}, std::chrono::milliseconds block_processor_batch_max_time_a = std::chrono::milliseconds (5000), int lmdb_max_dbs = 128, bool drop_unchecked = false, size_t batch_size = 512, bool backup_before_upgrade = false);
std::unique_ptr<nano::block_store> make_store (nano::logger_mt & logger, boost::filesystem::path const & path, bool open_read_only = false, bool add_db_postfix = false, nano::txn_tracking_config const & txn_tracking_config_a = nano::txn_tracking_config{}, std::chrono::milliseconds block_processor_batch_max_time_a = std::chrono::milliseconds (5000), int lmdb_max_dbs = 128, bool drop_unchecked = false, size_t batch_size = 512, bool backup_before_upgrade = false);
}
namespace std

View file

@ -1,5 +1,6 @@
#pragma once
#include <nano/lib/rep_weights.hpp>
#include <nano/secure/blockstore.hpp>
namespace nano
@ -23,7 +24,7 @@ public:
* If using a different store version than the latest then you may need
* to modify some of the objects in the store to be appropriate for the version before an upgrade.
*/
void initialize (nano::write_transaction const & transaction_a, nano::genesis const & genesis_a) override
void initialize (nano::write_transaction const & transaction_a, nano::genesis const & genesis_a, nano::rep_weights & rep_weights) override
{
auto hash_l (genesis_a.hash ());
assert (latest_v0_begin (transaction_a) == latest_v0_end ());
@ -32,7 +33,7 @@ public:
block_put (transaction_a, hash_l, *genesis_a.open, sideband);
confirmation_height_put (transaction_a, network_params.ledger.genesis_account, 1);
account_put (transaction_a, network_params.ledger.genesis_account, { hash_l, genesis_a.open->hash (), genesis_a.open->hash (), std::numeric_limits<nano::uint128_t>::max (), nano::seconds_since_epoch (), 1, nano::epoch::epoch_0 });
representation_put (transaction_a, network_params.ledger.genesis_account, std::numeric_limits<nano::uint128_t>::max ());
rep_weights.representation_put (network_params.ledger.genesis_account, std::numeric_limits<nano::uint128_t>::max ());
frontier_put (transaction_a, hash_l, network_params.ledger.genesis_account);
}
@ -44,15 +45,6 @@ public:
return result;
}
void representation_add (nano::write_transaction const & transaction_a, nano::block_hash const & source_a, nano::uint128_t const & amount_a) override
{
auto source_block (block_get (transaction_a, source_a));
assert (source_block != nullptr);
auto source_rep (source_block->representative ());
auto source_previous (representation_get (transaction_a, source_rep));
representation_put (transaction_a, source_rep, source_previous + amount_a);
}
bool account_exists (nano::transaction const & transaction_a, nano::account const & account_a) override
{
auto iterator (latest_begin (transaction_a, account_a));
@ -327,11 +319,6 @@ public:
return result;
}
nano::store_iterator<nano::account, nano::uint128_union> representation_end () override
{
return nano::store_iterator<nano::account, nano::uint128_union> (nullptr);
}
nano::store_iterator<nano::unchecked_key, nano::unchecked_info> unchecked_end () override
{
return nano::store_iterator<nano::unchecked_key, nano::unchecked_info> (nullptr);
@ -532,31 +519,6 @@ public:
release_assert (success (status));
}
nano::uint128_t representation_get (nano::transaction const & transaction_a, nano::account const & account_a) override
{
nano::db_val<Val> value;
auto status (get (transaction_a, tables::representation, nano::db_val<Val> (account_a), value));
release_assert (success (status) || not_found (status));
nano::uint128_t result = 0;
if (success (status))
{
nano::uint128_union rep;
nano::bufferstream stream (reinterpret_cast<uint8_t const *> (value.data ()), value.size ());
auto error (nano::try_read (stream, rep));
(void)error;
assert (!error);
result = rep.number ();
}
return result;
}
void representation_put (nano::write_transaction const & transaction_a, nano::account const & account_a, nano::uint128_union const & representation_a) override
{
nano::db_val<Val> rep (representation_a);
auto status (put (transaction_a, tables::representation, account_a, rep));
release_assert (success (status));
}
void unchecked_put (nano::write_transaction const & transaction_a, nano::unchecked_key const & key_a, nano::unchecked_info const & info_a) override
{
nano::db_val<Val> info (info_a);
@ -904,11 +866,6 @@ public:
return make_merge_iterator<nano::pending_key, nano::pending_info> (transaction_a, tables::pending_v0, tables::pending_v1);
}
nano::store_iterator<nano::account, nano::uint128_union> representation_begin (nano::transaction const & transaction_a) override
{
return make_iterator<nano::account, nano::uint128_union> (transaction_a, tables::representation);
}
nano::store_iterator<nano::unchecked_key, nano::unchecked_info> unchecked_begin (nano::transaction const & transaction_a) override
{
return make_iterator<nano::unchecked_key, nano::unchecked_info> (transaction_a, tables::unchecked);

View file

@ -1,3 +1,4 @@
#include <nano/lib/rep_weights.hpp>
#include <nano/lib/stats.hpp>
#include <nano/lib/utility.hpp>
#include <nano/lib/work.hpp>
@ -6,6 +7,14 @@
namespace
{
void representation_add (nano::transaction const & transaction_a, nano::ledger & ledger_a, nano::block_hash const & source_a, nano::uint128_t const & amount_a)
{
auto source_block (ledger_a.store.block_get (transaction_a, source_a));
assert (source_block != nullptr);
auto source_rep (source_block->representative ());
ledger_a.rep_weights.representation_add (source_rep, amount_a);
}
/**
* Roll back the visited block
*/
@ -35,7 +44,7 @@ public:
(void)error;
assert (!error);
ledger.store.pending_del (transaction, key);
ledger.store.representation_add (transaction, ledger.representative (transaction, hash), pending.amount.number ());
representation_add (transaction, ledger, ledger.representative (transaction, hash), pending.amount.number ());
ledger.change_latest (transaction, pending.source, block_a.hashables.previous, info.rep_block, ledger.balance (transaction, block_a.hashables.previous), info.block_count - 1);
ledger.store.block_del (transaction, hash);
ledger.store.frontier_del (transaction, hash);
@ -55,7 +64,7 @@ public:
auto error (ledger.store.account_get (transaction, destination_account, info));
(void)error;
assert (!error);
ledger.store.representation_add (transaction, ledger.representative (transaction, hash), 0 - amount);
representation_add (transaction, ledger, ledger.representative (transaction, hash), 0 - amount);
ledger.change_latest (transaction, destination_account, block_a.hashables.previous, representative, ledger.balance (transaction, block_a.hashables.previous), info.block_count - 1);
ledger.store.block_del (transaction, hash);
ledger.store.pending_put (transaction, nano::pending_key (destination_account, block_a.hashables.source), { source_account, amount, nano::epoch::epoch_0 });
@ -70,7 +79,7 @@ public:
auto amount (ledger.amount (transaction, block_a.hashables.source));
auto destination_account (ledger.account (transaction, hash));
auto source_account (ledger.account (transaction, block_a.hashables.source));
ledger.store.representation_add (transaction, ledger.representative (transaction, hash), 0 - amount);
representation_add (transaction, ledger, ledger.representative (transaction, hash), 0 - amount);
ledger.change_latest (transaction, destination_account, 0, 0, 0, 0);
ledger.store.block_del (transaction, hash);
ledger.store.pending_put (transaction, nano::pending_key (destination_account, block_a.hashables.source), { source_account, amount, nano::epoch::epoch_0 });
@ -87,8 +96,8 @@ public:
(void)error;
assert (!error);
auto balance (ledger.balance (transaction, block_a.hashables.previous));
ledger.store.representation_add (transaction, representative, balance);
ledger.store.representation_add (transaction, hash, 0 - balance);
representation_add (transaction, ledger, representative, balance);
representation_add (transaction, ledger, hash, 0 - balance);
ledger.store.block_del (transaction, hash);
ledger.change_latest (transaction, account, block_a.hashables.previous, representative, info.balance, info.block_count - 1);
ledger.store.frontier_del (transaction, hash);
@ -107,11 +116,11 @@ public:
auto balance (ledger.balance (transaction, block_a.hashables.previous));
auto is_send (block_a.hashables.balance < balance);
// Add in amount delta
ledger.store.representation_add (transaction, hash, 0 - block_a.hashables.balance.number ());
representation_add (transaction, ledger, hash, 0 - block_a.hashables.balance.number ());
if (!representative.is_zero ())
{
// Move existing representation
ledger.store.representation_add (transaction, representative, balance);
representation_add (transaction, ledger, representative, balance);
}
nano::account_info info;
@ -318,10 +327,10 @@ void ledger_processor::state_block_impl (nano::state_block const & block_a)
if (!info.rep_block.is_zero ())
{
// Move existing representation
ledger.store.representation_add (transaction, info.rep_block, 0 - info.balance.number ());
representation_add (transaction, ledger, info.rep_block, 0 - info.balance.number ());
}
// Add in amount delta
ledger.store.representation_add (transaction, hash, block_a.hashables.balance.number ());
representation_add (transaction, ledger, hash, block_a.hashables.balance.number ());
if (is_send)
{
@ -448,8 +457,8 @@ void ledger_processor::change_block (nano::change_block const & block_a)
nano::block_sideband sideband (nano::block_type::change, account, 0, info.balance, info.block_count + 1, nano::seconds_since_epoch ());
ledger.store.block_put (transaction, hash, block_a, sideband);
auto balance (ledger.balance (transaction, block_a.hashables.previous));
ledger.store.representation_add (transaction, hash, balance);
ledger.store.representation_add (transaction, info.rep_block, 0 - balance);
representation_add (transaction, ledger, hash, balance);
representation_add (transaction, ledger, info.rep_block, 0 - balance);
ledger.change_latest (transaction, account, hash, hash, info.balance, info.block_count + 1);
ledger.store.frontier_del (transaction, block_a.hashables.previous);
ledger.store.frontier_put (transaction, hash, account);
@ -499,7 +508,7 @@ void ledger_processor::send_block (nano::send_block const & block_a)
if (result.code == nano::process_result::progress)
{
auto amount (info.balance.number () - block_a.hashables.balance.number ());
ledger.store.representation_add (transaction, info.rep_block, 0 - amount);
representation_add (transaction, ledger, info.rep_block, 0 - amount);
nano::block_sideband sideband (nano::block_type::send, account, 0, block_a.hashables.balance /* unused */, info.block_count + 1, nano::seconds_since_epoch ());
ledger.store.block_put (transaction, hash, block_a, sideband);
ledger.change_latest (transaction, account, hash, info.rep_block, block_a.hashables.balance, info.block_count + 1);
@ -570,7 +579,7 @@ void ledger_processor::receive_block (nano::receive_block const & block_a)
nano::block_sideband sideband (nano::block_type::receive, account, 0, new_balance, info.block_count + 1, nano::seconds_since_epoch ());
ledger.store.block_put (transaction, hash, block_a, sideband);
ledger.change_latest (transaction, account, hash, info.rep_block, new_balance, info.block_count + 1);
ledger.store.representation_add (transaction, info.rep_block, pending.amount.number ());
representation_add (transaction, ledger, info.rep_block, pending.amount.number ());
ledger.store.frontier_del (transaction, block_a.hashables.previous);
ledger.store.frontier_put (transaction, hash, account);
result.account = account;
@ -633,7 +642,7 @@ void ledger_processor::open_block (nano::open_block const & block_a)
nano::block_sideband sideband (nano::block_type::open, block_a.hashables.account, 0, pending.amount, 1, nano::seconds_since_epoch ());
ledger.store.block_put (transaction, hash, block_a, sideband);
ledger.change_latest (transaction, block_a.hashables.account, hash, hash, pending.amount.number (), 1);
ledger.store.representation_add (transaction, hash, pending.amount.number ());
representation_add (transaction, ledger, hash, pending.amount.number ());
ledger.store.frontier_put (transaction, hash, block_a.hashables.account);
result.account = block_a.hashables.account;
result.amount = pending.amount;
@ -668,13 +677,24 @@ bool nano::shared_ptr_block_hash::operator() (std::shared_ptr<nano::block> const
return lhs->hash () == rhs->hash ();
}
nano::ledger::ledger (nano::block_store & store_a, nano::stat & stat_a, nano::uint256_union const & epoch_link_a, nano::account const & epoch_signer_a) :
nano::ledger::ledger (nano::block_store & store_a, nano::stat & stat_a, nano::uint256_union const & epoch_link_a, nano::account const & epoch_signer_a, bool cache_reps_a) :
store (store_a),
stats (stat_a),
check_bootstrap_weights (true),
epoch_link (epoch_link_a),
epoch_signer (epoch_signer_a)
{
if (!store.init_error () && cache_reps_a)
{
auto transaction = store.tx_begin_read ();
for (auto i (store.latest_begin (transaction)), n (store.latest_end ()); i != n; ++i)
{
nano::account_info const & info (i->second);
auto block (store.block_get (transaction, info.rep_block));
assert (block != nullptr);
rep_weights.representation_add (block->representative (), info.balance.number ());
}
}
}
// Balance for account containing hash
@ -835,7 +855,7 @@ nano::uint128_t nano::ledger::weight (nano::transaction const & transaction_a, n
check_bootstrap_weights = false;
}
}
return store.representation_get (transaction_a, account_a);
return rep_weights.representation_get (account_a);
}
// Rollback blocks until `block_a' doesn't exist or it tries to penetrate the confirmation height
@ -1101,9 +1121,10 @@ namespace nano
std::unique_ptr<seq_con_info_component> collect_seq_con_info (ledger & ledger, const std::string & name)
{
auto composite = std::make_unique<seq_con_info_composite> (name);
auto count = ledger.bootstrap_weights.size ();
auto count = ledger.bootstrap_weights_size.load ();
auto sizeof_element = sizeof (decltype (ledger.bootstrap_weights)::value_type);
composite->add_component (std::make_unique<seq_con_info_leaf> (seq_con_info{ "bootstrap_weights", count, sizeof_element }));
composite->add_component (collect_seq_con_info (ledger.rep_weights, "rep_weights"));
return composite;
}
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <nano/lib/config.hpp>
#include <nano/lib/rep_weights.hpp>
#include <nano/secure/common.hpp>
namespace nano
@ -18,7 +19,7 @@ using tally_t = std::map<nano::uint128_t, std::shared_ptr<nano::block>, std::gre
class ledger final
{
public:
ledger (nano::block_store &, nano::stat &, nano::uint256_union const & = 1, nano::account const & = 0);
ledger (nano::block_store &, nano::stat &, nano::uint256_union const & = 1, nano::account const & = 0, bool = true);
nano::account account (nano::transaction const &, nano::block_hash const &) const;
nano::uint128_t amount (nano::transaction const &, nano::block_hash const &);
nano::uint128_t balance (nano::transaction const &, nano::block_hash const &) const;
@ -50,8 +51,10 @@ public:
static nano::uint128_t const unit;
nano::network_params network_params;
nano::block_store & store;
nano::rep_weights rep_weights;
nano::stat & stats;
std::unordered_map<nano::account, nano::uint128_t> bootstrap_weights;
std::atomic<size_t> bootstrap_weights_size{ 0 };
uint64_t bootstrap_weight_max_blocks{ 1 };
std::atomic<bool> check_bootstrap_weights;
nano::uint256_union epoch_link;

View file

@ -56,9 +56,8 @@ TEST (system, receive_while_synchronizing)
uint32_t count (1000);
system.generate_mass_activity (count, *system.nodes[0]);
nano::keypair key;
nano::node_init init1;
auto node1 (std::make_shared<nano::node> (init1, system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto node1 (std::make_shared<nano::node> (system.io_ctx, 24001, nano::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (node1->init_error ());
auto channel (std::make_shared<nano::transport::channel_udp> (node1->network.udp_channels, system.nodes[0]->network.endpoint (), node1->network_params.protocol.protocol_version));
node1->network.send_keepalive (channel);
auto wallet (node1->wallets.create (1));
@ -90,14 +89,13 @@ TEST (system, receive_while_synchronizing)
TEST (ledger, deep_account_compute)
{
nano::logger_mt logger;
bool init (false);
auto store = nano::make_store (init, logger, nano::unique_path ());
ASSERT_FALSE (init);
auto store = nano::make_store (logger, nano::unique_path ());
ASSERT_FALSE (store->init_error ());
nano::stat stats;
nano::ledger ledger (*store, stats);
nano::genesis genesis;
auto transaction (store->tx_begin_write ());
store->initialize (transaction, genesis);
store->initialize (transaction, genesis, ledger.rep_weights);
nano::work_pool pool (std::numeric_limits<unsigned>::max ());
nano::keypair key;
auto balance (nano::genesis_amount - 1);