Skip to content

Commit

Permalink
refactor: do not use features expect two base_url instead, and improv…
Browse files Browse the repository at this point in the history
…e error handling

- refactor and update http client, url usage and methods
- refactor and add creation methods for cache struct, use specific method to build
  initial one
- add `get_block_header` method in http client, and refactor lib to use `bitcoin::BlockHeader`
  instead of custom mempool.space `BlockExtended` struct
- refactor lib and websocket to improve error handling, update some `unwrap()` usage and
  match errors for messages in `WebSocketStream`
- remove duplicated/unused deps, and use only necessary features
  • Loading branch information
oleonardolima committed Aug 9, 2022
1 parent 2b9e908 commit af3a572
Show file tree
Hide file tree
Showing 8 changed files with 387 additions and 441 deletions.
171 changes: 8 additions & 163 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 4 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
authors = ["Leonardo Souza <[email protected]>", "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"]
keywords = ["bitcoin", "blockchain", "blocks", "events", "mempool-space", "stream", "summer-of-bitcoin"]
readme = "README.md"
license = "MIT OR Apache-2.0"

Expand All @@ -15,29 +15,20 @@ async-stream = { version = "0.3.3"}
bitcoin = { version = "0.28", features = ["use-serde", "base64"] }
clap = { version = "3.0", features = ["derive"]}
env_logger = { version = "0.9.0" }
futures-core = { version = "0.3" }
futures-util = { version = "0.3" }
futures = { version = "0.3" }
log = { version = "0.4" }
reqwest = { version = "0.11.11" }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
tokio = { version = "1.19.2", features = ["io-util", "io-std", "macros", "net", "rt-multi-thread", "time"] }
tokio-stream = { version = "0.1.9" }
tokio = { version = "1.19.2", features = ["macros", "rt-multi-thread"] }
tokio-tungstenite = { version = "0.17.1", features = ["connect", "native-tls"]}
url = { version = "2.0.0" }
reqwest = { version = "0.11.11" }

[dev-dependencies]
testcontainers = { version = "^0.14.0" }
bitcoind = { version = "^0.26.1", features = ["22_0"] }
electrsd = { version = "^0.19.1", features = ["bitcoind_22_0", "electrs_0_9_1"] }
serial_test = { version = "0.7.0" }

[features]
default = ["mempool-backend"]
tls-secure = []
esplora-backend = []
mempool-backend = []

[lib]
name = "block_events"
path = "src/lib.rs"
Expand Down
44 changes: 32 additions & 12 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! All structs from mempool.space API
//! Also contains the main [`BlockEvent`]
use bitcoin::{Address, BlockHash, BlockHeader, TxMerkleNode};
use bitcoin::{Address, Block, BlockHash, BlockHeader, TxMerkleNode};

/// A structure that implements the equivalent `BlockExtended` type from mempool.space,
/// which is expected and parsed as response
Expand All @@ -19,27 +19,44 @@ pub struct BlockExtended {
pub id: BlockHash,
pub height: u32,
pub version: i32,
// none for genesis block
#[serde(alias = "previousblockhash")]
pub prev_blockhash: BlockHash,
pub prev_blockhash: Option<BlockHash>, // None for genesis block
pub merkle_root: TxMerkleNode,
#[serde(alias = "timestamp")]
pub time: u32,
pub bits: u32,
pub nonce: u32,
// add new fields if needed
}

// FIXME: (@leonardo.lima) Should this use serde_json or some other approach instead ?
impl From<BlockExtended> for BlockHeader {
fn from(extended: BlockExtended) -> BlockHeader {
fn from(extended: BlockExtended) -> Self {
BlockHeader {
version: (extended.version),
prev_blockhash: (extended.prev_blockhash),
merkle_root: (extended.merkle_root),
time: (extended.time),
bits: (extended.bits),
nonce: (extended.nonce),
version: extended.version,
prev_blockhash: extended
.prev_blockhash
.expect("Given `api::BlockExtended` does not have prev_blockhash field"),
merkle_root: extended.merkle_root,
time: extended.time,
bits: extended.bits,
nonce: extended.nonce,
}
}
}

impl From<Block> for BlockExtended {
fn from(block: Block) -> Self {
BlockExtended {
id: block.block_hash(),
height: block
.bip34_block_height()
.expect("Given `bitcoin::Block` does not have height encoded as bip34")
as u32,
version: block.header.version,
prev_blockhash: Some(block.header.prev_blockhash),
merkle_root: block.header.merkle_root,
time: block.header.time,
bits: block.header.bits,
nonce: block.header.nonce,
}
}
}
Expand All @@ -48,6 +65,9 @@ impl From<BlockExtended> for BlockHeader {
#[derive(serde::Deserialize, Debug)]
pub struct MempoolSpaceWebSocketMessage {
pub block: BlockExtended,
// pub mempool_info: MempoolInfo,
// pub da: DifficultyAdjustment,
// pub fees: RecommendedFee,
}

/// Structure that implements the standard fields for mempool.space WebSocket client message
Expand Down
Loading

0 comments on commit af3a572

Please sign in to comment.