@@ -2730,23 +2730,36 @@ def entry_witness(*args):
27302730 raise RuntimeError(f"unsupported entry witness arg: {arg!r}")
27312731 return "0x" + bytes(out).hex()
27322732
2733- def get_block(block_hash):
2734- block = rpc("get_block", [block_hash])
2735- if block is None:
2736- raise RuntimeError(f"block not found: {block_hash}")
2737- return block
2738-
2739- def get_block_by_number(number):
2740- block = rpc("get_block_by_number", [hex_u64(number)])
2741- if block is None:
2742- raise RuntimeError(f"block number not found: {number}")
2743- return block
2733+ def get_block(block_hash, attempts=20, delay_seconds=0.05):
2734+ block = None
2735+ for _ in range(attempts):
2736+ block = rpc("get_block", [block_hash])
2737+ if block is not None:
2738+ return block
2739+ time.sleep(delay_seconds)
2740+ raise RuntimeError(f"block not found: {block_hash}")
2741+
2742+ def get_block_by_number(number, attempts=20, delay_seconds=0.05):
2743+ block = None
2744+ for _ in range(attempts):
2745+ block = rpc("get_block_by_number", [hex_u64(number)])
2746+ if block is not None:
2747+ return block
2748+ time.sleep(delay_seconds)
2749+ raise RuntimeError(f"block number not found: {number}")
27442750
27452751RESERVED_SPENDABLE_OUTPOINTS = set()
27462752
27472753def spendable_outpoint_key(tx_hash, index):
27482754 return (tx_hash, int(index))
27492755
2756+ def reserve_spendable_outpoint(tx_hash, index):
2757+ key = spendable_outpoint_key(tx_hash, index)
2758+ if key in RESERVED_SPENDABLE_OUTPOINTS:
2759+ return False
2760+ RESERVED_SPENDABLE_OUTPOINTS.add(key)
2761+ return True
2762+
27502763def find_spendable_cellbase(max_blocks=64):
27512764 generated = []
27522765 for _ in range(max_blocks):
@@ -2762,7 +2775,11 @@ def find_spendable_cellbase(max_blocks=64):
27622775 if spendable_outpoint_key(cellbase["hash"], index) in RESERVED_SPENDABLE_OUTPOINTS:
27632776 continue
27642777 live_status = wait_live_cell(cellbase["hash"], index)
2765- if live_status and live_status.get("status") == "live":
2778+ if (
2779+ live_status
2780+ and live_status.get("status") == "live"
2781+ and reserve_spendable_outpoint(cellbase["hash"], index)
2782+ ):
27662783 return {
27672784 "block_hash": block_hash,
27682785 "tx_hash": cellbase["hash"],
@@ -2786,8 +2803,6 @@ def collect_spendable_cellbases(min_capacity, max_cells=256):
27862803 f"collected {total_capacity:#x} capacity from {len(cells)} cellbase cells, "
27872804 f"need at least {min_capacity:#x}"
27882805 )
2789- for cell in cells:
2790- RESERVED_SPENDABLE_OUTPOINTS.add(spendable_outpoint_key(cell["tx_hash"], cell["index"]))
27912806 return {
27922807 "cells": cells,
27932808 "total_capacity": total_capacity,
0 commit comments