Skip to content

Commit f1b99ac

Browse files
committed
test: refactor: deduplicate handle_utxocache_* logic
Carve out the comparison logic into a helper function to avoid code duplication.
1 parent ad90ba3 commit f1b99ac

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

test/functional/interface_usdt_utxocache.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -258,37 +258,33 @@ def test_add_spent(self):
258258
expected_utxocache_spents = []
259259
expected_utxocache_adds = []
260260

261+
def compare_utxo_with_event(utxo, event):
262+
"""Returns 1 if a utxo is identical to the event produced by BPF, otherwise"""
263+
try:
264+
assert_equal(utxo["txid"], bytes(event.txid[::-1]).hex())
265+
assert_equal(utxo["index"], event.index)
266+
assert_equal(utxo["height"], event.height)
267+
assert_equal(utxo["value"], event.value)
268+
assert_equal(utxo["is_coinbase"], event.is_coinbase)
269+
except AssertionError:
270+
self.log.exception("Assertion failed")
271+
return 0
272+
else:
273+
return 1
274+
261275
def handle_utxocache_add(_, data, __):
262276
nonlocal handle_add_succeeds
263277
event = ctypes.cast(data, ctypes.POINTER(UTXOCacheChange)).contents
264278
self.log.info(f"handle_utxocache_add(): {event}")
265279
add = expected_utxocache_adds.pop(0)
266-
try:
267-
assert_equal(add["txid"], bytes(event.txid[::-1]).hex())
268-
assert_equal(add["index"], event.index)
269-
assert_equal(add["height"], event.height)
270-
assert_equal(add["value"], event.value)
271-
assert_equal(add["is_coinbase"], event.is_coinbase)
272-
except AssertionError:
273-
self.log.exception("Assertion failed")
274-
else:
275-
handle_add_succeeds += 1
280+
handle_add_succeeds += compare_utxo_with_event(add, event)
276281

277282
def handle_utxocache_spent(_, data, __):
278283
nonlocal handle_spent_succeeds
279284
event = ctypes.cast(data, ctypes.POINTER(UTXOCacheChange)).contents
280285
self.log.info(f"handle_utxocache_spent(): {event}")
281286
spent = expected_utxocache_spents.pop(0)
282-
try:
283-
assert_equal(spent["txid"], bytes(event.txid[::-1]).hex())
284-
assert_equal(spent["index"], event.index)
285-
assert_equal(spent["height"], event.height)
286-
assert_equal(spent["value"], event.value)
287-
assert_equal(spent["is_coinbase"], event.is_coinbase)
288-
except AssertionError:
289-
self.log.exception("Assertion failed")
290-
else:
291-
handle_spent_succeeds += 1
287+
handle_spent_succeeds += compare_utxo_with_event(spent, event)
292288

293289
bpf["utxocache_add"].open_perf_buffer(handle_utxocache_add)
294290
bpf["utxocache_spent"].open_perf_buffer(handle_utxocache_spent)

0 commit comments

Comments
 (0)