blocktools.py raw

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