mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-05 14:06:27 -05:00
test: store utxocache events
By storing the events instead of doing the comparison inside the handle_utxocache_* functions, we simplify the overall logic and potentially making debugging easier, by allowing pdb to access the events. Mostly a refactor, but changes logging behaviour slightly by not raising and not calling self.log.exception("Assertion failed")
This commit is contained in:
parent
f1b99ac94f
commit
f5525ad680
1 changed files with 20 additions and 30 deletions
|
@ -252,39 +252,30 @@ class UTXOCacheTracepointTest(BitcoinTestFramework):
|
||||||
# that the handle_* functions succeeded.
|
# that the handle_* functions succeeded.
|
||||||
EXPECTED_HANDLE_ADD_SUCCESS = 2
|
EXPECTED_HANDLE_ADD_SUCCESS = 2
|
||||||
EXPECTED_HANDLE_SPENT_SUCCESS = 1
|
EXPECTED_HANDLE_SPENT_SUCCESS = 1
|
||||||
handle_add_succeeds = 0
|
|
||||||
handle_spent_succeeds = 0
|
|
||||||
|
|
||||||
expected_utxocache_spents = []
|
|
||||||
expected_utxocache_adds = []
|
expected_utxocache_adds = []
|
||||||
|
expected_utxocache_spents = []
|
||||||
|
|
||||||
|
actual_utxocache_adds = []
|
||||||
|
actual_utxocache_spents = []
|
||||||
|
|
||||||
def compare_utxo_with_event(utxo, event):
|
def compare_utxo_with_event(utxo, event):
|
||||||
"""Returns 1 if a utxo is identical to the event produced by BPF, otherwise"""
|
"""Compare a utxo dict to the event produced by BPF"""
|
||||||
try:
|
|
||||||
assert_equal(utxo["txid"], bytes(event.txid[::-1]).hex())
|
assert_equal(utxo["txid"], bytes(event.txid[::-1]).hex())
|
||||||
assert_equal(utxo["index"], event.index)
|
assert_equal(utxo["index"], event.index)
|
||||||
assert_equal(utxo["height"], event.height)
|
assert_equal(utxo["height"], event.height)
|
||||||
assert_equal(utxo["value"], event.value)
|
assert_equal(utxo["value"], event.value)
|
||||||
assert_equal(utxo["is_coinbase"], event.is_coinbase)
|
assert_equal(utxo["is_coinbase"], event.is_coinbase)
|
||||||
except AssertionError:
|
|
||||||
self.log.exception("Assertion failed")
|
|
||||||
return 0
|
|
||||||
else:
|
|
||||||
return 1
|
|
||||||
|
|
||||||
def handle_utxocache_add(_, data, __):
|
def handle_utxocache_add(_, data, __):
|
||||||
nonlocal handle_add_succeeds
|
|
||||||
event = ctypes.cast(data, ctypes.POINTER(UTXOCacheChange)).contents
|
event = ctypes.cast(data, ctypes.POINTER(UTXOCacheChange)).contents
|
||||||
self.log.info(f"handle_utxocache_add(): {event}")
|
self.log.info(f"handle_utxocache_add(): {event}")
|
||||||
add = expected_utxocache_adds.pop(0)
|
actual_utxocache_adds.append(event)
|
||||||
handle_add_succeeds += compare_utxo_with_event(add, event)
|
|
||||||
|
|
||||||
def handle_utxocache_spent(_, data, __):
|
def handle_utxocache_spent(_, data, __):
|
||||||
nonlocal handle_spent_succeeds
|
|
||||||
event = ctypes.cast(data, ctypes.POINTER(UTXOCacheChange)).contents
|
event = ctypes.cast(data, ctypes.POINTER(UTXOCacheChange)).contents
|
||||||
self.log.info(f"handle_utxocache_spent(): {event}")
|
self.log.info(f"handle_utxocache_spent(): {event}")
|
||||||
spent = expected_utxocache_spents.pop(0)
|
actual_utxocache_spents.append(event)
|
||||||
handle_spent_succeeds += compare_utxo_with_event(spent, event)
|
|
||||||
|
|
||||||
bpf["utxocache_add"].open_perf_buffer(handle_utxocache_add)
|
bpf["utxocache_add"].open_perf_buffer(handle_utxocache_add)
|
||||||
bpf["utxocache_spent"].open_perf_buffer(handle_utxocache_spent)
|
bpf["utxocache_spent"].open_perf_buffer(handle_utxocache_spent)
|
||||||
|
@ -320,19 +311,18 @@ class UTXOCacheTracepointTest(BitcoinTestFramework):
|
||||||
"is_coinbase": block_index == 0,
|
"is_coinbase": block_index == 0,
|
||||||
})
|
})
|
||||||
|
|
||||||
assert_equal(EXPECTED_HANDLE_ADD_SUCCESS, len(expected_utxocache_adds))
|
|
||||||
assert_equal(EXPECTED_HANDLE_SPENT_SUCCESS,
|
|
||||||
len(expected_utxocache_spents))
|
|
||||||
|
|
||||||
bpf.perf_buffer_poll(timeout=200)
|
bpf.perf_buffer_poll(timeout=200)
|
||||||
bpf.cleanup()
|
|
||||||
|
assert_equal(EXPECTED_HANDLE_ADD_SUCCESS, len(expected_utxocache_adds), len(actual_utxocache_adds))
|
||||||
|
assert_equal(EXPECTED_HANDLE_SPENT_SUCCESS, len(expected_utxocache_spents), len(actual_utxocache_spents))
|
||||||
|
|
||||||
self.log.info(
|
self.log.info(
|
||||||
f"check that we successfully traced {EXPECTED_HANDLE_ADD_SUCCESS} adds and {EXPECTED_HANDLE_SPENT_SUCCESS} spent")
|
f"check that we successfully traced {EXPECTED_HANDLE_ADD_SUCCESS} adds and {EXPECTED_HANDLE_SPENT_SUCCESS} spent")
|
||||||
assert_equal(0, len(expected_utxocache_adds))
|
for expected_utxo, actual_event in zip(expected_utxocache_adds + expected_utxocache_spents,
|
||||||
assert_equal(0, len(expected_utxocache_spents))
|
actual_utxocache_adds + actual_utxocache_spents):
|
||||||
assert_equal(EXPECTED_HANDLE_ADD_SUCCESS, handle_add_succeeds)
|
compare_utxo_with_event(expected_utxo, actual_event)
|
||||||
assert_equal(EXPECTED_HANDLE_SPENT_SUCCESS, handle_spent_succeeds)
|
|
||||||
|
bpf.cleanup()
|
||||||
|
|
||||||
def test_flush(self):
|
def test_flush(self):
|
||||||
""" Tests the utxocache:flush tracepoint API.
|
""" Tests the utxocache:flush tracepoint API.
|
||||||
|
|
Loading…
Add table
Reference in a new issue