Skip to content

Commit

Permalink
add replication client stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Dec 16, 2024
1 parent 97eca90 commit 40124eb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
27 changes: 25 additions & 2 deletions xmtp_debug/src/app/clients.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Different ways to create a [`crate::DbgClient`]
use xmtp_mls::XmtpApi;

use super::*;
use crate::app::types::*;

Expand Down Expand Up @@ -60,7 +62,7 @@ async fn new_client_inner(
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 = create_grpc(&url, is_secure, network.d14n).await?;

let nonce = 1;
let inbox_id = generate_inbox_id(&wallet.get_address(), &nonce).unwrap();
Expand Down Expand Up @@ -127,7 +129,7 @@ async fn existing_client_inner(
) -> 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 = create_grpc(&url, is_secure, network.d14n).await?;

let store = EncryptedMessageStore::new(
StorageOption::Persistent(db_path.clone().into_os_string().into_string().unwrap()),
Expand All @@ -145,3 +147,24 @@ async fn existing_client_inner(

Ok(client)
}

async fn create_grpc(
url: &url::Url,
is_secure: bool,
replication: bool,
) -> Result<Box<dyn XmtpApi>> {
if replication {
Ok(Box::new(
xmtp_api_grpc::replication_client::ClientV4::create(
url.as_str().to_string(),
"test".to_string(),
is_secure,
)
.await?,
))
} else {
Ok(Box::new(
crate::GrpcClient::create(url.as_str().to_string(), is_secure).await?,
))
}
}
32 changes: 26 additions & 6 deletions xmtp_debug/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ pub struct BackendOpts {
conflicts_with = "constant-backend"
)]
pub url: Option<url::Url>,
/// Enable the decentralization backend
#[arg(short, long)]
pub d14n: bool,
}

impl<'a> From<&'a BackendOpts> for u64 {
Expand All @@ -202,10 +205,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 +225,8 @@ 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 +238,20 @@ pub enum BackendKind {
Local,
}

impl BackendKind {
fn to_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
7 changes: 7 additions & 0 deletions xmtp_debug/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ 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.dev.test.network:443").unwrap());
pub static XMTP_LOCAL_D14N: LazyLock<Url> =
LazyLock::new(|| Url::parse("http://test:5556").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 40124eb

Please sign in to comment.