feature_segwit.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2016-present 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  """Test the SegWit changeover logic."""
   6  
   7  from decimal import Decimal
   8  
   9  from test_framework.address import (
  10      script_to_p2sh_p2wsh,
  11      script_to_p2wsh,
  12  )
  13  from test_framework.blocktools import (
  14      NORMAL_GBT_REQUEST_PARAMS,
  15      send_to_witness,
  16      witness_script,
  17  )
  18  from test_framework.descriptors import descsum_create
  19  from test_framework.messages import (
  20      COIN,
  21      COutPoint,
  22      CTransaction,
  23      CTxIn,
  24      CTxOut,
  25      tx_from_hex,
  26  )
  27  from test_framework.script import (
  28      CScript,
  29      OP_DROP,
  30      OP_TRUE,
  31  )
  32  from test_framework.script_util import (
  33      keys_to_multisig_script,
  34  )
  35  from test_framework.test_framework import BitcoinTestFramework
  36  from test_framework.util import (
  37      assert_equal,
  38      assert_greater_than_or_equal,
  39      assert_is_hex_string,
  40      assert_raises_rpc_error,
  41  )
  42  from test_framework.wallet_util import (
  43      get_generate_key,
  44  )
  45  
  46  NODE_0 = 0
  47  NODE_2 = 2
  48  P2WPKH = 0
  49  P2WSH = 1
  50  
  51  
  52  def getutxo(txid):
  53      utxo = {}
  54      utxo["vout"] = 0
  55      utxo["txid"] = txid
  56      return utxo
  57  
  58  
  59  def find_spendable_utxo(node, min_value):
  60      for utxo in node.listunspent(query_options={'minimumAmount': min_value}):
  61          if utxo['spendable']:
  62              return utxo
  63  
  64      raise AssertionError(f"Unspent output equal or higher than {min_value} not found")
  65  
  66  
  67  class SegWitTest(BitcoinTestFramework):
  68      def set_test_params(self):
  69          self.setup_clean_chain = True
  70          self.num_nodes = 3
  71          # This test tests SegWit both pre and post-activation, so use the normal BIP9 activation.
  72          self.extra_args = [
  73              [
  74                  "-acceptnonstdtxn=1",
  75                  "-testactivationheight=segwit@165",
  76                  "-addresstype=legacy",
  77              ],
  78              [
  79                  "-acceptnonstdtxn=1",
  80                  "-testactivationheight=segwit@165",
  81                  "-addresstype=legacy",
  82              ],
  83              [
  84                  "-acceptnonstdtxn=1",
  85                  "-testactivationheight=segwit@165",
  86                  "-addresstype=legacy",
  87              ],
  88          ]
  89          self.rpc_timeout = 120
  90  
  91      def skip_test_if_missing_module(self):
  92          self.skip_if_no_wallet()
  93  
  94      def setup_network(self):
  95          super().setup_network()
  96          self.connect_nodes(0, 2)
  97          self.sync_all()
  98  
  99      def success_mine(self, node, txid, sign, redeem_script=""):
 100          send_to_witness(1, node, getutxo(txid), self.pubkey[0], False, Decimal("49.998"), sign, redeem_script)
 101          block = self.generate(node, 1)
 102          assert_equal(len(node.getblock(block[0])["tx"]), 2)
 103          self.sync_blocks()
 104  
 105      def fail_accept(self, node, error_msg, txid, sign, redeem_script=""):
 106          assert_raises_rpc_error(-26, error_msg, send_to_witness, use_p2wsh=1, node=node, utxo=getutxo(txid), pubkey=self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=sign, insert_redeem_script=redeem_script)
 107  
 108      def run_test(self):
 109          self.generate(self.nodes[0], 161)  # block 161
 110  
 111          self.log.info("Verify sigops are counted in GBT with pre-BIP141 rules before the fork")
 112          txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
 113          tmpl = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
 114          assert_equal(tmpl['sizelimit'], 1000000)
 115          assert 'weightlimit' not in tmpl
 116          assert_equal(tmpl['sigoplimit'], 20000)
 117          assert_equal(tmpl['transactions'][0]['hash'], txid)
 118          assert_equal(tmpl['transactions'][0]['sigops'], 2)
 119          assert '!segwit' not in tmpl['rules']
 120          self.generate(self.nodes[0], 1)  # block 162
 121  
 122          balance_presetup = self.nodes[0].getbalance()
 123          self.pubkey = []
 124          p2sh_ids = []  # p2sh_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE embedded in p2sh
 125          wit_ids = []  # wit_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE via bare witness
 126          for i in range(3):
 127              key = get_generate_key()
 128              self.pubkey.append(key.pubkey)
 129  
 130              multiscript = keys_to_multisig_script([self.pubkey[-1]])
 131              p2sh_ms_addr = self.nodes[i].createmultisig(1, [self.pubkey[-1]], 'p2sh-segwit')['address']
 132              bip173_ms_addr = self.nodes[i].createmultisig(1, [self.pubkey[-1]], 'bech32')['address']
 133              assert_equal(p2sh_ms_addr, script_to_p2sh_p2wsh(multiscript))
 134              assert_equal(bip173_ms_addr, script_to_p2wsh(multiscript))
 135  
 136              p2sh_ms_desc = descsum_create(f"sh(wsh(multi(1,{key.privkey})))")
 137              bip173_ms_desc = descsum_create(f"wsh(multi(1,{key.privkey}))")
 138              assert_equal(self.nodes[i].deriveaddresses(p2sh_ms_desc)[0], p2sh_ms_addr)
 139              assert_equal(self.nodes[i].deriveaddresses(bip173_ms_desc)[0], bip173_ms_addr)
 140  
 141              sh_wpkh_desc = descsum_create(f"sh(wpkh({key.privkey}))")
 142              wpkh_desc = descsum_create(f"wpkh({key.privkey})")
 143              assert_equal(self.nodes[i].deriveaddresses(sh_wpkh_desc)[0], key.p2sh_p2wpkh_addr)
 144              assert_equal(self.nodes[i].deriveaddresses(wpkh_desc)[0], key.p2wpkh_addr)
 145  
 146              res = self.nodes[i].importdescriptors([
 147                  {"desc": p2sh_ms_desc, "timestamp": "now"},
 148                  {"desc": bip173_ms_desc, "timestamp": "now"},
 149                  {"desc": sh_wpkh_desc, "timestamp": "now"},
 150                  {"desc": wpkh_desc, "timestamp": "now"},
 151              ])
 152              assert all([r["success"] for r in res])
 153  
 154              p2sh_ids.append([])
 155              wit_ids.append([])
 156              for _ in range(2):
 157                  p2sh_ids[i].append([])
 158                  wit_ids[i].append([])
 159  
 160          for _ in range(5):
 161              for n in range(3):
 162                  for v in range(2):
 163                      wit_ids[n][v].append(send_to_witness(v, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[n], False, Decimal("49.999")))
 164                      p2sh_ids[n][v].append(send_to_witness(v, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[n], True, Decimal("49.999")))
 165  
 166          self.generate(self.nodes[0], 1)  # block 163
 167  
 168          # Make sure all nodes recognize the transactions as theirs
 169          assert_equal(self.nodes[0].getbalance(), balance_presetup - 60 * 50 + 20 * Decimal("49.999") + 50)
 170          assert_equal(self.nodes[1].getbalance(), 20 * Decimal("49.999"))
 171          assert_equal(self.nodes[2].getbalance(), 20 * Decimal("49.999"))
 172  
 173          self.log.info("Verify unsigned p2sh witness txs without a redeem script are invalid")
 174          self.fail_accept(self.nodes[2], "mempool-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_2][P2WPKH][1], sign=False)
 175          self.fail_accept(self.nodes[2], "mempool-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_2][P2WSH][1], sign=False)
 176  
 177          self.generate(self.nodes[0], 1)  # block 164
 178  
 179          self.log.info("Verify witness txs are mined as soon as segwit activates")
 180  
 181          send_to_witness(1, self.nodes[2], getutxo(wit_ids[NODE_2][P2WPKH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True)
 182          send_to_witness(1, self.nodes[2], getutxo(wit_ids[NODE_2][P2WSH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True)
 183          send_to_witness(1, self.nodes[2], getutxo(p2sh_ids[NODE_2][P2WPKH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True)
 184          send_to_witness(1, self.nodes[2], getutxo(p2sh_ids[NODE_2][P2WSH][0]), self.pubkey[0], encode_p2sh=False, amount=Decimal("49.998"), sign=True)
 185  
 186          assert_equal(len(self.nodes[2].getrawmempool()), 4)
 187          blockhash = self.generate(self.nodes[2], 1)[0]  # block 165 (first block with new rules)
 188          assert_equal(len(self.nodes[2].getrawmempool()), 0)
 189          segwit_tx_list = self.nodes[2].getblock(blockhash)["tx"]
 190          assert_equal(len(segwit_tx_list), 5)
 191  
 192          self.log.info("Verify default node can't accept txs with missing witness")
 193          # unsigned, no scriptsig
 194          self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Witness program hash mismatch)", wit_ids[NODE_0][P2WPKH][0], sign=False)
 195          self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Witness program was passed an empty witness)", wit_ids[NODE_0][P2WSH][0], sign=False)
 196          self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_0][P2WPKH][0], sign=False)
 197          self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Operation not valid with the current stack size)", p2sh_ids[NODE_0][P2WSH][0], sign=False)
 198          # unsigned with redeem script
 199          self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Witness program hash mismatch)", p2sh_ids[NODE_0][P2WPKH][0], sign=False, redeem_script=witness_script(False, self.pubkey[0]))
 200          self.fail_accept(self.nodes[0], "mempool-script-verify-flag-failed (Witness program was passed an empty witness)", p2sh_ids[NODE_0][P2WSH][0], sign=False, redeem_script=witness_script(True, self.pubkey[0]))
 201  
 202          # Coinbase contains the witness commitment nonce, check that RPC shows us
 203          coinbase_txid = self.nodes[2].getblock(blockhash)['tx'][0]
 204          coinbase_tx = self.nodes[2].gettransaction(txid=coinbase_txid, verbose=True)
 205          witnesses = coinbase_tx["decoded"]["vin"][0]["txinwitness"]
 206          assert_equal(len(witnesses), 1)
 207          assert_is_hex_string(witnesses[0])
 208          assert_equal(witnesses[0], '00' * 32)
 209  
 210          self.log.info("Verify witness txs without witness data are invalid after the fork")
 211          self.fail_accept(self.nodes[2], 'mempool-script-verify-flag-failed (Witness program hash mismatch)', wit_ids[NODE_2][P2WPKH][2], sign=False)
 212          self.fail_accept(self.nodes[2], 'mempool-script-verify-flag-failed (Witness program was passed an empty witness)', wit_ids[NODE_2][P2WSH][2], sign=False)
 213          self.fail_accept(self.nodes[2], 'mempool-script-verify-flag-failed (Witness program hash mismatch)', p2sh_ids[NODE_2][P2WPKH][2], sign=False, redeem_script=witness_script(False, self.pubkey[2]))
 214          self.fail_accept(self.nodes[2], 'mempool-script-verify-flag-failed (Witness program was passed an empty witness)', p2sh_ids[NODE_2][P2WSH][2], sign=False, redeem_script=witness_script(True, self.pubkey[2]))
 215  
 216          self.log.info("Verify default node can now use witness txs")
 217          self.success_mine(self.nodes[0], wit_ids[NODE_0][P2WPKH][0], True)
 218          self.success_mine(self.nodes[0], wit_ids[NODE_0][P2WSH][0], True)
 219          self.success_mine(self.nodes[0], p2sh_ids[NODE_0][P2WPKH][0], True)
 220          self.success_mine(self.nodes[0], p2sh_ids[NODE_0][P2WSH][0], True)
 221  
 222          self.log.info("Verify sigops are counted in GBT with BIP141 rules after the fork")
 223          txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
 224          raw_tx = self.nodes[0].getrawtransaction(txid, True)
 225          tmpl = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
 226          assert_greater_than_or_equal(tmpl['sizelimit'], 3999577)  # actual maximum size is lower due to minimum mandatory non-witness data
 227          assert_equal(tmpl['weightlimit'], 4000000)
 228          assert_equal(tmpl['sigoplimit'], 80000)
 229          assert_equal(tmpl['transactions'][0]['txid'], txid)
 230          expected_sigops = 9 if 'txinwitness' in raw_tx["vin"][0] else 8
 231          assert_equal(tmpl['transactions'][0]['sigops'], expected_sigops)
 232          assert '!segwit' in tmpl['rules']
 233  
 234          self.generate(self.nodes[0], 1)  # Mine a block to clear the gbt cache
 235  
 236          self.log.info("Non-segwit miners are able to use GBT response after activation.")
 237          # Create a 3-tx chain: tx1 (non-segwit input, paying to a segwit output) ->
 238          #                      tx2 (segwit input, paying to a non-segwit output) ->
 239          #                      tx3 (non-segwit input, paying to a non-segwit output).
 240          # tx1 is allowed to appear in the block, but no others.
 241          txid1 = send_to_witness(1, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.996"))
 242          assert txid1 in self.nodes[0].getrawmempool()
 243  
 244          tx1_hex = self.nodes[0].gettransaction(txid1)['hex']
 245          tx1 = tx_from_hex(tx1_hex)
 246  
 247          # Check that wtxid is properly reported in mempool entry (txid1)
 248          assert_equal(self.nodes[0].getmempoolentry(txid1)["wtxid"], tx1.wtxid_hex)
 249  
 250          # Check that weight and vsize are properly reported in mempool entry (txid1)
 251          assert_equal(self.nodes[0].getmempoolentry(txid1)["vsize"], tx1.get_vsize())
 252          assert_equal(self.nodes[0].getmempoolentry(txid1)["weight"], tx1.get_weight())
 253  
 254          # Now create tx2, which will spend from txid1.
 255          tx = CTransaction()
 256          tx.vin.append(CTxIn(COutPoint(int(txid1, 16), 0), b''))
 257          tx.vout.append(CTxOut(int(49.99 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
 258          tx2_hex = self.nodes[0].signrawtransactionwithwallet(tx.serialize().hex())['hex']
 259          txid2 = self.nodes[0].sendrawtransaction(tx2_hex)
 260          tx = tx_from_hex(tx2_hex)
 261          assert not tx.wit.is_null()
 262  
 263          # Check that wtxid is properly reported in mempool entry (txid2)
 264          assert_equal(self.nodes[0].getmempoolentry(txid2)["wtxid"], tx.wtxid_hex)
 265  
 266          # Check that weight and vsize are properly reported in mempool entry (txid2)
 267          assert_equal(self.nodes[0].getmempoolentry(txid2)["vsize"], tx.get_vsize())
 268          assert_equal(self.nodes[0].getmempoolentry(txid2)["weight"], tx.get_weight())
 269  
 270          # Now create tx3, which will spend from txid2
 271          tx = CTransaction()
 272          tx.vin.append(CTxIn(COutPoint(int(txid2, 16), 0), b""))
 273          tx.vout.append(CTxOut(int(49.95 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))  # Huge fee
 274          txid3 = self.nodes[0].sendrawtransaction(hexstring=tx.serialize().hex(), maxfeerate=0)
 275          assert tx.wit.is_null()
 276          assert txid3 in self.nodes[0].getrawmempool()
 277  
 278          # Check that getblocktemplate includes all transactions.
 279          template = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
 280          template_txids = [t['txid'] for t in template['transactions']]
 281          assert txid1 in template_txids
 282          assert txid2 in template_txids
 283          assert txid3 in template_txids
 284  
 285          # Check that wtxid is properly reported in mempool entry (txid3)
 286          assert_equal(self.nodes[0].getmempoolentry(txid3)["wtxid"], tx.wtxid_hex)
 287  
 288          # Check that weight and vsize are properly reported in mempool entry (txid3)
 289          assert_equal(self.nodes[0].getmempoolentry(txid3)["vsize"], tx.get_vsize())
 290          assert_equal(self.nodes[0].getmempoolentry(txid3)["weight"], tx.get_weight())
 291  
 292          # Mine a block to clear the gbt cache again.
 293          self.generate(self.nodes[0], 1)
 294  
 295  
 296  if __name__ == '__main__':
 297      SegWitTest(__file__).main()
 298