1#!/usr/bin/env python3 2# Copyright (c) 2015-2020 The Bitcoin Core developers 3# Distributed under the MIT software license, see the accompanying 4# file COPYING or http://www.opensource.org/licenses/mit-license.php. 5"""Utilities for manipulating blocks and transactions.""" 6 7from binascii import a2b_hex 8import struct 9import time 10import unittest 11 12from .address import ( 13 key_to_p2sh_p2wpkh, 14 key_to_p2wpkh, 15 script_to_p2sh_p2wsh, 16 script_to_p2wsh, 17) 18from .messages import ( 19 CBlock, 20 COIN, 21 COutPoint, 22 CTransaction, 23 CTxIn, 24 CTxInWitness, 25 CTxOut, 26 hash256, 27 hex_str_to_bytes, 28 ser_uint256, 29 tx_from_hex, 30 uint256_from_str, 31) 32from .script import ( 33 CScript, 34 CScriptNum, 35 CScriptOp, 36 OP_1, 37 OP_CHECKMULTISIG, 38 OP_CHECKSIG, 39 OP_RETURN, 40 OP_TRUE, 41) 42from .script_util import ( 43 key_to_p2wpkh_script, 44 script_to_p2wsh_script, 45) 46from .util import assert_equal 47 48WITNESS_SCALE_FACTOR = 4 49MAX_BLOCK_SIGOPS = 20000 50MAX_BLOCK_SIGOPS_WEIGHT = MAX_BLOCK_SIGOPS * WITNESS_SCALE_FACTOR 51 52# Genesis block time (regtest) 53TIME_GENESIS_BLOCK = 1296688602 54 55# Coinbase transaction outputs can only be spent after this number of new blocks (network rule) 56COINBASE_MATURITY = 100 57 58# From BIP141 59WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed" 60 61NORMAL_GBT_REQUEST_PARAMS = {"rules": ["segwit"]} 62 63 64def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl=None, txlist=None): 65 """Create a block (with regtest difficulty).""" 66 block = CBlock() 67 if tmpl is None: 68 tmpl = {} 69 block.nVersion = version or tmpl.get('version') or 1 70 block.nTime = ntime or tmpl.get('curtime') or int(time.time() + 600) 71 block.hashPrevBlock = hashprev or int(tmpl['previousblockhash'], 0x10) 72 if tmpl and not tmpl.get('bits') is None: 73 block.nBits = struct.unpack('>I', a2b_hex(tmpl['bits']))[0] 74 else: 75 block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams 76 if coinbase is None: 77 coinbase = create_coinbase(height=tmpl['height']) 78 block.vtx.append(coinbase) 79 if txlist: 80 for tx in txlist: 81 if not hasattr(tx, 'calc_sha256'): 82 tx = tx_from_hex(tx) 83 block.vtx.append(tx) 84 block.hashMerkleRoot = block.calc_merkle_root() 85 block.calc_sha256() 86 return block 87 88def get_witness_script(witness_root, witness_nonce): 89 witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root) + ser_uint256(witness_nonce))) 90 output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment) 91 return CScript([OP_RETURN, output_data]) 92 93def add_witness_commitment(block, nonce=0): 94 """Add a witness commitment to the block's coinbase transaction. 95 96 According to BIP141, blocks with witness rules active must commit to the 97 hash of all in-block transactions including witness.""" 98 # First calculate the merkle root of the block's 99 # transactions, with witnesses. 100 witness_nonce = nonce 101 witness_root = block.calc_witness_merkle_root() 102 # witness_nonce should go to coinbase witness. 103 block.vtx[0].wit.vtxinwit = [CTxInWitness()] 104 block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(witness_nonce)] 105 106 # witness commitment is the last OP_RETURN output in coinbase 107 block.vtx[0].vout.append(CTxOut(0, get_witness_script(witness_root, witness_nonce))) 108 block.vtx[0].rehash() 109 block.hashMerkleRoot = block.calc_merkle_root() 110 block.rehash() 111 112 113def script_BIP34_coinbase_height(height): 114 if height <= 16: 115 res = CScriptOp.encode_op_n(height) 116 # Append dummy to increase scriptSig size above 2 (see bad-cb-length consensus rule) 117 return CScript([res, OP_1]) 118 return CScript([CScriptNum(height)]) 119 120 121def create_coinbase(height, pubkey=None, extra_output_script=None, fees=0, nValue=50): 122 """Create a coinbase transaction. 123 124 If pubkey is passed in, the coinbase output will be a P2PK output; 125 otherwise an anyone-can-spend output. 126 127 If extra_output_script is given, make a 0-value output to that 128 script. This is useful to pad block weight/sigops as needed. """ 129 coinbase = CTransaction() 130 coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff), script_BIP34_coinbase_height(height), 0xffffffff)) 131 coinbaseoutput = CTxOut() 132 coinbaseoutput.nValue = nValue * COIN 133 if nValue == 50: 134 halvings = int(height / 150) # regtest 135 coinbaseoutput.nValue >>= halvings 136 coinbaseoutput.nValue += fees 137 if pubkey is not None: 138 coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG]) 139 else: 140 coinbaseoutput.scriptPubKey = CScript([OP_TRUE]) 141 coinbase.vout = [coinbaseoutput] 142 if extra_output_script is not None: 143 coinbaseoutput2 = CTxOut() 144 coinbaseoutput2.nValue = 0 145 coinbaseoutput2.scriptPubKey = extra_output_script 146 coinbase.vout.append(coinbaseoutput2) 147 coinbase.calc_sha256() 148 return coinbase 149 150def create_tx_with_script(prevtx, n, script_sig=b"", *, amount, script_pub_key=CScript()): 151 """Return one-input, one-output transaction object 152 spending the prevtx's n-th output with the given amount. 153 154 Can optionally pass scriptPubKey and scriptSig, default is anyone-can-spend output. 155 """ 156 tx = CTransaction() 157 assert n < len(prevtx.vout) 158 tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), script_sig, 0xffffffff)) 159 tx.vout.append(CTxOut(amount, script_pub_key)) 160 tx.calc_sha256() 161 return tx 162 163def create_transaction(node, txid, to_address, *, amount): 164 """ Return signed transaction spending the first output of the 165 input txid. Note that the node must have a wallet that can 166 sign for the output that is being spent. 167 """ 168 raw_tx = create_raw_transaction(node, txid, to_address, amount=amount) 169 tx = tx_from_hex(raw_tx) 170 return tx 171 172def create_raw_transaction(node, txid, to_address, *, amount): 173 """ Return raw signed transaction spending the first output of the 174 input txid. Note that the node must have a wallet that can sign 175 for the output that is being spent. 176 """ 177 psbt = node.createpsbt(inputs=[{"txid": txid, "vout": 0}], outputs={to_address: amount}) 178 for _ in range(2): 179 for w in node.listwallets(): 180 wrpc = node.get_wallet_rpc(w) 181 signed_psbt = wrpc.walletprocesspsbt(psbt) 182 psbt = signed_psbt['psbt'] 183 final_psbt = node.finalizepsbt(psbt) 184 assert_equal(final_psbt["complete"], True) 185 return final_psbt['hex'] 186 187def get_legacy_sigopcount_block(block, accurate=True): 188 count = 0 189 for tx in block.vtx: 190 count += get_legacy_sigopcount_tx(tx, accurate) 191 return count 192 193def get_legacy_sigopcount_tx(tx, accurate=True): 194 count = 0 195 for i in tx.vout: 196 count += i.scriptPubKey.GetSigOpCount(accurate) 197 for j in tx.vin: 198 # scriptSig might be of type bytes, so convert to CScript for the moment 199 count += CScript(j.scriptSig).GetSigOpCount(accurate) 200 return count 201 202def witness_script(use_p2wsh, pubkey): 203 """Create a scriptPubKey for a pay-to-witness TxOut. 204 205 This is either a P2WPKH output for the given pubkey, or a P2WSH output of a 206 1-of-1 multisig for the given pubkey. Returns the hex encoding of the 207 scriptPubKey.""" 208 if not use_p2wsh: 209 # P2WPKH instead 210 pkscript = key_to_p2wpkh_script(pubkey) 211 else: 212 # 1-of-1 multisig 213 witness_program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG]) 214 pkscript = script_to_p2wsh_script(witness_program) 215 return pkscript.hex() 216 217def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount): 218 """Return a transaction (in hex) that spends the given utxo to a segwit output. 219 220 Optionally wrap the segwit output using P2SH.""" 221 if use_p2wsh: 222 program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG]) 223 addr = script_to_p2sh_p2wsh(program) if encode_p2sh else script_to_p2wsh(program) 224 else: 225 addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey) 226 if not encode_p2sh: 227 assert_equal(node.getaddressinfo(addr)['scriptPubKey'], witness_script(use_p2wsh, pubkey)) 228 return node.createrawtransaction([utxo], {addr: amount}) 229 230def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""): 231 """Create a transaction spending a given utxo to a segwit output. 232 233 The output corresponds to the given pubkey: use_p2wsh determines whether to 234 use P2WPKH or P2WSH; encode_p2sh determines whether to wrap in P2SH. 235 sign=True will have the given node sign the transaction. 236 insert_redeem_script will be added to the scriptSig, if given.""" 237 tx_to_witness = create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount) 238 if (sign): 239 signed = node.signrawtransactionwithwallet(tx_to_witness) 240 assert "errors" not in signed or len(["errors"]) == 0 241 return node.sendrawtransaction(signed["hex"]) 242 else: 243 if (insert_redeem_script): 244 tx = tx_from_hex(tx_to_witness) 245 tx.vin[0].scriptSig += CScript([hex_str_to_bytes(insert_redeem_script)]) 246 tx_to_witness = tx.serialize().hex() 247 248 return node.sendrawtransaction(tx_to_witness) 249 250class TestFrameworkBlockTools(unittest.TestCase): 251 def test_create_coinbase(self): 252 height = 20 253 coinbase_tx = create_coinbase(height=height) 254 assert_equal(CScriptNum.decode(coinbase_tx.vin[0].scriptSig), height) 255