diff --git a/.changelog/unreleased/improvements/2891-sdk-query-height.md b/.changelog/unreleased/improvements/2891-sdk-query-height.md new file mode 100644 index 0000000000..a91275ff9b --- /dev/null +++ b/.changelog/unreleased/improvements/2891-sdk-query-height.md @@ -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)) \ No newline at end of file diff --git a/crates/node/src/bench_utils.rs b/crates/node/src/bench_utils.rs index e8290ed62c..ebe87797b1 100644 --- a/crates/node/src/bench_utils.rs +++ b/crates/node/src/bench_utils.rs @@ -832,11 +832,20 @@ impl Client for BenchShell { height: H, ) -> Result where - H: Into + Send, + H: TryInto + 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 @@ -896,11 +905,15 @@ impl Client for BenchShell { tendermint_rpc::Error, > where - H: Into + Send, + H: TryInto + 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 diff --git a/crates/node/src/shell/testing/node.rs b/crates/node/src/shell/testing/node.rs index d01a750edb..30f43bf259 100644 --- a/crates/node/src/shell/testing/node.rs +++ b/crates/node/src/shell/testing/node.rs @@ -846,10 +846,12 @@ impl<'a> Client for &'a MockNode { height: H, ) -> Result where - H: Into + Send, + H: TryInto + 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() @@ -890,11 +892,18 @@ impl<'a> Client for &'a MockNode { height: H, ) -> Result where - H: Into + Send, + H: TryInto + 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() diff --git a/crates/sdk/src/masp.rs b/crates/sdk/src/masp.rs index 9caeff7b0d..ac7f44e8c6 100644 --- a/crates/sdk/src/masp.rs +++ b/crates/sdk/src/masp.rs @@ -823,7 +823,7 @@ impl ShieldedContext { // 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 @@ -2025,7 +2025,7 @@ async fn get_indexed_masp_events_at_height( 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 diff --git a/crates/sdk/src/queries/mod.rs b/crates/sdk/src/queries/mod.rs index eb6232cf71..ed5fdb1836 100644 --- a/crates/sdk/src/queries/mod.rs +++ b/crates/sdk/src/queries/mod.rs @@ -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 @@ -263,9 +266,14 @@ pub trait Client { /// `/block`: get block at a given height. async fn block(&self, height: H) -> Result where - H: Into + Send, + H: TryInto + 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. @@ -289,10 +297,12 @@ pub trait Client { height: H, ) -> Result where - H: Into + Send, + H: TryInto + 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 } @@ -349,18 +359,28 @@ pub trait Client { max: H, ) -> Result where - H: Into + Send, + H: TryInto + 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(&self, height: H) -> Result where - H: Into + Send, + H: TryInto + 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 @@ -370,10 +390,14 @@ pub trait Client { height: H, ) -> Result where - H: Into + Send, + H: TryInto + 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