Skip to main content

Crate brk_types

Crate brk_types 

Source
Expand description

§brk_types

Bitcoin primitives shared across BRK and Bitview.

§What it provides

Types for heights, amounts, hashes, addresses, transactions, block templates and mempool state, including responses shared with brk_mempool such as MempoolInfo. Bitview’s index, value and state types (calendar indexes, protocol epochs, per-type address indexes, unit-typed scalars) live in bitview_primitives; query-protocol types and REST response DTOs live in bitview_types; TreeNode lives in bitview_catalog; CPFP linearization lives in brk_cpfp.

§Features

The default build has no vecdb, rawdb or schemars dependency, so BRK libraries such as brk_mempool and brk_rpc stay lean.

  • schemars adds the JSON Schema derives (and Version’s schema with storage) that Bitview’s API needs; bitview_primitives enables it.
  • storage adds byte/compression derives, vector traits, storage versions, and storage error conversions, for crates that persist domain values; catalog and API-only consumers leave it disabled.

Index names and aliases (Height, TxIndex), arithmetic (including CheckedSub), Serde, and JSON Schema (with schemars) remain available without storage. bitview_types::SeriesData::version and Rust client version endpoints expose the wire value as u32, not the storage engine’s Version type.

Verify the modes separately to avoid workspace feature unification hiding an accidental storage or schemars dependency:

cargo check -p brk_types --no-default-features
cargo check -p brk_types --features storage
cargo check -p brk_types --features schemars,storage
cargo tree -p bitview_catalog

§Example

ⓘ
use brk_types::Height;

let height = Height::new(840_000);
let reward = height.subsidy();
let blocks_left = height.left_before_next_halving();

Macros§

impl_float_ord_format
NaN-aware total order and formatting for a float newtype $t(float): NaN equals itself and orders first, and a non-finite value is written as JSON null / an empty CSV cell (an undefined value: Formattable::UNDEFINED).
impl_int_format
Display and Formattable for an integer newtype $t(int): the integer’s decimal digits.
impl_integer_index
The impls every integer index newtype (struct $t($inner)) shares: arithmetic with itself and usize, checked subtraction, conversions with $inner and usize (a usize is cast to $inner, so it must fit), and formatting through impl_int_format. Sentinels, other conversions, capacity and aliases stay on each type.
impl_store_value
Implements StoreValue for a newtype $t(...): a byte array is stored as is, an integer as its big-endian bytes.

Structs§

Address
Bitcoin address, or the hex public key of a P2PK output.
AddressMempoolStats
Address statistics in the mempool (unconfirmed transactions only), in mempool.space’s format.
AddressOutputType
The output type of an address: any OutputType but p2ms, op_return, empty_script and unknown_script. Parsing another type fails, naming the address types.
Bitcoin
An amount in bitcoin, as a decimal number (1 BTC = 100,000,000 satoshis).
BlkPosition
Position within a .blk file, encoding file index and byte offset
BlockHash
Double-SHA256 block-header hash, serialized in Bitcoin’s conventional hexadecimal byte order.
Cents
An amount in US cents (100 cents = 1 USD).
CentsSats
Cents × Sats (u128) - price in cents multiplied by amount in sats. Uses u128 because large amounts at any price can overflow u64.
CentsSigned
A signed amount in US cents (100 cents = 1 USD).
CoinbaseTag
Up to the first 100 bytes of a coinbase transaction’s first-input scriptSig. Bytes are preserved for storage and exposed as a string by mapping each byte to the same-valued Unicode code point. Pool attribution may search this raw value, but the value itself is not a normalized pool label.
ConfirmedCpfp
CPFP of a confirmed transaction with relatives in its block: its package there.
CpfpCluster
CPFP cluster: the connected component the seed belongs to, plus its linearization.
CpfpClusterChunk
One linearized chunk inside a CpfpCluster. txs follows CpfpCluster.txs order (topological); feerate is the chunk’s, shared by all its transactions.
CpfpClusterTx
CpfpClusterTxIndex
Position of a transaction inside a CpfpCluster.txs array. Cluster-local, has no meaning outside the enclosing cluster.
CpfpEntry
A transaction in a CPFP relationship.
CpfpInfo
CPFP (Child Pays For Parent) information for a mempool transaction.
Dollars
An amount in US dollars.
EmptyCpfp
CPFP of a transaction without known relatives: {"ancestors":[]}.
FeeRate
Fee rate stored in milli-sat/vB and exposed as sat/vB.
Height
A block height: how many blocks came before it.
Hex
Hex-encoded data as a JSON string.
Histogram
Fixed-length, log-scale bin histogram generic over the per-bin counter type: raw counts (u32) or the smoothed EMA (f64). Serializes as a flat JSON array of N values.
MempoolBlock
Block info in a mempool.space like format for fee estimation.
MempoolBlockContent
A projected mempool block with its transactions: block 0 is Bitcoin Core’s getblocktemplate selection. Returned by GET /api/mempool-blocks/{index}.
MempoolBlockDiff
A projected mempool block’s changes since a prior copy identified by since. Returned by GET /api/mempool-blocks/{index}/diff/{since}.
MempoolBlockHash
Content hash of a projected mempool block, including its statistics and complete transaction bodies. Opaque token, distinct from HTTP ETag formatting: pass back to GET /api/mempool-blocks/{index}/diff/{since} to follow changes. A decimal string, since JavaScript numbers can’t hold every 64-bit value.
MempoolBlockIndex
A projected mempool block’s position, as /api/v1/fees/mempool-blocks lists them: 0 is the next block, Bitcoin Core’s getblocktemplate selection.
MempoolEntryInfo
Mempool entry info from Bitcoin Core’s getmempoolentry.
MempoolInfo
Mempool statistics with incrementally maintained fee histogram.
MempoolRecentTx
Simplified mempool transaction for the /api/mempool/recent endpoint.
OutPoint
The output a transaction input spends: its transaction index and output position, written as {"tx_index": N, "vout": M} (coinbase inputs: {"tx_index": 4294967295, "vout": 4294967295}).
P2ABytes
The 2-byte witness program of a P2A (pay-to-anchor) output.
P2PK33Bytes
A compressed (33-byte) public key.
P2PK65Bytes
An uncompressed (65-byte) public key.
P2PKHBytes
The 20-byte public key hash of a P2PKH output.
P2SHBytes
The 20-byte script hash of a P2SH output.
P2TRBytes
The 32-byte output key of a P2TR output.
P2WPKHBytes
The 20-byte public key hash of a P2WPKH output.
P2WSHBytes
The 32-byte script hash of a P2WSH output.
PreciseRecommendedFees
Recommended fee rates in sat/vB, to the thousandth (/api/v1/fees/precise)
RawLockTime
Transaction locktime. Values below 500,000,000 are interpreted as block heights; values at or above are Unix timestamps.
ReadBlock
A block parsed from Core’s blk files, beside its raw bytes (txids hash from them without re-serializing) and where it sits in those files.
RecommendedFees
Recommended fee rates in whole sat/vB (/api/v1/fees/recommended)
RetainedRun
Consecutive transactions kept from the prior block, [start, count]: its positions start..start + count, in order.
Sats
An amount in satoshis (100,000,000 to a bitcoin).
SatsSigned
A signed amount in satoshis.
SigOpCount
Bitcoin Core’s CScript::GetSigOpCount, both modes in one walk: legacy counts every CHECKMULTISIG as 20, accurate as the OP_n right before it (20 without one). Every instruction sets that preceding opcode, signature opcodes included (rust-bitcoin’s accurate count keeps a stale OP_n across them). A malformed push ends the walk, as in Core.
SigOps
BIP-141 signature-operation cost: legacy and P2SH-redeem sigops count four and witness sigops one. Sums over many transactions or blocks use the full 64 bits.
SigOps32
A transaction’s BIP-141 signature-operation cost in 32 bits: the storage form of SigOps.
Timestamp
A Unix timestamp, in seconds.
Transaction
Transaction information compatible with mempool.space API format
TxIn
TxIndex
Chain-wide transaction index (0 = the genesis coinbase). For an in-block position, use BlockTxIndex instead.
TxOut
TxOutspend
TxStatus
TxVersionRaw
Raw transaction version: the protocol’s 32 bits read unsigned, as Esplora reports them. Unlike TxVersion (u8, indexed), this preserves non-standard values used in coinbase txs for miner signaling/branding.
Txid
A transaction ID: its hash.
TxidPrefix
First-8-bytes prefix of a txid, packed as a u64. Both From<&Txid> (via from_le_bytes) and StoreValue (via from_be_bytes, inverse of the to_be_bytes writer) are host-independent so on-disk keys are portable across architectures.
Unit
VSize
Virtual size in vbytes (weight / 4, rounded up). Max block vsize is ~1,000,000 vB.
Vin
Input index in the spending transaction
Vout
Index of the output being spent in the previous transaction (coinbase inputs: 4294967295)
Weight
Weight in weight units (WU): non-witness bytes count four and witness bytes one. A block holds at most 4,000,000 WU; sums over many blocks use the full 64 bits.
Weight32
A transaction’s or block’s weight in 32 bits: the storage form of Weight (a block holds at most 4,000,000 WU).

Enums§

AddressBytes
Cpfp
A transaction’s CPFP (Child Pays For Parent) answer, in mempool.space’s shapes, told apart by their fields (untagged on the wire): a mempool transaction’s package, a confirmed transaction’s package among its block’s transactions, or {"ancestors":[]} for an unknown transaction or a confirmed one without relatives in its block.
MempoolBlockDiffEntry
One stretch of the new block in a MempoolBlockDiff.
OutputType
An output’s script type (P2PKH, P2WPKH, P2SH, P2TR…).
OutputTypeNormalized
Output type names used by Esplora and mempool.space.

Constants§

ADDRESS_PAYLOAD_LENGTHS
Each address type with its payload length in bytes.
BLOCKS_PER_DIFF_EPOCH
BLOCKS_PER_HALVING
CPFP_CHAIN_LIMIT
Bitcoin Core’s default mempool ancestor/descendant chain cap, also used by mempool.space-style truncation in CPFP walks.
ONE_DAY_IN_SEC
ONE_DAY_IN_SEC_F64
ONE_HOUR_IN_SEC
TARGET_BLOCKS_PER_DAY
TARGET_BLOCKS_PER_DAY_F32
TARGET_BLOCKS_PER_DAY_F64

Traits§

CheckedSub
Checked subtraction for domain values; storage builds use vecdb’s identical trait instead.
StoreValue
Stable fixed-width encoding of a store key or value.

Functions§

weighted_percentiles
Vsize-weighted percentiles of a sorted (value, vsize) slice with Bitcoin Core’s getblockstats rank rule: for each percent p (0 to 100, nondecreasing), the first value whose cumulative vsize reaches p% of the total, in one walk.