Skip to content

Commit 01b5cfb

Browse files
committed
Merge bitcoin/bitcoin#23047: test: Use MiniWallet in mempool_persist
faae098 test: Check other fields are loaded correctly as well (MarcoFalke) fa4db92 test: Remove unused self.connect_nodes (MarcoFalke) fafb7b7 test: pep8 (MarcoFalke) fa32cb2 test: Use MiniWallet in mempool_persist (MarcoFalke) faca688 test: Add MiniWallet get_descriptor function (MarcoFalke) Pull request description: ACKs for top commit: laanwj: Code review ACK faae098 Tree-SHA512: 6124f16ee1f3f416c50dc07aebe8846ff7e2b7c8e5dd84f9517cb5f1df021b9e57ed7c7e17bc099a37c663cd93f6d417c5e0622c0b359956403d53e705eb5549
2 parents 8e9801b + faae098 commit 01b5cfb

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

test/functional/mempool_persist.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,34 @@
4646
assert_greater_than_or_equal,
4747
assert_raises_rpc_error,
4848
)
49+
from test_framework.wallet import MiniWallet
4950

5051

5152
class MempoolPersistTest(BitcoinTestFramework):
5253
def set_test_params(self):
5354
self.num_nodes = 3
5455
self.extra_args = [[], ["-persistmempool=0"], []]
5556

56-
def skip_test_if_missing_module(self):
57-
self.skip_if_no_wallet()
58-
5957
def run_test(self):
58+
self.mini_wallet = MiniWallet(self.nodes[2])
59+
self.mini_wallet.rescan_utxos()
60+
if self.is_sqlite_compiled():
61+
self.nodes[2].createwallet(
62+
wallet_name="watch",
63+
descriptors=True,
64+
disable_private_keys=True,
65+
load_on_startup=False,
66+
)
67+
wallet_watch = self.nodes[2].get_wallet_rpc("watch")
68+
assert_equal([{'success': True}], wallet_watch.importdescriptors([{'desc': self.mini_wallet.get_descriptor(), 'timestamp': 0}]))
69+
6070
self.log.debug("Send 5 transactions from node2 (to its own address)")
6171
tx_creation_time_lower = int(time.time())
6272
for _ in range(5):
63-
last_txid = self.nodes[2].sendtoaddress(self.nodes[2].getnewaddress(), Decimal("10"))
64-
node2_balance = self.nodes[2].getbalance()
73+
last_txid = self.mini_wallet.send_self_transfer(from_node=self.nodes[2])["txid"]
74+
if self.is_sqlite_compiled():
75+
self.nodes[2].syncwithvalidationinterfacequeue() # Flush mempool to wallet
76+
node2_balance = wallet_watch.getbalance()
6577
self.sync_all()
6678
tx_creation_time_higher = int(time.time())
6779

@@ -82,16 +94,16 @@ def run_test(self):
8294
assert_equal(total_fee_old, self.nodes[0].getmempoolinfo()['total_fee'])
8395
assert_equal(total_fee_old, sum(v['fees']['base'] for k, v in self.nodes[0].getrawmempool(verbose=True).items()))
8496

85-
tx_creation_time = self.nodes[0].getmempoolentry(txid=last_txid)['time']
97+
last_entry = self.nodes[0].getmempoolentry(txid=last_txid)
98+
tx_creation_time = last_entry['time']
8699
assert_greater_than_or_equal(tx_creation_time, tx_creation_time_lower)
87100
assert_greater_than_or_equal(tx_creation_time_higher, tx_creation_time)
88101

89102
# disconnect nodes & make a txn that remains in the unbroadcast set.
90103
self.disconnect_nodes(0, 1)
91-
assert(len(self.nodes[0].getpeerinfo()) == 0)
92-
assert(len(self.nodes[0].p2ps) == 0)
93-
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), Decimal("12"))
94-
self.connect_nodes(0, 2)
104+
assert_equal(len(self.nodes[0].getpeerinfo()), 0)
105+
assert_equal(len(self.nodes[0].p2ps), 0)
106+
self.mini_wallet.send_self_transfer(from_node=self.nodes[0])
95107

96108
self.log.debug("Stop-start the nodes. Verify that node0 has the transactions in its mempool and node1 does not. Verify that node2 calculates its balance correctly after loading wallet transactions.")
97109
self.stop_nodes()
@@ -111,17 +123,19 @@ def run_test(self):
111123
fees = self.nodes[0].getmempoolentry(txid=last_txid)['fees']
112124
assert_equal(fees['base'] + Decimal('0.00001000'), fees['modified'])
113125

114-
self.log.debug('Verify time is loaded correctly')
115-
assert_equal(tx_creation_time, self.nodes[0].getmempoolentry(txid=last_txid)['time'])
126+
self.log.debug('Verify all fields are loaded correctly')
127+
assert_equal(last_entry, self.nodes[0].getmempoolentry(txid=last_txid))
116128

117129
# Verify accounting of mempool transactions after restart is correct
118-
self.nodes[2].syncwithvalidationinterfacequeue() # Flush mempool to wallet
119-
assert_equal(node2_balance, self.nodes[2].getbalance())
130+
if self.is_sqlite_compiled():
131+
self.nodes[2].loadwallet("watch")
132+
wallet_watch = self.nodes[2].get_wallet_rpc("watch")
133+
self.nodes[2].syncwithvalidationinterfacequeue() # Flush mempool to wallet
134+
assert_equal(node2_balance, wallet_watch.getbalance())
120135

121-
# start node0 with wallet disabled so wallet transactions don't get resubmitted
122136
self.log.debug("Stop-start node0 with -persistmempool=0. Verify that it doesn't load its mempool.dat file.")
123137
self.stop_nodes()
124-
self.start_node(0, extra_args=["-persistmempool=0", "-disablewallet"])
138+
self.start_node(0, extra_args=["-persistmempool=0"])
125139
assert self.nodes[0].getmempoolinfo()["loaded"]
126140
assert_equal(len(self.nodes[0].getrawmempool()), 0)
127141

@@ -164,18 +178,18 @@ def test_persist_unbroadcast(self):
164178

165179
# ensure node0 doesn't have any connections
166180
# make a transaction that will remain in the unbroadcast set
167-
assert(len(node0.getpeerinfo()) == 0)
168-
assert(len(node0.p2ps) == 0)
169-
node0.sendtoaddress(self.nodes[1].getnewaddress(), Decimal("12"))
181+
assert_equal(len(node0.getpeerinfo()), 0)
182+
assert_equal(len(node0.p2ps), 0)
183+
self.mini_wallet.send_self_transfer(from_node=node0)
170184

171185
# shutdown, then startup with wallet disabled
172-
self.stop_nodes()
173-
self.start_node(0, extra_args=["-disablewallet"])
186+
self.restart_node(0, extra_args=["-disablewallet"])
174187

175188
# check that txn gets broadcast due to unbroadcast logic
176189
conn = node0.add_p2p_connection(P2PTxInvStore())
177-
node0.mockscheduler(16*60) # 15 min + 1 for buffer
190+
node0.mockscheduler(16 * 60) # 15 min + 1 for buffer
178191
self.wait_until(lambda: len(conn.get_invs()) == 1)
179192

180-
if __name__ == '__main__':
193+
194+
if __name__ == "__main__":
181195
MempoolPersistTest().main()

test/functional/test_framework/wallet.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
8282
def rescan_utxos(self):
8383
"""Drop all utxos and rescan the utxo set"""
8484
self._utxos = []
85-
res = self._test_node.scantxoutset(action="start", scanobjects=[f'raw({self._scriptPubKey.hex()})'])
85+
res = self._test_node.scantxoutset(action="start", scanobjects=[self.get_descriptor()])
8686
assert_equal(True, res['success'])
8787
for utxo in res['unspents']:
8888
self._utxos.append({'txid': utxo['txid'], 'vout': utxo['vout'], 'value': utxo['amount']})
@@ -110,12 +110,15 @@ def sign_tx(self, tx, fixed_length=True):
110110

111111
def generate(self, num_blocks):
112112
"""Generate blocks with coinbase outputs to the internal address, and append the outputs to the internal list"""
113-
blocks = self._test_node.generatetodescriptor(num_blocks, f'raw({self._scriptPubKey.hex()})')
113+
blocks = self._test_node.generatetodescriptor(num_blocks, self.get_descriptor())
114114
for b in blocks:
115115
cb_tx = self._test_node.getblock(blockhash=b, verbosity=2)['tx'][0]
116116
self._utxos.append({'txid': cb_tx['txid'], 'vout': 0, 'value': cb_tx['vout'][0]['value']})
117117
return blocks
118118

119+
def get_descriptor(self):
120+
return self._test_node.getdescriptorinfo(f'raw({self._scriptPubKey.hex()})')['descriptor']
121+
119122
def get_address(self):
120123
return self._address
121124

0 commit comments

Comments
 (0)