This project is a command-line interface (CLI) application written in Rust that connects to the Bitcoin peer-to-peer network. It provides a real-time, terminal-based user interface (TUI) to monitor network activity, connected peers, and log messages.
- Bitcoin P2P Network Connection: Connects to Bitcoin nodes using the standard P2P protocol.
- Peer Discovery: Utilizes DNS seeds to discover initial peers and exchanges peer addresses with connected nodes.
- Handshake Protocol: Implements the Bitcoin P2P handshake, including sending and receiving
versionandverackmessages. - Real-time Monitoring: Displays connected peers, their inbound/outbound traffic, and connection status.
- Log Viewer: Shows a live stream of network events, connection status, and errors in a scrollable log.
- TUI Interface: Provides an interactive terminal user interface for monitoring and basic control.
- Graceful Shutdown: Handles
Ctrl+Csignals for a clean exit, saving peer data. - Persistence: Stores known peer information and traffic statistics to a local file (
peers.json). - Message Handling: Processes common P2P messages like
version,verack,ping,pong,mempool,inv,tx,block,headers,getheaders,getdata, andaddr.
- Rust Toolchain: Ensure you have Rust and Cargo installed. You can install them from rustup.rs.
-
Clone the repository:
git clone https://github.com/gnostr-org/gnostr-bitcoin.git cd gnostr-bitcoin -
Build the application:
cargo build --release
After building, you can run the application using Cargo or directly execute the binary:
-
Using Cargo:
cargo run --release
-
Directly executing the binary: The binary will be located in
target/release/../target/release/gnostr-bitcoin
The application launches into a TUI that displays the following information:
- Block Height & Hash: Shows the current block height and hash of the connected peer.
- Instructions: Provides basic keybindings for navigation and control.
- Log: Displays real-time messages, connection events, and errors.
- Navigation: Use
Up/Downarrow keys to scroll through the log. PressEnterto auto-scroll to the bottom. - Visibility: Press
lto toggle the visibility of the log panel.
- Navigation: Use
- Peers: Lists connected peers, their total inbound/outbound traffic, and connection time.
- Visibility: Press
pto toggle the visibility of the peer list panel. - Sorting: Peers are sorted by inbound traffic (descending) and then by connection time (ascending).
- Visibility: Press
q: Quit the application.Tab: Cycle focus between UI elements (Block Height -> Instructions -> Log -> Peers -> Block Height).Up/Down: Scroll the log panel.Enter: Auto-scroll the log to the bottom.l: Toggle the visibility of the log panel.p: Toggle the visibility of the peer list panel.Esc: Enable auto-scrolling for the log if manual scrolling was used.
src/lib.rs: Contains core Bitcoin P2P protocol logic, message building, connection handling, and utility functions.src/main.rs: The application's entry point, responsible for setting up threads, managing shared state, initializing the TUI, and running the main application loop.src/ui.rs: Implements the terminal user interface using theratatuicrate, handling rendering and user input.Cargo.toml: Defines project dependencies and metadata.README.md: This file.
This project relies on several external crates, including:
anyhow: For flexible error handling.log&simplelog: For logging.sha2: For SHA-256 hashing (used for checksums).dirs: To find user-specific directories for configuration and logs.ctrlc: To handle Ctrl+C signals.serde&serde_json: For serializing/deserializing peer data.crossterm&ratatui: For building the terminal user interface.time: For timestamp formatting.
Contributions are welcome! Please refer to the project's issue tracker or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.