|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Test getnewblockhex |
| 3 | +""" |
| 4 | +from io import BytesIO |
| 5 | + |
| 6 | +from test_framework.messages import CBlock |
| 7 | +from test_framework.p2p import ( |
| 8 | + P2PDataStore, |
| 9 | +) |
| 10 | +from test_framework.test_framework import BitcoinTestFramework |
| 11 | +from test_framework.util import ( |
| 12 | + assert_equal, |
| 13 | + hex_str_to_bytes, |
| 14 | +) |
| 15 | + |
| 16 | +class GetNewBlockHexTest(BitcoinTestFramework): |
| 17 | + def set_test_params(self): |
| 18 | + self.setup_clean_chain = False |
| 19 | + self.num_nodes = 1 |
| 20 | + |
| 21 | + def run_test(self): |
| 22 | + self.log.info("Starting test...") |
| 23 | + self.bootstrap_p2p() |
| 24 | + |
| 25 | + node = self.nodes[0] |
| 26 | + |
| 27 | + height = node.getblockcount() |
| 28 | + assert_equal(height, 200) |
| 29 | + |
| 30 | + self.log.info("Call getnewblockhex with no args") |
| 31 | + hex = node.getnewblockhex() |
| 32 | + (height, hash) = self.mine_block(hex) |
| 33 | + assert_equal(height, 201) |
| 34 | + |
| 35 | + self.log.info("Call getnewblockhex with single string commitment") |
| 36 | + hex = node.getnewblockhex(0, None, "1234") |
| 37 | + assert "6a021234" in hex |
| 38 | + (height, hash) = self.mine_block(hex) |
| 39 | + assert_equal(height, 202) |
| 40 | + block = node.getblock(hash, 2) |
| 41 | + vout = block["tx"][0]["vout"] |
| 42 | + assert_equal(vout[0]["scriptPubKey"]["hex"], "6a021234") |
| 43 | + |
| 44 | + self.log.info("Call getnewblockhex with single string commitment with spaces") |
| 45 | + # ParseHex only validates hex chars, so spaces skipped |
| 46 | + hex = node.getnewblockhex(0, None, "1234 5678") |
| 47 | + assert "6a0412345678" in hex |
| 48 | + (height, hash) = self.mine_block(hex) |
| 49 | + assert_equal(height, 203) |
| 50 | + block = node.getblock(hash, 2) |
| 51 | + vout = block["tx"][0]["vout"] |
| 52 | + assert_equal(vout[0]["scriptPubKey"]["hex"], "6a0412345678") |
| 53 | + |
| 54 | + self.log.info("Call getnewblockhex with single commitment") |
| 55 | + hex = node.getnewblockhex(0, None, ["1234"]) |
| 56 | + assert "6a021234" in hex |
| 57 | + (height, hash) = self.mine_block(hex) |
| 58 | + assert_equal(height, 204) |
| 59 | + block = node.getblock(hash, 2) |
| 60 | + vout = block["tx"][0]["vout"] |
| 61 | + assert_equal(vout[0]["scriptPubKey"]["hex"], "6a021234") |
| 62 | + |
| 63 | + self.log.info("Call getnewblockhex with multiple commitments") |
| 64 | + hex = node.getnewblockhex(0, None, ["1234", "deadbeef"]) |
| 65 | + assert "6a021234" in hex |
| 66 | + assert "6a04deadbeef" in hex |
| 67 | + (height, hash) = self.mine_block(hex) |
| 68 | + assert_equal(height, 205) |
| 69 | + block = node.getblock(hash, 2) |
| 70 | + vout = block["tx"][0]["vout"] |
| 71 | + assert_equal(vout[0]["scriptPubKey"]["hex"], "6a021234") |
| 72 | + assert_equal(vout[1]["scriptPubKey"]["hex"], "6a04deadbeef") |
| 73 | + |
| 74 | + hex = node.getnewblockhex(0, None, ["1234", "dead", "cafe", "babe"]) |
| 75 | + assert "6a021234" in hex |
| 76 | + assert "6a02dead" in hex |
| 77 | + assert "6a02cafe" in hex |
| 78 | + assert "6a02babe" in hex |
| 79 | + (height, hash) = self.mine_block(hex) |
| 80 | + assert_equal(height, 206) |
| 81 | + block = node.getblock(hash, 2) |
| 82 | + vout = block["tx"][0]["vout"] |
| 83 | + assert_equal(vout[0]["scriptPubKey"]["hex"], "6a021234") |
| 84 | + assert_equal(vout[1]["scriptPubKey"]["hex"], "6a02dead") |
| 85 | + assert_equal(vout[2]["scriptPubKey"]["hex"], "6a02cafe") |
| 86 | + assert_equal(vout[3]["scriptPubKey"]["hex"], "6a02babe") |
| 87 | + |
| 88 | + self.log.info("Done.") |
| 89 | + |
| 90 | + def mine_block(self, hex): |
| 91 | + """Mine and submit a block from a given hex template. |
| 92 | +
|
| 93 | + Returns a tuple of the new chain height and the block hash.""" |
| 94 | + |
| 95 | + bytes = hex_str_to_bytes(hex) |
| 96 | + block = CBlock() |
| 97 | + block.deserialize(BytesIO(bytes)) |
| 98 | + block.solve() |
| 99 | + self.send_blocks([block], success=True) |
| 100 | + height = self.nodes[0].getblockcount() |
| 101 | + return (height, block.hash) |
| 102 | + |
| 103 | + def bootstrap_p2p(self, timeout=10): |
| 104 | + """Add a P2P connection to the node. |
| 105 | +
|
| 106 | + Helper to connect and wait for version handshake.""" |
| 107 | + self.helper_peer = self.nodes[0].add_p2p_connection(P2PDataStore()) |
| 108 | + # We need to wait for the initial getheaders from the peer before we |
| 109 | + # start populating our blockstore. If we don't, then we may run ahead |
| 110 | + # to the next subtest before we receive the getheaders. We'd then send |
| 111 | + # an INV for the next block and receive two getheaders - one for the |
| 112 | + # IBD and one for the INV. We'd respond to both and could get |
| 113 | + # unexpectedly disconnected if the DoS score for that error is 50. |
| 114 | + self.helper_peer.wait_for_getheaders(timeout=timeout) |
| 115 | + |
| 116 | + def reconnect_p2p(self, timeout=60): |
| 117 | + """Tear down and bootstrap the P2P connection to the node. |
| 118 | +
|
| 119 | + The node gets disconnected several times in this test. This helper |
| 120 | + method reconnects the p2p and restarts the network thread.""" |
| 121 | + self.nodes[0].disconnect_p2ps() |
| 122 | + self.bootstrap_p2p(timeout=timeout) |
| 123 | + |
| 124 | + def send_blocks(self, blocks, success=True, reject_reason=None, force_send=False, reconnect=False, timeout=960): |
| 125 | + """Sends blocks to test node. Syncs and verifies that tip has advanced to most recent block. |
| 126 | +
|
| 127 | + Call with success = False if the tip shouldn't advance to the most recent block.""" |
| 128 | + self.helper_peer.send_blocks_and_test(blocks, self.nodes[0], success=success, reject_reason=reject_reason, force_send=force_send, timeout=timeout, expect_disconnect=reconnect) |
| 129 | + |
| 130 | + if reconnect: |
| 131 | + self.reconnect_p2p(timeout=timeout) |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == '__main__': |
| 135 | + GetNewBlockHexTest().main() |
0 commit comments