Skip to content

Commit 9f767e8

Browse files
committed
test: use MiniWallet for p2p_blocksonly.py
This test can now be run even with the Bitcoin Core wallet disabled.
1 parent 779aaa7 commit 9f767e8

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

test/functional/p2p_blocksonly.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@
66

77
import time
88

9-
from test_framework.blocktools import create_transaction
109
from test_framework.messages import msg_tx
1110
from test_framework.p2p import P2PInterface, P2PTxInvStore
1211
from test_framework.test_framework import BitcoinTestFramework
1312
from test_framework.util import assert_equal
13+
from test_framework.wallet import MiniWallet
1414

1515

1616
class P2PBlocksOnly(BitcoinTestFramework):
1717
def set_test_params(self):
18+
self.setup_clean_chain = True
1819
self.num_nodes = 1
1920
self.extra_args = [["-blocksonly"]]
2021

21-
def skip_test_if_missing_module(self):
22-
self.skip_if_no_wallet()
23-
2422
def run_test(self):
23+
self.miniwallet = MiniWallet(self.nodes[0])
24+
# Add enough mature utxos to the wallet, so that all txs spend confirmed coins
25+
self.miniwallet.generate(2)
26+
self.nodes[0].generate(100)
27+
2528
self.blocksonly_mode_tests()
2629
self.blocks_relay_conn_tests()
2730

@@ -30,14 +33,14 @@ def blocksonly_mode_tests(self):
3033
assert_equal(self.nodes[0].getnetworkinfo()['localrelay'], False)
3134

3235
self.nodes[0].add_p2p_connection(P2PInterface())
33-
tx, txid, tx_hex = self.check_p2p_tx_violation()
36+
tx, txid, wtxid, tx_hex = self.check_p2p_tx_violation()
3437

3538
self.log.info('Check that txs from rpc are not rejected and relayed to other peers')
3639
tx_relay_peer = self.nodes[0].add_p2p_connection(P2PInterface())
3740
assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], True)
3841

3942
assert_equal(self.nodes[0].testmempoolaccept([tx_hex])[0]['allowed'], True)
40-
with self.nodes[0].assert_debug_log(['received getdata for: wtx {} peer=1'.format(txid)]):
43+
with self.nodes[0].assert_debug_log(['received getdata for: wtx {} peer=1'.format(wtxid)]):
4144
self.nodes[0].sendrawtransaction(tx_hex)
4245
tx_relay_peer.wait_for_tx(txid)
4346
assert_equal(self.nodes[0].getmempoolinfo()['size'], 1)
@@ -79,7 +82,7 @@ def blocks_relay_conn_tests(self):
7982
# Ensure we disconnect if a block-relay-only connection sends us a transaction
8083
self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=0, connection_type="block-relay-only")
8184
assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], False)
82-
_, txid, tx_hex = self.check_p2p_tx_violation(index=2)
85+
_, txid, _, tx_hex = self.check_p2p_tx_violation(index=2)
8386

8487
self.log.info("Check that txs from RPC are not sent to blockrelay connection")
8588
conn = self.nodes[0].add_outbound_p2p_connection(P2PTxInvStore(), p2p_idx=1, connection_type="block-relay-only")
@@ -95,19 +98,18 @@ def blocks_relay_conn_tests(self):
9598
def check_p2p_tx_violation(self, index=1):
9699
self.log.info('Check that txs from P2P are rejected and result in disconnect')
97100
input_txid = self.nodes[0].getblock(self.nodes[0].getblockhash(index), 2)['tx'][0]['txid']
98-
tx = create_transaction(self.nodes[0], input_txid, self.nodes[0].getnewaddress(), amount=(50 - 0.001))
99-
txid = tx.rehash()
100-
tx_hex = tx.serialize().hex()
101+
utxo_to_spend = self.miniwallet.get_utxo(txid=input_txid)
102+
spendtx = self.miniwallet.create_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_to_spend)
101103

102104
with self.nodes[0].assert_debug_log(['transaction sent in violation of protocol peer=0']):
103-
self.nodes[0].p2ps[0].send_message(msg_tx(tx))
105+
self.nodes[0].p2ps[0].send_message(msg_tx(spendtx['tx']))
104106
self.nodes[0].p2ps[0].wait_for_disconnect()
105107
assert_equal(self.nodes[0].getmempoolinfo()['size'], 0)
106108

107109
# Remove the disconnected peer
108110
del self.nodes[0].p2ps[0]
109111

110-
return tx, txid, tx_hex
112+
return spendtx['tx'], spendtx['txid'], spendtx['wtxid'], spendtx['hex']
111113

112114

113115
if __name__ == '__main__':

0 commit comments

Comments
 (0)