Chain API
BlockMap
Bases: MapBase
Dictionary-like interface for reading blocks from disk.
This map reads blocks from disk using block tree entries as keys.
Source code in pbk/chain.py
__getitem__
Read a block from disk using its block tree entry.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The block tree entry identifying which block to read.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Block
|
The full block data read from disk. |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If reading the block from disk fails. |
Source code in pbk/chain.py
BlockSpentOutputsMap
Bases: MapBase
Dictionary-like interface for reading block spent outputs (undo data).
This map reads spent output data (also known as undo data) from disk.
Source code in pbk/chain.py
__getitem__
Read block spent outputs from disk using a block tree entry.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The block tree entry identifying which block's spent outputs to read.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BlockSpentOutputs
|
The spent outputs data for the block. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If attempting to read spent outputs for the genesis block, which has no spent outputs. |
RuntimeError
|
If reading the spent outputs from disk fails. |
Source code in pbk/chain.py
BlockTreeEntryMap
Bases: MapBase
Dictionary-like interface for retrieving block tree entries by hash.
This map allows looking up block tree entries using their block hashes. It provides read-only dictionary access to the block index maintained by the chainstate manager.
Source code in pbk/chain.py
__getitem__
Retrieve a block tree entry by its block hash.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The block hash to look up.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BlockTreeEntry
|
The block tree entry corresponding to the hash. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the block hash is not found in the block index. |
Source code in pbk/chain.py
BlockTreeEntrySequence
Bases: LazySequence[BlockTreeEntry]
Lazily-evaluated sequence of block tree entries in a chain.
This sequence provides access to the chain's block tree entries indexed by height. It supports iteration, length queries, and membership testing. The sequence is a view into the chain and reflects the current state. Its members and length may change during its lifetime, for example when blocks are added to the chain, or when a reorg happens.
Warning
The chain must not be mutated while iterating over this sequence. For example, if a reorg happens while iterating, there is no guarantee that the results belong to the same chain.
Source code in pbk/chain.py
__contains__
Return True if other exists in the sequence.
Source code in pbk/chain.py
__init__
Create a sequence view of block tree entries.
| PARAMETER | DESCRIPTION |
|---|---|
chain
|
The chain object to create a sequence view for.
TYPE:
|
ChainType
Bases: IntEnum
Enumeration of supported Bitcoin network types.
Source code in pbk/chain.py
ChainParameters
Bases: KernelOpaquePtr
Chain parameters describing properties of a Bitcoin network.
Chain parameters define network-specific constants and rules. These are typically passed to context options when creating a kernel context.
Source code in pbk/chain.py
__init__
Create chain parameters for a specific network type.
| PARAMETER | DESCRIPTION |
|---|---|
chain_type
|
The Bitcoin network type to configure.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If the C constructor fails (propagated from base class). |
Source code in pbk/chain.py
ChainstateManagerOptions
Bases: KernelOpaquePtr
Configuration options for creating a chainstate manager.
These options specify how a chainstate manager should be initialized. Options need to be configured before passing it to a chainstate manager constructor.
Warning
Once a chainstate manager is initialized, changes to its options object are no longer reflected on the chainstate manager itself.
Source code in pbk/chain.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
__init__
Create chainstate manager options.
| PARAMETER | DESCRIPTION |
|---|---|
context
|
The kernel context to associate with.
TYPE:
|
datadir
|
Non-empty path to the directory containing chainstate data. The directory will be created if it doesn't exist.
TYPE:
|
blocks_dir
|
Non-empty path to the directory containing block data. The directory will be created if it doesn't exist.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If the C constructor fails due to invalid paths or other errors (propagated from base class). |
Source code in pbk/chain.py
set_wipe_dbs
Configure the wiping of the block tree database and the chainstate database.
Warning
If wipe_block_tree_db==True, pbk.ChainstateManager.init and pbk.ChainstateManager.import_blocks
must be called for the wiping to take effect.
| PARAMETER | DESCRIPTION |
|---|---|
wipe_block_tree_db
|
Whether to wipe the block tree database.
Should only be True if
TYPE:
|
wipe_chainstate_db
|
Whether to wipe the chainstate database.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
0 if the configuration was successful, 1 otherwise. |
Source code in pbk/chain.py
set_worker_threads_num
Set the number of worker threads for parallel validation.
| PARAMETER | DESCRIPTION |
|---|---|
worker_threads
|
Number of worker threads for the validation thread pool. When set to 0, no parallel verification is performed. The value is internally clamped between 0 and 15.
TYPE:
|
Source code in pbk/chain.py
update_block_tree_db_in_memory
Configure whether to use an in-memory block tree database.
When enabled, the block tree database is stored in memory instead of on disk. Useful for testing or temporary validation tasks.
| PARAMETER | DESCRIPTION |
|---|---|
block_tree_db_in_memory
|
True to use in-memory storage, False to use disk storage.
TYPE:
|
Source code in pbk/chain.py
update_chainstate_db_in_memory
Configure whether to use an in-memory chainstate database.
When enabled, the UTXO set and chainstate are stored in memory instead of on disk. Useful for testing or temporary validation tasks.
| PARAMETER | DESCRIPTION |
|---|---|
chainstate_db_in_memory
|
True to use in-memory storage, False to use disk storage.
TYPE:
|
Source code in pbk/chain.py
Chain
Bases: KernelOpaquePtr
View of the currently active blockchain.
This object represents the best-known chain and provides access to its block tree entries via height-based indexing. It is a dynamic view that always reflects the current active chain, but its contents may change when the chainstate manager processes new blocks or reorgs occur.
Note
The chain is a view with lifetime dependent on the chainstate manager it was retrieved from. Data retrieved from the chain is only consistent until new blocks are processed in the chainstate manager.
Source code in pbk/chain.py
block_tree_entries
property
Sequence of all block tree entries in the chain.
| RETURNS | DESCRIPTION |
|---|---|
BlockTreeEntrySequence
|
A lazy sequence supporting indexing and iteration over blocks |
BlockTreeEntrySequence
|
in the chain by height. |
height
property
Current height of the chain tip.
| RETURNS | DESCRIPTION |
|---|---|
int
|
Height of the chain tip. Genesis block is at height 0. |
__len__
ChainstateManager
Bases: KernelOpaquePtr
Central manager for blockchain validation and data retrieval.
The chainstate manager is the primary interface for validation tasks and accessing blockchain data. It maintains the block index, UTXO set, and active chain. This is a complex internal data structure that will expose more functionality over time.
The chainstate manager can be safely used from multiple threads.
Source code in pbk/chain.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
block_spent_outputs
property
Dictionary-like interface for reading block spent outputs (undo data).
| RETURNS | DESCRIPTION |
|---|---|
BlockSpentOutputsMap
|
A map that reads spent output data using block tree entries as keys. |
block_tree_entries
property
Dictionary-like interface for looking up block tree entries by hash.
| RETURNS | DESCRIPTION |
|---|---|
BlockTreeEntryMap
|
A map that retrieves block tree entries using block hashes as keys. |
blocks
property
Dictionary-like interface for reading blocks from disk.
| RETURNS | DESCRIPTION |
|---|---|
BlockMap
|
A map that reads full block data using block tree entries as keys. |
__init__
Create a chainstate manager.
Info
If wiping options were set to
(wipe_block_tree_db==False, wipe_chainstate_db==True), the chainstate database will be
rebuilt during initialization. This may take a long time.
Info
bitcoinkernel requires exclusive access to its data directories. The construction
of a chainstate manager will fail if another process (e.g. Bitcoin Core) is currently
using them.
| PARAMETER | DESCRIPTION |
|---|---|
chain_man_opts
|
Configuration options specifying data directories and other initialization parameters.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If instantiation is not supported (propagated from base class). |
RuntimeError
|
If creation fails due to invalid options, corrupted databases, or other errors (propagated from base class). |
Source code in pbk/chain.py
__repr__
get_active_chain
Get the currently active best-known blockchain.
Returns a view of the active chain that always reflects the current best chain. Note that the chain's contents may change as new blocks are processed or as a reorg occurs.
| RETURNS | DESCRIPTION |
|---|---|
Chain
|
The active chain view. |
Source code in pbk/chain.py
import_blocks
Import blocks from block files.
| PARAMETER | DESCRIPTION |
|---|---|
paths
|
List of filesystem paths to block files to import.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the import completed successfully, False otherwise. |
Source code in pbk/chain.py
process_block
Process and validate the passed in block with the chainstate manager.
Processing first does checks on the block, and if these passed, saves it to disk. It then validates the block against the utxo set. If it is valid, the chain is extended with it.
| PARAMETER | DESCRIPTION |
|---|---|
block
|
The block to process.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the block was not processed and saved to disk before, False otherwise. |
| RAISES | DESCRIPTION |
|---|---|
ProcessBlockException
|
If processing the block failed. Duplicate blocks do not throw. |