Skip to content

Commit

Permalink
CLI compare_rep_weights to compare ledger and hardcoded rep weights
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermelawless committed Apr 15, 2020
1 parent a4d4675 commit 2acb1a5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 25 deletions.
43 changes: 43 additions & 0 deletions nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <boost/program_options.hpp>
#include <boost/range/adaptor/reversed.hpp>

#include <numeric>
#include <sstream>

#include <argon2.h>
Expand Down Expand Up @@ -69,6 +70,7 @@ int main (int argc, char * const * argv)
("version", "Prints out version")
("config", boost::program_options::value<std::vector<std::string>>()->multitoken(), "Pass node configuration values. This takes precedence over any values in the configuration file. This option can be repeated multiple times.")
("daemon", "Start node daemon")
("compare_rep_weights", "Display a comparison between the representative weights from the ledger and the hardcoded bootstrap weights")
("debug_block_count", "Display the number of block")
("debug_bootstrap_generate", "Generate bootstrap sequence of blocks")
("debug_dump_frontier_unchecked_dependents", "Dump frontiers which have matching unchecked keys")
Expand Down Expand Up @@ -157,6 +159,47 @@ int main (int argc, char * const * argv)
}
daemon.run (data_path, flags);
}
else if (vm.count ("compare_rep_weights"))
{
nano::network_constants constants;
if (!constants.is_test_network ())
{
auto node_flags = nano::inactive_node_flag_defaults ();
nano::update_flags (node_flags, vm);
node_flags.generate_cache.reps = true;
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto node = inactive_node->node;

auto hardcoded = node->get_bootstrap_weights ().second;
auto ledger = node->ledger.cache.rep_weights.get_rep_amounts ();

nano::uint128_union total_ledger = std::accumulate (ledger.begin (), ledger.end (), nano::uint128_t{ 0 }, [](auto sum, auto & rep) { return sum + rep.second; });
nano::uint128_union total_hardcoded = std::accumulate (hardcoded.begin (), hardcoded.end (), nano::uint128_t{ 0 }, [](auto sum, auto & rep) { return sum + rep.second; });

std::vector<nano::uint128_t> mismatch_samples;
std::transform (hardcoded.begin (), hardcoded.end (), std::back_inserter (mismatch_samples), [&ledger](auto const & rep) {
auto ledger_rep (ledger.find (rep.first));
nano::uint128_t ledger_weight = (ledger_rep == ledger.end () ? 0 : ledger_rep->second);
auto absolute = ledger_weight > rep.second ? ledger_weight - rep.second : rep.second - ledger_weight;
return absolute;
});

nano::uint128_union mismatch_total = std::accumulate (mismatch_samples.begin (), mismatch_samples.end (), nano::uint128_t (0));
nano::uint128_union mismatch_mean = mismatch_total.number () / mismatch_samples.size ();

std::cout << boost::str (boost::format ("ledger weight %1% Mnano\nhardcoded weight %2% Mnano\nmismatched\n\ttotal %3% Mnano\n\tsamples %4%\n\tmean %5% Mnano\n")
% total_ledger.format_balance (nano::Mxrb_ratio, 0, true)
% total_hardcoded.format_balance (nano::Mxrb_ratio, 0, true)
% mismatch_total.format_balance (nano::Mxrb_ratio, 0, true)
% mismatch_samples.size ()
% mismatch_mean.format_balance (nano::Mxrb_ratio, 0, true));
}
else
{
std::cout << "Not available for the test network" << std::endl;
result = -1;
}
}
else if (vm.count ("debug_block_count"))
{
auto inactive_node = nano::default_inactive_node (data_path, vm);
Expand Down
62 changes: 37 additions & 25 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,36 +407,19 @@ node_seq (seq)

if ((network_params.network.is_live_network () || network_params.network.is_beta_network ()) && !flags.inactive_node)
{
auto bootstrap_weights = get_bootstrap_weights ();
// Use bootstrap weights if initial bootstrap is not completed
bool use_bootstrap_weight (false);
const uint8_t * weight_buffer = network_params.network.is_live_network () ? nano_bootstrap_weights_live : nano_bootstrap_weights_beta;
size_t weight_size = network_params.network.is_live_network () ? nano_bootstrap_weights_live_size : nano_bootstrap_weights_beta_size;
nano::bufferstream weight_stream ((const uint8_t *)weight_buffer, weight_size);
nano::uint128_union block_height;
if (!nano::try_read (weight_stream, block_height))
bool use_bootstrap_weight = ledger.cache.block_count < bootstrap_weights.first;
if (use_bootstrap_weight)
{
auto max_blocks = (uint64_t)block_height.number ();
use_bootstrap_weight = ledger.cache.block_count < max_blocks;
if (use_bootstrap_weight)
ledger.bootstrap_weight_max_blocks = bootstrap_weights.first;
ledger.bootstrap_weights = bootstrap_weights.second;
for (auto const & rep : ledger.bootstrap_weights)
{
ledger.bootstrap_weight_max_blocks = max_blocks;
while (true)
{
nano::account account;
if (nano::try_read (weight_stream, account.bytes))
{
break;
}
nano::amount weight;
if (nano::try_read (weight_stream, weight.bytes))
{
break;
}
logger.always_log ("Using bootstrap rep weight: ", account.to_account (), " -> ", weight.format_balance (Mxrb_ratio, 0, true), " XRB");
ledger.bootstrap_weights[account] = weight.number ();
}
logger.always_log ("Using bootstrap rep weight: ", rep.first.to_account (), " -> ", nano::uint128_union (rep.second).format_balance (Mxrb_ratio, 0, true), " XRB");
}
}

// Drop unchecked blocks if initial bootstrap is completed
if (!flags.disable_unchecked_drop && !use_bootstrap_weight && !flags.read_only)
{
Expand Down Expand Up @@ -1625,6 +1608,35 @@ void nano::node::epoch_upgrader_impl (nano::private_key const & prv_a, nano::epo
logger.always_log ("Epoch upgrade is completed");
}

std::pair<uint64_t, decltype (nano::ledger::bootstrap_weights)> nano::node::get_bootstrap_weights () const
{
std::unordered_map<nano::account, nano::uint128_t> weights;
const uint8_t * weight_buffer = network_params.network.is_live_network () ? nano_bootstrap_weights_live : nano_bootstrap_weights_beta;
size_t weight_size = network_params.network.is_live_network () ? nano_bootstrap_weights_live_size : nano_bootstrap_weights_beta_size;
nano::bufferstream weight_stream ((const uint8_t *)weight_buffer, weight_size);
nano::uint128_union block_height;
uint64_t max_blocks = 0;
if (!nano::try_read (weight_stream, block_height))
{
max_blocks = nano::narrow_cast<uint64_t> (block_height.number ());
while (true)
{
nano::account account;
if (nano::try_read (weight_stream, account.bytes))
{
break;
}
nano::amount weight;
if (nano::try_read (weight_stream, weight.bytes))
{
break;
}
weights[account] = weight.number ();
}
}
return { max_blocks, weights };
}

nano::inactive_node::inactive_node (boost::filesystem::path const & path_a, nano::node_flags const & node_flags_a) :
io_context (std::make_shared<boost::asio::io_context> ()),
alarm (*io_context),
Expand Down
1 change: 1 addition & 0 deletions nano/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class node final : public std::enable_shared_from_this<nano::node>
bool online () const;
bool init_error () const;
bool epoch_upgrader (nano::private_key const &, nano::epoch, uint64_t, uint64_t);
std::pair<uint64_t, decltype (nano::ledger::bootstrap_weights)> get_bootstrap_weights () const;
nano::worker worker;
nano::write_database_queue write_database_queue;
boost::asio::io_context & io_ctx;
Expand Down

0 comments on commit 2acb1a5

Please sign in to comment.