Skip to content

Commit a4b3bcc

Browse files
committed
feat(api): create BlockInformation struct
Since `f64` does not implement `PartialEq` (due to `NaN != NaN`), it cannot be derived, making it necessary to implement it explicitly.
1 parent cd4749b commit a4b3bcc

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

src/api.rs

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
//!
33
//! See: <https://github.com/Blockstream/esplora/blob/master/API.md>
44
5+
use bitcoin::hash_types;
6+
use serde::Deserialize;
7+
58
pub use bitcoin::consensus::{deserialize, serialize};
69
pub use bitcoin::hex::FromHex;
7-
use bitcoin::Weight;
810
pub use bitcoin::{
9-
transaction, Amount, BlockHash, OutPoint, ScriptBuf, ScriptHash, Transaction, TxIn, TxOut,
10-
Txid, Witness,
11+
absolute, block, transaction, Amount, BlockHash, OutPoint, ScriptBuf, ScriptHash, Transaction,
12+
TxIn, TxOut, Txid, Weight, Witness,
1113
};
1214

13-
use serde::Deserialize;
14-
1515
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
1616
pub struct PrevOut {
1717
pub value: u64,
@@ -88,6 +88,59 @@ pub struct BlockTime {
8888
pub height: u32,
8989
}
9090

91+
/// Information about a [`Block`].
92+
#[derive(Debug, Clone, Deserialize)]
93+
pub struct BlockInformation {
94+
/// The block's [`BlockHash`].
95+
pub id: BlockHash,
96+
/// The block's height.
97+
pub height: u32,
98+
/// The block's version.
99+
pub version: block::Version,
100+
/// The block's timestamp.
101+
pub timestamp: u64,
102+
/// The block's transaction count.
103+
pub tx_count: u64,
104+
/// The block's size, in bytes.
105+
pub size: u64,
106+
/// The block's weight, in virtual bytes.
107+
pub weight: u64,
108+
/// The merkle root of the transactions in the block.
109+
pub merkle_root: hash_types::TxMerkleNode,
110+
/// The [`BlockHash`] of the previous block (`None`) for the genesis block.
111+
pub previousblockhash: Option<BlockHash>,
112+
/// The block's MTP (Median Time Past).
113+
pub mediantime: u64,
114+
/// The block's nonce value.
115+
pub nonce: u32,
116+
/// The block's `bits` value (`bits` is a compact representation of the target).
117+
pub bits: u32,
118+
/// The block's difficulty target value.
119+
pub difficulty: f64,
120+
}
121+
122+
impl PartialEq for BlockInformation {
123+
fn eq(&self, other: &Self) -> bool {
124+
let Self { difficulty: d1, .. } = self;
125+
let Self { difficulty: d2, .. } = other;
126+
127+
self.id == other.id
128+
&& self.height == other.height
129+
&& self.version == other.version
130+
&& self.timestamp == other.timestamp
131+
&& self.tx_count == other.tx_count
132+
&& self.size == other.size
133+
&& self.weight == other.weight
134+
&& self.merkle_root == other.merkle_root
135+
&& self.previousblockhash == other.previousblockhash
136+
&& self.mediantime == other.mediantime
137+
&& self.nonce == other.nonce
138+
&& self.bits == other.bits
139+
&& ((d1.is_nan() && d2.is_nan()) || (d1 == d2))
140+
}
141+
}
142+
impl Eq for BlockInformation {}
143+
91144
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
92145
pub struct BlockSummary {
93146
pub id: BlockHash,

0 commit comments

Comments
 (0)