You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nulldummy.py 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2016 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 NULLDUMMY softfork.
  6. Connect to a single node.
  7. Generate 2 blocks (save the coinbases for later).
  8. Generate 427 more blocks.
  9. [Policy/Consensus] Check that NULLDUMMY compliant transactions are accepted in the 430th block.
  10. [Policy] Check that non-NULLDUMMY transactions are rejected before activation.
  11. [Consensus] Check that the new NULLDUMMY rules are not enforced on the 431st block.
  12. [Policy/Consensus] Check that the new NULLDUMMY rules are enforced on the 432nd block.
  13. """
  14. from test_framework.test_framework import BitcoinTestFramework
  15. from test_framework.util import *
  16. from test_framework.mininode import CTransaction, NetworkThread
  17. from test_framework.blocktools import create_coinbase, create_block, add_witness_commitment
  18. from test_framework.script import CScript
  19. from io import BytesIO
  20. import time
  21. NULLDUMMY_ERROR = "64: non-mandatory-script-verify-flag (Dummy CHECKMULTISIG argument must be zero)"
  22. def trueDummy(tx):
  23. scriptSig = CScript(tx.vin[0].scriptSig)
  24. newscript = []
  25. for i in scriptSig:
  26. if (len(newscript) == 0):
  27. assert(len(i) == 0)
  28. newscript.append(b'\x51')
  29. else:
  30. newscript.append(i)
  31. tx.vin[0].scriptSig = CScript(newscript)
  32. tx.rehash()
  33. class NULLDUMMYTest(BitcoinTestFramework):
  34. def set_test_params(self):
  35. self.num_nodes = 1
  36. self.setup_clean_chain = True
  37. self.extra_args = [['-whitelist=127.0.0.1', '-walletprematurewitness']]
  38. def run_test(self):
  39. self.address = self.nodes[0].getnewaddress()
  40. self.ms_address = self.nodes[0].addmultisigaddress(1,[self.address])
  41. self.wit_address = self.nodes[0].addwitnessaddress(self.address)
  42. self.wit_ms_address = self.nodes[0].addwitnessaddress(self.ms_address)
  43. NetworkThread().start() # Start up network handling in another thread
  44. self.coinbase_blocks = self.nodes[0].generate(2) # Block 2
  45. coinbase_txid = []
  46. for i in self.coinbase_blocks:
  47. coinbase_txid.append(self.nodes[0].getblock(i)['tx'][0])
  48. self.nodes[0].generate(427) # Block 429
  49. self.lastblockhash = self.nodes[0].getbestblockhash()
  50. self.tip = int("0x" + self.lastblockhash, 0)
  51. self.lastblockheight = 429
  52. self.lastblocktime = int(time.time()) + 429
  53. self.log.info("Test 1: NULLDUMMY compliant base transactions should be accepted to mempool and mined before activation [430]")
  54. test1txs = [self.create_transaction(self.nodes[0], coinbase_txid[0], self.ms_address, 49)]
  55. txid1 = self.nodes[0].sendrawtransaction(bytes_to_hex_str(test1txs[0].serialize_with_witness()), True)
  56. test1txs.append(self.create_transaction(self.nodes[0], txid1, self.ms_address, 48))
  57. txid2 = self.nodes[0].sendrawtransaction(bytes_to_hex_str(test1txs[1].serialize_with_witness()), True)
  58. test1txs.append(self.create_transaction(self.nodes[0], coinbase_txid[1], self.wit_ms_address, 49))
  59. txid3 = self.nodes[0].sendrawtransaction(bytes_to_hex_str(test1txs[2].serialize_with_witness()), True)
  60. self.block_submit(self.nodes[0], test1txs, False, True)
  61. self.log.info("Test 2: Non-NULLDUMMY base multisig transaction should not be accepted to mempool before activation")
  62. test2tx = self.create_transaction(self.nodes[0], txid2, self.ms_address, 47)
  63. trueDummy(test2tx)
  64. assert_raises_rpc_error(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, bytes_to_hex_str(test2tx.serialize_with_witness()), True)
  65. self.log.info("Test 3: Non-NULLDUMMY base transactions should be accepted in a block before activation [431]")
  66. self.block_submit(self.nodes[0], [test2tx], False, True)
  67. self.log.info("Test 4: Non-NULLDUMMY base multisig transaction is invalid after activation")
  68. test4tx = self.create_transaction(self.nodes[0], test2tx.hash, self.address, 46)
  69. test6txs=[CTransaction(test4tx)]
  70. trueDummy(test4tx)
  71. assert_raises_rpc_error(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, bytes_to_hex_str(test4tx.serialize_with_witness()), True)
  72. self.block_submit(self.nodes[0], [test4tx])
  73. self.log.info("Test 5: Non-NULLDUMMY P2WSH multisig transaction invalid after activation")
  74. test5tx = self.create_transaction(self.nodes[0], txid3, self.wit_address, 48)
  75. test6txs.append(CTransaction(test5tx))
  76. test5tx.wit.vtxinwit[0].scriptWitness.stack[0] = b'\x01'
  77. assert_raises_rpc_error(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, bytes_to_hex_str(test5tx.serialize_with_witness()), True)
  78. self.block_submit(self.nodes[0], [test5tx], True)
  79. self.log.info("Test 6: NULLDUMMY compliant base/witness transactions should be accepted to mempool and in block after activation [432]")
  80. for i in test6txs:
  81. self.nodes[0].sendrawtransaction(bytes_to_hex_str(i.serialize_with_witness()), True)
  82. self.block_submit(self.nodes[0], test6txs, True, True)
  83. def create_transaction(self, node, txid, to_address, amount):
  84. inputs = [{ "txid" : txid, "vout" : 0}]
  85. outputs = { to_address : amount }
  86. rawtx = node.createrawtransaction(inputs, outputs)
  87. signresult = node.signrawtransaction(rawtx)
  88. tx = CTransaction()
  89. f = BytesIO(hex_str_to_bytes(signresult['hex']))
  90. tx.deserialize(f)
  91. return tx
  92. def block_submit(self, node, txs, witness = False, accept = False):
  93. block = create_block(self.tip, create_coinbase(self.lastblockheight + 1), self.lastblocktime + 1)
  94. block.nVersion = 4
  95. for tx in txs:
  96. tx.rehash()
  97. block.vtx.append(tx)
  98. block.hashMerkleRoot = block.calc_merkle_root()
  99. witness and add_witness_commitment(block)
  100. block.rehash()
  101. block.solve()
  102. node.submitblock(bytes_to_hex_str(block.serialize(True)))
  103. if (accept):
  104. assert_equal(node.getbestblockhash(), block.hash)
  105. self.tip = block.sha256
  106. self.lastblockhash = block.hash
  107. self.lastblocktime += 1
  108. self.lastblockheight += 1
  109. else:
  110. assert_equal(node.getbestblockhash(), self.lastblockhash)
  111. if __name__ == '__main__':
  112. NULLDUMMYTest().main()