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

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

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mls_validation_service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ 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 = 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"
}
58 changes: 33 additions & 25 deletions xmtp_id/src/scw_verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ 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 url::Url;

Expand Down Expand Up @@ -71,6 +77,14 @@ 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
Expand All @@ -87,32 +101,26 @@ 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 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}")) {
codabrink marked this conversation as resolved.
Show resolved Hide resolved
*verif = Box::new(RpcSmartContractWalletVerifier::new(url));
};
});
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