Skip to content

Commit

Permalink
add replication client stubs (#1420)
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx authored Dec 16, 2024
1 parent 3e6725e commit df92c84
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 14 deletions.
9 changes: 2 additions & 7 deletions xmtp_debug/src/app/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ async fn new_client_inner(
wallet: &LocalWallet,
db_path: Option<PathBuf>,
) -> Result<crate::DbgClient> {
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();
Expand Down Expand Up @@ -125,9 +122,7 @@ async fn existing_client_inner(
network: &args::BackendOpts,
db_path: PathBuf,
) -> Result<crate::DbgClient> {
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()),
Expand Down
102 changes: 96 additions & 6 deletions xmtp_debug/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -193,6 +194,76 @@ pub struct BackendOpts {
conflicts_with = "constant-backend"
)]
pub url: Option<url::Url>,
#[arg(
short,
long,
group = "custom-backend",
conflicts_with = "constant-backend"
)]
pub payer_url: Option<url::Url>,
/// Enable the decentralization backend
#[arg(short, long)]
pub d14n: bool,
}

impl BackendOpts {
pub fn payer_url(&self) -> eyre::Result<url::Url> {
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<Box<dyn xmtp_mls::XmtpApi>> {
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 {
Expand All @@ -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,
}
}
}
Expand All @@ -219,8 +293,10 @@ impl From<BackendOpts> for u64 {

impl From<BackendOpts> 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))
}
}

Expand All @@ -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<BackendKind> for url::Url {
fn from(value: BackendKind) -> Self {
use BackendKind::*;
Expand Down
13 changes: 13 additions & 0 deletions xmtp_debug/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,18 @@ pub static XMTP_DEV: LazyLock<Url> =
LazyLock::new(|| Url::parse("https://grpc.dev.xmtp.network:443").unwrap());
pub static XMTP_LOCAL: LazyLock<Url> =
LazyLock::new(|| Url::parse("http://localhost:5556").unwrap());

pub static XMTP_PRODUCTION_D14N: LazyLock<Url> = LazyLock::new(|| Url::parse("").unwrap());
pub static XMTP_DEV_D14N: LazyLock<Url> =
LazyLock::new(|| Url::parse("https://grpc.testnet.xmtp.network:443").unwrap());
pub static XMTP_LOCAL_D14N: LazyLock<Url> =
LazyLock::new(|| Url::parse("http://localhots:5050").unwrap());

pub static XMTP_PRODUCTION_PAYER: LazyLock<Url> = LazyLock::new(|| Url::parse("").unwrap());
pub static XMTP_DEV_PAYER: LazyLock<Url> =
LazyLock::new(|| Url::parse("https://payer.testnet.xmtp.network:443").unwrap());
pub static XMTP_LOCAL_PAYER: LazyLock<Url> =
LazyLock::new(|| Url::parse("http://localhost:5050").unwrap());

pub static TMPDIR: LazyLock<TempDir> = LazyLock::<TempDir>::new(|| TempDir::new().unwrap());
pub const STORAGE_PREFIX: &str = "xdbg";
4 changes: 3 additions & 1 deletion xmtp_debug/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GrpcClient>;
// pub type DbgClient = xmtp_mls::client::Client<GrpcClient>;
type DbgClient = xmtp_mls::client::Client<Box<dyn XmtpApi>>;

#[macro_use]
extern crate tracing;
Expand Down

0 comments on commit df92c84

Please sign in to comment.