@@ -252,39 +252,30 @@ def test_add_spent(self):
252
252
# that the handle_* functions succeeded.
253
253
EXPECTED_HANDLE_ADD_SUCCESS = 2
254
254
EXPECTED_HANDLE_SPENT_SUCCESS = 1
255
- handle_add_succeeds = 0
256
- handle_spent_succeeds = 0
257
255
258
- expected_utxocache_spents = []
259
256
expected_utxocache_adds = []
257
+ expected_utxocache_spents = []
258
+
259
+ actual_utxocache_adds = []
260
+ actual_utxocache_spents = []
260
261
261
262
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
263
+ """Compare a utxo dict to the event produced by BPF"""
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 )
274
269
275
270
def handle_utxocache_add (_ , data , __ ):
276
- nonlocal handle_add_succeeds
277
271
event = ctypes .cast (data , ctypes .POINTER (UTXOCacheChange )).contents
278
272
self .log .info (f"handle_utxocache_add(): { event } " )
279
- add = expected_utxocache_adds .pop (0 )
280
- handle_add_succeeds += compare_utxo_with_event (add , event )
273
+ actual_utxocache_adds .append (event )
281
274
282
275
def handle_utxocache_spent (_ , data , __ ):
283
- nonlocal handle_spent_succeeds
284
276
event = ctypes .cast (data , ctypes .POINTER (UTXOCacheChange )).contents
285
277
self .log .info (f"handle_utxocache_spent(): { event } " )
286
- spent = expected_utxocache_spents .pop (0 )
287
- handle_spent_succeeds += compare_utxo_with_event (spent , event )
278
+ actual_utxocache_spents .append (event )
288
279
289
280
bpf ["utxocache_add" ].open_perf_buffer (handle_utxocache_add )
290
281
bpf ["utxocache_spent" ].open_perf_buffer (handle_utxocache_spent )
@@ -320,19 +311,18 @@ def handle_utxocache_spent(_, data, __):
320
311
"is_coinbase" : block_index == 0 ,
321
312
})
322
313
323
- assert_equal (EXPECTED_HANDLE_ADD_SUCCESS , len (expected_utxocache_adds ))
324
- assert_equal (EXPECTED_HANDLE_SPENT_SUCCESS ,
325
- len (expected_utxocache_spents ))
326
-
327
314
bpf .perf_buffer_poll (timeout = 200 )
328
- bpf .cleanup ()
315
+
316
+ assert_equal (EXPECTED_HANDLE_ADD_SUCCESS , len (expected_utxocache_adds ), len (actual_utxocache_adds ))
317
+ assert_equal (EXPECTED_HANDLE_SPENT_SUCCESS , len (expected_utxocache_spents ), len (actual_utxocache_spents ))
329
318
330
319
self .log .info (
331
320
f"check that we successfully traced { EXPECTED_HANDLE_ADD_SUCCESS } adds and { EXPECTED_HANDLE_SPENT_SUCCESS } spent" )
332
- assert_equal (0 , len (expected_utxocache_adds ))
333
- assert_equal (0 , len (expected_utxocache_spents ))
334
- assert_equal (EXPECTED_HANDLE_ADD_SUCCESS , handle_add_succeeds )
335
- assert_equal (EXPECTED_HANDLE_SPENT_SUCCESS , handle_spent_succeeds )
321
+ for expected_utxo , actual_event in zip (expected_utxocache_adds + expected_utxocache_spents ,
322
+ actual_utxocache_adds + actual_utxocache_spents ):
323
+ compare_utxo_with_event (expected_utxo , actual_event )
324
+
325
+ bpf .cleanup ()
336
326
337
327
def test_flush (self ):
338
328
""" Tests the utxocache:flush tracepoint API.
0 commit comments