Skip to content

Commit

Permalink
Upgrade default urls to paid/private if appropriate env vars are pres…
Browse files Browse the repository at this point in the history
…ent (#1128)

* Upgrade default urls to paid/private if appropriate env vars are present

* still take the json file from a provided path

* better description

* log when default
  • Loading branch information
codabrink authored Oct 9, 2024
1 parent 155d84c commit b69e713
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
4 changes: 4 additions & 0 deletions mls_validation_service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ pub(crate) struct Args {

#[arg(long, default_value_t = 50052)]
pub(crate) health_check_port: u32,

// A path to a json file in the same format as chain_urls_default.json in the codebase.
#[arg(long)]
pub(crate) chain_urls: Option<String>,
}
5 changes: 4 additions & 1 deletion mls_validation_service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let health_server = health_check_server(args.health_check_port as u16);

let scw_verifier = MultiSmartContractSignatureVerifier::new_from_file("chain_urls.json");
let scw_verifier = match args.chain_urls {
Some(path) => MultiSmartContractSignatureVerifier::new_from_file(path)?,
None => MultiSmartContractSignatureVerifier::default(),
};

let grpc_server = Server::builder()
.add_service(ValidationApiServer::new(ValidationService::new(
Expand Down
6 changes: 5 additions & 1 deletion xmtp_id/src/scw_verifier/chain_urls_default.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"1": "https://eth.llamarpc.com",
"10": "https://rpc.ankr.com/optimism",
"137": "https://polygon.llamarpc.com",
"324": "https://mainnet.era.zksync.io",
"8453": "https://base.llamarpc.com",
"42161": "https://arbitrum.llamarpc.com"
"42161": "https://arbitrum.llamarpc.com",
"59144": "https://linea-rpc.publicnode.com"
}
63 changes: 37 additions & 26 deletions xmtp_id/src/scw_verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ use ethers::{
providers::{Http, Provider, ProviderError},
types::{BlockNumber, Bytes},
};
use std::{collections::HashMap, fs, path::Path, str::FromStr};
use std::{
collections::HashMap,
env,
fs::{self},
io,
path::Path,
};
use thiserror::Error;
use tracing::info;
use url::Url;

pub use chain_rpc_verifier::*;
Expand Down Expand Up @@ -71,9 +78,17 @@ pub struct MultiSmartContractSignatureVerifier {
verifiers: HashMap<String, Box<dyn SmartContractSignatureVerifier>>,
}

impl Default for MultiSmartContractSignatureVerifier {
fn default() -> Self {
let urls: HashMap<String, Url> =
serde_json::from_str(DEFAULT_CHAIN_URLS).expect("DEFAULT_CHAIN_URLS is malformatted");
Self::new(urls).upgrade()
}
}

impl MultiSmartContractSignatureVerifier {
pub fn new(urls: HashMap<String, url::Url>) -> Self {
let verifiers: HashMap<String, Box<dyn SmartContractSignatureVerifier>> = urls
let verifiers = urls
.into_iter()
.map(|(chain_id, url)| {
(
Expand All @@ -87,32 +102,28 @@ impl MultiSmartContractSignatureVerifier {
Self { verifiers }
}

pub fn new_from_file(path: impl AsRef<Path>) -> Self {
let path = path.as_ref();

let file_str;
let json = if path.exists() {
file_str = fs::read_to_string(path).unwrap_or_else(|_| panic!("{path:?} is missing"));
&file_str
} else {
DEFAULT_CHAIN_URLS
};

let json: HashMap<String, String> =
serde_json::from_str(json).unwrap_or_else(|_| panic!("{path:?} is malformatted"));
pub fn new_from_file(path: impl AsRef<Path>) -> Result<Self, io::Error> {
let json = fs::read_to_string(path.as_ref())?;
let urls: HashMap<String, Url> = serde_json::from_str(&json).map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("Unable to deserialize json: {err:?}"),
)
})?;

let urls = json
.into_iter()
.map(|(id, url)| {
(
id,
Url::from_str(&url)
.unwrap_or_else(|_| panic!("unable to parse url in {path:?} ({url})")),
)
})
.collect();
Ok(Self::new(urls))
}

Self::new(urls)
/// Upgrade the default urls to paid/private/alternative urls if the env vars are present.
pub fn upgrade(mut self) -> Self {
self.verifiers.iter_mut().for_each(|(id, verif)| {
if let Ok(url) = env::var(format!("CHAIN_RPC_{id}")) {
*verif = Box::new(RpcSmartContractWalletVerifier::new(url));
} else {
info!("No upgraded chain url for chain {id}, using default.");
};
});
self
}
}

Expand Down
4 changes: 1 addition & 3 deletions xmtp_mls/src/utils/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ impl ClientBuilder<TestClient> {

pub async fn local_client(self) -> Self {
self.api_client(<TestClient as XmtpTestClient>::create_local().await)
.scw_signature_verifier(MultiSmartContractSignatureVerifier::new_from_file(
"chain_urls.json",
))
.scw_signature_verifier(MultiSmartContractSignatureVerifier::default())
}

pub async fn new_test_client(owner: &impl InboxOwner) -> Client<TestClient> {
Expand Down

0 comments on commit b69e713

Please sign in to comment.