diff --git a/mining_heavy3.py b/mining_heavy3.py index cea0855..f766b43 100644 --- a/mining_heavy3.py +++ b/mining_heavy3.py @@ -31,6 +31,7 @@ F = None is_regnet = False +heavy = True def read_int_from_map(map, index): @@ -59,6 +60,17 @@ def anneal3(mmap, n): return res +def anneal3_regnet(mmap, n): + """ + Converts 224 bits number into annealed version, hexstring + This uses a fake anneal for easier regtests + + :param n: a 224 = 7x32 bits + :return: 56 char in hex encoding. + """ + return "{:056x}".format(n) + + def bin_convert(string): return ''.join(format(ord(x), '8b').replace(' ', '0') for x in string) @@ -69,7 +81,10 @@ def diffme_heavy3(pool_address, nonce, db_block_hash): diff_result = 0 hash224 = sha224((pool_address + nonce + db_block_hash).encode("utf-8")).digest() hash224 = int.from_bytes(hash224, 'big') - annealed_sha = anneal3(MMAP, hash224) + if heavy or (not is_regnet): + annealed_sha = anneal3(MMAP, hash224) + else: + annealed_sha = anneal3_regnet(MMAP, hash224) bin_annealed_sha = bin_convert(annealed_sha) mining_condition = bin_convert(db_block_hash) while mining_condition[:diff] in bin_annealed_sha: @@ -144,6 +159,9 @@ def check_block(block_height_new, miner_address, nonce, db_block_hash, diff0, re def create_heavy3a(file_name="heavy3a.bin"): + if (not heavy) and is_regnet: + print("Regnet, no heavy file") + return print("Creating Junction Noise file, this usually takes a few minutes...") gen = DRBG(b"Bismuth is a chemical element with symbol Bi and atomic number 83. It is a pentavalent post-transition metal and one of the pnictogens with chemical properties resembling its lighter homologs arsenic and antimony.") # Size in Gb - No more than 4Gb from a single seed @@ -160,6 +178,9 @@ def mining_open(file_name="heavy3a.bin"): """ Opens the Junction MMapped file """ + if (not heavy) and is_regnet: + print("Regnet, no heavy file to open") + return global F global MMAP global RND_LEN @@ -193,9 +214,11 @@ def mining_close(): """ Close the MMAP access, HAS to be called at end of program. """ + if (not heavy) and is_regnet: + print("Regnet, no heavy file to close") + return global F global MMAP - try: assert MMAP MMAP.close() diff --git a/node.py b/node.py index f6155ac..ce7ead8 100644 --- a/node.py +++ b/node.py @@ -11,7 +11,7 @@ # issues with db? perhaps you missed a commit() or two -VERSION = "4.4.0.11" # Post fork candidate 9 +VERSION = "4.4.0.12" import functools import glob @@ -34,7 +34,7 @@ import options import peershandler import plugins -import tokensv2 as tokens # TODO: unused here +# import tokensv2 as tokens # TODO: unused here import wallet_keys from connections import send, receive from digest import * @@ -42,12 +42,12 @@ from libs import node, logger, keys, client from fork import Fork -#todo: migrate this to polysign\signer_crw.py +# todo: migrate this to polysign\signer_crw.py from Cryptodome.Hash import SHA from Cryptodome.PublicKey import RSA from Cryptodome.Signature import PKCS1_v1_5 import base64 -#/todo +# /todo fork = Fork() @@ -1729,6 +1729,9 @@ def setup_net_type(): if not 'regnet' in node.version: node.logger.app_log.error("Bad regnet version, check config.txt") sys.exit() + if not node.heavy: + node.logger.app_log.warning("Regnet with no heavy file...") + mining_heavy3.heavy = False node.logger.app_log.warning("Regnet init...") regnet.init(node.logger.app_log) regnet.DIGEST_BLOCK = digest_block @@ -2047,6 +2050,7 @@ def add_indices(db_handler: dbhandler.DbHandler): node.trace_db_calls = config.trace_db_calls node.heavy3_path = config.heavy3_path node.old_sqlite = config.old_sqlite + node.heavy = config.heavy node.logger.app_log = log.log("node.log", node.debug_level, node.terminal_output) node.logger.app_log.warning("Configuration settings loaded") @@ -2064,10 +2068,6 @@ def add_indices(db_handler: dbhandler.DbHandler): if not node.full_ledger: node.logger.app_log.warning("Cloning hyperblocks to ledger file") shutil.copy(node.hyper_path, node.ledger_path) # hacked to remove all the endless checks - # needed for docker logs - node.logger.app_log.warning(f"Checking Heavy3 file, can take up to 5 minutes...") - mining_heavy3.mining_open(node.heavy3_path) - node.logger.app_log.warning(f"Heavy3 file Ok!") try: # create a plugin manager, load all plugin modules and init node.plugin_manager = plugins.PluginManager(app_log=node.logger.app_log, config=config, init=True) @@ -2079,6 +2079,11 @@ def add_indices(db_handler: dbhandler.DbHandler): setup_net_type() load_keys() + # needed for docker logs + node.logger.app_log.warning(f"Checking Heavy3 file, can take up to 5 minutes...") + mining_heavy3.mining_open(node.heavy3_path) + node.logger.app_log.warning(f"Heavy3 file Ok!") + node.logger.app_log.warning(f"Status: Starting node version {VERSION}") node.startup_time = time.time() try: diff --git a/options.py b/options.py index 6f9b2fb..3898d98 100644 --- a/options.py +++ b/options.py @@ -10,6 +10,7 @@ class Get: "verify": ["bool", "verify"], "testnet": ["bool"], "regnet": ["bool"], + "heavy": ["bool"], "version": ["str", "version"], "version_allow": ["list"], "thread_limit": ["int", "thread_limit"], @@ -50,6 +51,7 @@ class Get: defaults = { "testnet": False, "regnet": False, + "heavy": True, "trace_db_calls": False, "mempool_ram": True, "heavy3_path": "./heavy3a.bin", diff --git a/regnet.py b/regnet.py index 090a98d..c772064 100644 --- a/regnet.py +++ b/regnet.py @@ -89,9 +89,21 @@ def generate_one_block(blockhash, mempool_txs, node, db_handler): i += 1 seed = ('%0x' % getrandbits(128 - 32)) prefix = ADDRESS + seed - possibles = [nonce for nonce in try_arr if - mining_condition in (mining.anneal3(mining.MMAP, int.from_bytes( - sha224((prefix + nonce + blockhash).encode("utf-8")).digest(), 'big')))] + # print("node heavy", node.heavy) + if node.heavy: + possibles = [nonce for nonce in try_arr if + mining_condition in ( + mining.anneal3(mining.MMAP, + int.from_bytes( + sha224((prefix + nonce + blockhash) + .encode("utf-8")).digest(), 'big')))] + else: + possibles = [nonce for nonce in try_arr if + mining_condition in ( + mining.anneal3_regnet(mining.MMAP, + int.from_bytes( + sha224((prefix + nonce + blockhash) + .encode("utf-8")).digest(), 'big')))] if possibles: nonce = seed + possibles[0] node.logger.app_log.warning("Generate got a block in {} tries len {}".format(i, len(possibles))) diff --git a/tests/common.py b/tests/common.py index b8ddc9a..289e6ac 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1,6 +1,8 @@ # Common functions used in tests # TODO: benchmark both, see use in polysign, signer_rsa (then take from there, class method) +from bismuthclient.bismuthclient import BismuthClient +from os.path import isfile def normalize_key(a): b = "-----BEGIN PUBLIC KEY-----\n" @@ -18,3 +20,14 @@ def normalize_key_alt(s: str) -> str: chunks.insert(0, "-----BEGIN PUBLIC KEY-----") chunks.append("-----END PUBLIC KEY-----") return "\n".join(chunks) + + +def get_client(verbose: bool=False): + # Helper to get a working and conencted BismuthClient no matter the test context + file_first = "../datadir/wallet.der" + file_second = "../wallet.der" + wallet_file = file_first if isfile(file_first) else file_second + client = BismuthClient(servers_list={'127.0.0.1:3030'}, wallet_file=wallet_file, verbose=verbose) + # Will raise and fail test if node is not connectible + assert client is not None + return client diff --git a/tests/config_custom.txt b/tests/config_custom.txt index 92bd99b..ba8ad44 100644 --- a/tests/config_custom.txt +++ b/tests/config_custom.txt @@ -7,6 +7,7 @@ heavy3_path=./heavy3a.bin debug=True port=3030 regnet=True +heavy=False version=regnet version_allow=regnet old_sqlite=True diff --git a/tests/test_mempool.py b/tests/test_mempool.py index 1e34065..be45a9c 100644 --- a/tests/test_mempool.py +++ b/tests/test_mempool.py @@ -5,11 +5,12 @@ from base64 import b64decode from time import sleep -from bismuthclient.bismuthclient import BismuthClient +# from bismuthclient.bismuthclient import BismuthClient +from common import get_client def test_mempool(myserver, verbose=False): - client = BismuthClient(servers_list={'127.0.0.1:3030'}, wallet_file='../datadir/wallet.der', verbose=verbose) + client = get_client(verbose=verbose) if verbose: print("Sending regtest_generate") res = client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds @@ -33,7 +34,7 @@ def test_mempool(myserver, verbose=False): def test_mpget_json(myserver, verbose=False): - client = BismuthClient(servers_list={'127.0.0.1:3030'}, wallet_file='../datadir/wallet.der') + client = get_client(verbose=verbose) client.command(command="regtest_generate", options=[1]) # Mine a block so we have some funds client.send(recipient=client.address, amount=1.0) # Tries to send 1.0 to self data1 = client.command(command="mpget") @@ -46,6 +47,7 @@ def test_mpget_json(myserver, verbose=False): # TODO: double check that output with stable release regnet # (precise pubkey format from mpgetjson) """ + Note: this comment seem tnot to be true anymore with the current stable reference. Double check. mpgetjson seemed to send pubkey without boundaries, while other json answers gave the full string. Where is this used? can we harmonize with no risk? Did not change the behaviour to ensure compatibility if this is important.