|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# |
| 3 | +# Distributed under the MIT/X11 software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +# |
| 6 | + |
| 7 | +from test_framework.mininode import * |
| 8 | +from test_framework.test_framework import BitcoinTestFramework |
| 9 | +from test_framework.util import * |
| 10 | +from test_framework.comptool import wait_until |
| 11 | +import time |
| 12 | + |
| 13 | +''' |
| 14 | +Test behavior of -maxuploadtarget. |
| 15 | +
|
| 16 | +* Verify that getdata requests for old blocks (>1week) are dropped |
| 17 | +if uploadtarget has been reached. |
| 18 | +* Verify that getdata requests for recent blocks are respecteved even |
| 19 | +if uploadtarget has been reached. |
| 20 | +* Verify that the upload counters are reset after 24 hours. |
| 21 | +''' |
| 22 | + |
| 23 | +# TestNode: bare-bones "peer". Used mostly as a conduit for a test to sending |
| 24 | +# p2p messages to a node, generating the messages in the main testing logic. |
| 25 | +class TestNode(NodeConnCB): |
| 26 | + def __init__(self): |
| 27 | + NodeConnCB.__init__(self) |
| 28 | + self.create_callback_map() |
| 29 | + self.connection = None |
| 30 | + self.ping_counter = 1 |
| 31 | + self.last_pong = msg_pong() |
| 32 | + self.block_receive_map = {} |
| 33 | + |
| 34 | + def add_connection(self, conn): |
| 35 | + self.connection = conn |
| 36 | + self.peer_disconnected = False |
| 37 | + |
| 38 | + def on_inv(self, conn, message): |
| 39 | + pass |
| 40 | + |
| 41 | + # Track the last getdata message we receive (used in the test) |
| 42 | + def on_getdata(self, conn, message): |
| 43 | + self.last_getdata = message |
| 44 | + |
| 45 | + def on_block(self, conn, message): |
| 46 | + message.block.calc_sha256() |
| 47 | + try: |
| 48 | + self.block_receive_map[message.block.sha256] += 1 |
| 49 | + except KeyError as e: |
| 50 | + self.block_receive_map[message.block.sha256] = 1 |
| 51 | + |
| 52 | + # Spin until verack message is received from the node. |
| 53 | + # We use this to signal that our test can begin. This |
| 54 | + # is called from the testing thread, so it needs to acquire |
| 55 | + # the global lock. |
| 56 | + def wait_for_verack(self): |
| 57 | + def veracked(): |
| 58 | + return self.verack_received |
| 59 | + return wait_until(veracked, timeout=10) |
| 60 | + |
| 61 | + def wait_for_disconnect(self): |
| 62 | + def disconnected(): |
| 63 | + return self.peer_disconnected |
| 64 | + return wait_until(disconnected, timeout=10) |
| 65 | + |
| 66 | + # Wrapper for the NodeConn's send_message function |
| 67 | + def send_message(self, message): |
| 68 | + self.connection.send_message(message) |
| 69 | + |
| 70 | + def on_pong(self, conn, message): |
| 71 | + self.last_pong = message |
| 72 | + |
| 73 | + def on_close(self, conn): |
| 74 | + self.peer_disconnected = True |
| 75 | + |
| 76 | + # Sync up with the node after delivery of a block |
| 77 | + def sync_with_ping(self, timeout=30): |
| 78 | + def received_pong(): |
| 79 | + return (self.last_pong.nonce == self.ping_counter) |
| 80 | + self.connection.send_message(msg_ping(nonce=self.ping_counter)) |
| 81 | + success = wait_until(received_pong, timeout) |
| 82 | + self.ping_counter += 1 |
| 83 | + return success |
| 84 | + |
| 85 | +class MaxUploadTest(BitcoinTestFramework): |
| 86 | + def __init__(self): |
| 87 | + self.utxo = [] |
| 88 | + |
| 89 | + # Some pre-processing to create a bunch of OP_RETURN txouts to insert into transactions we create |
| 90 | + # So we have big transactions and full blocks to fill up our block files |
| 91 | + # create one script_pubkey |
| 92 | + script_pubkey = "6a4d0200" #OP_RETURN OP_PUSH2 512 bytes |
| 93 | + for i in xrange (512): |
| 94 | + script_pubkey = script_pubkey + "01" |
| 95 | + # concatenate 128 txouts of above script_pubkey which we'll insert before the txout for change |
| 96 | + self.txouts = "81" |
| 97 | + for k in xrange(128): |
| 98 | + # add txout value |
| 99 | + self.txouts = self.txouts + "0000000000000000" |
| 100 | + # add length of script_pubkey |
| 101 | + self.txouts = self.txouts + "fd0402" |
| 102 | + # add script_pubkey |
| 103 | + self.txouts = self.txouts + script_pubkey |
| 104 | + |
| 105 | + def add_options(self, parser): |
| 106 | + parser.add_option("--testbinary", dest="testbinary", |
| 107 | + default=os.getenv("BITCOIND", "bitcoind"), |
| 108 | + help="bitcoind binary to test") |
| 109 | + |
| 110 | + def setup_chain(self): |
| 111 | + initialize_chain_clean(self.options.tmpdir, 2) |
| 112 | + |
| 113 | + def setup_network(self): |
| 114 | + # Start a node with maxuploadtarget of 200 MB (/24h) |
| 115 | + self.nodes = [] |
| 116 | + self.nodes.append(start_node(0, self.options.tmpdir, ["-debug", "-maxuploadtarget=200", "-blockmaxsize=999000"])) |
| 117 | + |
| 118 | + def mine_full_block(self, node, address): |
| 119 | + # Want to create a full block |
| 120 | + # We'll generate a 66k transaction below, and 14 of them is close to the 1MB block limit |
| 121 | + for j in xrange(14): |
| 122 | + if len(self.utxo) < 14: |
| 123 | + self.utxo = node.listunspent() |
| 124 | + inputs=[] |
| 125 | + outputs = {} |
| 126 | + t = self.utxo.pop() |
| 127 | + inputs.append({ "txid" : t["txid"], "vout" : t["vout"]}) |
| 128 | + remchange = t["amount"] - Decimal("0.001000") |
| 129 | + outputs[address]=remchange |
| 130 | + # Create a basic transaction that will send change back to ourself after account for a fee |
| 131 | + # And then insert the 128 generated transaction outs in the middle rawtx[92] is where the # |
| 132 | + # of txouts is stored and is the only thing we overwrite from the original transaction |
| 133 | + rawtx = node.createrawtransaction(inputs, outputs) |
| 134 | + newtx = rawtx[0:92] |
| 135 | + newtx = newtx + self.txouts |
| 136 | + newtx = newtx + rawtx[94:] |
| 137 | + # Appears to be ever so slightly faster to sign with SIGHASH_NONE |
| 138 | + signresult = node.signrawtransaction(newtx,None,None,"NONE") |
| 139 | + txid = node.sendrawtransaction(signresult["hex"], True) |
| 140 | + # Mine a full sized block which will be these transactions we just created |
| 141 | + node.generate(1) |
| 142 | + |
| 143 | + def run_test(self): |
| 144 | + # Before we connect anything, we first set the time on the node |
| 145 | + # to be in the past, otherwise things break because the CNode |
| 146 | + # time counters can't be reset backward after initialization |
| 147 | + old_time = int(time.time() - 2*60*60*24*7) |
| 148 | + self.nodes[0].setmocktime(old_time) |
| 149 | + |
| 150 | + # Generate some old blocks |
| 151 | + self.nodes[0].generate(130) |
| 152 | + |
| 153 | + # test_nodes[0] will only request old blocks |
| 154 | + # test_nodes[1] will only request new blocks |
| 155 | + # test_nodes[2] will test resetting the counters |
| 156 | + test_nodes = [] |
| 157 | + connections = [] |
| 158 | + |
| 159 | + for i in xrange(3): |
| 160 | + test_nodes.append(TestNode()) |
| 161 | + connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], test_nodes[i])) |
| 162 | + test_nodes[i].add_connection(connections[i]) |
| 163 | + |
| 164 | + NetworkThread().start() # Start up network handling in another thread |
| 165 | + [x.wait_for_verack() for x in test_nodes] |
| 166 | + |
| 167 | + # Test logic begins here |
| 168 | + |
| 169 | + # Now mine a big block |
| 170 | + self.mine_full_block(self.nodes[0], self.nodes[0].getnewaddress()) |
| 171 | + |
| 172 | + # Store the hash; we'll request this later |
| 173 | + big_old_block = self.nodes[0].getbestblockhash() |
| 174 | + old_block_size = self.nodes[0].getblock(big_old_block, True)['size'] |
| 175 | + big_old_block = int(big_old_block, 16) |
| 176 | + |
| 177 | + # Advance to two days ago |
| 178 | + self.nodes[0].setmocktime(int(time.time()) - 2*60*60*24) |
| 179 | + |
| 180 | + # Mine one more block, so that the prior block looks old |
| 181 | + self.mine_full_block(self.nodes[0], self.nodes[0].getnewaddress()) |
| 182 | + |
| 183 | + # We'll be requesting this new block too |
| 184 | + big_new_block = self.nodes[0].getbestblockhash() |
| 185 | + new_block_size = self.nodes[0].getblock(big_new_block)['size'] |
| 186 | + big_new_block = int(big_new_block, 16) |
| 187 | + |
| 188 | + # test_nodes[0] will test what happens if we just keep requesting the |
| 189 | + # the same big old block too many times (expect: disconnect) |
| 190 | + |
| 191 | + getdata_request = msg_getdata() |
| 192 | + getdata_request.inv.append(CInv(2, big_old_block)) |
| 193 | + |
| 194 | + max_bytes_per_day = 200*1024*1024 |
| 195 | + max_bytes_available = max_bytes_per_day - 144*1000000 |
| 196 | + success_count = max_bytes_available / old_block_size |
| 197 | + |
| 198 | + # 144MB will be reserved for relaying new blocks, so expect this to |
| 199 | + # succeed for ~70 tries. |
| 200 | + for i in xrange(success_count): |
| 201 | + test_nodes[0].send_message(getdata_request) |
| 202 | + test_nodes[0].sync_with_ping() |
| 203 | + assert_equal(test_nodes[0].block_receive_map[big_old_block], i+1) |
| 204 | + |
| 205 | + assert_equal(len(self.nodes[0].getpeerinfo()), 3) |
| 206 | + # At most a couple more tries should succeed (depending on how long |
| 207 | + # the test has been running so far). |
| 208 | + for i in xrange(3): |
| 209 | + test_nodes[0].send_message(getdata_request) |
| 210 | + test_nodes[0].wait_for_disconnect() |
| 211 | + assert_equal(len(self.nodes[0].getpeerinfo()), 2) |
| 212 | + print "Peer 0 disconnected after downloading old block too many times" |
| 213 | + |
| 214 | + # Requesting the current block on test_nodes[1] should succeed indefinitely, |
| 215 | + # even when over the max upload target. |
| 216 | + # We'll try 200 times |
| 217 | + getdata_request.inv = [CInv(2, big_new_block)] |
| 218 | + for i in xrange(200): |
| 219 | + test_nodes[1].send_message(getdata_request) |
| 220 | + test_nodes[1].sync_with_ping() |
| 221 | + assert_equal(test_nodes[1].block_receive_map[big_new_block], i+1) |
| 222 | + |
| 223 | + print "Peer 1 able to repeatedly download new block" |
| 224 | + |
| 225 | + # But if test_nodes[1] tries for an old block, it gets disconnected too. |
| 226 | + getdata_request.inv = [CInv(2, big_old_block)] |
| 227 | + test_nodes[1].send_message(getdata_request) |
| 228 | + test_nodes[1].wait_for_disconnect() |
| 229 | + assert_equal(len(self.nodes[0].getpeerinfo()), 1) |
| 230 | + |
| 231 | + print "Peer 1 disconnected after trying to download old block" |
| 232 | + |
| 233 | + print "Advancing system time on node to clear counters..." |
| 234 | + |
| 235 | + # If we advance the time by 24 hours, then the counters should reset, |
| 236 | + # and test_nodes[2] should be able to retrieve the old block. |
| 237 | + self.nodes[0].setmocktime(int(time.time())) |
| 238 | + test_nodes[2].sync_with_ping() |
| 239 | + test_nodes[2].send_message(getdata_request) |
| 240 | + test_nodes[2].sync_with_ping() |
| 241 | + assert_equal(test_nodes[2].block_receive_map[big_old_block], 1) |
| 242 | + |
| 243 | + print "Peer 2 able to download old block" |
| 244 | + |
| 245 | + [c.disconnect_node() for c in connections] |
| 246 | + |
| 247 | +if __name__ == '__main__': |
| 248 | + MaxUploadTest().main() |
0 commit comments