Skip to content

Commit

Permalink
chore: update README, remove trailing spaces, fix deps usage
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Lima <[email protected]>
  • Loading branch information
oleonardolima committed Jun 2, 2022
1 parent a227856 commit 21ff758
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 55 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
name = "block-events"
version = "0.1.0"
edition = "2021"
authors = ["Leonardo Souza <leonardo.lima@gmail.com>", "LLFourn <[email protected]>"]
repository = "https://github.com/oleonardolima/block-explorer-cli"
description = "A real-time stream block events library, covering connected and disconnected blocks."
keywords = ["bitcoin", "blockchain", "blocks", "mempool-space", "stream", "events"]
authors = ["Leonardo Souza <leonardolimasza@gmail.com>", "LLFourn <[email protected]>"]
repository = "https://github.com/oleonardolima/block-events"
description = "A real-time stream block events library, covering connected and disconnected blocks.\nThis a work in progress project for Summer of Bitcoin 2022."
keywords = ["bitcoin", "blockchain", "blocks", "mempool-space", "stream", "events", "summer-of-bitcoin"]
readme = "README.md"
license = "MIT OR Apache-2.0"

Expand All @@ -25,9 +25,9 @@ tokio-tungstenite = { version = "0.17.1", features = ["connect", "native-tls"]}
url = { version = "2.0.0" }

[lib]
name = "block_explorer_cli"
name = "block_events"
path = "src/lib.rs"

[[bin]]
name = "block-explorer-cli"
name = "block-events-cli"
path = "src/bin.rs"
97 changes: 62 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,79 @@
# A terminal block explorer for mempool.space websocket

A terminal block explorer exposing the features available on [mempool.space websocket API](https://mempool.space/docs/api/websocket).

Currently available features are:
- All data feed from mempool.space for: blocks, mempool-blocks, live-2h-chart, and stats.
- Subscription for address related data: track-address.
# Real-time stream of block events library

A library for consuming and subscribing to new block events in real-time from different sources:
- [ ] mempool.space - [WebSocket](https://mempool.space/docs/api/websocket) and [REST](https://mempool.space/docs/api/rest) APIs (under development)
- [ ] bitcoin core RPC `#TODO`
- [ ] bitcoin P2P `#TODO`

It's useful for projects to get notified for connected and disconnected new blocks, currently using the following as output in async manner:
``` rust
pub enum BlockEvent {
Connected(BlockExtended),
Disconnected((u32, BlockHash)),
Error(),
}
```

<br>
Can be also used through command-line interface (CLI), as `block-explorer-cli` and just passing the network: Regtest, Signet, Testnet and Mainnet.

> **NOTE**: The previous implemented track-address feature and other data, such as: mempool-blocks, stats... has been deprecated and it's not refactored yet.
## Requirements:

To use this CLI you must have rust and cargo installed in your computer, you can check it with:
To use the library as CLI or to contribute you must have rust and cargo installed in your computer, you can check it with:

``` sh
# check rust version, it should return its version if installed
rustc --version
# check cargo version, it should return its version if installed
cargo --version
```

If you do not have it installed, you can follow this tutorial from [The Rust Programming Language book](https://doc.rust-lang.org/book/ch01-01-installation.html)


<br>

## How to use:

If you have cargo and rust installed, you can run the following commands:

## Compiling and using the CLI:
To compile and use it as a command in terminal with no need of cargo, you can use the following command:
``` sh
# mainnet connection is default
cargo run -- track-address -a <your-btc-address>

# to use testnet
cargo run -- --endpoint mempool.space/testnet/api track-address -a <your-btc-address>
# from inside this repo
cargo install --path .
```

## Examples:
### Consuming new block events through the CLI:
``` sh
# all feed [blocks, mempool-blocks, live-2h-chart, and stats] for mainnet:
cargo run -- blocks-data

# or all feed [blocks, mempool-blocks, live-2h-chart, and stats] for testnet:
cargo run -- --endpoint mempool.space/testnet/api blocks-data
```
# testnet connection is set by default
cargo run -- data-stream --blocks

## Compiling and using:
To compile and use it as a command in terminal with no need of cargo, you can use the following command:

``` sh
cargo install --path .
# to use regtest, you need to pass it as a parameter
cargo run -- --network testnet data-stream --blocks
```
### Subscribing and consuming new block events through the lib:
``` rust
use anyhow::{self, Ok};
use bitcoin::Network;
use block_events::{fetch_data_stream, BlockEvent, MempoolSpaceWebSocketRequestData};
use futures_util::{ pin_mut, StreamExt};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// for regtest network
let network = Network::Regtest;
// for new blocks events
let blocks_data = MempoolSpaceWebSocketRequestData::Blocks;
// async fetch the data stream through the lib
let block_events_stream = fetch_data_stream(&network, &blocks_data).await?;

// consume and execute the code (current matching and printing) in async manner for each new block-event
pin_mut!(block_events_stream);
while let Some(block_event) = block_events_stream.next().await {
match block_event {
BlockEvent::Connected(block) => {
println!("Connected BlockEvent: {:#?}", block);
},
BlockEvent::Disconnected((height, block_hash)) => {
println!("Disconnected BlockEvent: [height {:#?}] [block_hash: {:#?}]", height, block_hash);
}
BlockEvent::Error() => {
eprint!("ERROR BlockEvent: received an error from the block-events stream");
}
}
};
Ok(())
}
```
17 changes: 7 additions & 10 deletions src/bin.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use bitcoin::{Address, Network};
use block_explorer_cli::BlockEvent;
use block_explorer_cli::{fetch_data_stream, MempoolSpaceWebSocketRequestData};
use block_events::{fetch_data_stream, BlockEvent, MempoolSpaceWebSocketRequestData};
use clap::{ArgGroup, Parser, Subcommand};
use futures_util::pin_mut;
use futures_util::StreamExt;
use futures_util::{pin_mut, StreamExt};
use serde::{Deserialize, Serialize};
use std::str::FromStr;

#[derive(Parser)]
#[clap(name = "CLI block explorer with mempool.space websocket - WIP")]
#[clap(author = "Leonardo L.")]
#[clap(version = "0.1")]
#[clap(about = "A work in progress CLI block explorer to be used with BDK, consuming data from mempool.space websocket.\n
This an initial competency test for Summer of Bitcoin 2022",
long_about = None)]
#[clap(name = "block-events-cli")]
#[clap(author = "Leonardo Souza <[email protected]>, LLFourn <[email protected]>")]
#[clap(version = "0.1.0")]
#[clap(long_about = "A CLI interface and tool to use with the block-events library.\n
This a work in progress project for Summer of Bitcoin 2022.")]

struct Cli {
#[clap(subcommand)]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod mempool_space;

pub use mempool_space::{subscribe_to_new_blocks, build_websocket_request_message};
pub use mempool_space::api::{MempoolSpaceWebSocketMessage, MempoolSpaceWebSocketRequestData, BlockEvent};

use anyhow::{anyhow};
use bitcoin::Network;
use futures_core::Stream;
Expand Down
8 changes: 4 additions & 4 deletions src/mempool_space/websocket.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use super::api::{MempoolSpaceWebSocketMessage, MempoolSpaceWebSocketRequestMessage, BlockEvent};

use anyhow::{anyhow, Ok};
use async_stream::stream;
use futures_util::{SinkExt, StreamExt};
use futures_util::stream::Stream;
use std::{time::Duration};
use super::api::{MempoolSpaceWebSocketMessage, MempoolSpaceWebSocketRequestMessage, BlockEvent};
use tokio_tungstenite::connect_async_tls_with_config;
use tokio_tungstenite::tungstenite::protocol::Message;
use url::Url;

use async_stream::stream;
use futures_util::stream::Stream;

pub async fn connect_and_publish_message(url: Url, message: &MempoolSpaceWebSocketRequestMessage) -> anyhow::Result<impl Stream<Item = BlockEvent>> {
let (mut websocket_stream, websocket_response) =
connect_async_tls_with_config(&url, None, None)
Expand Down

0 comments on commit 21ff758

Please sign in to comment.