p2p_leak_tx.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2017-present 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 transaction upload""" 6 7 from test_framework.messages import msg_getdata, CInv, MSG_TX, MSG_WTX 8 from test_framework.p2p import p2p_lock, P2PDataStore, P2PTxInvStore 9 from test_framework.test_framework import BitcoinTestFramework 10 from test_framework.util import ( 11 assert_equal, 12 ) 13 from test_framework.wallet import MiniWallet 14 15 import time 16 17 class P2PNode(P2PDataStore): 18 def on_inv(self, message): 19 pass 20 21 22 class P2PLeakTxTest(BitcoinTestFramework): 23 def set_test_params(self): 24 self.num_nodes = 1 25 26 def run_test(self): 27 self.gen_node = self.nodes[0] # The block and tx generating node 28 self.miniwallet = MiniWallet(self.gen_node) 29 self.mocktime = int(time.time()) 30 31 self.test_tx_in_block() 32 self.test_notfound_on_replaced_tx() 33 self.test_notfound_on_unannounced_tx() 34 35 def test_tx_in_block(self): 36 self.log.info("Check that a transaction in the last block is uploaded (beneficial for compact block relay)") 37 self.gen_node.setmocktime(self.mocktime) 38 inbound_peer = self.gen_node.add_p2p_connection(P2PNode()) 39 40 self.log.debug("Generate transaction and block") 41 inbound_peer.last_message.pop("inv", None) 42 43 wtxid = self.miniwallet.send_self_transfer(from_node=self.gen_node)["wtxid"] 44 rawmp = self.gen_node.getrawmempool(False, True) 45 pi = self.gen_node.getpeerinfo()[0] 46 assert_equal(rawmp["mempool_sequence"], 2) # our tx cause mempool activity 47 assert_equal(pi["last_inv_sequence"], 1) # that is after the last inv 48 assert_equal(pi["inv_to_send"], 1) # and our tx has been queued 49 self.mocktime += 120 50 self.gen_node.setmocktime(self.mocktime) 51 inbound_peer.wait_until(lambda: "inv" in inbound_peer.last_message and inbound_peer.last_message.get("inv").inv[0].hash == int(wtxid, 16)) 52 53 rawmp = self.gen_node.getrawmempool(False, True) 54 pi = self.gen_node.getpeerinfo()[0] 55 assert_equal(rawmp["mempool_sequence"], 2) # no mempool update 56 assert_equal(pi["last_inv_sequence"], 2) # announced the current mempool 57 assert_equal(pi["inv_to_send"], 0) # nothing left in the queue 58 59 want_tx = msg_getdata(inv=inbound_peer.last_message.get("inv").inv) 60 self.generate(self.gen_node, 1) 61 62 self.log.debug("Request transaction") 63 inbound_peer.last_message.pop("tx", None) 64 inbound_peer.send_and_ping(want_tx) 65 assert_equal(inbound_peer.last_message.get("tx").tx.wtxid_hex, wtxid) 66 67 def test_notfound_on_replaced_tx(self): 68 self.gen_node.disconnect_p2ps() 69 self.gen_node.setmocktime(self.mocktime) 70 inbound_peer = self.gen_node.add_p2p_connection(P2PTxInvStore()) 71 72 self.log.info("Transaction tx_a is broadcast") 73 tx_a = self.miniwallet.send_self_transfer(from_node=self.gen_node) 74 self.mocktime += 120 75 self.gen_node.setmocktime(self.mocktime) 76 inbound_peer.wait_for_broadcast(txns=[tx_a["wtxid"]]) 77 78 tx_b = tx_a["tx"] 79 tx_b.vout[0].nValue -= 9000 80 self.gen_node.sendrawtransaction(tx_b.serialize().hex()) 81 self.mocktime += 120 82 self.gen_node.setmocktime(self.mocktime) 83 inbound_peer.wait_until(lambda: "tx" in inbound_peer.last_message and inbound_peer.last_message.get("tx").tx.wtxid_hex == tx_b.wtxid_hex) 84 85 self.log.info("Re-request of tx_a after replacement is answered with notfound") 86 req_vec = [ 87 CInv(t=MSG_TX, h=int(tx_a["txid"], 16)), 88 CInv(t=MSG_WTX, h=int(tx_a["wtxid"], 16)), 89 ] 90 want_tx = msg_getdata() 91 want_tx.inv = req_vec 92 with p2p_lock: 93 inbound_peer.last_message.pop("notfound", None) 94 inbound_peer.last_message.pop("tx", None) 95 inbound_peer.send_and_ping(want_tx) 96 97 assert_equal(inbound_peer.last_message.get("notfound").vec, req_vec) 98 assert "tx" not in inbound_peer.last_message 99 100 def test_notfound_on_unannounced_tx(self): 101 self.log.info("Check that we don't leak txs to inbound peers that we haven't yet announced to") 102 self.gen_node.disconnect_p2ps() 103 inbound_peer = self.gen_node.add_p2p_connection(P2PNode()) # An "attacking" inbound peer 104 105 # Set a mock time so that time does not pass, and gen_node never announces the transaction 106 self.gen_node.setmocktime(self.mocktime) 107 wtxid = int(self.miniwallet.send_self_transfer(from_node=self.gen_node)["wtxid"], 16) 108 109 want_tx = msg_getdata() 110 want_tx.inv.append(CInv(t=MSG_WTX, h=wtxid)) 111 with p2p_lock: 112 inbound_peer.last_message.pop('notfound', None) 113 inbound_peer.send_and_ping(want_tx) 114 inbound_peer.wait_until(lambda: "notfound" in inbound_peer.last_message) 115 with p2p_lock: 116 assert_equal(inbound_peer.last_message.get("notfound").vec[0].hash, wtxid) 117 inbound_peer.last_message.pop('notfound') 118 119 # Move mocktime forward and wait for the announcement. 120 inbound_peer.last_message.pop('inv', None) 121 self.mocktime += 120 122 self.gen_node.setmocktime(self.mocktime) 123 inbound_peer.wait_for_inv([CInv(t=MSG_WTX, h=wtxid)], timeout=120) 124 125 # Send the getdata again, this time the node should send us a TX message. 126 inbound_peer.last_message.pop('tx', None) 127 inbound_peer.send_and_ping(want_tx) 128 self.wait_until(lambda: "tx" in inbound_peer.last_message) 129 assert_equal(wtxid, int(inbound_peer.last_message["tx"].tx.wtxid_hex, 16)) 130 131 132 if __name__ == '__main__': 133 P2PLeakTxTest(__file__).main()