Skip to content

Commit f252e68

Browse files
committed
assumeutxo test: Add RPC test for fake nTx and nChainTx values
The fake values will be removed in an upcoming commit, so it is useful to have test coverage confirming the change in behavior.
1 parent 9a459e3 commit f252e68

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

test/functional/feature_assumeutxo.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"""
3535
from shutil import rmtree
3636

37+
from dataclasses import dataclass
3738
from test_framework.messages import tx_from_hex
3839
from test_framework.test_framework import BitcoinTestFramework
3940
from test_framework.util import (
@@ -174,10 +175,18 @@ def run_test(self):
174175

175176
# Generate a series of blocks that `n0` will have in the snapshot,
176177
# but that n1 and n2 don't yet see.
178+
assert n0.getblockcount() == START_HEIGHT
179+
blocks = {START_HEIGHT: Block(n0.getbestblockhash(), 1, START_HEIGHT + 1)}
177180
for i in range(100):
181+
block_tx = 1
178182
if i % 3 == 0:
179183
self.mini_wallet.send_self_transfer(from_node=n0)
184+
block_tx += 1
180185
self.generate(n0, nblocks=1, sync_fun=self.no_op)
186+
height = n0.getblockcount()
187+
hash = n0.getbestblockhash()
188+
blocks[height] = Block(hash, block_tx, blocks[height-1].chain_tx + block_tx)
189+
181190

182191
self.log.info("-- Testing assumeutxo + some indexes + pruning")
183192

@@ -207,7 +216,7 @@ def run_test(self):
207216
assert_equal(
208217
dump_output['txoutset_hash'],
209218
"a4bf3407ccb2cc0145c49ebba8fa91199f8a3903daf0883875941497d2493c27")
210-
assert_equal(dump_output["nchaintx"], 334)
219+
assert_equal(dump_output["nchaintx"], blocks[SNAPSHOT_BASE_HEIGHT].chain_tx)
211220
assert_equal(n0.getblockchaininfo()["blocks"], SNAPSHOT_BASE_HEIGHT)
212221

213222
# Mine more blocks on top of the snapshot that n1 hasn't yet seen. This
@@ -228,6 +237,30 @@ def run_test(self):
228237
assert_equal(loaded['coins_loaded'], SNAPSHOT_BASE_HEIGHT)
229238
assert_equal(loaded['base_height'], SNAPSHOT_BASE_HEIGHT)
230239

240+
def check_tx_counts(final: bool) -> None:
241+
"""Check nTx and nChainTx intermediate values right after loading
242+
the snapshot, and final values after the snapshot is validated."""
243+
for height, block in blocks.items():
244+
tx = n1.getblockheader(block.hash)["nTx"]
245+
chain_tx = n1.getchaintxstats(nblocks=1, blockhash=block.hash)["txcount"]
246+
247+
# Intermediate nTx of the starting block should be real, but nTx of
248+
# later blocks should be fake 1 values set by snapshot loading code.
249+
if final or height == START_HEIGHT:
250+
assert_equal(tx, block.tx)
251+
else:
252+
assert_equal(tx, 1)
253+
254+
# Intermediate nChainTx of the starting block and snapshot block
255+
# should be real, but others will be fake values set by snapshot
256+
# loading code.
257+
if final or height in (START_HEIGHT, SNAPSHOT_BASE_HEIGHT):
258+
assert_equal(chain_tx, block.chain_tx)
259+
else:
260+
assert_equal(chain_tx, height + 1)
261+
262+
check_tx_counts(final=False)
263+
231264
normal, snapshot = n1.getchainstates()["chainstates"]
232265
assert_equal(normal['blocks'], START_HEIGHT)
233266
assert_equal(normal.get('snapshot_blockhash'), None)
@@ -291,6 +324,8 @@ def run_test(self):
291324
}
292325
self.wait_until(lambda: n1.getindexinfo() == completed_idx_state)
293326

327+
self.log.info("Re-check nTx and nChainTx values")
328+
check_tx_counts(final=True)
294329

295330
for i in (0, 1):
296331
n = self.nodes[i]
@@ -365,6 +400,11 @@ def run_test(self):
365400
self.connect_nodes(0, 2)
366401
self.wait_until(lambda: n2.getblockcount() == FINAL_HEIGHT)
367402

403+
@dataclass
404+
class Block:
405+
hash: str
406+
tx: int
407+
chain_tx: int
368408

369409
if __name__ == '__main__':
370410
AssumeutxoTest().main()

0 commit comments

Comments
 (0)