Skip to content

Commit fa78a2f

Browse files
author
MarcoFalke
committed
[tests] Test that nodes respond to getdata with notfound
If a node has not announced a tx at all, then it should respond to getdata messages for that tx with notfound, to avoid leaking tx origination privacy.
1 parent 2b88f67 commit fa78a2f

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

test/functional/p2p_leak_tx.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2017-2018 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 that we don't leak txs to inbound peers that we haven't yet announced to"""
6+
7+
from test_framework.messages import msg_getdata, CInv
8+
from test_framework.mininode import P2PDataStore
9+
from test_framework.test_framework import BitcoinTestFramework
10+
from test_framework.util import (
11+
assert_equal,
12+
)
13+
14+
15+
class P2PNode(P2PDataStore):
16+
def on_inv(self, msg):
17+
pass
18+
19+
20+
class P2PLeakTxTest(BitcoinTestFramework):
21+
def set_test_params(self):
22+
self.num_nodes = 1
23+
24+
def skip_test_if_missing_module(self):
25+
self.skip_if_no_wallet()
26+
27+
def run_test(self):
28+
gen_node = self.nodes[0] # The block and tx generating node
29+
gen_node.generate(1)
30+
31+
inbound_peer = self.nodes[0].add_p2p_connection(P2PNode()) # An "attacking" inbound peer
32+
33+
MAX_REPEATS = 100
34+
self.log.info("Running test up to {} times.".format(MAX_REPEATS))
35+
for i in range(MAX_REPEATS):
36+
self.log.info('Run repeat {}'.format(i + 1))
37+
txid = gen_node.sendtoaddress(gen_node.getnewaddress(), 0.01)
38+
39+
want_tx = msg_getdata()
40+
want_tx.inv.append(CInv(t=1, h=int(txid, 16)))
41+
inbound_peer.last_message.pop('notfound', None)
42+
inbound_peer.send_message(want_tx)
43+
inbound_peer.sync_with_ping()
44+
45+
if inbound_peer.last_message.get('notfound'):
46+
self.log.debug('tx {} was not yet announced to us.'.format(txid))
47+
self.log.debug("node has responded with a notfound message. End test.")
48+
assert_equal(inbound_peer.last_message['notfound'].vec[0].hash, int(txid, 16))
49+
inbound_peer.last_message.pop('notfound')
50+
break
51+
else:
52+
self.log.debug('tx {} was already announced to us. Try test again.'.format(txid))
53+
assert int(txid, 16) in [inv.hash for inv in inbound_peer.last_message['inv'].inv]
54+
55+
56+
if __name__ == '__main__':
57+
P2PLeakTxTest().main()

test/functional/test_framework/messages.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,23 @@ def __repr__(self):
12321232
return "msg_mempool()"
12331233

12341234

1235+
class msg_notfound:
1236+
__slots__ = ("vec", )
1237+
command = b"notfound"
1238+
1239+
def __init__(self, vec=None):
1240+
self.vec = vec or []
1241+
1242+
def deserialize(self, f):
1243+
self.vec = deser_vector(f, CInv)
1244+
1245+
def serialize(self):
1246+
return ser_vector(self.vec)
1247+
1248+
def __repr__(self):
1249+
return "msg_notfound(vec=%s)" % (repr(self.vec))
1250+
1251+
12351252
class msg_sendheaders:
12361253
__slots__ = ()
12371254
command = b"sendheaders"

test/functional/test_framework/mininode.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,38 @@
2121
import sys
2222
import threading
2323

24-
from test_framework.messages import CBlockHeader, MIN_VERSION_SUPPORTED, msg_addr, msg_block, MSG_BLOCK, msg_blocktxn, msg_cmpctblock, msg_feefilter, msg_getaddr, msg_getblocks, msg_getblocktxn, msg_getdata, msg_getheaders, msg_headers, msg_inv, msg_mempool, msg_ping, msg_pong, msg_reject, msg_sendcmpct, msg_sendheaders, msg_tx, MSG_TX, MSG_TYPE_MASK, msg_verack, msg_version, NODE_NETWORK, NODE_WITNESS, sha256
24+
from test_framework.messages import (
25+
CBlockHeader,
26+
MIN_VERSION_SUPPORTED,
27+
msg_addr,
28+
msg_block,
29+
MSG_BLOCK,
30+
msg_blocktxn,
31+
msg_cmpctblock,
32+
msg_feefilter,
33+
msg_getaddr,
34+
msg_getblocks,
35+
msg_getblocktxn,
36+
msg_getdata,
37+
msg_getheaders,
38+
msg_headers,
39+
msg_inv,
40+
msg_mempool,
41+
msg_notfound,
42+
msg_ping,
43+
msg_pong,
44+
msg_reject,
45+
msg_sendcmpct,
46+
msg_sendheaders,
47+
msg_tx,
48+
MSG_TX,
49+
MSG_TYPE_MASK,
50+
msg_verack,
51+
msg_version,
52+
NODE_NETWORK,
53+
NODE_WITNESS,
54+
sha256,
55+
)
2556
from test_framework.util import wait_until
2657

2758
logger = logging.getLogger("TestFramework.mininode")
@@ -40,6 +71,7 @@
4071
b"headers": msg_headers,
4172
b"inv": msg_inv,
4273
b"mempool": msg_mempool,
74+
b"notfound": msg_notfound,
4375
b"ping": msg_ping,
4476
b"pong": msg_pong,
4577
b"reject": msg_reject,
@@ -295,6 +327,7 @@ def on_getdata(self, message): pass
295327
def on_getheaders(self, message): pass
296328
def on_headers(self, message): pass
297329
def on_mempool(self, message): pass
330+
def on_notfound(self, message): pass
298331
def on_pong(self, message): pass
299332
def on_reject(self, message): pass
300333
def on_sendcmpct(self, message): pass

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
'feature_versionbits_warning.py',
153153
'rpc_preciousblock.py',
154154
'wallet_importprunedfunds.py',
155+
'p2p_leak_tx.py',
155156
'rpc_signmessage.py',
156157
'feature_nulldummy.py',
157158
'mempool_accept.py',

0 commit comments

Comments
 (0)