Iterate over bitcoin blocks

Overview

MIT license Crates

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 max_reorg parameter)
  • Blocks come with metadata like all block's previous outputs, it allows computing transactions fee.

Memory requirements and performance

Running iterate example on threadripper 1950X, Testnet @ 1970k, Mainnet @ 681k

Network --skip--prevout Memory Time
Mainnet false 10.3GB 1h:00m
Mainnet true 2.7GB 36m
Testnet false 3.2GB 5m
Testnet true 1.0GB 3m

Examples

Run examples with:

cargo run --release --example iterate
  • iterate iterate over blocks and print block fee
  • heaviest find the transaction with greatest weight
  • most_output find the transaction with most output

Similar projects

  • bitcoin-iterate this project inspired blocks_iterator, the differences are:
    • bitcoin-iterate is C-based
    • bitcoin-iterate it's more suited for shell piping, while blocks_itearator is intended to use as a rust library
    • bitcoin-iterate doesn't give previous outputs information
    • bitcoin-iterate is making two passage over blocks*.dat while blocks_iterator is doing one pass keeping out-order-blocks in memory (the latter is faster at the expense of higher memory usage)
  • 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 specifically intended for the Electrum protocol
Comments
  • Why are blocks being read & deserialized twice?

    Why are blocks being read & deserialized twice?

    In read_detect it seems like you already have a deserialized block, and then you throw it away and return File and offsets outside to have to read and deserialize it again later. Why not just return that block out?

    opened by dpc 8
  • iterate API

    iterate API

    Use the Iterator trait instead of the model with the channel.

    Also extract code from verify example to something like par_iter( Fn(BlockExtra) -> Vec<T>, Fn(T) ) with the second function running in parallel

    opened by RCasatta 5
  • refactor: make `ReadDetect` into a resource

    refactor: make `ReadDetect` into a resource

    This is to show the pattern I always use for threads in Rust. Feel free to ignore if you don't like it.

    ReadDetect represents a thread/worker. Creating it starts it, eliminating the need for the caller to bother handling anything about it. (There's really no point in creating the ReadDetect without starting it).

    The returned value is a real handle. It will join automatically on a drop (methods could be added to change that behavior if desired).

    There's a little bit of right drift as the start was inlined, but it could be refactored out into a static function if desired.

    opened by dpc 4
  • UTXO serialization

    UTXO serialization

    blocks_iterator uses a truncated representation of the utxo set, saving a lot of memory, but if we want to serialize the UTXO set with this structure is impossible.

    The best approach is probably to have a UTXO Map in the end, at the cost of duplicating information and using more RAM it doesn't change too much the library. The alternative is to optionally use a non truncated representation of the utxo map, with callbacks to operate on it

    opened by RCasatta 4
  • Consider

    Consider "hashing tx" stage

    As shown in perf report here https://github.com/rust-bitcoin/bitcoin_hashes/issues/160

    in the memory case a lot of time is spent in hashing bitcoin tx, consider having a pipeline stage creating the hash of tx and storing it in the BlockExtra so that following stages could simply use the cached value

    breaking binary format 
    opened by RCasatta 2
  • Save blocks positions

    Save blocks positions

    The first read of the blocks*dat detects the position of the block inside the file, this shouldn't change across runs apart from the last dat. Cache the position of the blocks in a binary file so that reading for detecting is necessary only for the latest blocks. (use file length in the key so that dat files half full are read again)

    opened by RCasatta 2
  • Considering serializing in truncmap only values < u32::MAX

    Considering serializing in truncmap only values < u32::MAX

    https://github.com/RCasatta/blocks_iterator/blob/0785ebfc652a91e026ccf6d211f1d75c33b49aaa/src/truncmap.rs#L12

    Without checking I guess very few outputs use more than 4 bytes, so an idea could be to use the full map for values greater than u32::MAX so that every value in the truncmap saves 4 bytes

    opened by RCasatta 2
  • `PipeIterator` without writing to `stdout()`

    `PipeIterator` without writing to `stdout()`

    PipeIterator should have the option to not write to stdout, so that re-serialization is skipped. (like for example changing Stdout to Option<Stdout>) This is not only useful for last job in a pipechain but you can avoid re-serialization altogether using named pipes, for example running two blocks consumer:

    mkfifo second.fifo
    $ blocks_iterator .... | tee second.fifo | first_job &
    cat second.fifo | second_job
    
    opened by RCasatta 1
  • Create a main executable, with --pipe option

    Create a main executable, with --pipe option

    the pipe option will disable logging on standard out (move to stderr?) and prints BlockExtra serialized blocks into stdout, allowing Unix style composition

    opened by RCasatta 1
  • Calculate UTXO set per block before inserting/deleting global UTXO set

    Calculate UTXO set per block before inserting/deleting global UTXO set

    Since many TXO are spent in the same block they are created, a possible optimization may be to calculate the UTXO per block before inserting in the global UTXO map (which is bigger and due to the space optimization is doing 2 lookup for every get)

    opened by RCasatta 1
  • add verify example

    add verify example

    ~~In draft because it requires updates to rust_bitcoin to handle verify flags~~ released

    ~~also, I am using ENABLE_ENDOMORPHISM in rust_bitcoinconsensus branch~~ not anymore to use released version

    opened by RCasatta 1
  • NOTWORKING: use cfg bench instead of feature unstable

    NOTWORKING: use cfg bench instead of feature unstable

    attempt to fix #58

    :~/git/blocks_iterator(cfg_bench)$ RUSTFLAGS='--cfg=bench' cargo +nightly bench
       Compiling blocks_iterator v1.0.0 (/home/casatta/git/blocks_iterator)
    error[E0432]: unresolved import `sha2`
       --> src/lib.rs:287:9
        |
    287 |     use sha2::{Digest, Sha256};
        |         ^^^^ use of undeclared crate or module `sha2`
    
    error: unused imports: `DB`, `Options`, `WriteBatch`
       --> src/utxo/db.rs:184:19
        |
    184 |     use rocksdb::{Options, WriteBatch, DB};
        |                   ^^^^^^^  ^^^^^^^^^^  ^^
        |
    note: the lint level is defined here
       --> src/lib.rs:18:9
        |
    18  | #![deny(unused_imports)]
        |         ^^^^^^^^^^^^^^
    
    error: unused import: `test::Bencher`
       --> src/utxo/db.rs:185:9
        |
    185 |     use test::Bencher;
        |         ^^^^^^^^^^^^^
    
    error: unused imports: `HashEngine`, `Hash`, `sha256`
       --> src/lib.rs:284:34
        |
    284 |     use crate::bitcoin::hashes::{sha256, Hash, HashEngine};
        |                                  ^^^^^^  ^^^^  ^^^^^^^^^^
    
    error: unused import: `crate::bitcoin::OutPoint`
       --> src/lib.rs:285:9
        |
    285 |     use crate::bitcoin::OutPoint;
        |         ^^^^^^^^^^^^^^^^^^^^^^^^
    
    error: unused import: `bitcoin::hashes::sha256::Midstate`
       --> src/lib.rs:286:9
        |
    286 |     use bitcoin::hashes::sha256::Midstate;
        |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    error: unused imports: `Bencher`, `black_box`
       --> src/lib.rs:288:16
        |
    288 |     use test::{black_box, Bencher};
        |                ^^^^^^^^^  ^^^^^^^
    
    For more information about this error, try `rustc --explain E0432`.
    
    opened by RCasatta 0
  • Dump UTXO set binary

    Dump UTXO set binary

    After https://github.com/RCasatta/blocks_iterator/commit/86da242ba8ac34c079e6002ae6ad6f59b70b9012 the database contains data to compute the UTXO set at the height of the last run.

    Create a binary bin/utxo_set.rs dumping the utxo set in a format like

    Vec<OutPoint, TxOut>

    plus height and a hash matching the one returned from bitcoin-cli gettxoutsetinfo

    opened by RCasatta 2
  • Bench as a standalone iterator step.

    Bench as a standalone iterator step.

    That's why I had benching as a standalone iterator step.

    https://github.com/dpc/block-iter/blob/40578bb5eca918e7570b8ab82370ee88cebb7a9f/libs/main/src/bench.rs#L6

    so one can insert .bench_x() at the end of a middle of an iterator chain and get stats. Though I now see that I forgot to make the bench_x be an iterator that returns the items it's benching, so right now it only works at the end, as a sort of of a .for_each() or .count().

    Originally posted by @dpc in https://github.com/RCasatta/blocks_iterator/issues/42#issuecomment-1107527823

    opened by RCasatta 0
  • On performance and memory usage

    On performance and memory usage

    I'm moving discussion from https://github.com/rust-bitcoin/rust-bitcoin/issues/595 here since I think this might be an ongoing discussion, and people might find it useful, and feels like we're off topic for a long time now.

    opened by dpc 25
Owner
Riccardo Casatta
Riccardo Casatta
Minimal Bitcoin wallet intended for teaching rust-bitcoin

Insanely minimal Bitcoin wallet intended for demonstration of Rust Bitcoin ecosystem Absolutely DO NOT use with mainnet funds!!! No privacy - address

Martin Habovštiak 4 May 5, 2023
Modern, lightweight & standard-compliant bitcoin wallet runtime & cli without rust-bitcoin dependencies

Bitcoin protocol command-line wallet & tools Modern, minimalistic & standard-compliant cold wallet from LNP/BP Standards Association. Contributing Con

BP: Bitcoin protocol 3 Jul 31, 2023
Rust-native building blocks for the Cardano blockchain ecosystem

Pallas Rust-native building blocks for the Cardano blockchain ecosystem. Introduction Pallas is an expanding collection of modules that re-implements

null 78 Dec 6, 2022
Common protocol for generating ZK proofs for blocks on different blockchains.

Proof Protocol Decoder A flexible protocol that clients (eg. full nodes) can use to easily generate block proofs for different chains. Specification I

Polygon Zero 3 Oct 5, 2023
A "Type 0" zkEVM. Prove validity of Ethereum blocks using RISC Zero's zkVM

zeth NEW: Zeth now supports Optimism blocks! Just pass in --network=optimism! Zeth is an open-source ZK block prover for Ethereum built on the RISC Ze

RISC Zero 222 Oct 26, 2023
Ethereum (and Ethereum like) indexer using P2P message to fetch blocks and transactions

Ethereum P2P indexer This project is an indexer for Ethereum and Ethereum forks. It takes advantage of the ETH (Ethereum Wire Protocol) to fetch block

null 5 Nov 10, 2023
A Rust library for working with Bitcoin SV

Rust-SV A library to build Bitcoin SV applications in Rust. Documentation Features P2P protocol messages (construction and serialization) Address enco

Brenton Gunning 51 Oct 13, 2022
The Parity Bitcoin client

The Parity Bitcoin client. Gitter Installing from source Installing the snap Running tests Going online Importing bitcoind database Command line inter

Parity Technologies 714 Dec 21, 2022
EXPERIMENTAL: Bitcoin Core Prometheus exporter based on User-Space, Statically Defined Tracing and eBPF.

bitcoind-observer An experimental Prometheus metric exporter for Bitcoin Core based on Userspace, Statically Defined Tracing and eBPF. This demo is ba

0xB10C 24 Nov 8, 2022
Rust command line program for Bitcoin brainwallet

brainwallet Rust command line program for Bitcoin brainwallet as implemented at https://www.bitaddress.org. To run it, first install Rust, e.g. from h

Frank Buss 3 Dec 30, 2021
Rustcoin - A LightWeight SDK For Bitcoin, Ethernum

Rustcoin - A LightWeight SDK For Bitcoin, Ethernum

Rust Coin 6 Jan 24, 2022
The Stage 2 building block to reach the dream of DSNs with Bitcoin DeFi

rust-nostr Intro A complete suite of nostr Bitcoin libraries that can be used to develop Decentralized Social Networks (DSN) with integrated Bitcoin f

Raj 82 Jan 2, 2023
A preview of the integration between Bitcoin and the Internet Computer.

Bitcoin Integration Developer Preview Overview The integration between the Internet Computer and Bitcoin will enable developers to build canisters tha

DFINITY 39 Sep 21, 2022
Library with support for de/serialization, parsing and executing on data-structures and network messages related to Bitcoin

Rust Bitcoin Library with support for de/serialization, parsing and executing on data-structures and network messages related to Bitcoin. Heads up for

Rust Bitcoin Community 1.3k Dec 29, 2022
interBTC A trust-minimized bridge from Bitcoin to Polkadot.

interBTC A trust-minimized bridge from Bitcoin to Polkadot. Explore the specification » Report Bug · Request Feature This repository is hosted on GitH

Interlay 192 Dec 27, 2022
A demo of the Internet Computer's Bitcoin API

Bitcoin Integration Demo A demo of the bitcoin endpoints on the Internet Computer. This demo is already deployed to the IC, so you can already try it

Islam El-Ashi 8 Jul 2, 2022
A package that has a collection of unspent p2wsh bitcoin transactions.

P2WSH-UTXO A package that has a collection of unspent p2wsh transactions. Useful to see what are potential unspent multisig transactions. Library Crea

Tony Giorgio 3 Oct 22, 2022
A Bitcoin wallet collider that brute forces random wallet addresses written in Rust.

Plutus-Rustus Bitcoin Brute Forcer A Bitcoin wallet collider that brute forces random wallet addresses written in Rust. This is a straight port of Plu

null 46 Dec 23, 2022
Low-level Bitcoin P2P Network Client

Peerlink What is Peerlink? Peerlink is a low-level network client for the Bitcoin P2P network written in Rust. It uses a nonblocking reactor to accept

Alfred Hodler 6 Dec 23, 2022