Skip to content

Commit

Permalink
Merge pull request #1590 from GuiGou12358/master
Browse files Browse the repository at this point in the history
Add a method to get the latest hash of the finalized header
  • Loading branch information
kvinwang authored Nov 24, 2024
2 parents 5e59e74 + 8e23a9f commit 6877d14
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/pink-libs/subrpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pink-subrpc"
version = "0.7.0"
version = "0.8.0"
authors = ["Phala Network"]
edition = "2021"
license = "Apache-2.0"
Expand Down
25 changes: 25 additions & 0 deletions crates/pink-libs/subrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,24 @@ pub fn get_block_hash(
Ok(H256(hash))
}

/// Gets the latest hash of the finalized header
pub fn get_finalized_head(
rpc_node: &str,
) -> core::result::Result<H256, Error> {
let data = format!(
r#"{{"id":1, "jsonrpc":"2.0", "method": "chain_getFinalizedHead"}}"#
)
.into_bytes();
let resp_body = call_rpc(rpc_node, data)?;
let genesis_hash: GenesisHash = json::from_slice(&resp_body).or(Err(Error::InvalidBody))?;
// bypass prefix 0x
let genesis_hash_result = &genesis_hash.result[2..];
let decoded_hash = hex::decode(genesis_hash_result).or(Err(Error::InvalidBody))?;
let hash: [u8; 32] = decoded_hash.try_into().or(Err(Error::InvalidBody))?;
Ok(H256(hash))
}


/// Gets revalant block header by given block hash
/// Only work with chains define generic patterns like `Header<u32, BlakeTwo256>`
pub fn get_header(
Expand Down Expand Up @@ -422,6 +440,13 @@ mod tests {
);
}

#[test]
fn can_get_finalized_head() {
pink_chain_extension::mock_ext::mock_all_ext();
let finalized_head = get_finalized_head("https://kusama-rpc.polkadot.io");
assert!(finalized_head.is_ok(), "failed to query finalized_head");
}

#[test]
fn can_correctly_encode() {
let genesis_hash: [u8; 32] =
Expand Down

0 comments on commit 6877d14

Please sign in to comment.