Skip to content

Commit dba6f82

Browse files
committed
test: adopt USDT utxocache interface tests
The USDT interface exposes process internals via the tracepoints. This means, the USDT interface tests somewhat awardly depend on these internals. If internals change, the tests have to adopt to that change. Previously, the USDT interface tests weren't run in the CI so changes could break the USDT interface tests without being noticed (e.g. bitcoin#25486). In fa13375 a 'self.rescan_utxos()' call was added in the 'generate()' function of the test framework. 'rescan_utxos()' causes the UTXO cache to be flushed. In the USDT interface tests for the 'utxocache:flush' trancepoint, 'generate()' is used. As the utxo cache is now flushed more often, the number of flushes the tests expectes need to be adopted. Also, the utxo cache has now a different size when being flushed. The utxocache tracepoint is tested by shutting the node down and pruning blocks, to test the 'for_prune' argument. Changes: - A list 'expected_flushes' is now used which contains 'mode', 'for_prune', and 'size' for each expected flush. - When a flush happens, the expected-flush is removed from the list. This list is checked to be empty (unchanged). - Previously, shutting down caused these two flushes: UTXOCacheFlush(duration=*, mode=ALWAYS, size=104, memory=*, for_prune=False) UTXOCacheFlush(duration=*, mode=ALWAYS, size=0, memory=*, for_prune=False) now it causes these flushes: UTXOCacheFlush(duration=*, mode=ALWAYS, size=2, memory=*, for_prune=False) UTXOCacheFlush(duration=*, mode=ALWAYS, size=0, memory=*, for_prune=False) The 104 UTXOs flushed previously were mainly coinbase UTXOs generated in previous tests and the test setup. These are now already flushed. - In the 'for_prune' test we previously hooked into the tracepoint before mining blocks. This changed to only get notified about the tracepoint being triggered for the prune. Here, the utxo cache is empty already as it has just been flushed in 'generate()'. old: UTXOCacheFlush(duration=*, mode=NONE, size=350, memory=*, for_prune=True) new: UTXOCacheFlush(duration=*, mode=NONE, size=0, memory=*, for_prune=True)
1 parent 220a5a2 commit dba6f82

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

Diff for: test/functional/interface_usdt_utxocache.py

+14-19
Original file line numberDiff line numberDiff line change
@@ -345,16 +345,17 @@ def test_flush(self):
345345
# that the handle_* functions succeeded.
346346
EXPECTED_HANDLE_FLUSH_SUCCESS = 3
347347
handle_flush_succeeds = 0
348-
possible_cache_sizes = set()
349-
expected_flushes = []
348+
expected_flushes = list()
350349

351350
def handle_utxocache_flush(_, data, __):
352351
nonlocal handle_flush_succeeds
353352
event = ctypes.cast(data, ctypes.POINTER(UTXOCacheFlush)).contents
354353
self.log.info(f"handle_utxocache_flush(): {event}")
355-
expected = expected_flushes.pop(0)
356-
assert_equal(expected["mode"], FLUSHMODE_NAME[event.mode])
357-
possible_cache_sizes.remove(event.size) # fails if size not in set
354+
expected_flushes.remove({
355+
"mode": FLUSHMODE_NAME[event.mode],
356+
"for_prune": event.for_prune,
357+
"size": event.size
358+
})
358359
# sanity checks only
359360
assert(event.memory > 0)
360361
assert(event.duration > 0)
@@ -363,25 +364,27 @@ def handle_utxocache_flush(_, data, __):
363364
bpf["utxocache_flush"].open_perf_buffer(handle_utxocache_flush)
364365

365366
self.log.info("stop the node to flush the UTXO cache")
366-
UTXOS_IN_CACHE = 104 # might need to be changed if the eariler tests are modified
367+
UTXOS_IN_CACHE = 2 # might need to be changed if the eariler tests are modified
367368
# A node shutdown causes two flushes. One that flushes UTXOS_IN_CACHE
368369
# UTXOs and one that flushes 0 UTXOs. Normally the 0-UTXO-flush is the
369370
# second flush, however it can happen that the order changes.
370-
possible_cache_sizes = {UTXOS_IN_CACHE, 0}
371-
flush_for_shutdown = {"mode": "ALWAYS", "for_prune": False}
372-
expected_flushes.extend([flush_for_shutdown, flush_for_shutdown])
371+
expected_flushes.append({"mode": "ALWAYS", "for_prune": False, "size": UTXOS_IN_CACHE})
372+
expected_flushes.append({"mode": "ALWAYS", "for_prune": False, "size": 0})
373373
self.stop_node(0)
374374

375375
bpf.perf_buffer_poll(timeout=200)
376376
bpf.cleanup()
377377

378378
self.log.info("check that we don't expect additional flushes")
379379
assert_equal(0, len(expected_flushes))
380-
assert_equal(0, len(possible_cache_sizes))
381380

382381
self.log.info("restart the node with -prune")
383382
self.start_node(0, ["-fastprune=1", "-prune=1"])
384383

384+
BLOCKS_TO_MINE = 350
385+
self.log.info(f"mine {BLOCKS_TO_MINE} blocks to be able to prune")
386+
self.generate(self.wallet, BLOCKS_TO_MINE)
387+
385388
self.log.info("test the utxocache:flush tracepoint API with pruning")
386389
self.log.info("hook into the utxocache:flush tracepoint")
387390
ctx = USDT(pid=self.nodes[0].process.pid)
@@ -390,15 +393,8 @@ def handle_utxocache_flush(_, data, __):
390393
bpf = BPF(text=utxocache_flushes_program, usdt_contexts=[ctx], debug=0)
391394
bpf["utxocache_flush"].open_perf_buffer(handle_utxocache_flush)
392395

393-
BLOCKS_TO_MINE = 350
394-
self.log.info(f"mine {BLOCKS_TO_MINE} blocks to be able to prune")
395-
self.generate(self.wallet, BLOCKS_TO_MINE)
396-
# we added BLOCKS_TO_MINE coinbase UTXOs to the cache
397-
possible_cache_sizes = {BLOCKS_TO_MINE}
398-
expected_flushes.append(
399-
{"mode": "NONE", "for_prune": True, "size_fn": lambda x: x == BLOCKS_TO_MINE})
400-
401396
self.log.info(f"prune blockchain to trigger a flush for pruning")
397+
expected_flushes.append({"mode": "NONE", "for_prune": True, "size": 0})
402398
self.nodes[0].pruneblockchain(315)
403399

404400
bpf.perf_buffer_poll(timeout=500)
@@ -407,7 +403,6 @@ def handle_utxocache_flush(_, data, __):
407403
self.log.info(
408404
f"check that we don't expect additional flushes and that the handle_* function succeeded")
409405
assert_equal(0, len(expected_flushes))
410-
assert_equal(0, len(possible_cache_sizes))
411406
assert_equal(EXPECTED_HANDLE_FLUSH_SUCCESS, handle_flush_succeeds)
412407

413408

0 commit comments

Comments
 (0)