Create a block (with regtest difficulty).
(hashprev=None, coinbase=None, *, ntime=None, height=None, version=None, tmpl=None, txlist=None)
| 96 | return f"{target:064x}" |
| 97 | |
| 98 | def create_block(hashprev=None, coinbase=None, *, ntime=None, height=None, version=None, tmpl=None, txlist=None): |
| 99 | """Create a block (with regtest difficulty).""" |
| 100 | block = CBlock() |
| 101 | if tmpl is None: |
| 102 | tmpl = {} |
| 103 | block.nVersion = version or tmpl.get('version') or VERSIONBITS_LAST_OLD_BLOCK_VERSION |
| 104 | block.nTime = ntime or tmpl.get('curtime') or int(time.time() + 600) |
| 105 | block.hashPrevBlock = hashprev or int(tmpl['previousblockhash'], 0x10) |
| 106 | if tmpl and tmpl.get('bits') is not None: |
| 107 | block.nBits = struct.unpack('>I', bytes.fromhex(tmpl['bits']))[0] |
| 108 | else: |
| 109 | block.nBits = REGTEST_N_BITS |
| 110 | if coinbase is None: |
| 111 | coinbase = create_coinbase(height=height or tmpl["height"]) |
| 112 | block.vtx.append(coinbase) |
| 113 | if txlist: |
| 114 | for tx in txlist: |
| 115 | if type(tx) is str: |
| 116 | tx = tx_from_hex(tx) |
| 117 | block.vtx.append(tx) |
| 118 | block.hashMerkleRoot = block.calc_merkle_root() |
| 119 | return block |
| 120 | |
| 121 | def create_empty_fork(node, fork_length=FORK_LENGTH): |
| 122 | ''' |