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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 __init__(self):
  35. super().__init__()
  36. self.num_nodes = 1
  37. self.setup_clean_chain = True
  38. def setup_network(self):
  39. # Must set the blockversion for this test
  40. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir,
  41. extra_args=[['-whitelist=127.0.0.1', '-walletprematurewitness']])
  42. def run_test(self):
  43. self.address = self.nodes[0].getnewaddress()
  44. self.ms_address = self.nodes[0].addmultisigaddress(1,[self.address])
  45. self.wit_address = self.nodes[0].addwitnessaddress(self.address)
  46. self.wit_ms_address = self.nodes[0].addwitnessaddress(self.ms_address)
  47. NetworkThread().start() # Start up network handling in another thread
  48. self.coinbase_blocks = self.nodes[0].generate(2) # Block 2
  49. coinbase_txid = []
  50. for i in self.coinbase_blocks:
  51. coinbase_txid.append(self.nodes[0].getblock(i)['tx'][0])
  52. self.nodes[0].generate(427) # Block 429
  53. self.lastblockhash = self.nodes[0].getbestblockhash()
  54. self.tip = int("0x" + self.lastblockhash, 0)
  55. self.lastblockheight = 429
  56. self.lastblocktime = int(time.time()) + 429
  57. self.log.info("Test 1: NULLDUMMY compliant base transactions should be accepted to mempool and mined before activation [430]")
  58. test1txs = [self.create_transaction(self.nodes[0], coinbase_txid[0], self.ms_address, 49)]
  59. txid1 = self.nodes[0].sendrawtransaction(bytes_to_hex_str(test1txs[0].serialize_with_witness()), True)
  60. test1txs.append(self.create_transaction(self.nodes[0], txid1, self.ms_address, 48))
  61. txid2 = self.nodes[0].sendrawtransaction(bytes_to_hex_str(test1txs[1].serialize_with_witness()), True)
  62. test1txs.append(self.create_transaction(self.nodes[0], coinbase_txid[1], self.wit_ms_address, 49))
  63. txid3 = self.nodes[0].sendrawtransaction(bytes_to_hex_str(test1txs[2].serialize_with_witness()), True)
  64. self.block_submit(self.nodes[0], test1txs, False, True)
  65. self.log.info("Test 2: Non-NULLDUMMY base multisig transaction should not be accepted to mempool before activation")
  66. test2tx = self.create_transaction(self.nodes[0], txid2, self.ms_address, 47)
  67. trueDummy(test2tx)
  68. assert_raises_jsonrpc(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, bytes_to_hex_str(test2tx.serialize_with_witness()), True)
  69. self.log.info("Test 3: Non-NULLDUMMY base transactions should be accepted in a block before activation [431]")
  70. self.block_submit(self.nodes[0], [test2tx], False, True)
  71. self.log.info("Test 4: Non-NULLDUMMY base multisig transaction is invalid after activation")
  72. test4tx = self.create_transaction(self.nodes[0], test2tx.hash, self.address, 46)
  73. test6txs=[CTransaction(test4tx)]
  74. trueDummy(test4tx)
  75. assert_raises_jsonrpc(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, bytes_to_hex_str(test4tx.serialize_with_witness()), True)
  76. self.block_submit(self.nodes[0], [test4tx])
  77. self.log.info("Test 5: Non-NULLDUMMY P2WSH multisig transaction invalid after activation")
  78. test5tx = self.create_transaction(self.nodes[0], txid3, self.wit_address, 48)
  79. test6txs.append(CTransaction(test5tx))
  80. test5tx.wit.vtxinwit[0].scriptWitness.stack[0] = b'\x01'
  81. assert_raises_jsonrpc(-26, NULLDUMMY_ERROR, self.nodes[0].sendrawtransaction, bytes_to_hex_str(test5tx.serialize_with_witness()), True)
  82. self.block_submit(self.nodes[0], [test5tx], True)
  83. self.log.info("Test 6: NULLDUMMY compliant base/witness transactions should be accepted to mempool and in block after activation [432]")
  84. for i in test6txs:
  85. self.nodes[0].sendrawtransaction(bytes_to_hex_str(i.serialize_with_witness()), True)
  86. self.block_submit(self.nodes[0], test6txs, True, True)
  87. def create_transaction(self, node, txid, to_address, amount):
  88. inputs = [{ "txid" : txid, "vout" : 0}]
  89. outputs = { to_address : amount }
  90. rawtx = node.createrawtransaction(inputs, outputs)
  91. signresult = node.signrawtransaction(rawtx)
  92. tx = CTransaction()
  93. f = BytesIO(hex_str_to_bytes(signresult['hex']))
  94. tx.deserialize(f)
  95. return tx
  96. def block_submit(self, node, txs, witness = False, accept = False):
  97. block = create_block(self.tip, create_coinbase(self.lastblockheight + 1), self.lastblocktime + 1)
  98. block.nVersion = 4
  99. for tx in txs:
  100. tx.rehash()
  101. block.vtx.append(tx)
  102. block.hashMerkleRoot = block.calc_merkle_root()
  103. witness and add_witness_commitment(block)
  104. block.rehash()
  105. block.solve()
  106. node.submitblock(bytes_to_hex_str(block.serialize(True)))
  107. if (accept):
  108. assert_equal(node.getbestblockhash(), block.hash)
  109. self.tip = block.sha256
  110. self.lastblockhash = block.hash
  111. self.lastblocktime += 1
  112. self.lastblockheight += 1
  113. else:
  114. assert_equal(node.getbestblockhash(), self.lastblockhash)
  115. if __name__ == '__main__':
  116. NULLDUMMYTest().main()