Create a block (with regtest difficulty).
(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl=None, txlist=None)
| 73 | |
| 74 | |
| 75 | def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl=None, txlist=None): |
| 76 | """Create a block (with regtest difficulty).""" |
| 77 | block = CBlock() |
| 78 | if tmpl is None: |
| 79 | tmpl = {} |
| 80 | block.nVersion = version or tmpl.get('version') or VERSIONBITS_LAST_OLD_BLOCK_VERSION |
| 81 | block.nTime = ntime or tmpl.get('curtime') or int(time.time() + 600) |
| 82 | block.hashPrevBlock = hashprev or int(tmpl['previousblockhash'], 0x10) |
| 83 | if tmpl and not tmpl.get('bits') is None: |
| 84 | block.nBits = struct.unpack('>I', bytes.fromhex(tmpl['bits']))[0] |
| 85 | else: |
| 86 | block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams |
| 87 | if coinbase is None: |
| 88 | coinbase = create_coinbase(height=tmpl['height']) |
| 89 | block.vtx.append(coinbase) |
| 90 | block.proof = CProof(bytearray.fromhex('51'), bytearray.fromhex('')) |
| 91 | block.block_height = get_coinbase_height(coinbase) |
| 92 | if txlist: |
| 93 | for tx in txlist: |
| 94 | if not hasattr(tx, 'calc_sha256'): |
| 95 | tx = tx_from_hex(tx) |
| 96 | block.vtx.append(tx) |
| 97 | block.hashMerkleRoot = block.calc_merkle_root() |
| 98 | block.calc_sha256() |
| 99 | return block |
| 100 | |
| 101 | def get_witness_script(witness_root, witness_nonce): |
| 102 | witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root) + ser_uint256(witness_nonce))) |