Skip to content

Commit fa320de

Browse files
author
MarcoFalke
committed
test: Add test for p2p_blocksonly
1 parent fa3872e commit fa320de

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

test/functional/p2p_blocksonly.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2019 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 p2p blocksonly"""
6+
7+
from test_framework.messages import msg_tx, CTransaction, FromHex
8+
from test_framework.mininode import P2PInterface
9+
from test_framework.test_framework import BitcoinTestFramework
10+
from test_framework.util import assert_equal
11+
12+
13+
class P2PBlocksOnly(BitcoinTestFramework):
14+
def set_test_params(self):
15+
self.setup_clean_chain = False
16+
self.num_nodes = 1
17+
self.extra_args = [["-blocksonly"]]
18+
19+
def run_test(self):
20+
self.nodes[0].add_p2p_connection(P2PInterface())
21+
22+
self.log.info('Check that txs from p2p are rejected')
23+
prevtx = self.nodes[0].getblock(self.nodes[0].getblockhash(1), 2)['tx'][0]
24+
rawtx = self.nodes[0].createrawtransaction(
25+
inputs=[{
26+
'txid': prevtx['txid'],
27+
'vout': 0
28+
}],
29+
outputs=[{
30+
self.nodes[0].get_deterministic_priv_key().address: 50 - 0.00125
31+
}],
32+
)
33+
sigtx = self.nodes[0].signrawtransactionwithkey(
34+
hexstring=rawtx,
35+
privkeys=[self.nodes[0].get_deterministic_priv_key().key],
36+
prevtxs=[{
37+
'txid': prevtx['txid'],
38+
'vout': 0,
39+
'scriptPubKey': prevtx['vout'][0]['scriptPubKey']['hex'],
40+
}],
41+
)['hex']
42+
assert_equal(self.nodes[0].getnetworkinfo()['localrelay'], False)
43+
with self.nodes[0].assert_debug_log(['transaction sent in violation of protocol peer=0']):
44+
self.nodes[0].p2p.send_message(msg_tx(FromHex(CTransaction(), sigtx)))
45+
self.nodes[0].p2p.sync_with_ping()
46+
assert_equal(self.nodes[0].getmempoolinfo()['size'], 0)
47+
48+
self.log.info('Check that txs from rpc are not rejected and relayed to other peers')
49+
assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], True)
50+
txid = self.nodes[0].testmempoolaccept([sigtx])[0]['txid']
51+
with self.nodes[0].assert_debug_log(['received getdata for: tx {} peer=0'.format(txid)]):
52+
self.nodes[0].sendrawtransaction(sigtx)
53+
self.nodes[0].p2p.wait_for_tx(txid)
54+
assert_equal(self.nodes[0].getmempoolinfo()['size'], 1)
55+
56+
57+
if __name__ == '__main__':
58+
P2PBlocksOnly().main()

test/functional/test_framework/mininode.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,14 @@ def wait_for_disconnect(self, timeout=60):
361361

362362
# Message receiving helper methods
363363

364+
def wait_for_tx(self, txid, timeout=60):
365+
def test_function():
366+
if not self.last_message.get('tx'):
367+
return False
368+
return self.last_message['tx'].tx.rehash() == txid
369+
370+
wait_until(test_function, timeout=timeout, lock=mininode_lock)
371+
364372
def wait_for_block(self, blockhash, timeout=60):
365373
test_function = lambda: self.last_message.get("block") and self.last_message["block"].block.rehash() == blockhash
366374
wait_until(test_function, timeout=timeout, lock=mininode_lock)

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
'rpc_net.py',
142142
'wallet_keypool.py',
143143
'p2p_mempool.py',
144+
'p2p_blocksonly.py',
144145
'mining_prioritisetransaction.py',
145146
'p2p_invalid_locator.py',
146147
'p2p_invalid_block.py',

0 commit comments

Comments
 (0)