Skip to content

Commit 2834f8c

Browse files
committed
crc: update get_block_hash api, #6250
1 parent f42264a commit 2834f8c

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

stacks-node/src/burnchains/rpc/bitcoin_rpc_client/mod.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,22 @@ impl<'de> Deserialize<'de> for TxidWrapperResponse {
298298
}
299299
}
300300

301+
/// Response mainly used as deserialization wrapper for [`BurnchainHeaderHash`]
302+
struct BurnchainHeaderHashWrapperResponse(pub BurnchainHeaderHash);
303+
304+
/// Deserializes a JSON string (hex-encoded, big-endian) into [`BurnchainHeaderHash`],
305+
/// and wrap it into [`BurnchainHeaderHashWrapperResponse`]
306+
impl<'de> Deserialize<'de> for BurnchainHeaderHashWrapperResponse {
307+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
308+
where
309+
D: Deserializer<'de>,
310+
{
311+
let hex_str: String = Deserialize::deserialize(deserializer)?;
312+
let bhh = BurnchainHeaderHash::from_hex(&hex_str).map_err(serde::de::Error::custom)?;
313+
Ok(BurnchainHeaderHashWrapperResponse(bhh))
314+
}
315+
}
316+
301317
/// Client for interacting with a Bitcoin RPC service.
302318
#[derive(Debug)]
303319
pub struct BitcoinRpcClient {
@@ -651,13 +667,16 @@ impl BitcoinRpcClient {
651667
/// * `height` - The height (block number) of the block whose hash is requested.
652668
///
653669
/// # Returns
654-
/// A `String` representing the block hash in hexadecimal format.
670+
/// A [`BurnchainHeaderHash`] representing the block hash.
655671
///
656672
/// # Availability
657673
/// - **Since**: Bitcoin Core **v0.9.0**.
658-
pub fn get_block_hash(&self, height: u64) -> BitcoinRpcClientResult<String> {
659-
Ok(self
660-
.global_ep
661-
.send(&self.client_id, "getblockhash", vec![height.into()])?)
674+
pub fn get_block_hash(&self, height: u64) -> BitcoinRpcClientResult<BurnchainHeaderHash> {
675+
let response = self.global_ep.send::<BurnchainHeaderHashWrapperResponse>(
676+
&self.client_id,
677+
"getblockhash",
678+
vec![height.into()],
679+
)?;
680+
Ok(response.0)
662681
}
663682
}

stacks-node/src/burnchains/rpc/bitcoin_rpc_client/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ fn test_invalidate_block_ok() {
958958
#[test]
959959
fn test_get_block_hash_ok() {
960960
let height = 1;
961-
let expected_hash = "0000";
961+
let expected_hash = utils::BITCOIN_BLOCK_HASH;
962962

963963
let expected_request = json!({
964964
"jsonrpc": "2.0",
@@ -984,8 +984,8 @@ fn test_get_block_hash_ok() {
984984

985985
let client = utils::setup_client(&server);
986986

987-
let hash = client.get_block_hash(height).expect("Should be ok!");
988-
assert_eq!(expected_hash, hash);
987+
let bhh = client.get_block_hash(height).expect("Should be ok!");
988+
assert_eq!(expected_hash, bhh.to_hex());
989989
}
990990

991991
#[test]

stacks-node/src/tests/bitcoin_rpc_integrations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,10 +612,10 @@ fn test_get_block_hash_ok() {
612612

613613
let client = BitcoinRpcClient::from_stx_config(&config).expect("Client creation ok!");
614614

615-
let hash = client
615+
let bhh = client
616616
.get_block_hash(0)
617617
.expect("Should return regtest genesis block hash!");
618-
assert_eq!(BITCOIN_REGTEST_FIRST_BLOCK_HASH, hash);
618+
assert_eq!(BITCOIN_REGTEST_FIRST_BLOCK_HASH, bhh.to_hex());
619619
}
620620

621621
#[ignore]

0 commit comments

Comments
 (0)