btclib.testing package

Submodules

btclib.testing.blocks module

Mainnet blocks, serialized as bitcoin-cli getblock <hash> 0 returns them.

NAMES lists the files: the blocks at heights 170, 200000 and 481824, the last with its witnesses. read_bytes returns one of them. tests/_data/README.md records each block’s hash and the day it was pulled.

btclib.testing.blocks.read_bytes(name: str) → bytes[source]

Return the bytes of the block file name, one of NAMES.

btclib.testing.strategies module

Hypothesis strategies for the transaction types, scripts and blocks.

Each transaction strategy draws values the type’s own assert_valid accepts, and never a coinbase input: the property they serve is parse(serialize(x)) == x, and a value the library refuses to build is not a value to round-trip. Boundaries are drawn on purpose, not left to chance: the null transaction id, an amount of zero and the largest one, and the lowest and highest of each four-byte field. Some lengths reach 253, where a var_int takes three bytes.

COMMAND_LISTS draws commands as script.parse spells them, so parse(serialize(c)) == c. MIXED_COMMAND_LISTS adds integers, bytes, lower-case hex and the aliases. SCRIPTS draws bytes, well formed or not.

SCRIPT_PUB_KEYS draws the standard output scripts of every type the classifier names, and bytes of any kind, on every network. BLOCK_HEADERS draws headers assert_valid accepts, with no work done: its bits are any four bytes. Its times are whole seconds, the only ones the four bytes hold without loss. BLOCKS draws blocks that assert_valid accepts with REGTEST_POW_LIMIT_BITS as the limit, mainnet’s work being out of reach of a test; each has a coinbase, up to MAX_BLOCK_TRANSACTIONS - 1 other transactions and, where any carries a witness, the BIP141 commitment.

This module needs the testing extra, pip install "btclib[testing]", which brings in hypothesis.

Module contents

Module btclib.testing: fixtures for the tests of btclib and its callers.

blocks holds mainnet blocks as bytes. strategies holds hypothesis strategies for transactions, scripts and blocks.

strategies is imported by name, as in from btclib.testing import strategies. It is not in __all__, so that from btclib.testing import * works without hypothesis. tests/imports_test.py holds that.