From 8f829b606f4962c5133697e28c0f3a7b595b4214 Mon Sep 17 00:00:00 2001 From: Dakota Brink Date: Wed, 9 Oct 2024 14:04:04 -0400 Subject: [PATCH 1/4] Upgrade default urls to paid/private if appropriate env vars are present --- mls_validation_service/src/main.rs | 2 +- .../src/scw_verifier/chain_urls_default.json | 6 +- xmtp_id/src/scw_verifier/mod.rs | 58 +++++++++++-------- xmtp_mls/src/utils/test.rs | 4 +- 4 files changed, 40 insertions(+), 30 deletions(-) diff --git a/mls_validation_service/src/main.rs b/mls_validation_service/src/main.rs index dedcbc540..b133f8547 100644 --- a/mls_validation_service/src/main.rs +++ b/mls_validation_service/src/main.rs @@ -30,7 +30,7 @@ async fn main() -> Result<(), Box> { 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( diff --git a/xmtp_id/src/scw_verifier/chain_urls_default.json b/xmtp_id/src/scw_verifier/chain_urls_default.json index a079c84b5..47f07c32b 100644 --- a/xmtp_id/src/scw_verifier/chain_urls_default.json +++ b/xmtp_id/src/scw_verifier/chain_urls_default.json @@ -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" } diff --git a/xmtp_id/src/scw_verifier/mod.rs b/xmtp_id/src/scw_verifier/mod.rs index 9d69eb84d..44a6667d2 100644 --- a/xmtp_id/src/scw_verifier/mod.rs +++ b/xmtp_id/src/scw_verifier/mod.rs @@ -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; @@ -71,6 +77,14 @@ pub struct MultiSmartContractSignatureVerifier { verifiers: HashMap>, } +impl Default for MultiSmartContractSignatureVerifier { + fn default() -> Self { + let urls: HashMap = + serde_json::from_str(DEFAULT_CHAIN_URLS).expect("DEFAULT_CHAIN_URLS is malformatted"); + Self::new(urls).upgrade() + } +} + impl MultiSmartContractSignatureVerifier { pub fn new(urls: HashMap) -> Self { let verifiers: HashMap> = urls @@ -87,32 +101,26 @@ impl MultiSmartContractSignatureVerifier { Self { verifiers } } - pub fn new_from_file(path: impl AsRef) -> 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 = - serde_json::from_str(json).unwrap_or_else(|_| panic!("{path:?} is malformatted")); + pub fn new_from_file(path: impl AsRef) -> Result { + let json = fs::read_to_string(path.as_ref())?; + let urls: HashMap = 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}")) { + *verif = Box::new(RpcSmartContractWalletVerifier::new(url)); + }; + }); + self } } diff --git a/xmtp_mls/src/utils/test.rs b/xmtp_mls/src/utils/test.rs index 9f183ed49..fad72951e 100755 --- a/xmtp_mls/src/utils/test.rs +++ b/xmtp_mls/src/utils/test.rs @@ -109,9 +109,7 @@ impl ClientBuilder { pub async fn local_client(self) -> Self { self.api_client(::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 { From 561b414ccbb9c66c12d5e5f339eaeeb28694bf86 Mon Sep 17 00:00:00 2001 From: Dakota Brink Date: Wed, 9 Oct 2024 14:09:46 -0400 Subject: [PATCH 2/4] still take the json file from a provided path --- mls_validation_service/src/config.rs | 4 ++++ mls_validation_service/src/main.rs | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/mls_validation_service/src/config.rs b/mls_validation_service/src/config.rs index 2fefae233..0b0f2a80e 100644 --- a/mls_validation_service/src/config.rs +++ b/mls_validation_service/src/config.rs @@ -10,4 +10,8 @@ pub(crate) struct Args { #[arg(long, default_value_t = 50052)] pub(crate) health_check_port: u32, + + // A json formatted chain rpc urls in the same format as chain_urls_default.json in the codebase. + #[arg(long)] + pub(crate) chain_urls: Option, } diff --git a/mls_validation_service/src/main.rs b/mls_validation_service/src/main.rs index b133f8547..f3b5a0673 100644 --- a/mls_validation_service/src/main.rs +++ b/mls_validation_service/src/main.rs @@ -30,7 +30,10 @@ async fn main() -> Result<(), Box> { let health_server = health_check_server(args.health_check_port as u16); - let scw_verifier = MultiSmartContractSignatureVerifier::default(); + 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( From 66eb1b773ca844745ab00e719904b65bb46a699a Mon Sep 17 00:00:00 2001 From: Dakota Brink Date: Wed, 9 Oct 2024 14:11:55 -0400 Subject: [PATCH 3/4] better description --- mls_validation_service/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mls_validation_service/src/config.rs b/mls_validation_service/src/config.rs index 0b0f2a80e..8de6da78d 100644 --- a/mls_validation_service/src/config.rs +++ b/mls_validation_service/src/config.rs @@ -11,7 +11,7 @@ pub(crate) struct Args { #[arg(long, default_value_t = 50052)] pub(crate) health_check_port: u32, - // A json formatted chain rpc urls in the same format as chain_urls_default.json in the codebase. + // 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, } From 852160d2115977e4ddd0d34102f5feba6fc881a8 Mon Sep 17 00:00:00 2001 From: Dakota Brink Date: Wed, 9 Oct 2024 14:21:40 -0400 Subject: [PATCH 4/4] log when default --- xmtp_id/src/scw_verifier/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/xmtp_id/src/scw_verifier/mod.rs b/xmtp_id/src/scw_verifier/mod.rs index 44a6667d2..002491151 100644 --- a/xmtp_id/src/scw_verifier/mod.rs +++ b/xmtp_id/src/scw_verifier/mod.rs @@ -16,6 +16,7 @@ use std::{ path::Path, }; use thiserror::Error; +use tracing::info; use url::Url; pub use chain_rpc_verifier::*; @@ -87,7 +88,7 @@ impl Default for MultiSmartContractSignatureVerifier { impl MultiSmartContractSignatureVerifier { pub fn new(urls: HashMap) -> Self { - let verifiers: HashMap> = urls + let verifiers = urls .into_iter() .map(|(chain_id, url)| { ( @@ -113,11 +114,13 @@ impl MultiSmartContractSignatureVerifier { Ok(Self::new(urls)) } - /// Upgrade the default urls to paid/private urls if the env vars are present. + /// 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