[](https://github.com/RCasatta/blocks_iterator/blob/master/LICENSE)
[](https://crates.io/crates/blocks_iterator)
[](https://docs.rs/blocks_iterator)
# Blocks iterator
Iterates over Bitcoin blocks, decoding data inside Bitcoin Core's blocks directory.
Features:
* Blocks are returned in height order, it avoids following reorgs (see [`Config::max_reorg`] parameter)
* Blocks come with extra data [`BlockExtra`] like all block's previous outputs, it allows computing
transactions fee or [verifying](https://github.com/RCasatta/blocks_iterator/blob/master/cli/examples/verify.rs)
scripts in the blockchain.
Note:
Bitcoin Core 28.0 introduced xoring of bitcoin blocks and this project doesn't yet support reading the blocks directory when xored. You can disable xoring in core via `-blocksxor=0`.
## Iteration modes
### In rust programs
Used as a library blocks could be iterated via the [`iter()`] method like:
```rust
// "blocks" dir contains first 400 testnet blocks
let conf = blocks_iterator::Config::new("../blocks", bitcoin::Network::Testnet);
let mut total_fee = 0u64;
for b in blocks_iterator::iter(conf) {
total_fee += b.fee().expect("fee available cause we are keeping prevouts");
}
// Only a bunch of tx with fee exists on testnet with height < 400
// in blocks: 385, 387, 389, 390, 392, 394
assert_eq!(total_fee, 450_000u64);
```
When the task to be performed is computational costly, like verifying spending conditions, it is
suggested to parallelize the execution like it's done with rayon (or similar) in the
[verify](https://github.com/RCasatta/blocks_iterator/blob/master/cli/examples/verify.rs) example
(note `par_bridge()` call).
### Through Pipes
Other than inside Rust programs, ordered blocks with previous outputs could be iterated using Unix pipes.
```sh
$ cargo build --release
$ cargo build --release --examples
Run examples with:
```sh
cargo run --release --example verify
```
* [heaviest](cli/examples/heaviest_pipe.rs) find the transaction with greatest weight
* [most_output](cli/examples/most_output_pipe.rs) find the transaction with most output
* [outputs_versions](cli/examples/outputs_versions.rs) Count outputs witness version
* [signatures_in_witness](cli/examples/signatures_in_witness.rs) Count signatures in witness
* [verify](cli/examples/verify.rs) verify transactions in blocks using libbitcoin-consensus. Consumers are run in parallel fashion.
## Version 1.0 meaning
The `1.0` is not to be intended as *battle-tested production-ready* library, the binary format of
`BlockExtra` changed and I wanted to highlight it with major version rollout.
## Similar projects
* [bitcoin-iterate](https://github.com/rustyrussell/bitcoin-iterate) this project inspired blocks_iterator, the differences are:
* It is C-based
* It's more suited for shell piping, while blocks_iterator can be used with piping but also as a rust library
* It doesn't give previous outputs information
* It is making two passage over `blocks*.dat` serially, while blocks_iterator is doing two passes in parallel.
* [rust-bitcoin-indexer](https://github.com/dpc/rust-bitcoin-indexer) this project requires longer setup times (about 9 hours indexing) and a postgre database, once the indexing is finished it allows fast queries on relational database.
* [electrs](https://github.com/romanz/electrs) specifically intended for the Electrum protocol
## MSRV
Check minimum rust version run in CI, as of Aug 2023 is:
* `1.60.0` without features (needs some pinning, check CI)
* `1.67.0` with features.