bitcoin_explorer
is an efficient library for reading
bitcoin-core binary blockchain file as a database (utilising multi-threading).
This package uses pyO3. Thus is also available in python.
This library is designed for both python and rust.
For rust users, include bitcoin-explorer="0.1"
in Cargo.toml
.
For python users, pip install bitcoin-explorer
.
Currently, only macOS py39 wheels are provided.
For pip to build the package on other OS or python version,
make sure that rust
toolchain is installed,
and cmake
is also installed.
For rust users, go to Rust Documentation
For python documentation, go to Documentation.
This package deals with the binary file of another software Bitcoin Core
.
It might not be compatible with older Bitcoin Core versions.
Currently, it is compatible with Bitcoin Core version
Bitcoin Core version v0.21.1.0-g194b9b8792d9b0798fdb570b79fa51f1d1f5ebaf
Copyright (C) 2009-2020 The Bitcoin Core developers
.
The current performance bottleneck is disk IO. SSD allows faster performance.
On HDD, iterating from heights 600000 to 700000 takes less than 30 minutes.
Iterating through connected blocks
from 0 to 700000 takes 5 hours.
```rust use bitcoin_explorer::api::{BitcoinDB, FBlock}; use std::path::Path;
let path = Path::new("/Users/me/bitcoin").unwrap();
// launch without reading txindex let db = BitcoinDB::new(path, false).unwrap();
// get block of height 600000 let block: FBlock = db.getblockfull(600000).unwrap(); ```
```rust use bitcoin_explorer::api::{BitcoinDB, STransaction, Txid, FromHex}; use std::path::Path;
let path = Path::new("/Users/me/bitcoin").unwrap();
// !!must launch with txindex=true!! let db = BitcoinDB::new(path, true).unwrap();
// get transaction // e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468 let txidstr = "e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468"; let txid = Txid::fromhex(txidstr).unwrap(); let tx: STransaction = db.gettransaction_simple(&txid).unwrap(); ```
```rust use bitcoin_explorer::api::BitcoinDB; use std::path::Path;
let path = Path::new("/Users/me/bitcoin").unwrap();
// launch without reading txindex let db = BitcoinDB::new(path, false).unwrap();
// iterate over block from 600000 to 700000 for block in db.getblockfulliterseq(600000, 700000) { for tx in block.txdata { println!("do something for this transaction"); } } ```
```rust use bitcoin_explorer::api::BitcoinDB; use std::path::Path;
let path = Path::new("/Users/me/bitcoin").unwrap();
// launch without reading txindex let db = BitcoinDB::new(path, false).unwrap();
// iterate over block from 0 to 700000 for block in db.getblocksimpleconnectediter(700000) { for tx in block.txdata { println!("do something for this transaction"); } } ```
It contains one class BitcoinDB
.
```python import bitcoin_explorer as bex
--datadir
argument for bitcoind
.db = bex.BitcoinDB("~/Bitcoin")
db.getmaxheight()
db.get_block(1000)
db.get_block(1000, connected=True)
db.gethashfrom_height(1000)
db.getblockheader(1000)
db.getheightfrom_hash("some hash")
levelDB
each time, thus it is relatively slow.db.get_transaction("some txid")
db.getheightfrom_txid("some txid")
db.parse_script("some hex script pubic key")
for block in db.getblockiterrange(start=1000, end=2000): dosomething_with(block)
for block in db.getblockiterarray(heights=[1, 3, 5, 7, 9]): dosomething_with(block)
for block in db.getblockiterrange(end=700000, connected=True): dosomething_with(block) ```