Skip to content

Commit 1da5e45

Browse files
committed
test: use MiniWallet for feature_dbcrash.py
This test can now be run even with the Bitcoin Core wallet disabled.
1 parent 59ac8ba commit 1da5e45

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

test/functional/feature_dbcrash.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@
3030
import random
3131
import time
3232

33+
from test_framework.blocktools import COINBASE_MATURITY
3334
from test_framework.messages import (
3435
COIN,
35-
COutPoint,
36-
CTransaction,
37-
CTxIn,
38-
CTxOut,
3936
)
4037
from test_framework.test_framework import BitcoinTestFramework
4138
from test_framework.util import (
4239
assert_equal,
43-
create_confirmed_utxos,
40+
)
41+
from test_framework.wallet import (
42+
MiniWallet,
43+
getnewdestination,
4444
)
4545

4646

@@ -66,13 +66,9 @@ def set_test_params(self):
6666
self.node3_args = ["-blockmaxweight=4000000", "-acceptnonstdtxn"]
6767
self.extra_args = [self.node0_args, self.node1_args, self.node2_args, self.node3_args]
6868

69-
def skip_test_if_missing_module(self):
70-
self.skip_if_no_wallet()
71-
7269
def setup_network(self):
7370
self.add_nodes(self.num_nodes, extra_args=self.extra_args)
7471
self.start_nodes()
75-
self.import_deterministic_coinbase_privkeys()
7672
# Leave them unconnected, we'll use submitblock directly in this test
7773

7874
def restart_node(self, node_index, expected_tip):
@@ -190,34 +186,36 @@ def generate_small_transactions(self, node, count, utxo_list):
190186
num_transactions = 0
191187
random.shuffle(utxo_list)
192188
while len(utxo_list) >= 2 and num_transactions < count:
193-
tx = CTransaction()
194-
input_amount = 0
195-
for _ in range(2):
196-
utxo = utxo_list.pop()
197-
tx.vin.append(CTxIn(COutPoint(int(utxo['txid'], 16), utxo['vout'])))
198-
input_amount += int(utxo['amount'] * COIN)
199-
output_amount = (input_amount - FEE) // 3
200-
201-
if output_amount <= 0:
189+
utxos_to_spend = [utxo_list.pop() for _ in range(2)]
190+
input_amount = int(sum([utxo['value'] for utxo in utxos_to_spend]) * COIN)
191+
if input_amount < FEE:
202192
# Sanity check -- if we chose inputs that are too small, skip
203193
continue
204194

205-
for _ in range(3):
206-
tx.vout.append(CTxOut(output_amount, bytes.fromhex(utxo['scriptPubKey'])))
195+
tx = self.wallet.create_self_transfer_multi(
196+
from_node=node,
197+
utxos_to_spend=utxos_to_spend,
198+
num_outputs=3,
199+
fee_per_output=FEE // 3)
207200

208-
# Sign and send the transaction to get into the mempool
209-
tx_signed_hex = node.signrawtransactionwithwallet(tx.serialize().hex())['hex']
210-
node.sendrawtransaction(tx_signed_hex)
201+
# Send the transaction to get into the mempool (skip fee-checks to run faster)
202+
node.sendrawtransaction(hexstring=tx.serialize().hex(), maxfeerate=0)
211203
num_transactions += 1
212204

213205
def run_test(self):
206+
self.wallet = MiniWallet(self.nodes[3])
207+
self.wallet.rescan_utxos()
208+
initial_height = self.nodes[3].getblockcount()
209+
self.generate(self.nodes[3], COINBASE_MATURITY, sync_fun=self.no_op)
210+
214211
# Track test coverage statistics
215212
self.restart_counts = [0, 0, 0] # Track the restarts for nodes 0-2
216213
self.crashed_on_restart = 0 # Track count of crashes during recovery
217214

218215
# Start by creating a lot of utxos on node3
219-
initial_height = self.nodes[3].getblockcount()
220-
utxo_list = create_confirmed_utxos(self, self.nodes[3].getnetworkinfo()['relayfee'], self.nodes[3], 5000, sync_fun=self.no_op)
216+
utxo_list = self.wallet.send_self_transfer_multi(from_node=self.nodes[3], num_outputs=5000)['new_utxos']
217+
self.generate(self.nodes[3], 1, sync_fun=self.no_op)
218+
assert_equal(len(self.nodes[3].getrawmempool()), 0)
221219
self.log.info(f"Prepped {len(utxo_list)} utxo entries")
222220

223221
# Sync these blocks with the other nodes
@@ -257,13 +255,14 @@ def run_test(self):
257255
self.nodes[3],
258256
nblocks=min(10, current_height + 1 - self.nodes[3].getblockcount()),
259257
# new address to avoid mining a block that has just been invalidated
260-
address=self.nodes[3].getnewaddress(),
258+
address=getnewdestination()[2],
261259
sync_fun=self.no_op,
262260
))
263261
self.log.debug(f"Syncing {len(block_hashes)} new blocks...")
264262
self.sync_node3blocks(block_hashes)
265-
utxo_list = self.nodes[3].listunspent()
266-
self.log.debug(f"Node3 utxo count: {len(utxo_list)}")
263+
self.wallet.rescan_utxos()
264+
utxo_list = self.wallet.get_utxos()
265+
self.log.debug(f"MiniWallet utxo count: {len(utxo_list)}")
267266

268267
# Check that the utxo hashes agree with node3
269268
# Useful side effect: each utxo cache gets flushed here, so that we

test/functional/test_framework/wallet.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ def get_utxo(self, *, txid: str = '', vout: Optional[int] = None, mark_as_spent=
167167
else:
168168
return self._utxos[index]
169169

170+
def get_utxos(self, *, mark_as_spent=True):
171+
"""Returns the list of all utxos and optionally mark them as spent"""
172+
utxos = deepcopy(self._utxos)
173+
if mark_as_spent:
174+
self._utxos = []
175+
return utxos
176+
170177
def send_self_transfer(self, **kwargs):
171178
"""Create and send a tx with the specified fee_rate. Fee may be exact or at most one satoshi higher than needed."""
172179
tx = self.create_self_transfer(**kwargs)

0 commit comments

Comments
 (0)