Skip to content

Commit 1bf441f

Browse files
fanquakevijaydasmp
authored andcommitted
Merge bitcoin#27944: test: various USDT functional test cleanups (27831 follow-ups)
9f55773 test: refactor: usdt_mempool: store all events (stickies-v) bc43270 test: refactor: remove unnecessary nonlocal (stickies-v) 326db63 test: log sanity check assertion failures (stickies-v) f5525ad test: store utxocache events (stickies-v) f1b99ac test: refactor: deduplicate handle_utxocache_* logic (stickies-v) ad90ba3 test: refactor: rename inbound to is_inbound (stickies-v) afc0224 test: refactor: remove unnecessary blocks_checked counter (stickies-v) Pull request description: Various cleanups to the USDT functional tests, largely (but not exclusively) follow-ups to bitcoin#27831 (review). Except for slightly different logging behaviour in "test: store utxocache events" and "test: log sanity check assertion failures", this is a refactor PR, removing unnecessary code and (imo) making it more readable and maintainable. The rationale for each change is in the corresponding commit message. Note: except for "test: store utxocache events" (which relies on its parent, and I separated into two commits because we may want the parent but not the child), all commits are stand-alone and I'm okay with dropping one/multiple commits if they turn out to be controversial or undesired. ACKs for top commit: 0xB10C: ACK 9f55773. Reviewed the code and ran the USDT interface tests. I stepped through the commits and think all changes are reasonable. Tree-SHA512: 6c37a0265b6c26d4f9552a056a690b8f86f7304bd33b4419febd8b17369cf6af799cb87c16df35d0c2a1b839ad31de24661d4384eafa88816c2051c522fd3bf5
1 parent 953db9b commit 1bf441f

File tree

3 files changed

+35
-49
lines changed

3 files changed

+35
-49
lines changed

test/functional/interface_usdt_net.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,24 @@ def __repr__(self):
121121
checked_outbound_version_msg = 0
122122
events = []
123123

124-
def check_p2p_message(event, inbound):
124+
def check_p2p_message(event, is_inbound):
125125
nonlocal checked_inbound_version_msg, checked_outbound_version_msg
126126
if event.msg_type.decode("utf-8") == "version":
127127
self.log.info(
128-
f"check_p2p_message(): {'inbound' if inbound else 'outbound'} {event}")
128+
f"check_p2p_message(): {'inbound' if is_inbound else 'outbound'} {event}")
129129
peer = self.nodes[0].getpeerinfo()[0]
130130
msg = msg_version()
131131
msg.deserialize(BytesIO(bytes(event.msg[:event.msg_size])))
132132
assert_equal(peer["id"], event.peer_id, peer["id"])
133133
assert_equal(peer["addr"], event.peer_addr.decode("utf-8"))
134134
assert_equal(peer["connection_type"],
135135
event.peer_conn_type.decode("utf-8"))
136-
if inbound:
136+
if is_inbound:
137137
checked_inbound_version_msg += 1
138138
else:
139139
checked_outbound_version_msg += 1
140140

141141
def handle_inbound(_, data, __):
142-
nonlocal events
143142
event = ctypes.cast(data, ctypes.POINTER(P2PMessage)).contents
144143
events.append((event, True))
145144

@@ -157,8 +156,8 @@ def handle_outbound(_, data, __):
157156

158157
self.log.info(
159158
"check receipt and content of in- and outbound version messages")
160-
for event, inbound in events:
161-
check_p2p_message(event, inbound)
159+
for event, is_inbound in events:
160+
check_p2p_message(event, is_inbound)
162161
assert_equal(EXPECTED_INOUTBOUND_VERSION_MSG,
163162
checked_inbound_version_msg)
164163
assert_equal(EXPECTED_INOUTBOUND_VERSION_MSG,

test/functional/interface_usdt_utxocache.py

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -254,43 +254,30 @@ def test_add_spent(self):
254254
# that the handle_* functions succeeded.
255255
EXPECTED_HANDLE_ADD_SUCCESS = 2
256256
EXPECTED_HANDLE_SPENT_SUCCESS = 1
257-
handle_add_succeeds = 0
258-
handle_spent_succeeds = 0
259257

260-
expected_utxocache_spents = []
261258
expected_utxocache_adds = []
259+
expected_utxocache_spents = []
260+
261+
actual_utxocache_adds = []
262+
actual_utxocache_spents = []
263+
264+
def compare_utxo_with_event(utxo, event):
265+
"""Compare a utxo dict to the event produced by BPF"""
266+
assert_equal(utxo["txid"], bytes(event.txid[::-1]).hex())
267+
assert_equal(utxo["index"], event.index)
268+
assert_equal(utxo["height"], event.height)
269+
assert_equal(utxo["value"], event.value)
270+
assert_equal(utxo["is_coinbase"], event.is_coinbase)
262271

263272
def handle_utxocache_add(_, data, __):
264-
nonlocal handle_add_succeeds
265273
event = ctypes.cast(data, ctypes.POINTER(UTXOCacheChange)).contents
266274
self.log.info(f"handle_utxocache_add(): {event}")
267-
add = expected_utxocache_adds.pop(0)
268-
try:
269-
assert_equal(add["txid"], bytes(event.txid[::-1]).hex())
270-
assert_equal(add["index"], event.index)
271-
assert_equal(add["height"], event.height)
272-
assert_equal(add["value"], event.value)
273-
assert_equal(add["is_coinbase"], event.is_coinbase)
274-
except AssertionError:
275-
self.log.exception("Assertion failed")
276-
else:
277-
handle_add_succeeds += 1
275+
actual_utxocache_adds.append(event)
278276

279277
def handle_utxocache_spent(_, data, __):
280-
nonlocal handle_spent_succeeds
281278
event = ctypes.cast(data, ctypes.POINTER(UTXOCacheChange)).contents
282279
self.log.info(f"handle_utxocache_spent(): {event}")
283-
spent = expected_utxocache_spents.pop(0)
284-
try:
285-
assert_equal(spent["txid"], bytes(event.txid[::-1]).hex())
286-
assert_equal(spent["index"], event.index)
287-
assert_equal(spent["height"], event.height)
288-
assert_equal(spent["value"], event.value)
289-
assert_equal(spent["is_coinbase"], event.is_coinbase)
290-
except AssertionError:
291-
self.log.exception("Assertion failed")
292-
else:
293-
handle_spent_succeeds += 1
280+
actual_utxocache_spents.append(event)
294281

295282
bpf["utxocache_add"].open_perf_buffer(handle_utxocache_add)
296283
bpf["utxocache_spent"].open_perf_buffer(handle_utxocache_spent)
@@ -326,19 +313,18 @@ def handle_utxocache_spent(_, data, __):
326313
"is_coinbase": block_index == 0,
327314
})
328315

329-
assert_equal(EXPECTED_HANDLE_ADD_SUCCESS, len(expected_utxocache_adds))
330-
assert_equal(EXPECTED_HANDLE_SPENT_SUCCESS,
331-
len(expected_utxocache_spents))
332-
333316
bpf.perf_buffer_poll(timeout=200)
334-
bpf.cleanup()
317+
318+
assert_equal(EXPECTED_HANDLE_ADD_SUCCESS, len(expected_utxocache_adds), len(actual_utxocache_adds))
319+
assert_equal(EXPECTED_HANDLE_SPENT_SUCCESS, len(expected_utxocache_spents), len(actual_utxocache_spents))
335320

336321
self.log.info(
337322
f"check that we successfully traced {EXPECTED_HANDLE_ADD_SUCCESS} adds and {EXPECTED_HANDLE_SPENT_SUCCESS} spent")
338-
assert_equal(0, len(expected_utxocache_adds))
339-
assert_equal(0, len(expected_utxocache_spents))
340-
assert_equal(EXPECTED_HANDLE_ADD_SUCCESS, handle_add_succeeds)
341-
assert_equal(EXPECTED_HANDLE_SPENT_SUCCESS, handle_spent_succeeds)
323+
for expected_utxo, actual_event in zip(expected_utxocache_adds + expected_utxocache_spents,
324+
actual_utxocache_adds + actual_utxocache_spents):
325+
compare_utxo_with_event(expected_utxo, actual_event)
326+
327+
bpf.cleanup()
342328

343329
def test_flush(self):
344330
""" Tests the utxocache:flush tracepoint API.
@@ -368,9 +354,13 @@ def handle_utxocache_flush(_, data, __):
368354
assert_equal(expected["mode"], FLUSHMODE_NAME[event.mode])
369355
possible_cache_sizes.remove(event.size) # fails if size not in set
370356
# sanity checks only
371-
assert(event.memory > 0)
372-
assert(event.duration > 0)
373-
handle_flush_succeeds += 1
357+
try:
358+
assert event.memory > 0
359+
assert event.duration > 0
360+
except AssertionError:
361+
self.log.exception("Assertion error")
362+
else:
363+
handle_flush_succeeds += 1
374364

375365
bpf["utxocache_flush"].open_perf_buffer(handle_utxocache_flush)
376366

test/functional/interface_usdt_validation.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ def __repr__(self):
8686
self.duration)
8787

8888
BLOCKS_EXPECTED = 2
89-
blocks_checked = 0
9089
expected_blocks = dict()
9190
events = []
9291

@@ -98,7 +97,6 @@ def __repr__(self):
9897
usdt_contexts=[ctx], debug=0, cflags=["-Wno-error=implicit-function-declaration"])
9998

10099
def handle_blockconnected(_, data, __):
101-
nonlocal events, blocks_checked
102100
event = ctypes.cast(data, ctypes.POINTER(Block)).contents
103101
self.log.info(f"handle_blockconnected(): {event}")
104102
block_hash = bytes(event.hash[::-1]).hex()
@@ -111,7 +109,6 @@ def handle_blockconnected(_, data, __):
111109
# only plausibility checks
112110
assert(event.duration > 0)
113111
del expected_blocks[block_hash]
114-
blocks_checked += 1
115112

116113
bpf["block_connected"].open_perf_buffer(
117114
handle_blockconnected)
@@ -136,7 +133,7 @@ def handle_blockconnected(_, data, __):
136133
# only plausibility checks
137134
assert event.duration > 0
138135
del expected_blocks[block_hash]
139-
assert_equal(BLOCKS_EXPECTED, blocks_checked)
136+
assert_equal(BLOCKS_EXPECTED, len(events))
140137
assert_equal(0, len(expected_blocks))
141138

142139
bpf.cleanup()

0 commit comments

Comments
 (0)