| Crates.io | regtest-block-explorer |
| lib.rs | regtest-block-explorer |
| version | 0.1.1 |
| created_at | 2025-12-09 15:19:04.466085+00 |
| updated_at | 2025-12-09 15:26:19.085345+00 |
| description | A CLI tool for indexing Bitcoin regtest blocks via RPC and serving a block explorer API. |
| homepage | |
| repository | https://github.com/Macnelson9/Rust_Project |
| max_upload_size | |
| id | 1975640 |
| size | 1,280,376 |
A command-line tool for indexing and exploring Bitcoin regtest blocks. It allows users to fetch blocks from a running Bitcoin regtest node via RPC or parse them from local .blk files, store them in a SQLite database, and serve block/transaction data via a RESTful web API.
.blk files.bitcoin-explore index to populate the database, then bitcoin-explore serve to start the API server.This tool is ideal for developers testing Bitcoin applications or exploring blockchain data without needing a full mainnet node.
bitcoin crate for parsing and handling Bitcoin data structures.clap for command-line argument handling.The architecture is modular: separate modules for database operations, data models, web handlers, and block parsing. This keeps the code organized and testable.
bitcoin-explore/
├── Cargo.toml # Project metadata, dependencies, and build configuration
├── README.md # This file
└── src/
├── main.rs # CLI entry point: defines commands and starts server
├── lib.rs # Module declarations (db, models, parser, handlers)
├── db.rs # Database functions: init, insert, query operations
├── models.rs # Data structures: BlockResponse, TxResponse, etc.
├── handlers.rs # Web API handlers: functions for each endpoint
└── parser.rs # Block parsing: reads .blk files and extracts data
Cargo.toml: Defines the project name (bitcoin-explore), version, dependencies (e.g., actix-web, bitcoin, rusqlite), and metadata for crates.io publishing.src/main.rs: The main binary. Uses clap to parse CLI arguments into subcommands (index, serve). Handles RPC indexing or file parsing, then starts the web server with routes.src/lib.rs: Declares public modules (db, models, parser, handlers) for reuse across the project.src/db.rs: Manages SQLite database. Functions include init_db (creates tables), insert_block/insert_tx (stores data), and various query_* functions (retrieves data).src/models.rs: Defines structs for API responses (e.g., BlockResponse, StatsResponse) and internal data (e.g., BlockSummary). Uses Serde for JSON serialization.src/handlers.rs: Contains async functions for each API endpoint. Each handler locks the database, queries data, and returns JSON responses.src/parser.rs: Parses Bitcoin blocks from .blk files. Reads file streams, checks magic bytes, and deserializes blocks using the bitcoin crate.Once published:
cargo install regtest-block-explorer
git clone https://github.com/Macnelson9/Rust_Project.git
cd Rust_Project
cargo build --release
cargo test
The tool has two main commands: index (to populate the database) and serve (to start the API).
~/.bitcoin/regtest/blocks/blk00000.dat).Fetches and stores block data in the database.
Syntax:
bitcoin-explore index [--from-file <PATH>]
Options:
--from-file <PATH>: (Optional) Path to the directory containing .blk files (e.g., /home/user/.bitcoin/regtest/blocks). If omitted, uses RPC from a local regtest node.Examples:
RPC mode (requires running regtest node on port 18443):
regtest-block-explorer index
http://127.0.0.1:18443 via RPC, fetches all blocks, and indexes them. Useful for live data from a node.File mode:
regtest-block-explorer index --from-file ~/.bitcoin/regtest/blocks
.blk files directly. No node required; faster for existing data, but data must be available locally.Output: Progress messages (e.g., "Indexed block at height X"). Creates/updates blocks.db in the current directory.
Starts the web server to query indexed data.
Syntax:
regtest-block-explorer serve [--port <PORT>]
Options:
--port <PORT>: (Optional) Port to run the server on (default: 8080).Example:
regtest-block-explorer serve --port 3000
http://127.0.0.1:<PORT>. Requires prior indexing; serves data from blocks.db.Output: Lists available endpoints and starts listening. Use Ctrl+C to stop.
bitcoind -regtest -rpcuser=user -rpcpassword=pass -rpcport=18443 -datadir=$HOME/.bitcoin
regtest-block-explorer index
regtest-block-explorer serve
curl http://127.0.0.1:8080/stats
All endpoints return JSON. Run regtest-block-explorer serve to start the server.
GET /block/{hash}: Get a block by its hash.
curl http://127.0.0.1:8080/block/00000000...GET /block/height/{height}: Get a block by height.
curl http://127.0.0.1:8080/block/height/0GET /tx/{txid}: Get a transaction by ID.
curl http://127.0.0.1:8080/tx/abcdef...GET /blocks/latest?limit=10: Get the latest blocks.
limit (default: 10, max: 100).curl "http://127.0.0.1:8080/blocks/latest?limit=5"GET /stats: Get blockchain statistics.
curl http://127.0.0.1:8080/statsGET /health: Health check.
curl http://127.0.0.1:8080/health{"status": "healthy", "service": "block-explorer-backend"}GET /blocks?page=1&limit=20: Get all blocks with pagination.
page (default: 1), limit (default: 20, max: 100).curl "http://127.0.0.1:8080/blocks?page=2&limit=10"git checkout -b feature-name.cargo test.For issues or suggestions, open an issue on GitHub.
This project is licensed under the MIT License or Apache-2.0 (see LICENSE file).
For beginners: This tool introduces Rust concepts like async programming, database interactions, and web APIs. Explore the code to learn more!