Skip to content

Commit fac3dcf

Browse files
author
MarcoFalke
committed
test: Generate one block for each send in wallet_import_rescan
This ... * ensures that enough coins are available/spendable, even when more variants are added * ensures that all mempool txs are mined, even when more variants are added * makes the test more specific to test that the confirmation height is properly reported and timestamps are correctly handled in the test logic * prepares the test for a future, where blocks are skipped for rescan if they are deemed irrelevant by a filter (c.f. BIP157)
1 parent fe00192 commit fac3dcf

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

test/functional/wallet_import_rescan.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ def do_import(self, timestamp):
6464
}], {"rescan": self.rescan in (Rescan.yes, Rescan.late_timestamp)})
6565
assert_equal(response, [{"success": True}])
6666

67-
def check(self, txid=None, amount=None, confirmations=None):
67+
def check(self, txid=None, amount=None, confirmation_height=None):
6868
"""Verify that listtransactions/listreceivedbyaddress return expected values."""
6969

7070
txs = self.node.listtransactions(label=self.label, count=10000, include_watchonly=True)
71+
current_height = self.node.getblockcount()
7172
assert_equal(len(txs), self.expected_txs)
7273

7374
addresses = self.node.listreceivedbyaddress(minconf=0, include_watchonly=True, address_filter=self.address['address'])
@@ -82,13 +83,13 @@ def check(self, txid=None, amount=None, confirmations=None):
8283
assert_equal(tx["category"], "receive")
8384
assert_equal(tx["label"], self.label)
8485
assert_equal(tx["txid"], txid)
85-
assert_equal(tx["confirmations"], confirmations)
86+
assert_equal(tx["confirmations"], 1 + current_height - confirmation_height)
8687
assert_equal("trusted" not in tx, True)
8788

8889
address, = [ad for ad in addresses if txid in ad["txids"]]
8990
assert_equal(address["address"], self.address["address"])
9091
assert_equal(address["amount"], self.expected_balance)
91-
assert_equal(address["confirmations"], confirmations)
92+
assert_equal(address["confirmations"], 1 + current_height - confirmation_height)
9293
# Verify the transaction is correctly marked watchonly depending on
9394
# whether the transaction pays to an imported public key or
9495
# imported private key. The test setup ensures that transaction
@@ -151,13 +152,16 @@ def run_test(self):
151152
variant.key = self.nodes[1].dumpprivkey(variant.address["address"])
152153
variant.initial_amount = 1 - (i + 1) / 64
153154
variant.initial_txid = self.nodes[0].sendtoaddress(variant.address["address"], variant.initial_amount)
155+
self.nodes[0].generate(1) # Generate one block for each send
156+
variant.confirmation_height = self.nodes[0].getblockcount()
157+
variant.timestamp = self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())["time"]
154158

155-
# Generate a block containing the initial transactions, then another
156-
# block further in the future (past the rescan window).
157-
self.nodes[0].generate(1)
159+
# Generate a block further in the future (past the rescan window).
158160
assert_equal(self.nodes[0].getrawmempool(), [])
159-
timestamp = self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())["time"]
160-
set_node_times(self.nodes, timestamp + TIMESTAMP_WINDOW + 1)
161+
set_node_times(
162+
self.nodes,
163+
self.nodes[0].getblockheader(self.nodes[0].getbestblockhash())["time"] + TIMESTAMP_WINDOW + 1,
164+
)
161165
self.nodes[0].generate(1)
162166
self.sync_all()
163167

@@ -167,11 +171,11 @@ def run_test(self):
167171
self.log.info('Run import for variant {}'.format(variant))
168172
expect_rescan = variant.rescan == Rescan.yes
169173
variant.node = self.nodes[2 + IMPORT_NODES.index(ImportNode(variant.prune, expect_rescan))]
170-
variant.do_import(timestamp)
174+
variant.do_import(variant.timestamp)
171175
if expect_rescan:
172176
variant.expected_balance = variant.initial_amount
173177
variant.expected_txs = 1
174-
variant.check(variant.initial_txid, variant.initial_amount, 2)
178+
variant.check(variant.initial_txid, variant.initial_amount, variant.confirmation_height)
175179
else:
176180
variant.expected_balance = 0
177181
variant.expected_txs = 0
@@ -181,9 +185,9 @@ def run_test(self):
181185
for i, variant in enumerate(IMPORT_VARIANTS):
182186
variant.sent_amount = 1 - (2 * i + 1) / 128
183187
variant.sent_txid = self.nodes[0].sendtoaddress(variant.address["address"], variant.sent_amount)
188+
self.nodes[0].generate(1) # Generate one block for each send
189+
variant.confirmation_height = self.nodes[0].getblockcount()
184190

185-
# Generate a block containing the new transactions.
186-
self.nodes[0].generate(1)
187191
assert_equal(self.nodes[0].getrawmempool(), [])
188192
self.sync_all()
189193

@@ -192,7 +196,7 @@ def run_test(self):
192196
self.log.info('Run check for variant {}'.format(variant))
193197
variant.expected_balance += variant.sent_amount
194198
variant.expected_txs += 1
195-
variant.check(variant.sent_txid, variant.sent_amount, 1)
199+
variant.check(variant.sent_txid, variant.sent_amount, variant.confirmation_height)
196200

197201
if __name__ == "__main__":
198202
ImportRescanTest().main()

0 commit comments

Comments
 (0)