From 0270b35528388241a6e6b843503cea342c640c30 Mon Sep 17 00:00:00 2001 From: Nsable-sh Date: Tue, 11 Aug 2026 15:11:40 -0300 Subject: tests: add deterministic chain tool fixtures --- tests/scripts/test_bitflash_tools.py | 178 ++++++++++++++++++++++++++++++++++- 1 file changed, 177 insertions(+), 1 deletion(-) (limited to 'tests/scripts/test_bitflash_tools.py') diff --git a/tests/scripts/test_bitflash_tools.py b/tests/scripts/test_bitflash_tools.py index ff569e1..4b18d1e 100644 --- a/tests/scripts/test_bitflash_tools.py +++ b/tests/scripts/test_bitflash_tools.py @@ -12,6 +12,7 @@ from pathlib import Path ROOT = Path(__file__).resolve().parents[2] SCRIPTS = ROOT / "scripts" +FIXTURES = ROOT / "tests" / "fixtures" / "chain-tools" sys.path.insert(0, str(SCRIPTS)) import bitflash_chain as chain # noqa: E402 @@ -45,12 +46,130 @@ def write_block_file(path, blocks): f.write(payload) +def p2pkh_script(byte): + return "76a914%s88ac" % (bytes([byte]) * 20).hex() + + +def make_tx(vin, vout): + tx = { + "version": 1, + "vin": vin, + "vout": vout, + "lock_time": 0, + } + tx["txid"] = chain.hash_hex(chain.ser_tx(tx)) + return tx + + +def make_coinbase(label, script_byte, value=50 * chain.COIN): + return make_tx( + [{ + "prevout_hash": "0" * 64, + "prevout_n": 0xFFFFFFFF, + "script_sig_hex": label.encode("ascii").hex(), + "sequence": 0xFFFFFFFF, + }], + [{ + "value_satoshis": value, + "value_btf": chain.format_money(value), + "script_pub_key_hex": p2pkh_script(script_byte), + }], + ) + + +def make_spend(prev_txid, prev_n, outputs, label): + return make_tx( + [{ + "prevout_hash": prev_txid, + "prevout_n": prev_n, + "script_sig_hex": label.encode("ascii").hex(), + "sequence": 0xFFFFFFFF, + }], + [{ + "value_satoshis": value, + "value_btf": chain.format_money(value), + "script_pub_key_hex": p2pkh_script(script_byte), + } for value, script_byte in outputs], + ) + + +def make_block(prev_hash, txs, ntime, nonce): + merkle = chain.merkle_root([tx["txid"] for tx in txs]) + header = ( + struct.pack("