Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitcoinClient add get_block_height rpc method #663

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@
}

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())

Check warning on line 51 in bin/prover-client/src/operators/l1_batch.rs

View check run for this annotation

Codecov / codecov/patch

bin/prover-client/src/operators/l1_batch.rs#L51

Added line #L51 was not covered by tests
.await
.map_err(|e| ProvingTaskError::RpcError(e.to_string()))?
.block_height();
.map_err(|e| ProvingTaskError::RpcError(e.to_string()))?;

Check warning on line 53 in bin/prover-client/src/operators/l1_batch.rs

View check run for this annotation

Codecov / codecov/patch

bin/prover-client/src/operators/l1_batch.rs#L53

Added line #L53 was not covered by tests

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 @@
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 @@
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?;

Check warning on line 252 in crates/btcio/src/rpc/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/btcio/src/rpc/client.rs#L249-L252

Added lines #L249 - L252 were not covered by tests

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

Check warning on line 256 in crates/btcio/src/rpc/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/btcio/src/rpc/client.rs#L254-L256

Added lines #L254 - L256 were not covered by tests

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 @@
Ok(block)
}

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

Check warning on line 76 in crates/btcio/src/test_utils.rs

View check run for this annotation

Codecov / codecov/patch

crates/btcio/src/test_utils.rs#L74-L76

Added lines #L74 - L76 were not covered by tests

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