From df92c845cbf91c41238fafb8989cdb8981b86883 Mon Sep 17 00:00:00 2001 From: Andrew Plaza Date: Mon, 16 Dec 2024 12:45:09 -0500 Subject: [PATCH] add replication client stubs (#1420) --- xmtp_debug/src/app/clients.rs | 9 +-- xmtp_debug/src/args.rs | 102 ++++++++++++++++++++++++++++++++-- xmtp_debug/src/constants.rs | 13 +++++ xmtp_debug/src/main.rs | 4 +- 4 files changed, 114 insertions(+), 14 deletions(-) diff --git a/xmtp_debug/src/app/clients.rs b/xmtp_debug/src/app/clients.rs index 6824ca39e..66e92356e 100644 --- a/xmtp_debug/src/app/clients.rs +++ b/xmtp_debug/src/app/clients.rs @@ -57,10 +57,7 @@ async fn new_client_inner( wallet: &LocalWallet, db_path: Option, ) -> Result { - let url = url::Url::from(network.clone()); - let is_secure = url.scheme() == "https"; - trace!(url = %url, is_secure, "create grpc"); - let api = crate::GrpcClient::create(url.as_str().to_string(), is_secure).await?; + let api = network.connect().await?; let nonce = 1; let inbox_id = generate_inbox_id(&wallet.get_address(), &nonce).unwrap(); @@ -125,9 +122,7 @@ async fn existing_client_inner( network: &args::BackendOpts, db_path: PathBuf, ) -> Result { - let url = url::Url::from(network.clone()); - let is_secure = url.scheme() == "https"; - let api = crate::GrpcClient::create(url.as_str().to_string(), is_secure).await?; + let api = network.connect().await?; let store = EncryptedMessageStore::new( StorageOption::Persistent(db_path.clone().into_os_string().into_string().unwrap()), diff --git a/xmtp_debug/src/args.rs b/xmtp_debug/src/args.rs index d4ca414ce..4d6b1583b 100644 --- a/xmtp_debug/src/args.rs +++ b/xmtp_debug/src/args.rs @@ -3,6 +3,7 @@ use std::path::PathBuf; use clap::{Args, Parser, Subcommand, ValueEnum}; use clap_verbosity_flag::{InfoLevel, Verbosity}; +use color_eyre::eyre; use xxhash_rust::xxh3; mod types; pub use types::*; @@ -193,6 +194,76 @@ pub struct BackendOpts { conflicts_with = "constant-backend" )] pub url: Option, + #[arg( + short, + long, + group = "custom-backend", + conflicts_with = "constant-backend" + )] + pub payer_url: Option, + /// Enable the decentralization backend + #[arg(short, long)] + pub d14n: bool, +} + +impl BackendOpts { + pub fn payer_url(&self) -> eyre::Result { + use BackendKind::*; + + if let Some(p) = &self.payer_url { + return Ok(p.clone()); + } + + match (self.backend, self.d14n) { + (Dev, false) => eyre::bail!("No payer for V3"), + (Production, false) => eyre::bail!("No payer for V3"), + (Local, false) => eyre::bail!("No payer for V3"), + (Dev, true) => Ok((*crate::constants::XMTP_DEV_PAYER).clone()), + (Production, true) => Ok((*crate::constants::XMTP_PRODUCTION_PAYER).clone()), + (Local, true) => Ok((*crate::constants::XMTP_LOCAL_PAYER).clone()), + } + } + + pub fn network_url(&self) -> url::Url { + use BackendKind::*; + + if let Some(n) = &self.url { + return n.clone(); + } + + match (self.backend, self.d14n) { + (Dev, false) => (*crate::constants::XMTP_DEV).clone(), + (Production, false) => (*crate::constants::XMTP_PRODUCTION).clone(), + (Local, false) => (*crate::constants::XMTP_LOCAL).clone(), + (Dev, true) => (*crate::constants::XMTP_DEV_D14N).clone(), + (Production, true) => (*crate::constants::XMTP_PRODUCTION_D14N).clone(), + (Local, true) => (*crate::constants::XMTP_LOCAL_D14N).clone(), + } + } + + pub async fn connect(&self) -> eyre::Result> { + let network = self.network_url(); + let is_secure = network.scheme() == "https"; + + if self.d14n { + let payer = self.payer_url()?; + trace!(url = %network, payer = %payer, is_secure, "create grpc"); + + Ok(Box::new( + xmtp_api_grpc::replication_client::ClientV4::create( + network.as_str().to_string(), + payer.as_str().to_string(), + is_secure, + ) + .await?, + )) + } else { + trace!(url = %network, is_secure, "create grpc"); + Ok(Box::new( + crate::GrpcClient::create(network.as_str().to_string(), is_secure).await?, + )) + } + } } impl<'a> From<&'a BackendOpts> for u64 { @@ -202,10 +273,13 @@ impl<'a> From<&'a BackendOpts> for u64 { if let Some(ref url) = value.url { xxh3::xxh3_64(url.as_str().as_bytes()) } else { - match value.backend { - Production => 2, - Dev => 1, - Local => 0, + match (value.backend, value.d14n) { + (Production, false) => 2, + (Dev, false) => 1, + (Local, false) => 0, + (Production, true) => 5, + (Dev, true) => 4, + (Local, true) => 3, } } } @@ -219,8 +293,10 @@ impl From for u64 { impl From for url::Url { fn from(value: BackendOpts) -> Self { - let BackendOpts { backend, url } = value; - url.unwrap_or(backend.into()) + let BackendOpts { + backend, url, d14n, .. + } = value; + url.unwrap_or(backend.to_url(d14n)) } } @@ -232,6 +308,20 @@ pub enum BackendKind { Local, } +impl BackendKind { + fn to_network_url(self, d14n: bool) -> url::Url { + use BackendKind::*; + match (self, d14n) { + (Dev, false) => (*crate::constants::XMTP_DEV).clone(), + (Production, false) => (*crate::constants::XMTP_PRODUCTION).clone(), + (Local, false) => (*crate::constants::XMTP_LOCAL).clone(), + (Dev, true) => (*crate::constants::XMTP_DEV_D14N).clone(), + (Production, true) => (*crate::constants::XMTP_PRODUCTION_D14N).clone(), + (Local, true) => (*crate::constants::XMTP_LOCAL_D14N).clone(), + } + } +} + impl From for url::Url { fn from(value: BackendKind) -> Self { use BackendKind::*; diff --git a/xmtp_debug/src/constants.rs b/xmtp_debug/src/constants.rs index fb970d518..93205e2e5 100644 --- a/xmtp_debug/src/constants.rs +++ b/xmtp_debug/src/constants.rs @@ -9,5 +9,18 @@ pub static XMTP_DEV: LazyLock = LazyLock::new(|| Url::parse("https://grpc.dev.xmtp.network:443").unwrap()); pub static XMTP_LOCAL: LazyLock = LazyLock::new(|| Url::parse("http://localhost:5556").unwrap()); + +pub static XMTP_PRODUCTION_D14N: LazyLock = LazyLock::new(|| Url::parse("").unwrap()); +pub static XMTP_DEV_D14N: LazyLock = + LazyLock::new(|| Url::parse("https://grpc.testnet.xmtp.network:443").unwrap()); +pub static XMTP_LOCAL_D14N: LazyLock = + LazyLock::new(|| Url::parse("http://localhots:5050").unwrap()); + +pub static XMTP_PRODUCTION_PAYER: LazyLock = LazyLock::new(|| Url::parse("").unwrap()); +pub static XMTP_DEV_PAYER: LazyLock = + LazyLock::new(|| Url::parse("https://payer.testnet.xmtp.network:443").unwrap()); +pub static XMTP_LOCAL_PAYER: LazyLock = + LazyLock::new(|| Url::parse("http://localhost:5050").unwrap()); + pub static TMPDIR: LazyLock = LazyLock::::new(|| TempDir::new().unwrap()); pub const STORAGE_PREFIX: &str = "xdbg"; diff --git a/xmtp_debug/src/main.rs b/xmtp_debug/src/main.rs index 10dbeb294..fbaf4deb7 100644 --- a/xmtp_debug/src/main.rs +++ b/xmtp_debug/src/main.rs @@ -7,8 +7,10 @@ use clap::Parser; use color_eyre::eyre::Result; use xmtp_api_grpc::grpc_api_helper::Client as GrpcClient; +use xmtp_mls::XmtpApi; -pub type DbgClient = xmtp_mls::client::Client; +// pub type DbgClient = xmtp_mls::client::Client; +type DbgClient = xmtp_mls::client::Client>; #[macro_use] extern crate tracing;