Skip to content

Commit 93c515c

Browse files
Merge #6: Add get_tip_hash API call
297172d Add `get_tip_hash` API call (Elias Rohrer) Pull request description: This PR adds support for getting the `BlockHash` of the current blockchain tip. While this is currently not strictly necessary for LDK integration, it might be nice to have in the future. Top commit has no ACKs. Tree-SHA512: 07c0d37542f37fc3fc2b8dec6c0f5222e1e39ede7e0601d2e0bbbcb961282284ed34ad4df00f86f2715ba5a9ad80ac204ec1408eaeb35e62608f7e0f5ee8ffb3
2 parents 1b15ba9 + 297172d commit 93c515c

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/async.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
//! Esplora by way of `reqwest` HTTP client.
1313
1414
use std::collections::HashMap;
15+
use std::str::FromStr;
1516

1617
use bitcoin::consensus::{deserialize, serialize};
1718
use bitcoin::hashes::hex::{FromHex, ToHex};
1819
use bitcoin::hashes::{sha256, Hash};
19-
use bitcoin::{BlockHeader, Script, Transaction, Txid};
20+
use bitcoin::{BlockHash, BlockHeader, Script, Transaction, Txid};
2021

2122
#[allow(unused_imports)]
2223
use log::{debug, error, info, trace};
@@ -176,6 +177,19 @@ impl AsyncClient {
176177
Ok(req.error_for_status()?.text().await?.parse()?)
177178
}
178179

180+
/// Get the [`BlockHash`] of the current blockchain tip.
181+
pub async fn get_tip_hash(&self) -> Result<BlockHash, Error> {
182+
let resp = self
183+
.client
184+
.get(&format!("{}/blocks/tip/hash", self.url))
185+
.send()
186+
.await?;
187+
188+
Ok(BlockHash::from_str(
189+
&resp.error_for_status()?.text().await?,
190+
)?)
191+
}
192+
179193
/// Get confirmed transaction history for the specified address/scripthash,
180194
/// sorted with newest first. Returns 25 transactions per page.
181195
/// More can be requested by specifying the last txid seen by the previous query.

src/blocking.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use std::collections::HashMap;
1515
use std::io;
1616
use std::io::Read;
17+
use std::str::FromStr;
1718
use std::time::Duration;
1819

1920
#[allow(unused_imports)]
@@ -24,7 +25,7 @@ use ureq::{Agent, Proxy, Response};
2425
use bitcoin::consensus::{deserialize, serialize};
2526
use bitcoin::hashes::hex::{FromHex, ToHex};
2627
use bitcoin::hashes::{sha256, Hash};
27-
use bitcoin::{BlockHeader, Script, Transaction, Txid};
28+
use bitcoin::{BlockHash, BlockHeader, Script, Transaction, Txid};
2829

2930
use crate::{Builder, Error, MerkleProof, OutputStatus, Tx, TxStatus};
3031

@@ -186,7 +187,7 @@ impl BlockingClient {
186187
}
187188
}
188189

189-
/// Get the current height of the blockchain tip
190+
/// Get the height of the current blockchain tip.
190191
pub fn get_height(&self) -> Result<u32, Error> {
191192
let resp = self
192193
.agent
@@ -200,6 +201,20 @@ impl BlockingClient {
200201
}
201202
}
202203

204+
/// Get the [`BlockHash`] of the current blockchain tip.
205+
pub fn get_tip_hash(&self) -> Result<BlockHash, Error> {
206+
let resp = self
207+
.agent
208+
.get(&format!("{}/blocks/tip/hash", self.url))
209+
.call();
210+
211+
match resp {
212+
Ok(resp) => Ok(BlockHash::from_str(&resp.into_string()?)?),
213+
Err(ureq::Error::Status(code, _)) => Err(Error::HttpResponse(code)),
214+
Err(e) => Err(Error::Ureq(e)),
215+
}
216+
}
217+
203218
/// Get an map where the key is the confirmation target (in number of blocks)
204219
/// and the value is the estimated feerate (in sat/vB).
205220
pub fn get_fee_estimates(&self) -> Result<HashMap<String, f64>, Error> {

0 commit comments

Comments
 (0)