p2p_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 segwit transactions and blocks on P2P network."""
   6  from decimal import Decimal
   7  import random
   8  
   9  from test_framework.blocktools import (
  10      WITNESS_COMMITMENT_HEADER,
  11      add_witness_commitment,
  12      create_block,
  13  )
  14  from test_framework.messages import (
  15      MAX_BIP125_RBF_SEQUENCE,
  16      CBlockHeader,
  17      CInv,
  18      COutPoint,
  19      CTransaction,
  20      CTxIn,
  21      CTxInWitness,
  22      CTxOut,
  23      CTxWitness,
  24      MAX_BLOCK_WEIGHT,
  25      MSG_BLOCK,
  26      MSG_TX,
  27      MSG_WITNESS_FLAG,
  28      MSG_WITNESS_TX,
  29      MSG_WTX,
  30      NODE_NETWORK,
  31      NODE_WITNESS,
  32      msg_no_witness_block,
  33      msg_getdata,
  34      msg_headers,
  35      msg_inv,
  36      msg_tx,
  37      msg_block,
  38      msg_no_witness_tx,
  39      ser_uint256,
  40      ser_vector,
  41      sha256,
  42  )
  43  from test_framework.p2p import (
  44      P2PInterface,
  45      p2p_lock,
  46      P2P_SERVICES,
  47  )
  48  from test_framework.script import (
  49      CScript,
  50      CScriptNum,
  51      CScriptOp,
  52      MAX_SCRIPT_ELEMENT_SIZE,
  53      OP_0,
  54      OP_1,
  55      OP_2,
  56      OP_16,
  57      OP_2DROP,
  58      OP_CHECKMULTISIG,
  59      OP_CHECKSIG,
  60      OP_DROP,
  61      OP_ELSE,
  62      OP_ENDIF,
  63      OP_IF,
  64      OP_RETURN,
  65      OP_TRUE,
  66      SIGHASH_ALL,
  67      SIGHASH_ANYONECANPAY,
  68      SIGHASH_NONE,
  69      SIGHASH_SINGLE,
  70      hash160,
  71      sign_input_legacy,
  72      sign_input_segwitv0,
  73  )
  74  from test_framework.script_util import (
  75      key_to_p2pk_script,
  76      key_to_p2wpkh_script,
  77      keyhash_to_p2pkh_script,
  78      script_to_p2sh_script,
  79      script_to_p2wsh_script,
  80  )
  81  from test_framework.test_framework import BitcoinTestFramework
  82  from test_framework.util import (
  83      assert_not_equal,
  84      assert_greater_than_or_equal,
  85      assert_equal,
  86      assert_raises_rpc_error,
  87      ensure_for,
  88      softfork_active,
  89  )
  90  from test_framework.wallet import MiniWallet
  91  from test_framework.wallet_util import generate_keypair
  92  
  93  
  94  MAX_SIGOP_COST = 80000
  95  
  96  SEGWIT_HEIGHT = 120
  97  
  98  class UTXO():
  99      """Used to keep track of anyone-can-spend outputs that we can use in the tests."""
 100      def __init__(self, sha256, n, value):
 101          self.sha256 = sha256
 102          self.n = n
 103          self.nValue = value
 104  
 105  
 106  def subtest(func):
 107      """Wraps the subtests for logging and state assertions."""
 108      def func_wrapper(self, *args, **kwargs):
 109          self.log.info("Subtest: {} (Segwit active = {})".format(func.__name__, self.segwit_active))
 110          # Assert segwit status is as expected
 111          assert_equal(softfork_active(self.nodes[0], 'segwit'), self.segwit_active)
 112          func(self, *args, **kwargs)
 113          # Each subtest should leave some utxos for the next subtest
 114          assert self.utxo
 115          self.sync_blocks()
 116          # Assert segwit status is as expected at end of subtest
 117          assert_equal(softfork_active(self.nodes[0], 'segwit'), self.segwit_active)
 118  
 119      return func_wrapper
 120  
 121  
 122  def sign_p2pk_witness_input(script, tx_to, in_idx, hashtype, value, key):
 123      """Add signature for a P2PK witness script."""
 124      tx_to.wit.vtxinwit[in_idx].scriptWitness.stack = [script]
 125      sign_input_segwitv0(tx_to, in_idx, script, value, key, hashtype)
 126  
 127  def test_transaction_acceptance(node, p2p, tx, with_witness, accepted, reason=None):
 128      """Send a transaction to the node and check that it's accepted to the mempool
 129  
 130      - Submit the transaction over the p2p interface
 131      - use the getrawmempool rpc to check for acceptance."""
 132      reason = [reason] if reason else []
 133      with node.assert_debug_log(expected_msgs=reason):
 134          p2p.send_and_ping(msg_tx(tx) if with_witness else msg_no_witness_tx(tx))
 135          assert_equal(tx.txid_hex in node.getrawmempool(), accepted)
 136  
 137  
 138  def test_witness_block(node, p2p, block, accepted, with_witness=True, reason=None):
 139      """Send a block to the node and check that it's accepted
 140  
 141      - Submit the block over the p2p interface
 142      - use the getbestblockhash rpc to check for acceptance."""
 143      reason = [reason] if reason else []
 144      with node.assert_debug_log(expected_msgs=reason):
 145          p2p.send_and_ping(msg_block(block) if with_witness else msg_no_witness_block(block))
 146          assert_equal(node.getbestblockhash() == block.hash_hex, accepted)
 147  
 148  
 149  class TestP2PConn(P2PInterface):
 150      def __init__(self, wtxidrelay=False):
 151          super().__init__(wtxidrelay=wtxidrelay)
 152          self.getdataset = set()
 153          self.last_wtxidrelay = []
 154          self.lastgetdata = []
 155          self.wtxidrelay = wtxidrelay
 156  
 157      # Don't send getdata message replies to invs automatically.
 158      # We'll send the getdata messages explicitly in the test logic.
 159      def on_inv(self, message):
 160          pass
 161  
 162      def on_getdata(self, message):
 163          self.lastgetdata = message.inv
 164          for inv in message.inv:
 165              self.getdataset.add(inv.hash)
 166  
 167      def on_wtxidrelay(self, message):
 168          self.last_wtxidrelay.append(message)
 169  
 170      def announce_tx_and_wait_for_getdata(self, tx, success=True, use_wtxid=False):
 171          if success:
 172              # sanity check
 173              assert (self.wtxidrelay and use_wtxid) or (not self.wtxidrelay and not use_wtxid)
 174          with p2p_lock:
 175              self.last_message.pop("getdata", None)
 176          if use_wtxid:
 177              wtxid = tx.wtxid_int
 178              self.send_without_ping(msg_inv(inv=[CInv(MSG_WTX, wtxid)]))
 179          else:
 180              self.send_without_ping(msg_inv(inv=[CInv(MSG_TX, tx.txid_int)]))
 181  
 182          if success:
 183              if use_wtxid:
 184                  self.wait_for_getdata([wtxid])
 185              else:
 186                  self.wait_for_getdata([tx.txid_int])
 187          else:
 188              ensure_for(duration=5, f=lambda: not self.last_message.get("getdata"))
 189  
 190      def announce_block_and_wait_for_getdata(self, block, use_header, timeout=60):
 191          with p2p_lock:
 192              self.last_message.pop("getdata", None)
 193          msg = msg_headers()
 194          msg.headers = [CBlockHeader(block)]
 195          if use_header:
 196              self.send_without_ping(msg)
 197          else:
 198              self.send_without_ping(msg_inv(inv=[CInv(MSG_BLOCK, block.hash_int)]))
 199              self.wait_for_getheaders(block_hash=block.hashPrevBlock, timeout=timeout)
 200              self.send_without_ping(msg)
 201          self.wait_for_getdata([block.hash_int], timeout=timeout)
 202  
 203      def request_block(self, blockhash, inv_type, timeout=60):
 204          with p2p_lock:
 205              self.last_message.pop("block", None)
 206          self.send_without_ping(msg_getdata(inv=[CInv(inv_type, blockhash)]))
 207          self.wait_for_block(blockhash, timeout=timeout)
 208          return self.last_message["block"].block
 209  
 210  class SegWitTest(BitcoinTestFramework):
 211      def set_test_params(self):
 212          self.setup_clean_chain = True
 213          self.num_nodes = 2
 214          # whitelist peers to speed up tx relay / mempool sync
 215          self.noban_tx_relay = True
 216          # This test tests SegWit both pre and post-activation, so use the normal BIP9 activation.
 217          self.extra_args = [
 218              # -par=1 should not affect validation outcome or logging/reported failures. It is kept
 219              # here to exercise the code path still (as it is distinct for multithread script
 220              # validation).
 221              ["-acceptnonstdtxn=1", f"-testactivationheight=segwit@{SEGWIT_HEIGHT}", "-par=1"],
 222              ["-acceptnonstdtxn=0", f"-testactivationheight=segwit@{SEGWIT_HEIGHT}"],
 223          ]
 224  
 225      # Helper functions
 226  
 227      def build_next_block(self):
 228          """Build a block on top of node0's tip."""
 229          tip = self.nodes[0].getbestblockhash()
 230          height = self.nodes[0].getblockcount() + 1
 231          block_time = self.nodes[0].getblockheader(tip)["mediantime"] + 1
 232          block = create_block(int(tip, 16), height=height, ntime=block_time)
 233          return block
 234  
 235      def update_witness_block_with_transactions(self, block, tx_list, nonce=0):
 236          """Add list of transactions to block, adds witness commitment, then solves."""
 237          block.vtx.extend(tx_list)
 238          add_witness_commitment(block, nonce)
 239          block.solve()
 240  
 241      def run_test(self):
 242          # Setup the p2p connections
 243          # self.test_node sets P2P_SERVICES, i.e. NODE_WITNESS | NODE_NETWORK
 244          self.test_node = self.nodes[0].add_p2p_connection(TestP2PConn(), services=P2P_SERVICES)
 245          # self.old_node sets only NODE_NETWORK
 246          self.old_node = self.nodes[0].add_p2p_connection(TestP2PConn(), services=NODE_NETWORK)
 247          # self.std_node is for testing node1 (requires standard txs)
 248          self.std_node = self.nodes[1].add_p2p_connection(TestP2PConn(), services=P2P_SERVICES)
 249          # self.std_wtx_node is for testing node1 with wtxid relay
 250          self.std_wtx_node = self.nodes[1].add_p2p_connection(TestP2PConn(wtxidrelay=True), services=P2P_SERVICES)
 251  
 252          assert_not_equal(self.test_node.nServices & NODE_WITNESS, 0)
 253  
 254          # Keep a place to store utxo's that can be used in later tests
 255          self.utxo = []
 256  
 257          self.log.info("Starting tests before segwit activation")
 258          self.segwit_active = False
 259          self.wallet = MiniWallet(self.nodes[0])
 260  
 261          self.test_non_witness_transaction()
 262          self.test_v0_outputs_arent_spendable()
 263          self.test_block_relay()
 264          self.test_unnecessary_witness_before_segwit_activation()
 265          self.test_witness_tx_relay_before_segwit_activation()
 266          self.test_standardness_v0()
 267  
 268          self.log.info("Advancing to segwit activation")
 269          self.advance_to_segwit_active()
 270  
 271          # Segwit status 'active'
 272  
 273          self.test_p2sh_witness()
 274          self.test_witness_commitments()
 275          self.test_block_malleability()
 276          self.test_witness_block_size()
 277          self.test_submit_block()
 278          self.test_extra_witness_data()
 279          self.test_max_witness_push_length()
 280          self.test_max_witness_script_length()
 281          self.test_witness_input_length()
 282          self.test_block_relay()
 283          self.test_tx_relay_after_segwit_activation()
 284          self.test_standardness_v0()
 285          self.test_segwit_versions()
 286          self.test_premature_coinbase_witness_spend()
 287          self.test_uncompressed_pubkey()
 288          self.test_signature_version_1()
 289          self.test_non_standard_witness_blinding()
 290          self.test_non_standard_witness()
 291          self.test_witness_sigops()
 292          self.test_superfluous_witness()
 293          self.test_wtxid_relay()
 294  
 295      # Individual tests
 296  
 297      @subtest
 298      def test_non_witness_transaction(self):
 299          """See if sending a regular transaction works, and create a utxo to use in later tests."""
 300          # Mine a block with an anyone-can-spend coinbase,
 301          # let it mature, then try to spend it.
 302  
 303          block = self.build_next_block()
 304          block.solve()
 305          self.test_node.send_and_ping(msg_no_witness_block(block))  # make sure the block was processed
 306          txid = block.vtx[0].txid_int
 307  
 308          self.generate(self.wallet, 99)  # let the block mature
 309  
 310          # Create a transaction that spends the coinbase
 311          tx = CTransaction()
 312          tx.vin.append(CTxIn(COutPoint(txid, 0), b""))
 313          tx.vout.append(CTxOut(49 * 100000000, CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
 314  
 315          # Check that serializing it with or without witness is the same
 316          # This is a sanity check of our testing framework.
 317          assert_equal(msg_no_witness_tx(tx).serialize(), msg_tx(tx).serialize())
 318  
 319          self.test_node.send_and_ping(msg_tx(tx))  # make sure the block was processed
 320          assert tx.txid_hex in self.nodes[0].getrawmempool()
 321          # Save this transaction for later
 322          self.utxo.append(UTXO(tx.txid_int, 0, 49 * 100000000))
 323          self.generate(self.nodes[0], 1)
 324  
 325      @subtest
 326      def test_unnecessary_witness_before_segwit_activation(self):
 327          """Verify that blocks with witnesses are rejected before activation."""
 328  
 329          tx = CTransaction()
 330          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
 331          tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, CScript([OP_TRUE])))
 332          tx.wit.vtxinwit.append(CTxInWitness())
 333          tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([CScriptNum(1)])]
 334  
 335          # Verify the hash with witness differs from the txid
 336          # (otherwise our testing framework must be broken!)
 337          assert_not_equal(tx.txid_hex, tx.wtxid_hex)
 338  
 339          # Construct a block that includes the transaction.
 340          block = self.build_next_block()
 341          self.update_witness_block_with_transactions(block, [tx])
 342          # Sending witness data before activation is not allowed (anti-spam
 343          # rule).
 344          test_witness_block(self.nodes[0], self.test_node, block, accepted=False, reason='unexpected-witness')
 345  
 346          # But it should not be permanently marked bad...
 347          # Resend without witness information.
 348          self.test_node.send_and_ping(msg_no_witness_block(block))  # make sure the block was processed
 349          assert_equal(self.nodes[0].getbestblockhash(), block.hash_hex)
 350  
 351          # Update our utxo list; we spent the first entry.
 352          self.utxo.pop(0)
 353          self.utxo.append(UTXO(tx.txid_int, 0, tx.vout[0].nValue))
 354  
 355      @subtest
 356      def test_block_relay(self):
 357          """Test that block requests to NODE_WITNESS peer are with MSG_WITNESS_FLAG.
 358  
 359          This is true regardless of segwit activation.
 360          Also test that we don't ask for blocks from unupgraded peers."""
 361  
 362          blocktype = 2 | MSG_WITNESS_FLAG
 363  
 364          # test_node has set NODE_WITNESS, so all getdata requests should be for
 365          # witness blocks.
 366          # Test announcing a block via inv results in a getdata, and that
 367          # announcing a block with a header results in a getdata
 368          block1 = self.build_next_block()
 369          block1.solve()
 370  
 371          # Send an empty headers message, to clear out any prior getheaders
 372          # messages that our peer may be waiting for us on.
 373          self.test_node.send_without_ping(msg_headers())
 374  
 375          self.test_node.announce_block_and_wait_for_getdata(block1, use_header=False)
 376          assert_equal(self.test_node.last_message["getdata"].inv[0].type, blocktype)
 377          test_witness_block(self.nodes[0], self.test_node, block1, True)
 378  
 379          block2 = self.build_next_block()
 380          block2.solve()
 381  
 382          self.test_node.announce_block_and_wait_for_getdata(block2, use_header=True)
 383          assert_equal(self.test_node.last_message["getdata"].inv[0].type, blocktype)
 384          test_witness_block(self.nodes[0], self.test_node, block2, True)
 385  
 386          # Check that we can getdata for witness blocks or regular blocks,
 387          # and the right thing happens.
 388          if not self.segwit_active:
 389              # Before activation, we should be able to request old blocks with
 390              # or without witness, and they should be the same.
 391              chain_height = self.nodes[0].getblockcount()
 392              # Pick 10 random blocks on main chain, and verify that getdata's
 393              # for MSG_BLOCK, MSG_WITNESS_BLOCK, and rpc getblock() are equal.
 394              all_heights = list(range(chain_height + 1))
 395              random.shuffle(all_heights)
 396              all_heights = all_heights[0:10]
 397              for height in all_heights:
 398                  block_hash = self.nodes[0].getblockhash(height)
 399                  rpc_block = self.nodes[0].getblock(block_hash, False)
 400                  block_hash = int(block_hash, 16)
 401                  block = self.test_node.request_block(block_hash, 2)
 402                  wit_block = self.test_node.request_block(block_hash, 2 | MSG_WITNESS_FLAG)
 403                  assert_equal(block.serialize(), wit_block.serialize())
 404                  assert_equal(block.serialize(), bytes.fromhex(rpc_block))
 405          else:
 406              # After activation, witness blocks and non-witness blocks should
 407              # be different.  Verify rpc getblock() returns witness blocks, while
 408              # getdata respects the requested type.
 409              block = self.build_next_block()
 410              self.update_witness_block_with_transactions(block, [])
 411              # This gives us a witness commitment.
 412              assert_equal(len(block.vtx[0].wit.vtxinwit), 1)
 413              assert_equal(len(block.vtx[0].wit.vtxinwit[0].scriptWitness.stack), 1)
 414              test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
 415              # Now try to retrieve it...
 416              rpc_block = self.nodes[0].getblock(block.hash_hex, False)
 417              non_wit_block = self.test_node.request_block(block.hash_int, 2)
 418              wit_block = self.test_node.request_block(block.hash_int, 2 | MSG_WITNESS_FLAG)
 419              assert_equal(wit_block.serialize(), bytes.fromhex(rpc_block))
 420              assert_equal(wit_block.serialize(False), non_wit_block.serialize())
 421              assert_equal(wit_block.serialize(), block.serialize())
 422  
 423              # Test size, vsize, weight
 424              rpc_details = self.nodes[0].getblock(block.hash_hex, True)
 425              assert_equal(rpc_details["size"], len(block.serialize()))
 426              assert_equal(rpc_details["strippedsize"], len(block.serialize(False)))
 427              assert_equal(rpc_details["weight"], block.get_weight())
 428  
 429              # Upgraded node should not ask for blocks from unupgraded
 430              block4 = self.build_next_block()
 431              block4.solve()
 432              self.old_node.getdataset = set()
 433  
 434              # Blocks can be requested via direct-fetch (immediately upon processing the announcement)
 435              # or via parallel download (with an indeterminate delay from processing the announcement)
 436              # so to test that a block is NOT requested, we could guess a time period to sleep for,
 437              # and then check. We can avoid the sleep() by taking advantage of transaction getdata's
 438              # being processed after block getdata's, and announce a transaction as well,
 439              # and then check to see if that particular getdata has been received.
 440              # Since 0.14, inv's will only be responded to with a getheaders, so send a header
 441              # to announce this block.
 442              msg = msg_headers()
 443              msg.headers = [CBlockHeader(block4)]
 444              self.old_node.send_without_ping(msg)
 445              self.old_node.announce_tx_and_wait_for_getdata(block4.vtx[0])
 446              assert block4.hash_int not in self.old_node.getdataset
 447  
 448      @subtest
 449      def test_v0_outputs_arent_spendable(self):
 450          """Test that v0 outputs aren't spendable before segwit activation.
 451  
 452          ~6 months after segwit activation, the SCRIPT_VERIFY_WITNESS flag was
 453          backdated so that it applies to all blocks, going back to the genesis
 454          block.
 455  
 456          Consequently, version 0 witness outputs are never spendable without
 457          witness, and so can't be spent before segwit activation (the point at which
 458          blocks are permitted to contain witnesses)."""
 459  
 460          # Create two outputs, a p2wsh and p2sh-p2wsh
 461          witness_script = CScript([OP_TRUE])
 462          script_pubkey = script_to_p2wsh_script(witness_script)
 463          p2sh_script_pubkey = script_to_p2sh_script(script_pubkey)
 464  
 465          value = self.utxo[0].nValue // 3
 466  
 467          tx = CTransaction()
 468          tx.vin = [CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b'')]
 469          tx.vout = [CTxOut(value, script_pubkey), CTxOut(value, p2sh_script_pubkey)]
 470          tx.vout.append(CTxOut(value, CScript([OP_TRUE])))
 471          txid = tx.txid_int
 472  
 473          # Add it to a block
 474          block = self.build_next_block()
 475          self.update_witness_block_with_transactions(block, [tx])
 476          # Verify that segwit isn't activated. A block serialized with witness
 477          # should be rejected prior to activation.
 478          test_witness_block(self.nodes[0], self.test_node, block, accepted=False, with_witness=True, reason='unexpected-witness')
 479          # Now send the block without witness. It should be accepted
 480          test_witness_block(self.nodes[0], self.test_node, block, accepted=True, with_witness=False)
 481  
 482          # Now try to spend the outputs. This should fail since SCRIPT_VERIFY_WITNESS is always enabled.
 483          p2wsh_tx = CTransaction()
 484          p2wsh_tx.vin = [CTxIn(COutPoint(txid, 0), b'')]
 485          p2wsh_tx.vout = [CTxOut(value, CScript([OP_TRUE]))]
 486          p2wsh_tx.wit.vtxinwit.append(CTxInWitness())
 487          p2wsh_tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])]
 488  
 489          p2sh_p2wsh_tx = CTransaction()
 490          p2sh_p2wsh_tx.vin = [CTxIn(COutPoint(txid, 1), CScript([script_pubkey]))]
 491          p2sh_p2wsh_tx.vout = [CTxOut(value, CScript([OP_TRUE]))]
 492          p2sh_p2wsh_tx.wit.vtxinwit.append(CTxInWitness())
 493          p2sh_p2wsh_tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])]
 494  
 495          for tx in [p2wsh_tx, p2sh_p2wsh_tx]:
 496  
 497              block = self.build_next_block()
 498              self.update_witness_block_with_transactions(block, [tx])
 499  
 500              # When the block is serialized with a witness, the block will be rejected because witness
 501              # data isn't allowed in blocks that don't commit to witness data.
 502              test_witness_block(self.nodes[0], self.test_node, block, accepted=False, with_witness=True, reason='unexpected-witness')
 503  
 504              # When the block is serialized without witness, validation fails because the transaction is
 505              # invalid (transactions are always validated with SCRIPT_VERIFY_WITNESS so a segwit v0 transaction
 506              # without a witness is invalid).
 507              test_witness_block(self.nodes[0], self.test_node, block, accepted=False, with_witness=False,
 508                                 reason='block-script-verify-flag-failed (Witness program was passed an empty witness)')
 509  
 510          self.utxo.pop(0)
 511          self.utxo.append(UTXO(txid, 2, value))
 512  
 513      @subtest
 514      def test_witness_tx_relay_before_segwit_activation(self):
 515  
 516          # Generate a transaction that doesn't require a witness, but send it
 517          # with a witness.  Should be rejected for premature-witness, but should
 518          # not be added to recently rejected list.
 519          tx = CTransaction()
 520          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
 521          tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
 522          tx.wit.vtxinwit.append(CTxInWitness())
 523          tx.wit.vtxinwit[0].scriptWitness.stack = [b'a']
 524  
 525          tx_hash = tx.txid_int
 526          tx_value = tx.vout[0].nValue
 527  
 528          # Verify that if a peer doesn't set nServices to include NODE_WITNESS,
 529          # the getdata is just for the non-witness portion.
 530          self.old_node.announce_tx_and_wait_for_getdata(tx)
 531          assert_equal(self.old_node.last_message["getdata"].inv[0].type, MSG_TX)
 532  
 533          # Since we haven't delivered the tx yet, inv'ing the same tx from
 534          # a witness transaction ought not result in a getdata.
 535          self.test_node.announce_tx_and_wait_for_getdata(tx, success=False)
 536  
 537          # Delivering this transaction with witness should fail (no matter who
 538          # its from)
 539          assert_equal(len(self.nodes[0].getrawmempool()), 0)
 540          assert_equal(len(self.nodes[1].getrawmempool()), 0)
 541          test_transaction_acceptance(self.nodes[0], self.old_node, tx, with_witness=True, accepted=False)
 542          test_transaction_acceptance(self.nodes[0], self.test_node, tx, with_witness=True, accepted=False)
 543  
 544          # But eliminating the witness should fix it
 545          test_transaction_acceptance(self.nodes[0], self.test_node, tx, with_witness=False, accepted=True)
 546  
 547          # Cleanup: mine the first transaction and update utxo
 548          self.generate(self.nodes[0], 1)
 549          assert_equal(len(self.nodes[0].getrawmempool()), 0)
 550  
 551          self.utxo.pop(0)
 552          self.utxo.append(UTXO(tx_hash, 0, tx_value))
 553  
 554      @subtest
 555      def test_standardness_v0(self):
 556          """Test V0 txout standardness.
 557  
 558          V0 segwit outputs and inputs are always standard.
 559          V0 segwit inputs may only be mined after activation, but not before."""
 560  
 561          witness_script = CScript([OP_TRUE])
 562          script_pubkey = script_to_p2wsh_script(witness_script)
 563          p2sh_script_pubkey = script_to_p2sh_script(witness_script)
 564  
 565          # First prepare a p2sh output (so that spending it will pass standardness)
 566          p2sh_tx = CTransaction()
 567          p2sh_tx.vin = [CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b"")]
 568          p2sh_tx.vout = [CTxOut(self.utxo[0].nValue - 1000, p2sh_script_pubkey)]
 569  
 570          # Mine it on test_node to create the confirmed output.
 571          test_transaction_acceptance(self.nodes[0], self.test_node, p2sh_tx, with_witness=True, accepted=True)
 572          self.generate(self.nodes[0], 1)
 573  
 574          # Now test standardness of v0 P2WSH outputs.
 575          # Start by creating a transaction with two outputs.
 576          tx = CTransaction()
 577          tx.vin = [CTxIn(COutPoint(p2sh_tx.txid_int, 0), CScript([witness_script]))]
 578          tx.vout = [CTxOut(p2sh_tx.vout[0].nValue - 10000, script_pubkey)]
 579          tx.vout.append(CTxOut(8000, script_pubkey))  # Might burn this later
 580          tx.vin[0].nSequence = MAX_BIP125_RBF_SEQUENCE  # Just to have the option to bump this tx from the mempool
 581  
 582          # This is always accepted, since the mempool policy is to consider segwit as always active
 583          # and thus allow segwit outputs
 584          test_transaction_acceptance(self.nodes[1], self.std_node, tx, with_witness=True, accepted=True)
 585  
 586          # Now create something that looks like a P2PKH output. This won't be spendable.
 587          witness_hash = sha256(witness_script)
 588          script_pubkey = CScript([OP_0, hash160(witness_hash)])
 589          tx2 = CTransaction()
 590          # tx was accepted, so we spend the second output.
 591          tx2.vin = [CTxIn(COutPoint(tx.txid_int, 1), b"")]
 592          tx2.vout = [CTxOut(7000, script_pubkey)]
 593          tx2.wit.vtxinwit.append(CTxInWitness())
 594          tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
 595  
 596          test_transaction_acceptance(self.nodes[1], self.std_node, tx2, with_witness=True, accepted=True)
 597  
 598          # Now update self.utxo for later tests.
 599          tx3 = CTransaction()
 600          # tx and tx2 were both accepted.  Don't bother trying to reclaim the
 601          # P2PKH output; just send tx's first output back to an anyone-can-spend.
 602          self.sync_mempools([self.nodes[0], self.nodes[1]])
 603          tx3.vin = [CTxIn(COutPoint(tx.txid_int, 0), b"")]
 604          tx3.vout = [CTxOut(tx.vout[0].nValue - 1000, CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE]))]
 605          tx3.wit.vtxinwit.append(CTxInWitness())
 606          tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
 607          if not self.segwit_active:
 608              # Just check mempool acceptance, but don't add the transaction to the mempool, since witness is disallowed
 609              # in blocks and the tx is impossible to mine right now.
 610              testres3 = self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()])
 611              testres3[0]["fees"].pop("effective-feerate")
 612              testres3[0]["fees"].pop("effective-includes")
 613              assert_equal(testres3,
 614                  [{
 615                      'txid': tx3.txid_hex,
 616                      'wtxid': tx3.wtxid_hex,
 617                      'allowed': True,
 618                      'vsize': tx3.get_vsize(),
 619                      'fees': {
 620                          'base': Decimal('0.00001000'),
 621                      },
 622                  }],
 623              )
 624              # Create the same output as tx3, but by replacing tx
 625              tx3_out = tx3.vout[0]
 626              tx3 = tx
 627              tx3.vout = [tx3_out]
 628              testres3_replaced = self.nodes[0].testmempoolaccept([tx3.serialize_with_witness().hex()])
 629              testres3_replaced[0]["fees"].pop("effective-feerate")
 630              testres3_replaced[0]["fees"].pop("effective-includes")
 631              assert_equal(testres3_replaced,
 632                  [{
 633                      'txid': tx3.txid_hex,
 634                      'wtxid': tx3.wtxid_hex,
 635                      'allowed': True,
 636                      'vsize': tx3.get_vsize(),
 637                      'fees': {
 638                          'base': Decimal('0.00011000'),
 639                      },
 640                  }],
 641              )
 642          test_transaction_acceptance(self.nodes[0], self.test_node, tx3, with_witness=True, accepted=True)
 643  
 644          self.generate(self.nodes[0], 1)
 645          self.utxo.pop(0)
 646          self.utxo.append(UTXO(tx3.txid_int, 0, tx3.vout[0].nValue))
 647          assert_equal(len(self.nodes[1].getrawmempool()), 0)
 648  
 649      @subtest
 650      def advance_to_segwit_active(self):
 651          """Mine enough blocks to activate segwit."""
 652          assert not softfork_active(self.nodes[0], 'segwit')
 653          height = self.nodes[0].getblockcount()
 654          self.generate(self.nodes[0], SEGWIT_HEIGHT - height - 2)
 655          assert not softfork_active(self.nodes[0], 'segwit')
 656          self.generate(self.nodes[0], 1)
 657          assert softfork_active(self.nodes[0], 'segwit')
 658          self.segwit_active = True
 659  
 660      @subtest
 661      def test_p2sh_witness(self):
 662          """Test P2SH wrapped witness programs."""
 663  
 664          # Prepare the p2sh-wrapped witness output
 665          witness_script = CScript([OP_DROP, OP_TRUE])
 666          p2wsh_pubkey = script_to_p2wsh_script(witness_script)
 667          script_pubkey = script_to_p2sh_script(p2wsh_pubkey)
 668          script_sig = CScript([p2wsh_pubkey])  # a push of the redeem script
 669  
 670          # Fund the P2SH output
 671          tx = CTransaction()
 672          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
 673          tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, script_pubkey))
 674  
 675          # Verify mempool acceptance and block validity
 676          test_transaction_acceptance(self.nodes[0], self.test_node, tx, with_witness=False, accepted=True)
 677          block = self.build_next_block()
 678          self.update_witness_block_with_transactions(block, [tx])
 679          test_witness_block(self.nodes[0], self.test_node, block, accepted=True, with_witness=True)
 680          self.sync_blocks()
 681  
 682          # Now test attempts to spend the output.
 683          spend_tx = CTransaction()
 684          spend_tx.vin.append(CTxIn(COutPoint(tx.txid_int, 0), script_sig))
 685          spend_tx.vout.append(CTxOut(tx.vout[0].nValue - 1000, CScript([OP_TRUE])))
 686  
 687          # This transaction should not be accepted into the mempool pre- or
 688          # post-segwit.  Mempool acceptance will use SCRIPT_VERIFY_WITNESS which
 689          # will require a witness to spend a witness program regardless of
 690          # segwit activation.  Note that older bitcoind's that are not
 691          # segwit-aware would also reject this for failing CLEANSTACK.
 692          with self.nodes[0].assert_debug_log(
 693                  expected_msgs=[spend_tx.txid_hex, 'was not accepted: mempool-script-verify-flag-failed (Witness program was passed an empty witness)']):
 694              test_transaction_acceptance(self.nodes[0], self.test_node, spend_tx, with_witness=False, accepted=False)
 695  
 696          # The transaction was detected as witness stripped above and not added to the reject
 697          # filter. Trying again will check it again and result in the same error.
 698          with self.nodes[0].assert_debug_log(
 699                  expected_msgs=[spend_tx.txid_hex, 'was not accepted: mempool-script-verify-flag-failed (Witness program was passed an empty witness)']):
 700              test_transaction_acceptance(self.nodes[0], self.test_node, spend_tx, with_witness=False, accepted=False)
 701  
 702          # Try to put the witness script in the scriptSig, should also fail.
 703          spend_tx.vin[0].scriptSig = CScript([p2wsh_pubkey, b'a'])
 704          with self.nodes[0].assert_debug_log(
 705                  expected_msgs=[spend_tx.txid_hex, 'was not accepted: mempool-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element)']):
 706              test_transaction_acceptance(self.nodes[0], self.test_node, spend_tx, with_witness=False, accepted=False)
 707  
 708          # Now put the witness script in the witness, should succeed after
 709          # segwit activates.
 710          spend_tx.vin[0].scriptSig = script_sig
 711          spend_tx.wit.vtxinwit.append(CTxInWitness())
 712          spend_tx.wit.vtxinwit[0].scriptWitness.stack = [b'a', witness_script]
 713  
 714          # Verify mempool acceptance
 715          test_transaction_acceptance(self.nodes[0], self.test_node, spend_tx, with_witness=True, accepted=True)
 716          block = self.build_next_block()
 717          self.update_witness_block_with_transactions(block, [spend_tx])
 718  
 719          # If we're after activation, then sending this with witnesses should be valid.
 720          # This no longer works before activation, because SCRIPT_VERIFY_WITNESS
 721          # is always set.
 722          # TODO: rewrite this test to make clear that it only works after activation.
 723          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
 724  
 725          # Update self.utxo
 726          self.utxo.pop(0)
 727          self.utxo.append(UTXO(spend_tx.txid_int, 0, spend_tx.vout[0].nValue))
 728  
 729      @subtest
 730      def test_witness_commitments(self):
 731          """Test witness commitments.
 732  
 733          This test can only be run after segwit has activated."""
 734  
 735          # First try a correct witness commitment.
 736          block = self.build_next_block()
 737          add_witness_commitment(block)
 738          block.solve()
 739  
 740          # Test the test -- witness serialization should be different
 741          assert_not_equal(msg_block(block).serialize(), msg_no_witness_block(block).serialize())
 742  
 743          # This empty block should be valid.
 744          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
 745  
 746          # Try to tweak the nonce
 747          block_2 = self.build_next_block()
 748          add_witness_commitment(block_2, nonce=28)
 749          block_2.solve()
 750  
 751          # The commitment should have changed!
 752          assert_not_equal(block_2.vtx[0].vout[-1], block.vtx[0].vout[-1])
 753  
 754          # This should also be valid.
 755          test_witness_block(self.nodes[0], self.test_node, block_2, accepted=True)
 756  
 757          # Now test commitments with actual transactions
 758          tx = CTransaction()
 759          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
 760  
 761          # Let's construct a witness script
 762          witness_script = CScript([OP_TRUE])
 763          script_pubkey = script_to_p2wsh_script(witness_script)
 764          tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, script_pubkey))
 765  
 766          # tx2 will spend tx1, and send back to a regular anyone-can-spend address
 767          tx2 = CTransaction()
 768          tx2.vin.append(CTxIn(COutPoint(tx.txid_int, 0), b""))
 769          tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, witness_script))
 770          tx2.wit.vtxinwit.append(CTxInWitness())
 771          tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
 772  
 773          block_3 = self.build_next_block()
 774          self.update_witness_block_with_transactions(block_3, [tx, tx2], nonce=1)
 775          # Add an extra OP_RETURN output that matches the witness commitment template,
 776          # even though it has extra data after the incorrect commitment.
 777          # This block should fail.
 778          block_3.vtx[0].vout.append(CTxOut(0, CScript([OP_RETURN, WITNESS_COMMITMENT_HEADER + ser_uint256(2), 10])))
 779          block_3.hashMerkleRoot = block_3.calc_merkle_root()
 780          block_3.solve()
 781  
 782          test_witness_block(self.nodes[0], self.test_node, block_3, accepted=False, reason='bad-witness-merkle-match')
 783  
 784          # Add a different commitment with different nonce, but in the
 785          # right location, and with some funds burned(!).
 786          # This should succeed (nValue shouldn't affect finding the
 787          # witness commitment).
 788          add_witness_commitment(block_3, nonce=0)
 789          block_3.vtx[0].vout[0].nValue -= 1
 790          block_3.vtx[0].vout[-1].nValue += 1
 791          block_3.hashMerkleRoot = block_3.calc_merkle_root()
 792          assert_equal(len(block_3.vtx[0].vout), 4)  # 3 OP_returns
 793          block_3.solve()
 794          test_witness_block(self.nodes[0], self.test_node, block_3, accepted=True)
 795  
 796          # Finally test that a block with no witness transactions can
 797          # omit the commitment.
 798          block_4 = self.build_next_block()
 799          tx3 = CTransaction()
 800          tx3.vin.append(CTxIn(COutPoint(tx2.txid_int, 0), b""))
 801          tx3.vout.append(CTxOut(tx.vout[0].nValue - 1000, witness_script))
 802          block_4.vtx.append(tx3)
 803          block_4.hashMerkleRoot = block_4.calc_merkle_root()
 804          block_4.solve()
 805          test_witness_block(self.nodes[0], self.test_node, block_4, with_witness=False, accepted=True)
 806  
 807          # Update available utxo's for use in later test.
 808          self.utxo.pop(0)
 809          self.utxo.append(UTXO(tx3.txid_int, 0, tx3.vout[0].nValue))
 810  
 811      @subtest
 812      def test_block_malleability(self):
 813  
 814          # Make sure that a block that has too big a virtual size
 815          # because of a too-large coinbase witness is not permanently
 816          # marked bad.
 817          block = self.build_next_block()
 818          add_witness_commitment(block)
 819          block.solve()
 820  
 821          block.vtx[0].wit.vtxinwit[0].scriptWitness.stack.append(b'a' * 5000000)
 822          assert block.get_weight() > MAX_BLOCK_WEIGHT
 823  
 824          # We can't send over the p2p network, because this is too big to relay
 825          assert_equal('bad-witness-nonce-size', self.nodes[0].submitblock(block.serialize().hex()))
 826  
 827          assert_not_equal(self.nodes[0].getbestblockhash(), block.hash_hex)
 828  
 829          block.vtx[0].wit.vtxinwit[0].scriptWitness.stack.pop()
 830          assert block.get_weight() < MAX_BLOCK_WEIGHT
 831          assert_equal(None, self.nodes[0].submitblock(block.serialize().hex()))
 832  
 833          assert_equal(self.nodes[0].getbestblockhash(), block.hash_hex)
 834  
 835          # Build a relayable-but-invalid block
 836          relayable_block = self.build_next_block()
 837          add_witness_commitment(relayable_block)
 838          relayable_block.solve()
 839  
 840          # Append extra witness data to the coinbase input that triggers the
 841          # same validation rejection but keeps the block under the weight limit
 842          # so it can be sent via P2P.
 843          relayable_block.vtx[0].wit.vtxinwit[0].scriptWitness.stack.append(b'a' * 100_000)
 844  
 845          # Ensure it's relayable by weight
 846          assert_greater_than_or_equal(MAX_BLOCK_WEIGHT, relayable_block.get_weight())
 847  
 848          # Send over P2P and expect rejection for the same reason
 849          test_witness_block(self.nodes[0], self.test_node, relayable_block,
 850                             accepted=False, reason='bad-witness-nonce-size')
 851  
 852          # Node should still be on the previous tip
 853          assert_not_equal(self.nodes[0].getbestblockhash(), relayable_block.hash_hex)
 854  
 855          # Now fix the block by removing the extra witness data
 856          relayable_block.vtx[0].wit.vtxinwit[0].scriptWitness.stack.pop()
 857  
 858          # Confirm the block is still relayable by weight
 859          assert relayable_block.get_weight() <= MAX_BLOCK_WEIGHT
 860  
 861          # Send the corrected block and expect acceptance
 862          test_witness_block(self.nodes[0], self.test_node, relayable_block, accepted=True)
 863          assert_equal(self.nodes[0].getbestblockhash(), relayable_block.hash_hex)
 864  
 865          # Now make sure that malleating the witness reserved value doesn't
 866          # result in a block permanently marked bad.
 867          block = self.build_next_block()
 868          add_witness_commitment(block)
 869          block.solve()
 870  
 871          # Change the nonce -- should not cause the block to be permanently
 872          # failed
 873          block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(1)]
 874          test_witness_block(self.nodes[0], self.test_node, block, accepted=False, reason='bad-witness-merkle-match')
 875  
 876          # Changing the witness reserved value doesn't change the block hash
 877          block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(0)]
 878          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
 879  
 880      @subtest
 881      def test_witness_block_size(self):
 882          # TODO: Test that non-witness carrying blocks can't exceed 1MB
 883          # Skipping this test for now; this is covered in feature_block.py
 884  
 885          # Test that witness-bearing blocks are limited at ceil(base + wit/4) <= 1MB.
 886          block = self.build_next_block()
 887  
 888          assert len(self.utxo) > 0
 889  
 890          # Create a P2WSH transaction.
 891          # The witness script will be a bunch of OP_2DROP's, followed by OP_TRUE.
 892          # This should give us plenty of room to tweak the spending tx's
 893          # virtual size.
 894          NUM_DROPS = 200  # 201 max ops per script!
 895          NUM_OUTPUTS = 50
 896  
 897          witness_script = CScript([OP_2DROP] * NUM_DROPS + [OP_TRUE])
 898          script_pubkey = script_to_p2wsh_script(witness_script)
 899  
 900          prevout = COutPoint(self.utxo[0].sha256, self.utxo[0].n)
 901          value = self.utxo[0].nValue
 902  
 903          parent_tx = CTransaction()
 904          parent_tx.vin.append(CTxIn(prevout, b""))
 905          child_value = int(value / NUM_OUTPUTS)
 906          for _ in range(NUM_OUTPUTS):
 907              parent_tx.vout.append(CTxOut(child_value, script_pubkey))
 908          parent_tx.vout[0].nValue -= 50000
 909          assert parent_tx.vout[0].nValue > 0
 910  
 911          child_tx = CTransaction()
 912          for i in range(NUM_OUTPUTS):
 913              child_tx.vin.append(CTxIn(COutPoint(parent_tx.txid_int, i), b""))
 914          child_tx.vout = [CTxOut(value - 100000, CScript([OP_TRUE]))]
 915          for _ in range(NUM_OUTPUTS):
 916              child_tx.wit.vtxinwit.append(CTxInWitness())
 917              child_tx.wit.vtxinwit[-1].scriptWitness.stack = [b'a' * 195] * (2 * NUM_DROPS) + [witness_script]
 918          self.update_witness_block_with_transactions(block, [parent_tx, child_tx])
 919  
 920          additional_bytes = MAX_BLOCK_WEIGHT - block.get_weight()
 921          i = 0
 922          while additional_bytes > 0:
 923              # Add some more bytes to each input until we hit MAX_BLOCK_WEIGHT+1
 924              extra_bytes = min(additional_bytes + 1, 55)
 925              block.vtx[-1].wit.vtxinwit[int(i / (2 * NUM_DROPS))].scriptWitness.stack[i % (2 * NUM_DROPS)] = b'a' * (195 + extra_bytes)
 926              additional_bytes -= extra_bytes
 927              i += 1
 928  
 929          block.vtx[0].vout.pop()  # Remove old commitment
 930          add_witness_commitment(block)
 931          block.solve()
 932          assert_equal(block.get_weight(), MAX_BLOCK_WEIGHT + 1)
 933          # Make sure that our test case would exceed the old max-network-message
 934          # limit
 935          assert len(block.serialize()) > 2 * 1024 * 1024
 936  
 937          test_witness_block(self.nodes[0], self.test_node, block, accepted=False, reason='bad-blk-weight')
 938  
 939          # Now resize the second transaction to make the block fit.
 940          cur_length = len(block.vtx[-1].wit.vtxinwit[0].scriptWitness.stack[0])
 941          block.vtx[-1].wit.vtxinwit[0].scriptWitness.stack[0] = b'a' * (cur_length - 1)
 942          block.vtx[0].vout.pop()
 943          add_witness_commitment(block)
 944          block.solve()
 945          assert_equal(block.get_weight(), MAX_BLOCK_WEIGHT)
 946  
 947          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
 948  
 949          # Update available utxo's
 950          self.utxo.pop(0)
 951          self.utxo.append(UTXO(block.vtx[-1].txid_int, 0, block.vtx[-1].vout[0].nValue))
 952  
 953      @subtest
 954      def test_submit_block(self):
 955          """Test that submitblock adds the nonce automatically when possible."""
 956          block = self.build_next_block()
 957  
 958          # Try using a custom nonce and then don't supply it.
 959          # This shouldn't possibly work.
 960          add_witness_commitment(block, nonce=1)
 961          block.vtx[0].wit = CTxWitness()  # drop the nonce
 962          block.solve()
 963          assert_equal('bad-witness-merkle-match', self.nodes[0].submitblock(block.serialize().hex()))
 964          assert_not_equal(self.nodes[0].getbestblockhash(), block.hash_hex)
 965  
 966          # Now redo commitment with the standard nonce, but let bitcoind fill it in.
 967          add_witness_commitment(block, nonce=0)
 968          block.vtx[0].wit = CTxWitness()
 969          block.solve()
 970          assert_equal(None, self.nodes[0].submitblock(block.serialize().hex()))
 971          assert_equal(self.nodes[0].getbestblockhash(), block.hash_hex)
 972  
 973          # This time, add a tx with non-empty witness, but don't supply
 974          # the commitment.
 975          block_2 = self.build_next_block()
 976  
 977          add_witness_commitment(block_2)
 978  
 979          block_2.solve()
 980  
 981          # Drop commitment and nonce -- submitblock should not fill in.
 982          block_2.vtx[0].vout.pop()
 983          block_2.vtx[0].wit = CTxWitness()
 984  
 985          assert_equal('bad-txnmrklroot', self.nodes[0].submitblock(block_2.serialize().hex()))
 986          # Tip should not advance!
 987          assert_not_equal(self.nodes[0].getbestblockhash(), block_2.hash_hex)
 988  
 989      @subtest
 990      def test_extra_witness_data(self):
 991          """Test extra witness data in a transaction."""
 992  
 993          block = self.build_next_block()
 994  
 995          witness_script = CScript([OP_DROP, OP_TRUE])
 996          script_pubkey = script_to_p2wsh_script(witness_script)
 997  
 998          # First try extra witness data on a tx that doesn't require a witness
 999          tx = CTransaction()
1000          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
1001          tx.vout.append(CTxOut(self.utxo[0].nValue - 2000, script_pubkey))
1002          tx.vout.append(CTxOut(1000, CScript([OP_TRUE])))  # non-witness output
1003          tx.wit.vtxinwit.append(CTxInWitness())
1004          tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([])]
1005          self.update_witness_block_with_transactions(block, [tx])
1006  
1007          # Extra witness data should not be allowed.
1008          test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1009                             reason='block-script-verify-flag-failed (Witness provided for non-witness script)')
1010  
1011          # Try extra signature data.  Ok if we're not spending a witness output.
1012          block.vtx[1].wit.vtxinwit = []
1013          block.vtx[1].vin[0].scriptSig = CScript([OP_0])
1014          add_witness_commitment(block)
1015          block.solve()
1016  
1017          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1018  
1019          # Now try extra witness/signature data on an input that DOES require a
1020          # witness
1021          tx2 = CTransaction()
1022          tx2.vin.append(CTxIn(COutPoint(tx.txid_int, 0), b""))  # witness output
1023          tx2.vin.append(CTxIn(COutPoint(tx.txid_int, 1), b""))  # non-witness
1024          tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_TRUE])))
1025          tx2.wit.vtxinwit.extend([CTxInWitness(), CTxInWitness()])
1026          tx2.wit.vtxinwit[0].scriptWitness.stack = [CScript([CScriptNum(1)]), CScript([CScriptNum(1)]), witness_script]
1027          tx2.wit.vtxinwit[1].scriptWitness.stack = []
1028  
1029          block = self.build_next_block()
1030          self.update_witness_block_with_transactions(block, [tx2])
1031  
1032          # This has extra witness data, so it should fail.
1033          test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1034                             reason='block-script-verify-flag-failed (Stack size must be exactly one after execution)')
1035  
1036          # Now get rid of the extra witness, but add extra scriptSig data
1037          tx2.vin[0].scriptSig = CScript([OP_TRUE])
1038          tx2.vin[1].scriptSig = CScript([OP_TRUE])
1039          tx2.wit.vtxinwit[0].scriptWitness.stack.pop(0)
1040          tx2.wit.vtxinwit[1].scriptWitness.stack = []
1041          add_witness_commitment(block)
1042          block.solve()
1043  
1044          # This has extra signature data for a witness input, so it should fail.
1045          test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1046                             reason='block-script-verify-flag-failed (Witness requires empty scriptSig)')
1047  
1048          # Now get rid of the extra scriptsig on the witness input, and verify
1049          # success (even with extra scriptsig data in the non-witness input)
1050          tx2.vin[0].scriptSig = b""
1051          add_witness_commitment(block)
1052          block.solve()
1053  
1054          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1055  
1056          # Update utxo for later tests
1057          self.utxo.pop(0)
1058          self.utxo.append(UTXO(tx2.txid_int, 0, tx2.vout[0].nValue))
1059  
1060      @subtest
1061      def test_max_witness_push_length(self):
1062          """Test that witness stack can only allow up to MAX_SCRIPT_ELEMENT_SIZE byte pushes."""
1063  
1064          block = self.build_next_block()
1065  
1066          witness_script = CScript([OP_DROP, OP_TRUE])
1067          script_pubkey = script_to_p2wsh_script(witness_script)
1068  
1069          tx = CTransaction()
1070          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
1071          tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, script_pubkey))
1072  
1073          tx2 = CTransaction()
1074          tx2.vin.append(CTxIn(COutPoint(tx.txid_int, 0), b""))
1075          tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, CScript([OP_TRUE])))
1076          tx2.wit.vtxinwit.append(CTxInWitness())
1077          # First try a 521-byte stack element
1078          tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a' * (MAX_SCRIPT_ELEMENT_SIZE + 1), witness_script]
1079  
1080          self.update_witness_block_with_transactions(block, [tx, tx2])
1081          test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1082                             reason='block-script-verify-flag-failed (Push value size limit exceeded)')
1083  
1084          # Now reduce the length of the stack element
1085          tx2.wit.vtxinwit[0].scriptWitness.stack[0] = b'a' * (MAX_SCRIPT_ELEMENT_SIZE)
1086  
1087          add_witness_commitment(block)
1088          block.solve()
1089          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1090  
1091          # Update the utxo for later tests
1092          self.utxo.pop()
1093          self.utxo.append(UTXO(tx2.txid_int, 0, tx2.vout[0].nValue))
1094  
1095      @subtest
1096      def test_max_witness_script_length(self):
1097          """Test that witness outputs greater than 10kB can't be spent."""
1098  
1099          MAX_WITNESS_SCRIPT_LENGTH = 10000
1100  
1101          # This script is 19 max pushes (9937 bytes), then 64 more opcode-bytes.
1102          long_witness_script = CScript([b'a' * MAX_SCRIPT_ELEMENT_SIZE] * 19 + [OP_DROP] * 63 + [OP_TRUE])
1103          assert_equal(len(long_witness_script), MAX_WITNESS_SCRIPT_LENGTH + 1)
1104          long_script_pubkey = script_to_p2wsh_script(long_witness_script)
1105  
1106          block = self.build_next_block()
1107  
1108          tx = CTransaction()
1109          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
1110          tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, long_script_pubkey))
1111  
1112          tx2 = CTransaction()
1113          tx2.vin.append(CTxIn(COutPoint(tx.txid_int, 0), b""))
1114          tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, CScript([OP_TRUE])))
1115          tx2.wit.vtxinwit.append(CTxInWitness())
1116          tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a'] * 44 + [long_witness_script]
1117  
1118          self.update_witness_block_with_transactions(block, [tx, tx2])
1119  
1120          test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1121                             reason='block-script-verify-flag-failed (Script is too big)')
1122  
1123          # Try again with one less byte in the witness script
1124          witness_script = CScript([b'a' * MAX_SCRIPT_ELEMENT_SIZE] * 19 + [OP_DROP] * 62 + [OP_TRUE])
1125          assert_equal(len(witness_script), MAX_WITNESS_SCRIPT_LENGTH)
1126          script_pubkey = script_to_p2wsh_script(witness_script)
1127  
1128          tx.vout[0] = CTxOut(tx.vout[0].nValue, script_pubkey)
1129          tx2.vin[0].prevout.hash = tx.txid_int
1130          tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a'] * 43 + [witness_script]
1131          block.vtx = [block.vtx[0]]
1132          self.update_witness_block_with_transactions(block, [tx, tx2])
1133          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1134  
1135          self.utxo.pop()
1136          self.utxo.append(UTXO(tx2.txid_int, 0, tx2.vout[0].nValue))
1137  
1138      @subtest
1139      def test_witness_input_length(self):
1140          """Test that vin length must match vtxinwit length."""
1141  
1142          witness_script = CScript([OP_DROP, OP_TRUE])
1143          script_pubkey = script_to_p2wsh_script(witness_script)
1144  
1145          # Create a transaction that splits our utxo into many outputs
1146          tx = CTransaction()
1147          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
1148          value = self.utxo[0].nValue
1149          for _ in range(10):
1150              tx.vout.append(CTxOut(int(value / 10), script_pubkey))
1151          tx.vout[0].nValue -= 1000
1152          assert tx.vout[0].nValue >= 0
1153  
1154          block = self.build_next_block()
1155          self.update_witness_block_with_transactions(block, [tx])
1156          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1157  
1158          # Try various ways to spend tx that should all break.
1159          # This "broken" transaction serializer will not normalize
1160          # the length of vtxinwit.
1161          class BrokenCTransaction(CTransaction):
1162              def serialize_with_witness(self):
1163                  flags = 0
1164                  if not self.wit.is_null():
1165                      flags |= 1
1166                  r = b""
1167                  r += self.version.to_bytes(4, "little")
1168                  if flags:
1169                      dummy = []
1170                      r += ser_vector(dummy)
1171                      r += flags.to_bytes(1, "little")
1172                  r += ser_vector(self.vin)
1173                  r += ser_vector(self.vout)
1174                  if flags & 1:
1175                      r += self.wit.serialize()
1176                  r += self.nLockTime.to_bytes(4, "little")
1177                  return r
1178  
1179          tx2 = BrokenCTransaction()
1180          for i in range(10):
1181              tx2.vin.append(CTxIn(COutPoint(tx.txid_int, i), b""))
1182          tx2.vout.append(CTxOut(value - 3000, CScript([OP_TRUE])))
1183  
1184          # First try using a too long vtxinwit
1185          for i in range(11):
1186              tx2.wit.vtxinwit.append(CTxInWitness())
1187              tx2.wit.vtxinwit[i].scriptWitness.stack = [b'a', witness_script]
1188  
1189          block = self.build_next_block()
1190          self.update_witness_block_with_transactions(block, [tx2])
1191          test_witness_block(self.nodes[0], self.test_node, block, accepted=False, reason='bad-txnmrklroot')
1192  
1193          # Now try using a too short vtxinwit
1194          tx2.wit.vtxinwit.pop()
1195          tx2.wit.vtxinwit.pop()
1196  
1197          block.vtx = [block.vtx[0]]
1198          self.update_witness_block_with_transactions(block, [tx2])
1199          # This block doesn't result in a specific reject reason, but an iostream exception:
1200          with self.nodes[0].assert_debug_log(["Exception 'DataStream::read(): end of data"]):
1201              test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
1202  
1203          # Now make one of the intermediate witnesses be incorrect
1204          tx2.wit.vtxinwit.append(CTxInWitness())
1205          tx2.wit.vtxinwit[-1].scriptWitness.stack = [b'a', witness_script]
1206          tx2.wit.vtxinwit[5].scriptWitness.stack = [witness_script]
1207  
1208          block.vtx = [block.vtx[0]]
1209          self.update_witness_block_with_transactions(block, [tx2])
1210          test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1211                             reason='block-script-verify-flag-failed (Operation not valid with the current stack size)')
1212  
1213          # Fix the broken witness and the block should be accepted.
1214          tx2.wit.vtxinwit[5].scriptWitness.stack = [b'a', witness_script]
1215          block.vtx = [block.vtx[0]]
1216          self.update_witness_block_with_transactions(block, [tx2])
1217          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1218  
1219          self.utxo.pop()
1220          self.utxo.append(UTXO(tx2.txid_int, 0, tx2.vout[0].nValue))
1221  
1222      @subtest
1223      def test_tx_relay_after_segwit_activation(self):
1224          """Test transaction relay after segwit activation.
1225  
1226          After segwit activates, verify that mempool:
1227          - rejects transactions with unnecessary/extra witnesses
1228          - accepts transactions with valid witnesses
1229          and that witness transactions are relayed to non-upgraded peers."""
1230  
1231          # Generate a transaction that doesn't require a witness, but send it
1232          # with a witness.  Should be rejected because we can't use a witness
1233          # when spending a non-witness output.
1234          tx = CTransaction()
1235          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
1236          tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
1237          tx.wit.vtxinwit.append(CTxInWitness())
1238          tx.wit.vtxinwit[0].scriptWitness.stack = [b'a']
1239  
1240          tx_hash = tx.txid_int
1241  
1242          # Verify that unnecessary witnesses are rejected.
1243          self.test_node.announce_tx_and_wait_for_getdata(tx)
1244          assert_equal(len(self.nodes[0].getrawmempool()), 0)
1245          test_transaction_acceptance(self.nodes[0], self.test_node, tx, with_witness=True, accepted=False)
1246  
1247          # Verify that removing the witness succeeds.
1248          test_transaction_acceptance(self.nodes[0], self.test_node, tx, with_witness=False, accepted=True)
1249  
1250          # Now try to add extra witness data to a valid witness tx.
1251          witness_script = CScript([OP_TRUE])
1252          script_pubkey = script_to_p2wsh_script(witness_script)
1253          tx2 = CTransaction()
1254          tx2.vin.append(CTxIn(COutPoint(tx_hash, 0), b""))
1255          tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, script_pubkey))
1256  
1257          tx3 = CTransaction()
1258          tx3.vin.append(CTxIn(COutPoint(tx2.txid_int, 0), b""))
1259          tx3.wit.vtxinwit.append(CTxInWitness())
1260  
1261          # Add too-large for IsStandard witness and check that it does not enter reject filter
1262          p2sh_script = CScript([OP_TRUE])
1263          witness_script2 = CScript([b'a' * 400000])
1264          tx3.vout.append(CTxOut(tx2.vout[0].nValue - 1000, script_to_p2sh_script(p2sh_script)))
1265          tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_script2]
1266  
1267          # Node will not be blinded to the transaction, requesting it any number of times
1268          # if it is being announced via txid relay.
1269          # Node will be blinded to the transaction via wtxid, however.
1270          self.std_node.announce_tx_and_wait_for_getdata(tx3)
1271          self.std_wtx_node.announce_tx_and_wait_for_getdata(tx3, use_wtxid=True)
1272          test_transaction_acceptance(self.nodes[1], self.std_node, tx3, True, False, 'tx-size')
1273          self.std_node.announce_tx_and_wait_for_getdata(tx3)
1274          self.std_wtx_node.announce_tx_and_wait_for_getdata(tx3, use_wtxid=True, success=False)
1275  
1276          # Remove witness stuffing, instead add extra witness push on stack
1277          tx3.vout[0] = CTxOut(tx2.vout[0].nValue - 1000, CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE]))
1278          tx3.wit.vtxinwit[0].scriptWitness.stack = [CScript([CScriptNum(1)]), witness_script]
1279  
1280          test_transaction_acceptance(self.nodes[0], self.test_node, tx2, with_witness=True, accepted=True)
1281          test_transaction_acceptance(self.nodes[0], self.test_node, tx3, with_witness=True, accepted=False)
1282  
1283          # Now do the opposite: strip the witness entirely. This will be detected as witness stripping and
1284          # the (w)txid won't be added to the reject filter: we can try again and get the same error.
1285          tx3.wit.vtxinwit[0].scriptWitness.stack = []
1286          reason = "was not accepted: mempool-script-verify-flag-failed (Witness program was passed an empty witness)"
1287          test_transaction_acceptance(self.nodes[0], self.test_node, tx3, with_witness=False, accepted=False, reason=reason)
1288          test_transaction_acceptance(self.nodes[0], self.test_node, tx3, with_witness=False, accepted=False, reason=reason)
1289  
1290          # Get rid of the extra witness, and verify acceptance.
1291          tx3.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
1292          # Also check that old_node gets a tx announcement, even though this is
1293          # a witness transaction.
1294          self.old_node.wait_for_inv([CInv(MSG_TX, tx2.txid_int)])  # wait until tx2 was inv'ed
1295          test_transaction_acceptance(self.nodes[0], self.test_node, tx3, with_witness=True, accepted=True)
1296          self.old_node.wait_for_inv([CInv(MSG_TX, tx3.txid_int)])
1297  
1298          # Test that getrawtransaction returns correct witness information
1299          # hash, size, vsize
1300          raw_tx = self.nodes[0].getrawtransaction(tx3.txid_hex, 1)
1301          assert_equal(raw_tx["hash"], tx3.wtxid_hex)
1302          assert_equal(raw_tx["size"], len(tx3.serialize_with_witness()))
1303          vsize = tx3.get_vsize()
1304          assert_equal(raw_tx["vsize"], vsize)
1305          assert_equal(raw_tx["weight"], tx3.get_weight())
1306          assert_equal(len(raw_tx["vin"][0]["txinwitness"]), 1)
1307          assert_equal(raw_tx["vin"][0]["txinwitness"][0], witness_script.hex())
1308          assert_not_equal(vsize, raw_tx["size"])
1309  
1310          # Cleanup: mine the transactions and update utxo for next test
1311          self.generate(self.nodes[0], 1)
1312          assert_equal(len(self.nodes[0].getrawmempool()), 0)
1313  
1314          self.utxo.pop(0)
1315          self.utxo.append(UTXO(tx3.txid_int, 0, tx3.vout[0].nValue))
1316  
1317      @subtest
1318      def test_segwit_versions(self):
1319          """Test validity of future segwit version transactions.
1320  
1321          Future segwit versions are non-standard to spend, but valid in blocks.
1322          Sending to future segwit versions is always allowed.
1323          Can run this before and after segwit activation."""
1324  
1325          NUM_SEGWIT_VERSIONS = 17  # will test OP_0, OP1, ..., OP_16
1326          if len(self.utxo) < NUM_SEGWIT_VERSIONS:
1327              tx = CTransaction()
1328              tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
1329              split_value = (self.utxo[0].nValue - 4000) // NUM_SEGWIT_VERSIONS
1330              for _ in range(NUM_SEGWIT_VERSIONS):
1331                  tx.vout.append(CTxOut(split_value, CScript([OP_TRUE])))
1332              block = self.build_next_block()
1333              self.update_witness_block_with_transactions(block, [tx])
1334              test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1335              self.utxo.pop(0)
1336              for i in range(NUM_SEGWIT_VERSIONS):
1337                  self.utxo.append(UTXO(tx.txid_int, i, split_value))
1338  
1339          self.sync_blocks()
1340          temp_utxo = []
1341          tx = CTransaction()
1342          witness_script = CScript([OP_TRUE])
1343          witness_hash = sha256(witness_script)
1344          assert_equal(len(self.nodes[1].getrawmempool()), 0)
1345          for version in list(range(OP_1, OP_16 + 1)) + [OP_0]:
1346              # First try to spend to a future version segwit script_pubkey.
1347              if version == OP_1:
1348                  # Don't use 32-byte v1 witness (used by Taproot; see BIP 341)
1349                  script_pubkey = CScript([CScriptOp(version), witness_hash + b'\x00'])
1350              else:
1351                  script_pubkey = CScript([CScriptOp(version), witness_hash])
1352              tx.vin = [CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b"")]
1353              tx.vout = [CTxOut(self.utxo[0].nValue - 1000, script_pubkey)]
1354              test_transaction_acceptance(self.nodes[1], self.std_node, tx, with_witness=True, accepted=False)
1355              test_transaction_acceptance(self.nodes[0], self.test_node, tx, with_witness=True, accepted=True)
1356              self.utxo.pop(0)
1357              temp_utxo.append(UTXO(tx.txid_int, 0, tx.vout[0].nValue))
1358  
1359          self.generate(self.nodes[0], 1)  # Mine all the transactions
1360          assert_equal(len(self.nodes[0].getrawmempool()), 0)
1361  
1362          # Finally, verify that version 0 -> version 2 transactions
1363          # are standard
1364          script_pubkey = CScript([CScriptOp(OP_2), witness_hash])
1365          tx2 = CTransaction()
1366          tx2.vin = [CTxIn(COutPoint(tx.txid_int, 0), b"")]
1367          tx2.vout = [CTxOut(tx.vout[0].nValue - 1000, script_pubkey)]
1368          tx2.wit.vtxinwit.append(CTxInWitness())
1369          tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
1370          # Gets accepted to both policy-enforcing nodes and others.
1371          test_transaction_acceptance(self.nodes[0], self.test_node, tx2, with_witness=True, accepted=True)
1372          test_transaction_acceptance(self.nodes[1], self.std_node, tx2, with_witness=True, accepted=True)
1373          temp_utxo.pop()  # last entry in temp_utxo was the output we just spent
1374          temp_utxo.append(UTXO(tx2.txid_int, 0, tx2.vout[0].nValue))
1375  
1376          # Spend everything in temp_utxo into an segwit v1 output.
1377          tx3 = CTransaction()
1378          total_value = 0
1379          for i in temp_utxo:
1380              tx3.vin.append(CTxIn(COutPoint(i.sha256, i.n), b""))
1381              tx3.wit.vtxinwit.append(CTxInWitness())
1382              total_value += i.nValue
1383          tx3.wit.vtxinwit[-1].scriptWitness.stack = [witness_script]
1384          tx3.vout.append(CTxOut(total_value - 1000, script_pubkey))
1385  
1386          # First we test this transaction against std_node
1387          # making sure the txid is added to the reject filter
1388          self.std_node.announce_tx_and_wait_for_getdata(tx3)
1389          test_transaction_acceptance(self.nodes[1], self.std_node, tx3, with_witness=True, accepted=False, reason="bad-txns-nonstandard-inputs")
1390          # Now the node will no longer ask for getdata of this transaction when advertised by same txid
1391          self.std_node.announce_tx_and_wait_for_getdata(tx3, success=False)
1392  
1393          # Spending a higher version witness output is not allowed by policy,
1394          # even with the node that accepts non-standard txs.
1395          test_transaction_acceptance(self.nodes[0], self.test_node, tx3, with_witness=True, accepted=False, reason="reserved for soft-fork upgrades")
1396  
1397          # Building a block with the transaction must be valid, however.
1398          block = self.build_next_block()
1399          self.update_witness_block_with_transactions(block, [tx2, tx3])
1400          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1401          self.sync_blocks()
1402  
1403          # Add utxo to our list
1404          self.utxo.append(UTXO(tx3.txid_int, 0, tx3.vout[0].nValue))
1405  
1406      @subtest
1407      def test_premature_coinbase_witness_spend(self):
1408  
1409          block = self.build_next_block()
1410          # Change the output of the block to be a witness output.
1411          witness_script = CScript([OP_TRUE])
1412          script_pubkey = script_to_p2wsh_script(witness_script)
1413          block.vtx[0].vout[0].scriptPubKey = script_pubkey
1414          # This next line will rehash the coinbase and update the merkle
1415          # root, and solve.
1416          self.update_witness_block_with_transactions(block, [])
1417          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1418  
1419          spend_tx = CTransaction()
1420          spend_tx.vin = [CTxIn(COutPoint(block.vtx[0].txid_int, 0), b"")]
1421          spend_tx.vout = [CTxOut(block.vtx[0].vout[0].nValue, witness_script)]
1422          spend_tx.wit.vtxinwit.append(CTxInWitness())
1423          spend_tx.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
1424  
1425          # Now test a premature spend.
1426          self.generate(self.nodes[0], 98)
1427          block2 = self.build_next_block()
1428          self.update_witness_block_with_transactions(block2, [spend_tx])
1429          test_witness_block(self.nodes[0], self.test_node, block2, accepted=False, reason='bad-txns-premature-spend-of-coinbase')
1430  
1431          # Advancing one more block should allow the spend.
1432          self.generate(self.nodes[0], 1)
1433          block2 = self.build_next_block()
1434          self.update_witness_block_with_transactions(block2, [spend_tx])
1435          test_witness_block(self.nodes[0], self.test_node, block2, accepted=True)
1436          self.sync_blocks()
1437  
1438      @subtest
1439      def test_uncompressed_pubkey(self):
1440          """Test uncompressed pubkey validity in segwit transactions.
1441  
1442          Uncompressed pubkeys are no longer supported in default relay policy,
1443          but (for now) are still valid in blocks."""
1444  
1445          # Segwit transactions using uncompressed pubkeys are not accepted
1446          # under default policy, but should still pass consensus.
1447          key, pubkey = generate_keypair(compressed=False)
1448          assert_equal(len(pubkey), 65)  # This should be an uncompressed pubkey
1449  
1450          utxo = self.utxo.pop(0)
1451  
1452          # Test 1: P2WPKH
1453          # First create a P2WPKH output that uses an uncompressed pubkey
1454          pubkeyhash = hash160(pubkey)
1455          script_pkh = key_to_p2wpkh_script(pubkey)
1456          tx = CTransaction()
1457          tx.vin.append(CTxIn(COutPoint(utxo.sha256, utxo.n), b""))
1458          tx.vout.append(CTxOut(utxo.nValue - 1000, script_pkh))
1459  
1460          # Confirm it in a block.
1461          block = self.build_next_block()
1462          self.update_witness_block_with_transactions(block, [tx])
1463          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1464  
1465          # Now try to spend it. Send it to a P2WSH output, which we'll
1466          # use in the next test.
1467          witness_script = key_to_p2pk_script(pubkey)
1468          script_wsh = script_to_p2wsh_script(witness_script)
1469  
1470          tx2 = CTransaction()
1471          tx2.vin.append(CTxIn(COutPoint(tx.txid_int, 0), b""))
1472          tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, script_wsh))
1473          script = keyhash_to_p2pkh_script(pubkeyhash)
1474          tx2.wit.vtxinwit.append(CTxInWitness())
1475          tx2.wit.vtxinwit[0].scriptWitness.stack = [pubkey]
1476          sign_input_segwitv0(tx2, 0, script, tx.vout[0].nValue, key)
1477  
1478          # Should fail policy test.
1479          test_transaction_acceptance(self.nodes[0], self.test_node, tx2, True, False, 'mempool-script-verify-flag-failed (Using non-compressed keys in segwit)')
1480          # But passes consensus.
1481          block = self.build_next_block()
1482          self.update_witness_block_with_transactions(block, [tx2])
1483          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1484  
1485          # Test 2: P2WSH
1486          # Try to spend the P2WSH output created in last test.
1487          # Send it to a P2SH(P2WSH) output, which we'll use in the next test.
1488          script_p2sh = script_to_p2sh_script(script_wsh)
1489          script_sig = CScript([script_wsh])
1490  
1491          tx3 = CTransaction()
1492          tx3.vin.append(CTxIn(COutPoint(tx2.txid_int, 0), b""))
1493          tx3.vout.append(CTxOut(tx2.vout[0].nValue - 1000, script_p2sh))
1494          tx3.wit.vtxinwit.append(CTxInWitness())
1495          sign_p2pk_witness_input(witness_script, tx3, 0, SIGHASH_ALL, tx2.vout[0].nValue, key)
1496  
1497          # Should fail policy test.
1498          test_transaction_acceptance(self.nodes[0], self.test_node, tx3, True, False, 'mempool-script-verify-flag-failed (Using non-compressed keys in segwit)')
1499          # But passes consensus.
1500          block = self.build_next_block()
1501          self.update_witness_block_with_transactions(block, [tx3])
1502          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1503  
1504          # Test 3: P2SH(P2WSH)
1505          # Try to spend the P2SH output created in the last test.
1506          # Send it to a P2PKH output, which we'll use in the next test.
1507          script_pubkey = keyhash_to_p2pkh_script(pubkeyhash)
1508          tx4 = CTransaction()
1509          tx4.vin.append(CTxIn(COutPoint(tx3.txid_int, 0), script_sig))
1510          tx4.vout.append(CTxOut(tx3.vout[0].nValue - 1000, script_pubkey))
1511          tx4.wit.vtxinwit.append(CTxInWitness())
1512          sign_p2pk_witness_input(witness_script, tx4, 0, SIGHASH_ALL, tx3.vout[0].nValue, key)
1513  
1514          # Should fail policy test.
1515          test_transaction_acceptance(self.nodes[0], self.test_node, tx4, True, False, 'mempool-script-verify-flag-failed (Using non-compressed keys in segwit)')
1516          block = self.build_next_block()
1517          self.update_witness_block_with_transactions(block, [tx4])
1518          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1519  
1520          # Test 4: Uncompressed pubkeys should still be valid in non-segwit
1521          # transactions.
1522          tx5 = CTransaction()
1523          tx5.vin.append(CTxIn(COutPoint(tx4.txid_int, 0), b""))
1524          tx5.vout.append(CTxOut(tx4.vout[0].nValue - 1000, CScript([OP_TRUE])))
1525          tx5.vin[0].scriptSig = CScript([pubkey])
1526          sign_input_legacy(tx5, 0, script_pubkey, key)
1527          # Should pass policy and consensus.
1528          test_transaction_acceptance(self.nodes[0], self.test_node, tx5, True, True)
1529          block = self.build_next_block()
1530          self.update_witness_block_with_transactions(block, [tx5])
1531          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1532          self.utxo.append(UTXO(tx5.txid_int, 0, tx5.vout[0].nValue))
1533  
1534      @subtest
1535      def test_signature_version_1(self):
1536          key, pubkey = generate_keypair()
1537          witness_script = key_to_p2pk_script(pubkey)
1538          script_pubkey = script_to_p2wsh_script(witness_script)
1539  
1540          # First create a witness output for use in the tests.
1541          tx = CTransaction()
1542          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
1543          tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, script_pubkey))
1544  
1545          test_transaction_acceptance(self.nodes[0], self.test_node, tx, with_witness=True, accepted=True)
1546          # Mine this transaction in preparation for following tests.
1547          block = self.build_next_block()
1548          self.update_witness_block_with_transactions(block, [tx])
1549          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1550          self.sync_blocks()
1551          self.utxo.pop(0)
1552  
1553          # Test each hashtype
1554          prev_utxo = UTXO(tx.txid_int, 0, tx.vout[0].nValue)
1555          for sigflag in [0, SIGHASH_ANYONECANPAY]:
1556              for hashtype in [SIGHASH_ALL, SIGHASH_NONE, SIGHASH_SINGLE]:
1557                  hashtype |= sigflag
1558                  block = self.build_next_block()
1559                  tx = CTransaction()
1560                  tx.vin.append(CTxIn(COutPoint(prev_utxo.sha256, prev_utxo.n), b""))
1561                  tx.vout.append(CTxOut(prev_utxo.nValue - 1000, script_pubkey))
1562                  tx.wit.vtxinwit.append(CTxInWitness())
1563                  # Too-large input value
1564                  sign_p2pk_witness_input(witness_script, tx, 0, hashtype, prev_utxo.nValue + 1, key)
1565                  self.update_witness_block_with_transactions(block, [tx])
1566                  test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1567                                     reason='block-script-verify-flag-failed (Script evaluated without error '
1568                                            'but finished with a false/empty top stack element')
1569  
1570                  # Too-small input value
1571                  sign_p2pk_witness_input(witness_script, tx, 0, hashtype, prev_utxo.nValue - 1, key)
1572                  block.vtx.pop()  # remove last tx
1573                  self.update_witness_block_with_transactions(block, [tx])
1574                  test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1575                                     reason='block-script-verify-flag-failed (Script evaluated without error '
1576                                            'but finished with a false/empty top stack element')
1577  
1578                  # Now try correct value
1579                  sign_p2pk_witness_input(witness_script, tx, 0, hashtype, prev_utxo.nValue, key)
1580                  block.vtx.pop()
1581                  self.update_witness_block_with_transactions(block, [tx])
1582                  test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1583  
1584                  prev_utxo = UTXO(tx.txid_int, 0, tx.vout[0].nValue)
1585  
1586          # Test combinations of signature hashes.
1587          # Split the utxo into a lot of outputs.
1588          # Randomly choose up to 10 to spend, sign with different hashtypes, and
1589          # output to a random number of outputs.  Repeat NUM_SIGHASH_TESTS times.
1590          # Ensure that we've tested a situation where we use SIGHASH_SINGLE with
1591          # an input index > number of outputs.
1592          NUM_SIGHASH_TESTS = 500
1593          temp_utxos = []
1594          tx = CTransaction()
1595          tx.vin.append(CTxIn(COutPoint(prev_utxo.sha256, prev_utxo.n), b""))
1596          split_value = prev_utxo.nValue // NUM_SIGHASH_TESTS
1597          for _ in range(NUM_SIGHASH_TESTS):
1598              tx.vout.append(CTxOut(split_value, script_pubkey))
1599          tx.wit.vtxinwit.append(CTxInWitness())
1600          sign_p2pk_witness_input(witness_script, tx, 0, SIGHASH_ALL, prev_utxo.nValue, key)
1601          for i in range(NUM_SIGHASH_TESTS):
1602              temp_utxos.append(UTXO(tx.txid_int, i, split_value))
1603  
1604          block = self.build_next_block()
1605          self.update_witness_block_with_transactions(block, [tx])
1606          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1607  
1608          block = self.build_next_block()
1609          used_sighash_single_out_of_bounds = False
1610          for i in range(NUM_SIGHASH_TESTS):
1611              # Ping regularly to keep the connection alive
1612              if (not i % 100):
1613                  self.test_node.sync_with_ping()
1614              # Choose random number of inputs to use.
1615              num_inputs = random.randint(1, 10)
1616              # Create a slight bias for producing more utxos
1617              num_outputs = random.randint(1, 11)
1618              random.shuffle(temp_utxos)
1619              assert len(temp_utxos) > num_inputs
1620              tx = CTransaction()
1621              total_value = 0
1622              for i in range(num_inputs):
1623                  tx.vin.append(CTxIn(COutPoint(temp_utxos[i].sha256, temp_utxos[i].n), b""))
1624                  tx.wit.vtxinwit.append(CTxInWitness())
1625                  total_value += temp_utxos[i].nValue
1626              split_value = total_value // num_outputs
1627              for _ in range(num_outputs):
1628                  tx.vout.append(CTxOut(split_value, script_pubkey))
1629              for i in range(num_inputs):
1630                  # Now try to sign each input, using a random hashtype.
1631                  anyonecanpay = 0
1632                  if random.randint(0, 1):
1633                      anyonecanpay = SIGHASH_ANYONECANPAY
1634                  hashtype = random.randint(1, 3) | anyonecanpay
1635                  sign_p2pk_witness_input(witness_script, tx, i, hashtype, temp_utxos[i].nValue, key)
1636                  if (hashtype == SIGHASH_SINGLE and i >= num_outputs):
1637                      used_sighash_single_out_of_bounds = True
1638              for i in range(num_outputs):
1639                  temp_utxos.append(UTXO(tx.txid_int, i, split_value))
1640              temp_utxos = temp_utxos[num_inputs:]
1641  
1642              block.vtx.append(tx)
1643  
1644              # Test the block periodically, if we're close to maxblocksize
1645              if block.get_weight() > MAX_BLOCK_WEIGHT - 4000:
1646                  self.update_witness_block_with_transactions(block, [])
1647                  test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1648                  block = self.build_next_block()
1649  
1650          if (not used_sighash_single_out_of_bounds):
1651              self.log.info("WARNING: this test run didn't attempt SIGHASH_SINGLE with out-of-bounds index value")
1652          # Test the transactions we've added to the block
1653          if (len(block.vtx) > 1):
1654              self.update_witness_block_with_transactions(block, [])
1655              test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1656  
1657          # Now test witness version 0 P2PKH transactions
1658          pubkeyhash = hash160(pubkey)
1659          script_pkh = key_to_p2wpkh_script(pubkey)
1660          tx = CTransaction()
1661          tx.vin.append(CTxIn(COutPoint(temp_utxos[0].sha256, temp_utxos[0].n), b""))
1662          tx.vout.append(CTxOut(temp_utxos[0].nValue, script_pkh))
1663          tx.wit.vtxinwit.append(CTxInWitness())
1664          sign_p2pk_witness_input(witness_script, tx, 0, SIGHASH_ALL, temp_utxos[0].nValue, key)
1665          tx2 = CTransaction()
1666          tx2.vin.append(CTxIn(COutPoint(tx.txid_int, 0), b""))
1667          tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_TRUE])))
1668  
1669          script = keyhash_to_p2pkh_script(pubkeyhash)
1670          tx2.wit.vtxinwit.append(CTxInWitness())
1671          sign_input_segwitv0(tx2, 0, script, tx.vout[0].nValue, key)
1672          signature = tx2.wit.vtxinwit[0].scriptWitness.stack.pop()
1673  
1674          # Check that we can't have a scriptSig
1675          tx2.vin[0].scriptSig = CScript([signature, pubkey])
1676          block = self.build_next_block()
1677          self.update_witness_block_with_transactions(block, [tx, tx2])
1678          test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1679                             reason='block-script-verify-flag-failed (Witness requires empty scriptSig)')
1680  
1681          # Move the signature to the witness.
1682          block.vtx.pop()
1683          tx2.wit.vtxinwit.append(CTxInWitness())
1684          tx2.wit.vtxinwit[0].scriptWitness.stack = [signature, pubkey]
1685          tx2.vin[0].scriptSig = b""
1686  
1687          self.update_witness_block_with_transactions(block, [tx2])
1688          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1689  
1690          temp_utxos.pop(0)
1691  
1692          # Update self.utxos for later tests by creating two outputs
1693          # that consolidate all the coins in temp_utxos.
1694          output_value = sum(i.nValue for i in temp_utxos) // 2
1695  
1696          tx = CTransaction()
1697          index = 0
1698          # Just spend to our usual anyone-can-spend output
1699          tx.vout = [CTxOut(output_value, CScript([OP_TRUE]))] * 2
1700          for i in temp_utxos:
1701              # Use SIGHASH_ALL|SIGHASH_ANYONECANPAY so we can build up
1702              # the signatures as we go.
1703              tx.vin.append(CTxIn(COutPoint(i.sha256, i.n), b""))
1704              tx.wit.vtxinwit.append(CTxInWitness())
1705              sign_p2pk_witness_input(witness_script, tx, index, SIGHASH_ALL | SIGHASH_ANYONECANPAY, i.nValue, key)
1706              index += 1
1707          block = self.build_next_block()
1708          self.update_witness_block_with_transactions(block, [tx])
1709          test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
1710  
1711          for i in range(len(tx.vout)):
1712              self.utxo.append(UTXO(tx.txid_int, i, tx.vout[i].nValue))
1713  
1714      @subtest
1715      def test_non_standard_witness_blinding(self):
1716          """Test behavior of unnecessary witnesses in transactions does not blind the node for the transaction"""
1717  
1718          # Create a p2sh output -- this is so we can pass the standardness
1719          # rules (an anyone-can-spend OP_TRUE would be rejected, if not wrapped
1720          # in P2SH).
1721          p2sh_program = CScript([OP_TRUE])
1722          script_pubkey = script_to_p2sh_script(p2sh_program)
1723  
1724          # Now check that unnecessary witnesses can't be used to blind a node
1725          # to a transaction, eg by violating standardness checks.
1726          tx = CTransaction()
1727          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
1728          tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, script_pubkey))
1729          test_transaction_acceptance(self.nodes[0], self.test_node, tx, False, True)
1730          self.generate(self.nodes[0], 1)
1731  
1732          # We'll add an unnecessary witness to this transaction that would cause
1733          # it to be non-standard, to test that violating policy with a witness
1734          # doesn't blind a node to a transaction.  Transactions
1735          # rejected for having a witness shouldn't be added
1736          # to the rejection cache.
1737          tx2 = CTransaction()
1738          tx2.vin.append(CTxIn(COutPoint(tx.txid_int, 0), CScript([p2sh_program])))
1739          tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, script_pubkey))
1740          tx2.wit.vtxinwit.append(CTxInWitness())
1741          tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a' * 400]
1742          # This will be rejected due to a policy check:
1743          # No witness is allowed, since it is not a witness program but a p2sh program
1744          test_transaction_acceptance(self.nodes[1], self.std_node, tx2, True, False, 'bad-witness-nonstandard')
1745  
1746          # If we send without witness, it should be accepted.
1747          test_transaction_acceptance(self.nodes[1], self.std_node, tx2, False, True)
1748  
1749          # Now create a new anyone-can-spend utxo for the next test.
1750          tx3 = CTransaction()
1751          tx3.vin.append(CTxIn(COutPoint(tx2.txid_int, 0), CScript([p2sh_program])))
1752          tx3.vout.append(CTxOut(tx2.vout[0].nValue - 1000, CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
1753          test_transaction_acceptance(self.nodes[0], self.test_node, tx2, False, True)
1754          test_transaction_acceptance(self.nodes[0], self.test_node, tx3, False, True)
1755  
1756          self.generate(self.nodes[0], 1)
1757  
1758          # Update our utxo list; we spent the first entry.
1759          self.utxo.pop(0)
1760          self.utxo.append(UTXO(tx3.txid_int, 0, tx3.vout[0].nValue))
1761  
1762      @subtest
1763      def test_non_standard_witness(self):
1764          """Test detection of non-standard P2WSH witness"""
1765          pad = chr(1).encode('latin-1')
1766  
1767          # Create scripts for tests
1768          scripts = []
1769          scripts.append(CScript([OP_DROP] * 100))
1770          scripts.append(CScript([OP_DROP] * 99))
1771          scripts.append(CScript([pad * 59] * 59 + [OP_DROP] * 60))
1772          scripts.append(CScript([pad * 59] * 59 + [OP_DROP] * 61))
1773  
1774          p2wsh_scripts = []
1775  
1776          tx = CTransaction()
1777          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
1778  
1779          # For each script, generate a pair of P2WSH and P2SH-P2WSH output.
1780          outputvalue = (self.utxo[0].nValue - 1000) // (len(scripts) * 2)
1781          for i in scripts:
1782              p2wsh = script_to_p2wsh_script(i)
1783              p2wsh_scripts.append(p2wsh)
1784              tx.vout.append(CTxOut(outputvalue, p2wsh))
1785              tx.vout.append(CTxOut(outputvalue, script_to_p2sh_script(p2wsh)))
1786          txid = tx.txid_int
1787          test_transaction_acceptance(self.nodes[0], self.test_node, tx, with_witness=False, accepted=True)
1788  
1789          self.generate(self.nodes[0], 1)
1790  
1791          # Creating transactions for tests
1792          p2wsh_txs = []
1793          p2sh_txs = []
1794          for i in range(len(scripts)):
1795              p2wsh_tx = CTransaction()
1796              p2wsh_tx.vin.append(CTxIn(COutPoint(txid, i * 2)))
1797              p2wsh_tx.vout.append(CTxOut(outputvalue - 5000, CScript([OP_0, hash160(b"")])))
1798              p2wsh_tx.wit.vtxinwit.append(CTxInWitness())
1799              p2wsh_txs.append(p2wsh_tx)
1800              p2sh_tx = CTransaction()
1801              p2sh_tx.vin.append(CTxIn(COutPoint(txid, i * 2 + 1), CScript([p2wsh_scripts[i]])))
1802              p2sh_tx.vout.append(CTxOut(outputvalue - 5000, CScript([OP_0, hash160(b"")])))
1803              p2sh_tx.wit.vtxinwit.append(CTxInWitness())
1804              p2sh_txs.append(p2sh_tx)
1805  
1806          # Testing native P2WSH
1807          # Witness stack size, excluding witnessScript, over 100 is non-standard
1808          p2wsh_txs[0].wit.vtxinwit[0].scriptWitness.stack = [pad] * 101 + [scripts[0]]
1809          test_transaction_acceptance(self.nodes[1], self.std_node, p2wsh_txs[0], True, False, 'bad-witness-nonstandard')
1810          # Non-standard nodes should accept
1811          test_transaction_acceptance(self.nodes[0], self.test_node, p2wsh_txs[0], True, True)
1812  
1813          # Stack element size over 80 bytes is non-standard
1814          p2wsh_txs[1].wit.vtxinwit[0].scriptWitness.stack = [pad * 81] * 100 + [scripts[1]]
1815          test_transaction_acceptance(self.nodes[1], self.std_node, p2wsh_txs[1], True, False, 'bad-witness-nonstandard')
1816          # Non-standard nodes should accept
1817          test_transaction_acceptance(self.nodes[0], self.test_node, p2wsh_txs[1], True, True)
1818          # Standard nodes should accept if element size is not over 80 bytes
1819          p2wsh_txs[1].wit.vtxinwit[0].scriptWitness.stack = [pad * 80] * 100 + [scripts[1]]
1820          test_transaction_acceptance(self.nodes[1], self.std_node, p2wsh_txs[1], True, True)
1821  
1822          # witnessScript size at 3600 bytes is standard
1823          p2wsh_txs[2].wit.vtxinwit[0].scriptWitness.stack = [pad, pad, scripts[2]]
1824          test_transaction_acceptance(self.nodes[0], self.test_node, p2wsh_txs[2], True, True)
1825          test_transaction_acceptance(self.nodes[1], self.std_node, p2wsh_txs[2], True, True)
1826  
1827          # witnessScript size at 3601 bytes is non-standard
1828          p2wsh_txs[3].wit.vtxinwit[0].scriptWitness.stack = [pad, pad, pad, scripts[3]]
1829          test_transaction_acceptance(self.nodes[1], self.std_node, p2wsh_txs[3], True, False, 'bad-witness-nonstandard')
1830          # Non-standard nodes should accept
1831          test_transaction_acceptance(self.nodes[0], self.test_node, p2wsh_txs[3], True, True)
1832  
1833          # Repeating the same tests with P2SH-P2WSH
1834          p2sh_txs[0].wit.vtxinwit[0].scriptWitness.stack = [pad] * 101 + [scripts[0]]
1835          test_transaction_acceptance(self.nodes[1], self.std_node, p2sh_txs[0], True, False, 'bad-witness-nonstandard')
1836          test_transaction_acceptance(self.nodes[0], self.test_node, p2sh_txs[0], True, True)
1837          p2sh_txs[1].wit.vtxinwit[0].scriptWitness.stack = [pad * 81] * 100 + [scripts[1]]
1838          test_transaction_acceptance(self.nodes[1], self.std_node, p2sh_txs[1], True, False, 'bad-witness-nonstandard')
1839          test_transaction_acceptance(self.nodes[0], self.test_node, p2sh_txs[1], True, True)
1840          p2sh_txs[1].wit.vtxinwit[0].scriptWitness.stack = [pad * 80] * 100 + [scripts[1]]
1841          test_transaction_acceptance(self.nodes[1], self.std_node, p2sh_txs[1], True, True)
1842          p2sh_txs[2].wit.vtxinwit[0].scriptWitness.stack = [pad, pad, scripts[2]]
1843          test_transaction_acceptance(self.nodes[0], self.test_node, p2sh_txs[2], True, True)
1844          test_transaction_acceptance(self.nodes[1], self.std_node, p2sh_txs[2], True, True)
1845          p2sh_txs[3].wit.vtxinwit[0].scriptWitness.stack = [pad, pad, pad, scripts[3]]
1846          test_transaction_acceptance(self.nodes[1], self.std_node, p2sh_txs[3], True, False, 'bad-witness-nonstandard')
1847          test_transaction_acceptance(self.nodes[0], self.test_node, p2sh_txs[3], True, True)
1848  
1849          self.generate(self.nodes[0], 1)  # Mine and clean up the mempool of non-standard node
1850          # Valid but non-standard transactions in a block should be accepted by standard node
1851          self.sync_blocks()
1852          assert_equal(len(self.nodes[0].getrawmempool()), 0)
1853          assert_equal(len(self.nodes[1].getrawmempool()), 0)
1854  
1855          self.utxo.pop(0)
1856  
1857      @subtest
1858      def test_witness_sigops(self):
1859          """Test sigop counting is correct inside witnesses."""
1860  
1861          # Keep this under MAX_OPS_PER_SCRIPT (201)
1862          witness_script = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKMULTISIG] * 5 + [OP_CHECKSIG] * 193 + [OP_ENDIF])
1863          script_pubkey = script_to_p2wsh_script(witness_script)
1864  
1865          sigops_per_script = 20 * 5 + 193 * 1
1866          # We'll produce 2 extra outputs, one with a program that would take us
1867          # over max sig ops, and one with a program that would exactly reach max
1868          # sig ops
1869          outputs = (MAX_SIGOP_COST // sigops_per_script) + 2
1870          extra_sigops_available = MAX_SIGOP_COST % sigops_per_script
1871  
1872          # We chose the number of checkmultisigs/checksigs to make this work:
1873          assert extra_sigops_available < 100  # steer clear of MAX_OPS_PER_SCRIPT
1874  
1875          # This script, when spent with the first
1876          # N(=MAX_SIGOP_COST//sigops_per_script) outputs of our transaction,
1877          # would push us just over the block sigop limit.
1878          witness_script_toomany = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKSIG] * (extra_sigops_available + 1) + [OP_ENDIF])
1879          script_pubkey_toomany = script_to_p2wsh_script(witness_script_toomany)
1880  
1881          # If we spend this script instead, we would exactly reach our sigop
1882          # limit (for witness sigops).
1883          witness_script_justright = CScript([OP_TRUE, OP_IF, OP_TRUE, OP_ELSE] + [OP_CHECKSIG] * (extra_sigops_available) + [OP_ENDIF])
1884          script_pubkey_justright = script_to_p2wsh_script(witness_script_justright)
1885  
1886          # First split our available utxo into a bunch of outputs
1887          split_value = self.utxo[0].nValue // outputs
1888          tx = CTransaction()
1889          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
1890          for _ in range(outputs):
1891              tx.vout.append(CTxOut(split_value, script_pubkey))
1892          tx.vout[-2].scriptPubKey = script_pubkey_toomany
1893          tx.vout[-1].scriptPubKey = script_pubkey_justright
1894  
1895          block_1 = self.build_next_block()
1896          self.update_witness_block_with_transactions(block_1, [tx])
1897          test_witness_block(self.nodes[0], self.test_node, block_1, accepted=True)
1898  
1899          tx2 = CTransaction()
1900          # If we try to spend the first n-1 outputs from tx, that should be
1901          # too many sigops.
1902          total_value = 0
1903          for i in range(outputs - 1):
1904              tx2.vin.append(CTxIn(COutPoint(tx.txid_int, i), b""))
1905              tx2.wit.vtxinwit.append(CTxInWitness())
1906              tx2.wit.vtxinwit[-1].scriptWitness.stack = [witness_script]
1907              total_value += tx.vout[i].nValue
1908          tx2.wit.vtxinwit[-1].scriptWitness.stack = [witness_script_toomany]
1909          tx2.vout.append(CTxOut(total_value, CScript([OP_TRUE])))
1910  
1911          block_2 = self.build_next_block()
1912          self.update_witness_block_with_transactions(block_2, [tx2])
1913          test_witness_block(self.nodes[0], self.test_node, block_2, accepted=False, reason='bad-blk-sigops')
1914  
1915          # Try dropping the last input in tx2, and add an output that has
1916          # too many sigops (contributing to legacy sigop count).
1917          checksig_count = (extra_sigops_available // 4) + 1
1918          script_pubkey_checksigs = CScript([OP_CHECKSIG] * checksig_count)
1919          tx2.vout.append(CTxOut(0, script_pubkey_checksigs))
1920          tx2.vin.pop()
1921          tx2.wit.vtxinwit.pop()
1922          tx2.vout[0].nValue -= tx.vout[-2].nValue
1923          block_3 = self.build_next_block()
1924          self.update_witness_block_with_transactions(block_3, [tx2])
1925          test_witness_block(self.nodes[0], self.test_node, block_3, accepted=False, reason='bad-blk-sigops')
1926  
1927          # If we drop the last checksig in this output, the tx should succeed.
1928          block_4 = self.build_next_block()
1929          tx2.vout[-1].scriptPubKey = CScript([OP_CHECKSIG] * (checksig_count - 1))
1930          self.update_witness_block_with_transactions(block_4, [tx2])
1931          test_witness_block(self.nodes[0], self.test_node, block_4, accepted=True)
1932  
1933          # Reset the tip back down for the next test
1934          self.sync_blocks()
1935          for x in self.nodes:
1936              x.invalidateblock(block_4.hash_hex)
1937  
1938          # Try replacing the last input of tx2 to be spending the last
1939          # output of tx
1940          block_5 = self.build_next_block()
1941          tx2.vout.pop()
1942          tx2.vin.append(CTxIn(COutPoint(tx.txid_int, outputs - 1), b""))
1943          tx2.wit.vtxinwit.append(CTxInWitness())
1944          tx2.wit.vtxinwit[-1].scriptWitness.stack = [witness_script_justright]
1945          self.update_witness_block_with_transactions(block_5, [tx2])
1946          test_witness_block(self.nodes[0], self.test_node, block_5, accepted=True)
1947  
1948          # TODO: test p2sh sigop counting
1949  
1950          # Cleanup and prep for next test
1951          self.utxo.pop(0)
1952          self.utxo.append(UTXO(tx2.txid_int, 0, tx2.vout[0].nValue))
1953  
1954      @subtest
1955      def test_superfluous_witness(self):
1956          # Serialization of tx that puts witness flag to 3 always
1957          def serialize_with_bogus_witness(tx):
1958              flags = 3
1959              r = b""
1960              r += tx.version.to_bytes(4, "little")
1961              if flags:
1962                  dummy = []
1963                  r += ser_vector(dummy)
1964                  r += flags.to_bytes(1, "little")
1965              r += ser_vector(tx.vin)
1966              r += ser_vector(tx.vout)
1967              if flags & 1:
1968                  if (len(tx.wit.vtxinwit) != len(tx.vin)):
1969                      # vtxinwit must have the same length as vin
1970                      tx.wit.vtxinwit = tx.wit.vtxinwit[:len(tx.vin)]
1971                      for _ in range(len(tx.wit.vtxinwit), len(tx.vin)):
1972                          tx.wit.vtxinwit.append(CTxInWitness())
1973                  r += tx.wit.serialize()
1974              r += tx.nLockTime.to_bytes(4, "little")
1975              return r
1976  
1977          class msg_bogus_tx(msg_tx):
1978              def serialize(self):
1979                  return serialize_with_bogus_witness(self.tx)
1980  
1981          tx = self.wallet.create_self_transfer()['tx']
1982          assert_raises_rpc_error(-22, "TX decode failed", self.nodes[0].decoderawtransaction, hexstring=serialize_with_bogus_witness(tx).hex(), iswitness=True)
1983          with self.nodes[0].assert_debug_log(['Unknown transaction optional data']):
1984              self.test_node.send_and_ping(msg_bogus_tx(tx))
1985          tx.wit.vtxinwit = []  # drop witness
1986          assert_raises_rpc_error(-22, "TX decode failed", self.nodes[0].decoderawtransaction, hexstring=serialize_with_bogus_witness(tx).hex(), iswitness=True)
1987          with self.nodes[0].assert_debug_log(['Superfluous witness record']):
1988              self.test_node.send_and_ping(msg_bogus_tx(tx))
1989  
1990      @subtest
1991      def test_wtxid_relay(self):
1992          # Use brand new nodes to avoid contamination from earlier tests
1993          self.wtx_node = self.nodes[0].add_p2p_connection(TestP2PConn(wtxidrelay=True), services=P2P_SERVICES)
1994          self.tx_node = self.nodes[0].add_p2p_connection(TestP2PConn(wtxidrelay=False), services=P2P_SERVICES)
1995  
1996          # Check wtxidrelay feature negotiation message through connecting a new peer
1997          def received_wtxidrelay():
1998              return (len(self.wtx_node.last_wtxidrelay) > 0)
1999          self.wtx_node.wait_until(received_wtxidrelay)
2000  
2001          # Create a Segwit output from the latest UTXO
2002          # and announce it to the network
2003          witness_script = CScript([OP_TRUE])
2004          script_pubkey = script_to_p2wsh_script(witness_script)
2005  
2006          tx = CTransaction()
2007          tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
2008          tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, script_pubkey))
2009  
2010          # Create a Segwit transaction
2011          tx2 = CTransaction()
2012          tx2.vin.append(CTxIn(COutPoint(tx.txid_int, 0), b""))
2013          tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, script_pubkey))
2014          tx2.wit.vtxinwit.append(CTxInWitness())
2015          tx2.wit.vtxinwit[0].scriptWitness.stack = [witness_script]
2016  
2017          # Announce Segwit transaction with wtxid
2018          # and wait for getdata
2019          self.wtx_node.announce_tx_and_wait_for_getdata(tx2, use_wtxid=True)
2020          with p2p_lock:
2021              lgd = self.wtx_node.lastgetdata[:]
2022          assert_equal(lgd, [CInv(MSG_WTX, tx2.wtxid_int)])
2023  
2024          # Announce Segwit transaction from non wtxidrelay peer
2025          # and wait for getdata
2026          self.tx_node.announce_tx_and_wait_for_getdata(tx2, use_wtxid=False)
2027          with p2p_lock:
2028              lgd = self.tx_node.lastgetdata[:]
2029          assert_equal(lgd, [CInv(MSG_TX|MSG_WITNESS_FLAG, tx2.txid_int)])
2030  
2031          # Send tx2 through; it's an orphan so won't be accepted
2032          with p2p_lock:
2033              self.wtx_node.last_message.pop("getdata", None)
2034          test_transaction_acceptance(self.nodes[0], self.wtx_node, tx2, with_witness=True, accepted=False)
2035  
2036          # Disconnect tx_node to avoid the possibility of it being selected for orphan resolution.
2037          self.tx_node.peer_disconnect()
2038  
2039          # Expect a request for parent (tx) by txid despite use of WTX peer
2040          self.wtx_node.wait_for_getdata([tx.txid_int], timeout=60)
2041          with p2p_lock:
2042              lgd = self.wtx_node.lastgetdata[:]
2043          assert_equal(lgd, [CInv(MSG_WITNESS_TX, tx.txid_int)])
2044  
2045          # Send tx through
2046          test_transaction_acceptance(self.nodes[0], self.wtx_node, tx, with_witness=False, accepted=True)
2047  
2048          # Check tx2 is there now
2049          assert_equal(tx2.txid_hex in self.nodes[0].getrawmempool(), True)
2050  
2051  
2052  if __name__ == '__main__':
2053      SegWitTest(__file__).main()
2054