|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2022 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
""" Tests the validation:* tracepoint API interface.
|
|
|
|
See https://github.com/bitcoin/bitcoin/blob/master/doc/tracing.md#context-validation
|
|
|
|
"""
|
|
|
|
|
|
|
|
import ctypes
|
|
|
|
import time
|
|
|
|
|
|
|
|
# Test will be skipped if we don't have bcc installed
|
|
|
|
try:
|
|
|
|
from bcc import BPF, USDT # type: ignore[import]
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import assert_equal
|
|
|
|
|
|
|
|
|
|
|
|
validation_blockconnected_program = """
|
|
|
|
#include <uapi/linux/ptrace.h>
|
|
|
|
|
|
|
|
typedef signed long long i64;
|
|
|
|
|
|
|
|
struct connected_block
|
|
|
|
{
|
|
|
|
char hash[32];
|
|
|
|
int height;
|
|
|
|
i64 transactions;
|
|
|
|
int inputs;
|
|
|
|
i64 sigops;
|
|
|
|
u64 duration;
|
|
|
|
};
|
|
|
|
|
|
|
|
BPF_PERF_OUTPUT(block_connected);
|
|
|
|
int trace_block_connected(struct pt_regs *ctx) {
|
|
|
|
struct connected_block block = {};
|
|
|
|
bpf_usdt_readarg_p(1, ctx, &block.hash, 32);
|
|
|
|
bpf_usdt_readarg(2, ctx, &block.height);
|
|
|
|
bpf_usdt_readarg(3, ctx, &block.transactions);
|
|
|
|
bpf_usdt_readarg(4, ctx, &block.inputs);
|
|
|
|
bpf_usdt_readarg(5, ctx, &block.sigops);
|
|
|
|
bpf_usdt_readarg(6, ctx, &block.duration);
|
|
|
|
block_connected.perf_submit(ctx, &block, sizeof(block));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class ValidationTracepointTest(BitcoinTestFramework):
|
|
|
|
def set_test_params(self):
|
|
|
|
self.num_nodes = 1
|
|
|
|
|
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_platform_not_linux()
|
|
|
|
self.skip_if_no_bitcoind_tracepoints()
|
|
|
|
self.skip_if_no_python_bcc()
|
|
|
|
self.skip_if_no_bpf_permissions()
|
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
# Tests the validation:block_connected tracepoint by generating blocks
|
|
|
|
# and comparing the values passed in the tracepoint arguments with the
|
|
|
|
# blocks.
|
|
|
|
# See https://github.com/bitcoin/bitcoin/blob/master/doc/tracing.md#tracepoint-validationblock_connected
|
|
|
|
|
|
|
|
class Block(ctypes.Structure):
|
|
|
|
_fields_ = [
|
|
|
|
("hash", ctypes.c_ubyte * 32),
|
|
|
|
("height", ctypes.c_int),
|
|
|
|
("transactions", ctypes.c_int64),
|
|
|
|
("inputs", ctypes.c_int),
|
|
|
|
("sigops", ctypes.c_int64),
|
|
|
|
("duration", ctypes.c_uint64),
|
|
|
|
]
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "ConnectedBlock(hash=%s height=%d, transactions=%d, inputs=%d, sigops=%d, duration=%d)" % (
|
|
|
|
bytes(self.hash[::-1]).hex(),
|
|
|
|
self.height,
|
|
|
|
self.transactions,
|
|
|
|
self.inputs,
|
|
|
|
self.sigops,
|
|
|
|
self.duration)
|
|
|
|
|
|
|
|
BLOCKS_EXPECTED = 2
|
|
|
|
expected_blocks = dict()
|
Manage exceptions in bcc callback functions
Exceptions are not propagated in ctype callback functions used by bcc.
This means an AssertionError exception raised by check_equal() to signal
a failed assertion is not getting caught and properly logged. Instead,
the error is logged to stdout and execution of the handler stops.
The current workaround to check whether all check_equal() assertions in
a callback succeeded is to increment a success counter after the
assertions (which only gets incremented if none exception is raised and
stops execution). Then, outside the callback, the success counter can be
used to check whether a callback executed successfully.
One issue with the described workaround is that when an exception
occurs, there is no way of telling which of the check_equal() statements
caused the exception; moreover, there is no way of inspecting how the
pieces of data that got compared in check_equal() differed (often
a crucial clue when debugging what went wrong).
Two fixes to this problem come to mind. The first involves having the
callback function make event data accessible outside the callback and
inspecting the event using check_equal() outside the callback. This
solution still requires a counter in the callback to tell whether
a callback was actually executed or if instead the call to
perf_buffer_poll() timed out.
The second fix entails wrapping all relevant check_equal() statements
inside callback functions into try-catch blocks and manually logging
AssertionErrors. While not as elegant in terms of design, this approach
can be more pragmatic for more complex tests (e.g., ones involving
multiple events, events of different types, or the order of events).
The solution proposed here is to select the most pragmatic fix on
a case-by-case basis: Tests in interface_usdt_net.py,
interface_usdt_mempool.py and interface_usdt_validation.py have been
refactored to use the first approach, while the second approach was
chosen for interface_usdt_utxocache.py (partly to provide a reference
for the second approach, but mainly because the utxocache tests are the
most intricate tests, and refactoring them to use the first approach
would negatively impact their readability). Lastly,
interface_usdt_coinselection.py was kept unchanged because it does not
use check_equal() statements inside callback functions.
1 year ago
|
|
|
events = []
|
|
|
|
|
|
|
|
self.log.info("hook into the validation:block_connected tracepoint")
|
|
|
|
ctx = USDT(pid=self.nodes[0].process.pid)
|
|
|
|
ctx.enable_probe(probe="validation:block_connected",
|
|
|
|
fn_name="trace_block_connected")
|
|
|
|
bpf = BPF(text=validation_blockconnected_program,
|
|
|
|
usdt_contexts=[ctx], debug=0, cflags=["-Wno-error=implicit-function-declaration"])
|
|
|
|
|
|
|
|
def handle_blockconnected(_, data, __):
|
|
|
|
event = ctypes.cast(data, ctypes.POINTER(Block)).contents
|
|
|
|
self.log.info(f"handle_blockconnected(): {event}")
|
Manage exceptions in bcc callback functions
Exceptions are not propagated in ctype callback functions used by bcc.
This means an AssertionError exception raised by check_equal() to signal
a failed assertion is not getting caught and properly logged. Instead,
the error is logged to stdout and execution of the handler stops.
The current workaround to check whether all check_equal() assertions in
a callback succeeded is to increment a success counter after the
assertions (which only gets incremented if none exception is raised and
stops execution). Then, outside the callback, the success counter can be
used to check whether a callback executed successfully.
One issue with the described workaround is that when an exception
occurs, there is no way of telling which of the check_equal() statements
caused the exception; moreover, there is no way of inspecting how the
pieces of data that got compared in check_equal() differed (often
a crucial clue when debugging what went wrong).
Two fixes to this problem come to mind. The first involves having the
callback function make event data accessible outside the callback and
inspecting the event using check_equal() outside the callback. This
solution still requires a counter in the callback to tell whether
a callback was actually executed or if instead the call to
perf_buffer_poll() timed out.
The second fix entails wrapping all relevant check_equal() statements
inside callback functions into try-catch blocks and manually logging
AssertionErrors. While not as elegant in terms of design, this approach
can be more pragmatic for more complex tests (e.g., ones involving
multiple events, events of different types, or the order of events).
The solution proposed here is to select the most pragmatic fix on
a case-by-case basis: Tests in interface_usdt_net.py,
interface_usdt_mempool.py and interface_usdt_validation.py have been
refactored to use the first approach, while the second approach was
chosen for interface_usdt_utxocache.py (partly to provide a reference
for the second approach, but mainly because the utxocache tests are the
most intricate tests, and refactoring them to use the first approach
would negatively impact their readability). Lastly,
interface_usdt_coinselection.py was kept unchanged because it does not
use check_equal() statements inside callback functions.
1 year ago
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
bpf["block_connected"].open_perf_buffer(
|
|
|
|
handle_blockconnected)
|
|
|
|
|
|
|
|
self.log.info(f"mine {BLOCKS_EXPECTED} blocks")
|
|
|
|
generatetoaddress_duration = dict()
|
|
|
|
for _ in range(BLOCKS_EXPECTED):
|
|
|
|
start = time.time()
|
|
|
|
hash = self.generatetoaddress(self.nodes[0], 1, ADDRESS_BCRT1_UNSPENDABLE)[0]
|
|
|
|
generatetoaddress_duration[hash] = (time.time() - start) * 1e9 # in nanoseconds
|
|
|
|
expected_blocks[hash] = self.nodes[0].getblock(hash, 2)
|
|
|
|
|
|
|
|
bpf.perf_buffer_poll(timeout=200)
|
|
|
|
|
Manage exceptions in bcc callback functions
Exceptions are not propagated in ctype callback functions used by bcc.
This means an AssertionError exception raised by check_equal() to signal
a failed assertion is not getting caught and properly logged. Instead,
the error is logged to stdout and execution of the handler stops.
The current workaround to check whether all check_equal() assertions in
a callback succeeded is to increment a success counter after the
assertions (which only gets incremented if none exception is raised and
stops execution). Then, outside the callback, the success counter can be
used to check whether a callback executed successfully.
One issue with the described workaround is that when an exception
occurs, there is no way of telling which of the check_equal() statements
caused the exception; moreover, there is no way of inspecting how the
pieces of data that got compared in check_equal() differed (often
a crucial clue when debugging what went wrong).
Two fixes to this problem come to mind. The first involves having the
callback function make event data accessible outside the callback and
inspecting the event using check_equal() outside the callback. This
solution still requires a counter in the callback to tell whether
a callback was actually executed or if instead the call to
perf_buffer_poll() timed out.
The second fix entails wrapping all relevant check_equal() statements
inside callback functions into try-catch blocks and manually logging
AssertionErrors. While not as elegant in terms of design, this approach
can be more pragmatic for more complex tests (e.g., ones involving
multiple events, events of different types, or the order of events).
The solution proposed here is to select the most pragmatic fix on
a case-by-case basis: Tests in interface_usdt_net.py,
interface_usdt_mempool.py and interface_usdt_validation.py have been
refactored to use the first approach, while the second approach was
chosen for interface_usdt_utxocache.py (partly to provide a reference
for the second approach, but mainly because the utxocache tests are the
most intricate tests, and refactoring them to use the first approach
would negatively impact their readability). Lastly,
interface_usdt_coinselection.py was kept unchanged because it does not
use check_equal() statements inside callback functions.
1 year ago
|
|
|
self.log.info(f"check that we correctly traced {BLOCKS_EXPECTED} blocks")
|
|
|
|
for event in events:
|
|
|
|
block_hash = bytes(event.hash[::-1]).hex()
|
|
|
|
block = expected_blocks[block_hash]
|
|
|
|
assert_equal(block["hash"], block_hash)
|
|
|
|
assert_equal(block["height"], event.height)
|
|
|
|
assert_equal(len(block["tx"]), event.transactions)
|
|
|
|
assert_equal(len([tx["vin"] for tx in block["tx"]]), event.inputs)
|
|
|
|
assert_equal(0, event.sigops) # no sigops in coinbase tx
|
|
|
|
# only plausibility checks
|
|
|
|
assert event.duration > 0
|
|
|
|
# generatetoaddress (mining and connecting) takes longer than
|
|
|
|
# connecting the block. In case the duration unit is off, we'll
|
|
|
|
# detect it with this assert.
|
|
|
|
assert event.duration < generatetoaddress_duration[block_hash]
|
Manage exceptions in bcc callback functions
Exceptions are not propagated in ctype callback functions used by bcc.
This means an AssertionError exception raised by check_equal() to signal
a failed assertion is not getting caught and properly logged. Instead,
the error is logged to stdout and execution of the handler stops.
The current workaround to check whether all check_equal() assertions in
a callback succeeded is to increment a success counter after the
assertions (which only gets incremented if none exception is raised and
stops execution). Then, outside the callback, the success counter can be
used to check whether a callback executed successfully.
One issue with the described workaround is that when an exception
occurs, there is no way of telling which of the check_equal() statements
caused the exception; moreover, there is no way of inspecting how the
pieces of data that got compared in check_equal() differed (often
a crucial clue when debugging what went wrong).
Two fixes to this problem come to mind. The first involves having the
callback function make event data accessible outside the callback and
inspecting the event using check_equal() outside the callback. This
solution still requires a counter in the callback to tell whether
a callback was actually executed or if instead the call to
perf_buffer_poll() timed out.
The second fix entails wrapping all relevant check_equal() statements
inside callback functions into try-catch blocks and manually logging
AssertionErrors. While not as elegant in terms of design, this approach
can be more pragmatic for more complex tests (e.g., ones involving
multiple events, events of different types, or the order of events).
The solution proposed here is to select the most pragmatic fix on
a case-by-case basis: Tests in interface_usdt_net.py,
interface_usdt_mempool.py and interface_usdt_validation.py have been
refactored to use the first approach, while the second approach was
chosen for interface_usdt_utxocache.py (partly to provide a reference
for the second approach, but mainly because the utxocache tests are the
most intricate tests, and refactoring them to use the first approach
would negatively impact their readability). Lastly,
interface_usdt_coinselection.py was kept unchanged because it does not
use check_equal() statements inside callback functions.
1 year ago
|
|
|
del expected_blocks[block_hash]
|
|
|
|
assert_equal(BLOCKS_EXPECTED, len(events))
|
|
|
|
assert_equal(0, len(expected_blocks))
|
|
|
|
|
Manage exceptions in bcc callback functions
Exceptions are not propagated in ctype callback functions used by bcc.
This means an AssertionError exception raised by check_equal() to signal
a failed assertion is not getting caught and properly logged. Instead,
the error is logged to stdout and execution of the handler stops.
The current workaround to check whether all check_equal() assertions in
a callback succeeded is to increment a success counter after the
assertions (which only gets incremented if none exception is raised and
stops execution). Then, outside the callback, the success counter can be
used to check whether a callback executed successfully.
One issue with the described workaround is that when an exception
occurs, there is no way of telling which of the check_equal() statements
caused the exception; moreover, there is no way of inspecting how the
pieces of data that got compared in check_equal() differed (often
a crucial clue when debugging what went wrong).
Two fixes to this problem come to mind. The first involves having the
callback function make event data accessible outside the callback and
inspecting the event using check_equal() outside the callback. This
solution still requires a counter in the callback to tell whether
a callback was actually executed or if instead the call to
perf_buffer_poll() timed out.
The second fix entails wrapping all relevant check_equal() statements
inside callback functions into try-catch blocks and manually logging
AssertionErrors. While not as elegant in terms of design, this approach
can be more pragmatic for more complex tests (e.g., ones involving
multiple events, events of different types, or the order of events).
The solution proposed here is to select the most pragmatic fix on
a case-by-case basis: Tests in interface_usdt_net.py,
interface_usdt_mempool.py and interface_usdt_validation.py have been
refactored to use the first approach, while the second approach was
chosen for interface_usdt_utxocache.py (partly to provide a reference
for the second approach, but mainly because the utxocache tests are the
most intricate tests, and refactoring them to use the first approach
would negatively impact their readability). Lastly,
interface_usdt_coinselection.py was kept unchanged because it does not
use check_equal() statements inside callback functions.
1 year ago
|
|
|
bpf.cleanup()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
ValidationTracepointTest(__file__).main()
|