Skip to content

Commit

Permalink
BitcoinClient add get_block_height rpc method (#663)
Browse files Browse the repository at this point in the history
add get_block_height rpc
  • Loading branch information
MdTeach authored Feb 11, 2025
1 parent 95197d3 commit ab6cc42
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
12 changes: 3 additions & 9 deletions bin/prover-client/src/operators/l1_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use std::sync::Arc;
use bitcoin::{params::MAINNET, Block};
use strata_btcio::{
reader::query::get_verification_state,
rpc::{
traits::{ReaderRpc, WalletRpc},
BitcoinClient,
},
rpc::{traits::ReaderRpc, BitcoinClient},
};
use strata_primitives::{
params::RollupParams,
Expand Down Expand Up @@ -49,14 +46,11 @@ impl L1BatchOperator {
}

async fn get_block_height(&self, block_id: L1BlockId) -> Result<u64, ProvingTaskError> {
let block = self.get_block(block_id).await?;

let block_height = self
.btc_client
.get_transaction(&block.coinbase().expect("expect coinbase tx").compute_txid())
.get_block_height(&block_id.into())
.await
.map_err(|e| ProvingTaskError::RpcError(e.to_string()))?
.block_height();
.map_err(|e| ProvingTaskError::RpcError(e.to_string()))?;

Ok(block_height)
}
Expand Down
17 changes: 13 additions & 4 deletions crates/btcio/src/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ use crate::rpc::{
error::{BitcoinRpcError, ClientError},
traits::{BroadcasterRpc, ReaderRpc, SignerRpc, WalletRpc},
types::{
CreateRawTransaction, CreateWallet, GetBlockVerbosityZero, GetBlockchainInfo,
GetNewAddress, GetTransaction, GetTxOut, ImportDescriptor, ImportDescriptorResult,
ListDescriptors, ListTransactions, ListUnspent, PreviousTransactionOutput,
SignRawTransactionWithWallet, SubmitPackage, TestMempoolAccept,
CreateRawTransaction, CreateWallet, GetBlockVerbosityOne, GetBlockVerbosityZero,
GetBlockchainInfo, GetNewAddress, GetTransaction, GetTxOut, ImportDescriptor,
ImportDescriptorResult, ListDescriptors, ListTransactions, ListUnspent,
PreviousTransactionOutput, SignRawTransactionWithWallet, SubmitPackage, TestMempoolAccept,
},
};

Expand Down Expand Up @@ -246,6 +246,15 @@ impl ReaderRpc for BitcoinClient {
Ok(block)
}

async fn get_block_height(&self, hash: &BlockHash) -> ClientResult<u64> {
let block_verobose = self
.call::<GetBlockVerbosityOne>("getblock", &[to_value(hash.to_string())?])
.await?;

let block_height = block_verobose.height as u64;
Ok(block_height)
}

async fn get_block_at(&self, height: u64) -> ClientResult<Block> {
let hash = self.get_block_hash(height).await?;
self.get_block(&hash).await
Expand Down
3 changes: 3 additions & 0 deletions crates/btcio/src/rpc/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub trait ReaderRpc {
/// Gets a [`Block`] with the given hash.
async fn get_block(&self, hash: &BlockHash) -> ClientResult<Block>;

/// Gets a block height with the given hash.
async fn get_block_height(&self, hash: &BlockHash) -> ClientResult<u64>;

/// Gets a [`Block`] at given height.
async fn get_block_at(&self, height: u64) -> ClientResult<Block>;

Expand Down
4 changes: 4 additions & 0 deletions crates/btcio/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl ReaderRpc for TestBitcoinClient {
Ok(block)
}

async fn get_block_height(&self, _hash: &BlockHash) -> ClientResult<u64> {
Ok(100)
}

async fn get_block_at(&self, _height: u64) -> ClientResult<Block> {
let block: Block = deserialize(&hex::decode(TEST_BLOCKSTR).unwrap()).unwrap();
Ok(block)
Expand Down

0 comments on commit ab6cc42

Please sign in to comment.