From 0c42a1423a451d5e95605b0d62205de234e4e827 Mon Sep 17 00:00:00 2001 From: benluelo Date: Thu, 9 Jan 2025 06:52:40 +0000 Subject: [PATCH] feat(voyager): small rpc cli improvements --- lib/voyager-core/src/lib.rs | 2 +- voyager/src/cli.rs | 24 ++++++-- voyager/src/main.rs | 113 ++++++++++++++++++++++-------------- 3 files changed, 89 insertions(+), 50 deletions(-) diff --git a/lib/voyager-core/src/lib.rs b/lib/voyager-core/src/lib.rs index 810375ab1a..357529533d 100644 --- a/lib/voyager-core/src/lib.rs +++ b/lib/voyager-core/src/lib.rs @@ -272,7 +272,7 @@ pub struct ClientInfo { /// This is currently only used for threading the checksum for ibc-go /// 08-wasm clients, and can likely be removed when support for that IBC /// interface is dropped. - #[serde(default)] + #[serde(default, skip_serializing_if = "Value::is_null")] pub metadata: Value, } diff --git a/voyager/src/cli.rs b/voyager/src/cli.rs index 01e5376465..b7013e8022 100644 --- a/voyager/src/cli.rs +++ b/voyager/src/cli.rs @@ -187,25 +187,39 @@ pub enum RpcCmd { ClientState { #[arg(value_parser(|s: &str| ok(ChainId::new(s.to_owned()))))] on: ChainId, - // #[arg(value_parser(|s: &str| ok(RawClientId::new(s.parse::().unwrap_or_else(|_| Value::String(s.to_owned()))))))] - client_id: RawClientId, #[arg(value_parser(|s: &str| ok(IbcSpecId::new(s.to_owned()))))] ibc_spec_id: IbcSpecId, + client_id: RawClientId, #[arg(long, default_value_t = QueryHeight::Latest)] height: QueryHeight, #[arg(long, short = 'd', default_value_t = false)] decode: bool, }, - ConsensusState { + ClientMeta { #[arg(value_parser(|s: &str| ok(ChainId::new(s.to_owned()))))] on: ChainId, - // #[arg(value_parser(|s: &str| ok(RawClientId::new(s.parse::().unwrap_or_else(|_| Value::String(s.to_owned()))))))] - client_id: RawClientId, #[arg(value_parser(|s: &str| ok(IbcSpecId::new(s.to_owned()))))] ibc_spec_id: IbcSpecId, + client_id: RawClientId, #[arg(long, default_value_t = QueryHeight::Latest)] height: QueryHeight, + }, + ClientInfo { + #[arg(value_parser(|s: &str| ok(ChainId::new(s.to_owned()))))] + on: ChainId, + client_id: RawClientId, + #[arg(value_parser(|s: &str| ok(IbcSpecId::new(s.to_owned()))))] + ibc_spec_id: IbcSpecId, + }, + ConsensusState { + #[arg(value_parser(|s: &str| ok(ChainId::new(s.to_owned()))))] + on: ChainId, + #[arg(value_parser(|s: &str| ok(IbcSpecId::new(s.to_owned()))))] + ibc_spec_id: IbcSpecId, + client_id: RawClientId, trusted_height: Height, + #[arg(long, default_value_t = QueryHeight::Latest)] + height: QueryHeight, #[arg(long, short = 'd', default_value_t = false)] decode: bool, }, diff --git a/voyager/src/main.rs b/voyager/src/main.rs index 4a33d36a26..7136a89b75 100644 --- a/voyager/src/main.rs +++ b/voyager/src/main.rs @@ -500,8 +500,38 @@ async fn do_main(args: cli::AppArgs) -> anyhow::Result<()> { get_voyager_config()?.voyager.rpc_laddr ))?; + let ibc_handlers = [ + (IbcClassic::ID, IbcSpecHandler::new::()), + (IbcUnion::ID, IbcSpecHandler::new::()), + ] + .into_iter() + .collect::>(); + match rpc { RpcCmd::Info => print_json(&voyager_client.info().await?), + RpcCmd::ClientMeta { + on, + client_id, + ibc_spec_id, + height, + } => { + let client_meta = voyager_client + .client_meta(on.clone(), ibc_spec_id.clone(), height, client_id.clone()) + .await?; + + print_json(&client_meta); + } + RpcCmd::ClientInfo { + on, + client_id, + ibc_spec_id, + } => { + let client_info = voyager_client + .client_info(on.clone(), ibc_spec_id.clone(), client_id.clone()) + .await?; + + print_json(&client_info); + } RpcCmd::ClientState { on, client_id, @@ -509,13 +539,6 @@ async fn do_main(args: cli::AppArgs) -> anyhow::Result<()> { height, decode, } => { - let ibc_handlers = [ - (IbcClassic::ID, IbcSpecHandler::new::()), - (IbcUnion::ID, IbcSpecHandler::new::()) - ] - .into_iter() - .collect::>(); - let ibc_state = voyager_client .query_ibc_state( on.clone(), @@ -550,45 +573,47 @@ async fn do_main(args: cli::AppArgs) -> anyhow::Result<()> { } } RpcCmd::ConsensusState { - // on, - // client_id, - // ibc_spec_id, - // trusted_height, - // height, - // decode, + on, + client_id, + ibc_spec_id, + trusted_height, + height, + decode, .. } => { - // let ibc_state = voyager_client - // .query_client_consensus_state( - // on.clone(), - // height, - // client_id.clone(), - // trusted_height, - // ) - // .await?; - - // if decode { - // let client_info = voyager_client - // .client_info(on, ibc_spec_id, client_id) - // .await?; - - // let decoded = voyager_client - // .decode_consensus_state( - // client_info.client_type, - // client_info.ibc_interface, - // ibc_state.state, - // ) - // .await?; - - // print_json(&IbcState { - // height: ibc_state.height, - // state: decoded, - // }); - // } else { - // print_json(&ibc_state); - // } - - todo!() + let ibc_state = voyager_client + .query_ibc_state( + on.clone(), + ibc_spec_id.clone(), + height, + (ibc_handlers.get(&ibc_spec_id).unwrap().consensus_state_path)( + client_id.clone(), + trusted_height.to_string(), + )?, + ) + .await?; + + if decode { + let client_info = voyager_client + .client_info(on, ibc_spec_id.clone(), client_id) + .await?; + + let decoded = voyager_client + .decode_consensus_state( + client_info.client_type, + client_info.ibc_interface, + ibc_spec_id, + serde_json::from_value(ibc_state.state).unwrap(), + ) + .await?; + + print_json(&IbcState { + height: ibc_state.height, + state: decoded, + }); + } else { + print_json(&ibc_state); + } } } }