Skip to content

Commit fac66d0

Browse files
author
MarcoFalke
committed
test: Simplify p2p_blocksonly test with new miniwallet rescan_utxos method
1 parent 053a5fc commit fac66d0

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

test/functional/p2p_blocksonly.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import time
88

9-
from test_framework.blocktools import COINBASE_MATURITY
109
from test_framework.messages import msg_tx
1110
from test_framework.p2p import P2PInterface, P2PTxInvStore
1211
from test_framework.test_framework import BitcoinTestFramework
@@ -16,15 +15,13 @@
1615

1716
class P2PBlocksOnly(BitcoinTestFramework):
1817
def set_test_params(self):
19-
self.setup_clean_chain = True
2018
self.num_nodes = 1
2119
self.extra_args = [["-blocksonly"]]
2220

2321
def run_test(self):
2422
self.miniwallet = MiniWallet(self.nodes[0])
2523
# Add enough mature utxos to the wallet, so that all txs spend confirmed coins
26-
self.generate(self.miniwallet, 2)
27-
self.generate(self.nodes[0], COINBASE_MATURITY)
24+
self.miniwallet.rescan_utxos()
2825

2926
self.blocksonly_mode_tests()
3027
self.blocks_relay_conn_tests()
@@ -83,7 +80,7 @@ def blocks_relay_conn_tests(self):
8380
# Ensure we disconnect if a block-relay-only connection sends us a transaction
8481
self.nodes[0].add_outbound_p2p_connection(P2PInterface(), p2p_idx=0, connection_type="block-relay-only")
8582
assert_equal(self.nodes[0].getpeerinfo()[0]['relaytxes'], False)
86-
_, txid, _, tx_hex = self.check_p2p_tx_violation(index=2)
83+
_, txid, _, tx_hex = self.check_p2p_tx_violation()
8784

8885
self.log.info("Check that txs from RPC are not sent to blockrelay connection")
8986
conn = self.nodes[0].add_outbound_p2p_connection(P2PTxInvStore(), p2p_idx=1, connection_type="block-relay-only")
@@ -96,11 +93,9 @@ def blocks_relay_conn_tests(self):
9693
conn.sync_send_with_ping()
9794
assert(int(txid, 16) not in conn.get_invs())
9895

99-
def check_p2p_tx_violation(self, index=1):
96+
def check_p2p_tx_violation(self):
10097
self.log.info('Check that txs from P2P are rejected and result in disconnect')
101-
input_txid = self.nodes[0].getblock(self.nodes[0].getblockhash(index), 2)['tx'][0]['txid']
102-
utxo_to_spend = self.miniwallet.get_utxo(txid=input_txid)
103-
spendtx = self.miniwallet.create_self_transfer(from_node=self.nodes[0], utxo_to_spend=utxo_to_spend)
98+
spendtx = self.miniwallet.create_self_transfer(from_node=self.nodes[0])
10499

105100
with self.nodes[0].assert_debug_log(['transaction sent in violation of protocol peer=0']):
106101
self.nodes[0].p2ps[0].send_message(msg_tx(spendtx['tx']))

test/functional/test_framework/wallet.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
7979
self._address = ADDRESS_BCRT1_P2WSH_OP_TRUE
8080
self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey'])
8181

82+
def rescan_utxos(self):
83+
"""Drop all utxos and rescan the utxo set"""
84+
self._utxos = []
85+
res = self._test_node.scantxoutset(action="start", scanobjects=[f'raw({self._scriptPubKey.hex()})'])
86+
assert_equal(True, res['success'])
87+
for utxo in res['unspents']:
88+
self._utxos.append({'txid': utxo['txid'], 'vout': utxo['vout'], 'value': utxo['amount']})
89+
8290
def scan_blocks(self, *, start=1, num):
8391
"""Scan the blocks for self._address outputs and add them to self._utxos"""
8492
for i in range(start, start + num):

0 commit comments

Comments
 (0)