dncurrency/record_rep_weights.py
Minecon724 d914e33fd2
Some checks failed
Static Analyzers / clang_format (push) Waiting to run
Static Analyzers / cmake_format (push) Waiting to run
Static Analyzers / code_inspector (push) Waiting to run
code_sanitizers.yml / Snapshot network state (push) Failing after 0s
Code Flamegraphs / Linux [large_confirmation] (push) Waiting to run
Code Flamegraphs / Linux [large_direct_processing] (push) Waiting to run
Unit Tests / macOS [lmdb] (push) Waiting to run
Unit Tests / macOS [rocksdb] (push) Waiting to run
Unit Tests / Linux [lmdb | clang] (push) Waiting to run
Unit Tests / Linux [lmdb | gcc] (push) Waiting to run
Unit Tests / Linux [rocksdb | clang] (push) Waiting to run
Unit Tests / Linux [rocksdb | gcc] (push) Waiting to run
Unit Tests / Windows [lmdb] (push) Waiting to run
Unit Tests / Windows [rocksdb] (push) Waiting to run
Snapshot network state
2025-08-31 15:46:44 +02:00

76 lines
2.6 KiB
Python

import requests
import argparse
from base64 import b32decode
from binascii import hexlify
from datetime import date
parser = argparse.ArgumentParser(
description='Generate bootstrap representative 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(
"--limit", help="percentage of the active supply represented", default=0.99)
parser.add_argument(
"--cutoff", help="stop using bootstrap reps this many blocks before the current block height", default=250000, type=int)
args = parser.parse_args()
r = requests.post(args.rpc, data='{"action":"representatives"}')
p = r.json()
reps = []
tbl = bytes.maketrans(b'13456789abcdefghijkmnopqrstuwxyz',
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567')
for acc in p["representatives"]:
reps.append({
'account': acc,
'weight': int(p["representatives"][acc])
})
r = requests.post(args.rpc, data='{"action":"block_count"}')
p = r.json()
block_height = max(0, int(p["cemented"]) - args.cutoff)
print("cutoff block height is %d" % block_height)
reps.sort(key=lambda x: x["weight"], reverse=True)
supplymax = int(0)
for rep in reps:
supplymax += rep["weight"]
supplymax /= int('1000000000000000000000000000000')
supplymax = int(supplymax * args.limit)
supplymax *= int('1000000000000000000000000000000')
outputfile = 'bootstrap_weights_' + args.network + '.hpp'
today = date.today().strftime("%d-%m-%Y")
with open(outputfile, 'w') as of:
of.write(f"#pragma once\n\n#include <string>\n#include <vector>\n\nnamespace nano::weights\n{{\n")
of.write(f"// Bootstrap weights for {args.network} network as of {today}\n")
of.write(f"std::vector<std::pair<std::string, std::string>> preconfigured_weights_{args.network} = {{\n")
total = int(0)
count = 0
for rep in reps:
if rep["weight"] == 0:
break
acc_val = int(hexlify(b32decode(rep["account"].encode(
'utf-8').replace(b"dn_", b"").translate(tbl) + b"====")), 16)
of.write(f'\t{{ "{rep["account"]}", "{rep["weight"]}" }},\n')
total += rep["weight"]
count += 1
print(f'{rep["account"]} {rep["weight"]}')
if total >= supplymax:
break
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()