選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

bipdersig-p2p.py 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2015-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 BIP66 (DER SIG).
  6. Test that the DERSIG soft-fork activates at (regtest) height 1251.
  7. """
  8. from test_framework.test_framework import BitcoinTestFramework
  9. from test_framework.util import *
  10. from test_framework.mininode import *
  11. from test_framework.blocktools import create_coinbase, create_block
  12. from test_framework.script import CScript
  13. from io import BytesIO
  14. DERSIG_HEIGHT = 1251
  15. # Reject codes that we might receive in this test
  16. REJECT_INVALID = 16
  17. REJECT_OBSOLETE = 17
  18. REJECT_NONSTANDARD = 64
  19. # A canonical signature consists of:
  20. # <30> <total len> <02> <len R> <R> <02> <len S> <S> <hashtype>
  21. def unDERify(tx):
  22. """
  23. Make the signature in vin 0 of a tx non-DER-compliant,
  24. by adding padding after the S-value.
  25. """
  26. scriptSig = CScript(tx.vin[0].scriptSig)
  27. newscript = []
  28. for i in scriptSig:
  29. if (len(newscript) == 0):
  30. newscript.append(i[0:-1] + b'\0' + i[-1:])
  31. else:
  32. newscript.append(i)
  33. tx.vin[0].scriptSig = CScript(newscript)
  34. def create_transaction(node, coinbase, to_address, amount):
  35. from_txid = node.getblock(coinbase)['tx'][0]
  36. inputs = [{ "txid" : from_txid, "vout" : 0}]
  37. outputs = { to_address : amount }
  38. rawtx = node.createrawtransaction(inputs, outputs)
  39. signresult = node.signrawtransaction(rawtx)
  40. tx = CTransaction()
  41. tx.deserialize(BytesIO(hex_str_to_bytes(signresult['hex'])))
  42. return tx
  43. class BIP66Test(BitcoinTestFramework):
  44. def set_test_params(self):
  45. self.num_nodes = 1
  46. self.extra_args = [['-promiscuousmempoolflags=1', '-whitelist=127.0.0.1']]
  47. self.setup_clean_chain = True
  48. def run_test(self):
  49. node0 = NodeConnCB()
  50. connections = []
  51. connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], node0))
  52. node0.add_connection(connections[0])
  53. NetworkThread().start() # Start up network handling in another thread
  54. # wait_for_verack ensures that the P2P connection is fully up.
  55. node0.wait_for_verack()
  56. self.log.info("Mining %d blocks", DERSIG_HEIGHT - 2)
  57. self.coinbase_blocks = self.nodes[0].generate(DERSIG_HEIGHT - 2)
  58. self.nodeaddress = self.nodes[0].getnewaddress()
  59. self.log.info("Test that a transaction with non-DER signature can still appear in a block")
  60. spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[0],
  61. self.nodeaddress, 1.0)
  62. unDERify(spendtx)
  63. spendtx.rehash()
  64. tip = self.nodes[0].getbestblockhash()
  65. block_time = self.nodes[0].getblockheader(tip)['mediantime'] + 1
  66. block = create_block(int(tip, 16), create_coinbase(DERSIG_HEIGHT - 1), block_time)
  67. block.nVersion = 2
  68. block.vtx.append(spendtx)
  69. block.hashMerkleRoot = block.calc_merkle_root()
  70. block.rehash()
  71. block.solve()
  72. node0.send_and_ping(msg_block(block))
  73. assert_equal(self.nodes[0].getbestblockhash(), block.hash)
  74. self.log.info("Test that blocks must now be at least version 3")
  75. tip = block.sha256
  76. block_time += 1
  77. block = create_block(tip, create_coinbase(DERSIG_HEIGHT), block_time)
  78. block.nVersion = 2
  79. block.rehash()
  80. block.solve()
  81. node0.send_and_ping(msg_block(block))
  82. assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
  83. wait_until(lambda: "reject" in node0.last_message.keys(), lock=mininode_lock)
  84. with mininode_lock:
  85. assert_equal(node0.last_message["reject"].code, REJECT_OBSOLETE)
  86. assert_equal(node0.last_message["reject"].reason, b'bad-version(0x00000002)')
  87. assert_equal(node0.last_message["reject"].data, block.sha256)
  88. del node0.last_message["reject"]
  89. self.log.info("Test that transactions with non-DER signatures cannot appear in a block")
  90. block.nVersion = 3
  91. spendtx = create_transaction(self.nodes[0], self.coinbase_blocks[1],
  92. self.nodeaddress, 1.0)
  93. unDERify(spendtx)
  94. spendtx.rehash()
  95. # First we show that this tx is valid except for DERSIG by getting it
  96. # accepted to the mempool (which we can achieve with
  97. # -promiscuousmempoolflags).
  98. node0.send_and_ping(msg_tx(spendtx))
  99. assert spendtx.hash in self.nodes[0].getrawmempool()
  100. # Now we verify that a block with this transaction is invalid.
  101. block.vtx.append(spendtx)
  102. block.hashMerkleRoot = block.calc_merkle_root()
  103. block.rehash()
  104. block.solve()
  105. node0.send_and_ping(msg_block(block))
  106. assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
  107. wait_until(lambda: "reject" in node0.last_message.keys(), lock=mininode_lock)
  108. with mininode_lock:
  109. # We can receive different reject messages depending on whether
  110. # bitcoind is running with multiple script check threads. If script
  111. # check threads are not in use, then transaction script validation
  112. # happens sequentially, and bitcoind produces more specific reject
  113. # reasons.
  114. assert node0.last_message["reject"].code in [REJECT_INVALID, REJECT_NONSTANDARD]
  115. assert_equal(node0.last_message["reject"].data, block.sha256)
  116. if node0.last_message["reject"].code == REJECT_INVALID:
  117. # Generic rejection when a block is invalid
  118. assert_equal(node0.last_message["reject"].reason, b'block-validation-failed')
  119. else:
  120. assert b'Non-canonical DER signature' in node0.last_message["reject"].reason
  121. self.log.info("Test that a version 3 block with a DERSIG-compliant transaction is accepted")
  122. block.vtx[1] = create_transaction(self.nodes[0],
  123. self.coinbase_blocks[1], self.nodeaddress, 1.0)
  124. block.hashMerkleRoot = block.calc_merkle_root()
  125. block.rehash()
  126. block.solve()
  127. node0.send_and_ping(msg_block(block))
  128. assert_equal(int(self.nodes[0].getbestblockhash(), 16), block.sha256)
  129. if __name__ == '__main__':
  130. BIP66Test().main()