@@ -258,37 +258,33 @@ def test_add_spent(self):
258
258
expected_utxocache_spents = []
259
259
expected_utxocache_adds = []
260
260
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
+
261
275
def handle_utxocache_add (_ , data , __ ):
262
276
nonlocal handle_add_succeeds
263
277
event = ctypes .cast (data , ctypes .POINTER (UTXOCacheChange )).contents
264
278
self .log .info (f"handle_utxocache_add(): { event } " )
265
279
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 )
276
281
277
282
def handle_utxocache_spent (_ , data , __ ):
278
283
nonlocal handle_spent_succeeds
279
284
event = ctypes .cast (data , ctypes .POINTER (UTXOCacheChange )).contents
280
285
self .log .info (f"handle_utxocache_spent(): { event } " )
281
286
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 )
292
288
293
289
bpf ["utxocache_add" ].open_perf_buffer (handle_utxocache_add )
294
290
bpf ["utxocache_spent" ].open_perf_buffer (handle_utxocache_spent )
0 commit comments