Create weights in plain text by python script

This commit is contained in:
RickiNano 2024-10-26 14:05:45 +02:00
commit 202d552985
2 changed files with 17 additions and 60 deletions

View file

@ -7,7 +7,7 @@ from binascii import hexlify, unhexlify
parser = argparse.ArgumentParser(
description='Generate bootstrap representative weight file.')
parser.add_argument("output", type=str, help="output weight file")
parser.add_argument("network", type=str, help="Network name. Eg Live or Beta")
parser.add_argument("--rpc", help="node rpc host:port",
default="http://[::1]:7076")
parser.add_argument(
@ -43,9 +43,12 @@ for rep in reps:
supplymax /= int('1000000000000000000000000000000')
supplymax = int(supplymax * args.limit)
supplymax *= int('1000000000000000000000000000000')
outputfile = 'bootstrap_weights_' + args.network + '.hpp'
with open(args.output, 'wb') as of:
of.write(unhexlify("%032X" % block_height))
with open(outputfile, 'w') as of:
of.write(f"#pragma once\n\n#include <string>\n#include <vector>\nnamespace nano::weights\n{{\n")
of.write(f"// Bootstrap weights for {args.network} network\n")
of.write(f"std::vector<std::pair<std::string, std::string>> preconfigured_weights_{args.network} = {{\n")
total = int(0)
count = 0
@ -54,17 +57,20 @@ with open(args.output, 'wb') as of:
break
acc_val = int(hexlify(b32decode(rep["account"].encode(
'utf-8').replace(b"nano_", b"").translate(tbl) + b"====")), 16)
acc_bytes = unhexlify("%064X" % (((acc_val >> 36) & ((1 << 256) - 1))))
weight_bytes = unhexlify("%032X" % rep["weight"])
of.write(acc_bytes)
of.write(weight_bytes)
of.write(f'\t{{ "{rep["account"]}", "{rep["weight"]}" }},\n')
total += rep["weight"]
count += 1
print(rep["account"] + ": " + str(rep["weight"]))
print(f'{rep["account"]} {rep["weight"]}')
if total >= supplymax:
break
print("wrote %d rep weights" % count)
print("max supply %d" % supplymax)
of.write(f'}};\n')
of.write(f'uint64_t max_blocks_{args.network} = {block_height};\n')
of.write(f"}}\n")
print(f"wrote {count} rep weights")
print(f"max supply {supplymax}")
print(f"Weight file generated: {outputfile}")
of.close()

View file

@ -62,7 +62,6 @@ void nano::add_node_options (boost::program_options::options_description & descr
("diagnostics", "Run internal diagnostics")
("generate_config", boost::program_options::value<std::string> (), "Write configuration to stdout, populated with defaults suitable for this system. Pass the configuration type node, rpc or log. See also use_defaults.")
("update_config", "Reads the current node configuration and updates it with missing keys and values and delete keys that are no longer used. Updated configuration is written to stdout.")
("create_weights_file", "Generates data for bootstrap_weights_live.hpp or bootstrap_weights_beta.hpp file. Run on a fully bootstrapped ledger")
("key_create", "Generates a adhoc random keypair and prints it to stdout")
("key_expand", "Derive public key and account number from <key>")
("wallet_add_adhoc", "Insert <key> in to <wallet>")
@ -751,54 +750,6 @@ std::error_code nano::handle_node_options (boost::program_options::variables_map
std::cout << output;
}
}
else if (vm.count ("create_weights_file"))
{
auto inactive_node = nano::default_inactive_node (data_path, vm);
auto is_live = inactive_node->node->network_params.network.is_live_network ();
auto is_beta = inactive_node->node->network_params.network.is_beta_network ();
auto block_count = inactive_node->node->ledger.block_count ();
auto rep_amounts = inactive_node->node->ledger.cache.rep_weights.get_rep_amounts ();
std::vector<std::pair<nano::uint128_t, std::string>> representation;
for (auto & rep_amount : rep_amounts)
{
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 ());
auto now = std::chrono::system_clock::now ();
auto in_time_t = std::chrono::system_clock::to_time_t (now);
if (is_live)
{
std::cout << "// Bootstrap weights for live generated " << std::put_time (std::localtime (&in_time_t), "%d-%m-%Y") << std::endl;
std::cout << "std::vector<std::pair<std::string, std::string>> preconfigured_weights_live = {" << std::endl;
}
else if (is_beta)
{
std::cout << "// Bootstrap weights for beta generated " << std::put_time (std::localtime (&in_time_t), "%d-%m-%Y") << std::endl;
std::cout << "std::vector<std::pair<std::string, std::string>> preconfigured_weights_beta = {" << std::endl;
}
int count = 0;
for (const auto & [key, value] : representation)
{
if (count >= 200)
break;
std::cout << "\t{ \"" << value << "\", \"" << key << "\" }," << std::endl;
++count;
}
std::cout << "};" << std::endl;
if (is_live)
{
std::cout << "uint64_t max_blocks_live = " << block_count << ";" << std::endl;
}
else if (is_beta)
{
std::cout << "uint64_t max_blocks_beta = " << block_count << ";" << std::endl;
}
}
else if (vm.count ("diagnostics"))
{
auto inactive_node = nano::default_inactive_node (data_path, vm);