Skip to content

Commit

Permalink
Merge branch 'grarco/sdk-query-height' (#3441)
Browse files Browse the repository at this point in the history
* grarco/sdk-query-height:
  Changelog #2891
  Fallible trait bound for block height param in queries
  • Loading branch information
brentstone committed Jun 28, 2024
2 parents 48dfdb5 + ec16fd2 commit 36ab80e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/2891-sdk-query-height.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Queries methods now requests `TryInto` trait bound for block heights to reduce
the conversion error. ([\#2891](https://github.com/anoma/namada/issues/2891))
21 changes: 17 additions & 4 deletions crates/node/src/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,11 +832,20 @@ impl Client for BenchShell {
height: H,
) -> Result<tendermint_rpc::endpoint::block::Response, tendermint_rpc::Error>
where
H: Into<tendermint::block::Height> + Send,
H: TryInto<tendermint::block::Height> + Send,
{
// NOTE: atm this is only needed to query blocks at a specific height
// for masp transactions
let height = BlockHeight(height.into().into());
let height = BlockHeight(
height
.try_into()
.map_err(|_| {
tendermint_rpc::Error::parse(
"Failed to cast block height".to_string(),
)
})?
.into(),
);

// Given the way we setup and run benchmarks, the masp transactions can
// only present in the last block, we can mock the previous
Expand Down Expand Up @@ -896,11 +905,15 @@ impl Client for BenchShell {
tendermint_rpc::Error,
>
where
H: Into<namada::tendermint::block::Height> + Send,
H: TryInto<namada::tendermint::block::Height> + Send,
{
// NOTE: atm this is only needed to query block results at a specific
// height for masp transactions
let height = height.into();
let height = height.try_into().map_err(|_| {
tendermint_rpc::Error::parse(
"Failed to cast block height".to_string(),
)
})?;

// We can expect all the masp tranfers to have happened only in the last
// block
Expand Down
17 changes: 13 additions & 4 deletions crates/node/src/shell/testing/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,10 +846,12 @@ impl<'a> Client for &'a MockNode {
height: H,
) -> Result<tendermint_rpc::endpoint::block_results::Response, RpcError>
where
H: Into<namada::tendermint::block::Height> + Send,
H: TryInto<namada::tendermint::block::Height> + Send,
{
self.drive_mock_services_bg().await;
let height = height.into();
let height = height.try_into().map_err(|_| {
RpcError::parse("Failed to cast block height".to_string())
})?;
let locked = self.shell.lock().unwrap();
let events: Vec<_> = locked
.event_log()
Expand Down Expand Up @@ -890,11 +892,18 @@ impl<'a> Client for &'a MockNode {
height: H,
) -> Result<tendermint_rpc::endpoint::block::Response, RpcError>
where
H: Into<tendermint::block::Height> + Send,
H: TryInto<tendermint::block::Height> + Send,
{
// NOTE: atm this is only needed to query blocks at a
// specific height for masp transactions
let height = BlockHeight(height.into().into());
let height = BlockHeight(
height
.try_into()
.map_err(|_| {
RpcError::parse("Failed to cast block height".to_string())
})?
.into(),
);

self.blocks
.lock()
Expand Down
4 changes: 2 additions & 2 deletions crates/sdk/src/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ impl<U: ShieldedUtils + MaybeSend + MaybeSync> ShieldedContext<U> {
// minimal improvement and it's even hard to tell how many times
// we'd need a single masp tx to make this worth it
let block = client
.block(height as u32)
.block(height)
.await
.map_err(|e| Error::from(QueryError::General(e.to_string())))?
.block
Expand Down Expand Up @@ -2025,7 +2025,7 @@ async fn get_indexed_masp_events_at_height<C: Client + Sync>(
let first_idx_to_query = first_idx_to_query.unwrap_or_default();

Ok(client
.block_results(height.0 as u32)
.block_results(height.0)
.await
.map_err(|e| Error::from(QueryError::General(e.to_string())))?
.end_block_events
Expand Down
48 changes: 36 additions & 12 deletions crates/sdk/src/queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ mod shell;
mod types;
pub mod vp;

#[cfg(any(test, feature = "async-client"))]
const HEIGHT_CAST_ERR: &str = "Failed to cast block height";

// Most commonly expected patterns should be declared first
router! {RPC,
// Shell provides storage read access, block metadata and can dry-run a tx
Expand Down Expand Up @@ -263,9 +266,14 @@ pub trait Client {
/// `/block`: get block at a given height.
async fn block<H>(&self, height: H) -> Result<block::Response, RpcError>
where
H: Into<Height> + Send,
H: TryInto<Height> + Send,
{
self.perform(block::Request::new(height.into())).await
self.perform(block::Request::new(
height
.try_into()
.map_err(|_| RpcError::parse(HEIGHT_CAST_ERR.to_string()))?,
))
.await
}

/// `/block_search`: search for blocks by BeginBlock and EndBlock events.
Expand All @@ -289,10 +297,12 @@ pub trait Client {
height: H,
) -> Result<tendermint_rpc::endpoint::block_results::Response, RpcError>
where
H: Into<Height> + Send,
H: TryInto<Height> + Send,
{
self.perform(tendermint_rpc::endpoint::block_results::Request::new(
height.into(),
height
.try_into()
.map_err(|_| RpcError::parse(HEIGHT_CAST_ERR.to_string()))?,
))
.await
}
Expand Down Expand Up @@ -349,18 +359,28 @@ pub trait Client {
max: H,
) -> Result<blockchain::Response, RpcError>
where
H: Into<Height> + Send,
H: TryInto<Height> + Send,
{
self.perform(blockchain::Request::new(min.into(), max.into()))
.await
self.perform(blockchain::Request::new(
min.try_into()
.map_err(|_| RpcError::parse(HEIGHT_CAST_ERR.to_string()))?,
max.try_into()
.map_err(|_| RpcError::parse(HEIGHT_CAST_ERR.to_string()))?,
))
.await
}

/// `/commit`: get block commit at a given height.
async fn commit<H>(&self, height: H) -> Result<commit::Response, RpcError>
where
H: Into<Height> + Send,
H: TryInto<Height> + Send,
{
self.perform(commit::Request::new(height.into())).await
self.perform(commit::Request::new(
height
.try_into()
.map_err(|_| RpcError::parse(HEIGHT_CAST_ERR.to_string()))?,
))
.await
}

/// `/consensus_params`: get current consensus parameters at the specified
Expand All @@ -370,10 +390,14 @@ pub trait Client {
height: H,
) -> Result<consensus_params::Response, RpcError>
where
H: Into<Height> + Send,
H: TryInto<Height> + Send,
{
self.perform(consensus_params::Request::new(Some(height.into())))
.await
self.perform(consensus_params::Request::new(Some(
height
.try_into()
.map_err(|_| RpcError::parse(HEIGHT_CAST_ERR.to_string()))?,
)))
.await
}

/// `/consensus_state`: get current consensus state
Expand Down

0 comments on commit 36ab80e

Please sign in to comment.