Skip to content

Commit f2634fc

Browse files
committed
Change trait GetBlockHash to GetBlockInfo
It adds a new function get_block_headers to the original get_block_hash()
1 parent a147740 commit f2634fc

File tree

10 files changed

+52
-12
lines changed

10 files changed

+52
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
- Add `Excess` enum to handle remaining amount after coin selection.
1010
- Move change creation from `Wallet::create_tx` to `CoinSelectionAlgorithm::coin_select`.
1111
- Change the interface of `SqliteDatabase::new` to accept any type that implement AsRef<Path>
12+
- Change trait `GetBlockHash` to `GetBlockInfo`. A new function is added to the new trait `get_block_header` which expects a block height and returns corresponding block header. This is implemented on every blockchain backend.
1213

1314
## [v0.20.0] - [v0.19.0]
1415

src/blockchain/any.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,13 @@ impl GetTx for AnyBlockchain {
121121
}
122122

123123
#[maybe_async]
124-
impl GetBlockHash for AnyBlockchain {
124+
impl GetBlockInfo for AnyBlockchain {
125125
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error> {
126126
maybe_await!(impl_inner_method!(self, get_block_hash, height))
127127
}
128+
fn get_block_header(&self, height: u64) -> Result<BlockHeader, Error> {
129+
maybe_await!(impl_inner_method!(self, get_block_header, height))
130+
}
128131
}
129132

130133
#[maybe_async]

src/blockchain/compact_filters/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,19 @@ impl GetTx for CompactFiltersBlockchain {
260260
}
261261
}
262262

263-
impl GetBlockHash for CompactFiltersBlockchain {
263+
impl GetBlockInfo for CompactFiltersBlockchain {
264264
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error> {
265265
self.headers
266266
.get_block_hash(height as usize)?
267267
.ok_or(Error::CompactFilters(
268268
CompactFiltersError::BlockHashNotFound,
269269
))
270270
}
271+
fn get_block_header(&self, height: u64) -> Result<BlockHeader, Error> {
272+
self.headers
273+
.get_block_header(height as usize)?
274+
.ok_or(Error::CompactFilters(CompactFiltersError::InvalidHeaders))
275+
}
271276
}
272277

273278
impl WalletSync for CompactFiltersBlockchain {

src/blockchain/compact_filters/store.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@ impl ChainStore<Full> {
436436
}
437437

438438
pub fn get_block_hash(&self, height: usize) -> Result<Option<BlockHash>, CompactFiltersError> {
439+
match self.get_block_header(height)? {
440+
Some(header) => Ok(Some(header.block_hash())),
441+
None => Ok(None),
442+
}
443+
}
444+
445+
pub fn get_block_header(
446+
&self,
447+
height: usize,
448+
) -> Result<Option<BlockHeader>, CompactFiltersError> {
439449
let read_store = self.store.read().unwrap();
440450
let cf_handle = read_store.cf_handle(&self.cf_name).unwrap();
441451

@@ -444,7 +454,7 @@ impl ChainStore<Full> {
444454
data.map(|data| {
445455
let (header, _): (BlockHeader, Uint256) =
446456
deserialize(&data).map_err(|_| CompactFiltersError::DataCorruption)?;
447-
Ok::<_, CompactFiltersError>(header.block_hash())
457+
Ok::<_, CompactFiltersError>(header)
448458
})
449459
.transpose()
450460
}

src/blockchain/electrum.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,15 @@ impl GetTx for ElectrumBlockchain {
9898
}
9999
}
100100

101-
impl GetBlockHash for ElectrumBlockchain {
101+
impl GetBlockInfo for ElectrumBlockchain {
102102
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error> {
103103
let block_header = self.client.block_header(height as usize)?;
104104
Ok(block_header.block_hash())
105105
}
106+
fn get_block_header(&self, height: u64) -> Result<BlockHeader, Error> {
107+
let block_header = self.client.block_header(height as usize)?;
108+
Ok(block_header)
109+
}
106110
}
107111

108112
impl WalletSync for ElectrumBlockchain {

src/blockchain/esplora/reqwest.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,15 @@ impl GetTx for EsploraBlockchain {
118118
}
119119

120120
#[maybe_async]
121-
impl GetBlockHash for EsploraBlockchain {
121+
impl GetBlockInfo for EsploraBlockchain {
122122
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error> {
123123
let block_header = await_or_block!(self.url_client._get_header(height as u32))?;
124124
Ok(block_header.block_hash())
125125
}
126+
fn get_block_header(&self, height: u64) -> Result<BlockHeader, Error> {
127+
let block_header = await_or_block!(self.url_client._get_header(height as u32))?;
128+
Ok(block_header)
129+
}
126130
}
127131

128132
#[maybe_async]

src/blockchain/esplora/ureq.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,15 @@ impl GetTx for EsploraBlockchain {
112112
}
113113
}
114114

115-
impl GetBlockHash for EsploraBlockchain {
115+
impl GetBlockInfo for EsploraBlockchain {
116116
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error> {
117117
let block_header = self.url_client._get_header(height as u32)?;
118118
Ok(block_header.block_hash())
119119
}
120+
fn get_block_header(&self, height: u64) -> Result<BlockHeader, Error> {
121+
let block_header = await_or_block!(self.url_client._get_header(height as u32))?;
122+
Ok(block_header)
123+
}
120124
}
121125

122126
impl WalletSync for EsploraBlockchain {

src/blockchain/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::ops::Deref;
2121
use std::sync::mpsc::{channel, Receiver, Sender};
2222
use std::sync::Arc;
2323

24-
use bitcoin::{BlockHash, Transaction, Txid};
24+
use bitcoin::{BlockHash, BlockHeader, Transaction, Txid};
2525

2626
use crate::database::BatchDatabase;
2727
use crate::error::Error;
@@ -87,7 +87,7 @@ pub enum Capability {
8787

8888
/// Trait that defines the actions that must be supported by a blockchain backend
8989
#[maybe_async]
90-
pub trait Blockchain: WalletSync + GetHeight + GetTx + GetBlockHash {
90+
pub trait Blockchain: WalletSync + GetHeight + GetTx + GetBlockInfo {
9191
/// Return the set of [`Capability`] supported by this backend
9292
fn get_capabilities(&self) -> HashSet<Capability>;
9393
/// Broadcast a transaction
@@ -112,9 +112,11 @@ pub trait GetTx {
112112

113113
#[maybe_async]
114114
/// Trait for getting block hash by block height
115-
pub trait GetBlockHash {
115+
pub trait GetBlockInfo {
116116
/// fetch block hash given its height
117117
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error>;
118+
/// fetch block header given its height
119+
fn get_block_header(&self, height: u64) -> Result<BlockHeader, Error>;
118120
}
119121

120122
/// Trait for blockchains that can sync by updating the database directly.
@@ -367,10 +369,13 @@ impl<T: GetHeight> GetHeight for Arc<T> {
367369
}
368370

369371
#[maybe_async]
370-
impl<T: GetBlockHash> GetBlockHash for Arc<T> {
372+
impl<T: GetBlockInfo> GetBlockInfo for Arc<T> {
371373
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error> {
372374
maybe_await!(self.deref().get_block_hash(height))
373375
}
376+
fn get_block_header(&self, height: u64) -> Result<BlockHeader, Error> {
377+
maybe_await!(self.deref().get_block_header(height))
378+
}
374379
}
375380

376381
#[maybe_async]

src/blockchain/rpc.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,14 @@ impl GetHeight for RpcBlockchain {
169169
}
170170
}
171171

172-
impl GetBlockHash for RpcBlockchain {
172+
impl GetBlockInfo for RpcBlockchain {
173173
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error> {
174174
Ok(self.client.get_block_hash(height)?)
175175
}
176+
fn get_block_header(&self, height: u64) -> Result<BlockHeader, Error> {
177+
let block_hash = self.client.get_block_hash(height)?;
178+
Ok(self.client.get_block_header(&block_hash)?)
179+
}
176180
}
177181

178182
impl WalletSync for RpcBlockchain {

src/testutils/blockchain_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ macro_rules! bdk_blockchain_tests {
14191419
#[test]
14201420
fn test_get_block_hash() {
14211421
use bitcoincore_rpc::{ RpcApi };
1422-
use crate::blockchain::GetBlockHash;
1422+
use crate::blockchain::GetBlockInfo;
14231423

14241424
// create wallet with init_wallet
14251425
let (_, blockchain, _descriptors, mut test_client) = init_single_sig();

0 commit comments

Comments
 (0)