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

Generic IO #1746

Merged
merged 12 commits into from
Sep 25, 2023
2 changes: 2 additions & 0 deletions .changelog/unreleased/features/1746-generic-io.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Replaced standard IO in SDK and client code with a trait that allows custom
handling. ([\#1746](https://github.com/anoma/namada/pull/1746))
544 changes: 0 additions & 544 deletions apps/src/bin/namada-client/cli.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions apps/src/bin/namada-client/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use color_eyre::eyre::Result;
use namada_apps::cli::api::CliApi;
use namada_apps::cli::api::{CliApi, CliIo};
use namada_apps::facade::tendermint_rpc::HttpClient;
use namada_apps::{cli, logging};
use tracing_subscriber::filter::LevelFilter;
Expand All @@ -13,7 +13,7 @@ async fn main() -> Result<()> {
let _log_guard = logging::init_from_env_or(LevelFilter::INFO)?;

// run the CLI
CliApi::<()>::handle_client_command::<HttpClient>(
CliApi::<CliIo>::handle_client_command::<HttpClient>(
None,
cli::namada_client_cli()?,
)
Expand Down
4 changes: 2 additions & 2 deletions apps/src/bin/namada-relayer/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use color_eyre::eyre::Result;
use namada::tendermint_rpc::HttpClient;
use namada_apps::cli::api::CliApi;
use namada_apps::cli::api::{CliApi, CliIo};
use namada_apps::{cli, logging};
use tracing_subscriber::filter::LevelFilter;

Expand All @@ -14,5 +14,5 @@ async fn main() -> Result<()> {

let cmd = cli::namada_relayer_cli()?;
// run the CLI
CliApi::<()>::handle_relayer_command::<HttpClient>(None, cmd).await
CliApi::<CliIo>::handle_relayer_command::<HttpClient>(None, cmd).await
}
4 changes: 2 additions & 2 deletions apps/src/bin/namada-wallet/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use color_eyre::eyre::Result;
use namada_apps::cli;
use namada_apps::cli::api::CliApi;
use namada_apps::cli::api::{CliApi, CliIo};

pub fn main() -> Result<()> {
color_eyre::install()?;
let (cmd, ctx) = cli::namada_wallet_cli()?;
// run the CLI
CliApi::<()>::handle_wallet_command(cmd, ctx)
CliApi::<CliIo>::handle_wallet_command(cmd, ctx)
}
30 changes: 20 additions & 10 deletions apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ pub mod wallet;

use clap::{ArgGroup, ArgMatches, ColorChoice};
use color_eyre::eyre::Result;
use namada::types::io::DefaultIo;
use utils::*;
pub use utils::{dispatch_prompt, safe_exit, Cmd, TESTIN};

pub use self::context::Context;
use crate::cli::api::CliIo;

include!("../../version.rs");

Expand Down Expand Up @@ -2585,6 +2587,14 @@ pub mod args {
arg_default("gas-limit", DefaultFn(|| GasLimit::from(20_000)));
pub const FEE_TOKEN: ArgDefaultFromCtx<WalletAddress> =
arg_default_from_ctx("gas-token", DefaultFn(|| "NAM".parse().unwrap()));
pub const FEE_PAYER: Arg<WalletAddress> = arg("fee-payer");
pub const FEE_AMOUNT: ArgDefault<token::DenominatedAmount> = arg_default(
"fee-amount",
DefaultFn(|| token::DenominatedAmount {
amount: token::Amount::default(),
denom: NATIVE_MAX_DECIMAL_PLACES.into(),
}),
);
pub const GENESIS_PATH: Arg<PathBuf> = arg("genesis-path");
pub const GENESIS_VALIDATOR: ArgOpt<String> =
arg("genesis-validator").opt();
Expand Down Expand Up @@ -4233,14 +4243,6 @@ pub mod args {
}
}

impl CliToSdk<QueryPgf<SdkTypes>> for QueryPgf<CliTypes> {
fn to_sdk(self, ctx: &mut Context) -> QueryPgf<SdkTypes> {
QueryPgf::<SdkTypes> {
query: self.query.to_sdk(ctx),
}
}
}

impl Args for QueryPgf<CliTypes> {
fn parse(matches: &ArgMatches) -> Self {
let query = Query::parse(matches);
Expand All @@ -4253,6 +4255,14 @@ pub mod args {
}
}

impl CliToSdk<QueryPgf<SdkTypes>> for QueryPgf<CliTypes> {
fn to_sdk(self, ctx: &mut Context) -> QueryPgf<SdkTypes> {
QueryPgf::<SdkTypes> {
query: self.query.to_sdk(ctx),
}
}
}

impl CliToSdk<Withdraw<SdkTypes>> for Withdraw<CliTypes> {
fn to_sdk(self, ctx: &mut Context) -> Withdraw<SdkTypes> {
Withdraw::<SdkTypes> {
Expand Down Expand Up @@ -5653,7 +5663,7 @@ pub fn namada_client_cli() -> Result<NamadaClient> {
let global_args = args::Global::parse(&matches);
match cmd {
cmds::NamadaClient::WithContext(sub_cmd) => {
let context = Context::new(global_args)?;
let context = Context::new::<CliIo>(global_args)?;
Ok(NamadaClient::WithContext(Box::new((sub_cmd, context))))
}
cmds::NamadaClient::WithoutContext(sub_cmd) => {
Expand Down Expand Up @@ -5689,7 +5699,7 @@ pub fn namada_relayer_cli() -> Result<NamadaRelayer> {
cmds::EthBridgePool::WithContext(sub_cmd),
) => {
let global_args = args::Global::parse(&matches);
let context = Context::new(global_args)?;
let context = Context::new::<DefaultIo>(global_args)?;
Ok(NamadaRelayer::EthBridgePoolWithCtx(Box::new((
sub_cmd, context,
))))
Expand Down
14 changes: 10 additions & 4 deletions apps/src/lib/cli/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use namada::ledger::queries::Client;
use namada::ledger::rpc::wait_until_node_is_synched;
use namada::tendermint_rpc::HttpClient;
use namada::types::control_flow::Halt;
use namada::types::io::Io;
use tendermint_config::net::Address as TendermintAddress;

use crate::client::utils;
Expand All @@ -12,7 +13,7 @@ use crate::client::utils;
#[async_trait::async_trait(?Send)]
pub trait CliClient: Client + Sync {
fn from_tendermint_address(address: &mut TendermintAddress) -> Self;
async fn wait_until_node_is_synced(&self) -> Halt<()>;
async fn wait_until_node_is_synced<IO: Io>(&self) -> Halt<()>;
}

#[async_trait::async_trait(?Send)]
Expand All @@ -21,9 +22,14 @@ impl CliClient for HttpClient {
HttpClient::new(utils::take_config_address(address)).unwrap()
}

async fn wait_until_node_is_synced(&self) -> Halt<()> {
wait_until_node_is_synched(self).await
async fn wait_until_node_is_synced<IO: Io>(&self) -> Halt<()> {
wait_until_node_is_synched::<_, IO>(self).await
}
}

pub struct CliApi<IO>(PhantomData<IO>);
pub struct CliIo;

#[async_trait::async_trait(?Send)]
impl Io for CliIo {}

pub struct CliApi<IO: Io = CliIo>(PhantomData<IO>);
Loading
Loading