From f5232f403ee8f09c3bf83be865e326540b781740 Mon Sep 17 00:00:00 2001 From: Diana Carvalho Date: Thu, 6 Feb 2025 12:39:19 +0000 Subject: [PATCH] feat: Read default executors at compile time into a json This way we don't have issues with paths and trying to read files that are at other locations after compile time --- don't change below this line --- ENG-4088 Took 44 seconds Took 52 seconds --- src/encoding/evm/constants.rs | 3 ++- .../strategy_encoder_registry.rs | 6 +---- .../evm/strategy_encoder/strategy_encoders.rs | 2 +- .../evm/swap_encoder/swap_encoder_registry.rs | 25 ++++++++++++------- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/encoding/evm/constants.rs b/src/encoding/evm/constants.rs index 177b7d9..1426dd4 100644 --- a/src/encoding/evm/constants.rs +++ b/src/encoding/evm/constants.rs @@ -1 +1,2 @@ -pub const DEFAULT_EXECUTORS_FILE_PATH: &str = "src/encoding/config/executor_addresses.json"; +pub const DEFAULT_EXECUTORS_JSON: &str = + include_str!("../../../src/encoding/config/executor_addresses.json"); diff --git a/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs b/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs index 0109aa1..a245476 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoder_registry.rs @@ -3,7 +3,6 @@ use std::collections::HashMap; use crate::encoding::{ errors::EncodingError, evm::{ - constants::DEFAULT_EXECUTORS_FILE_PATH, strategy_encoder::strategy_encoders::{ExecutorStrategyEncoder, SplitSwapStrategyEncoder}, swap_encoder::swap_encoder_registry::SwapEncoderRegistry, }, @@ -26,10 +25,7 @@ impl StrategyEncoderRegistry for EVMStrategyEncoderRegistry { executors_file_path: Option<&str>, signer_pk: Option, ) -> Result { - let swap_encoder_registry = SwapEncoderRegistry::new( - executors_file_path.unwrap_or(DEFAULT_EXECUTORS_FILE_PATH), - chain.clone(), - )?; + let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain.clone())?; let mut strategies: HashMap> = HashMap::new(); strategies.insert( diff --git a/src/encoding/evm/strategy_encoder/strategy_encoders.rs b/src/encoding/evm/strategy_encoder/strategy_encoders.rs index 73e0f71..dd3abfc 100644 --- a/src/encoding/evm/strategy_encoder/strategy_encoders.rs +++ b/src/encoding/evm/strategy_encoder/strategy_encoders.rs @@ -506,7 +506,7 @@ mod tests { fn get_swap_encoder_registry() -> SwapEncoderRegistry { let eth_chain = eth_chain(); - SwapEncoderRegistry::new("src/encoding/config/executor_addresses.json", eth_chain).unwrap() + SwapEncoderRegistry::new(None, eth_chain).unwrap() } #[test] diff --git a/src/encoding/evm/swap_encoder/swap_encoder_registry.rs b/src/encoding/evm/swap_encoder/swap_encoder_registry.rs index f0bd2a8..401592d 100644 --- a/src/encoding/evm/swap_encoder/swap_encoder_registry.rs +++ b/src/encoding/evm/swap_encoder/swap_encoder_registry.rs @@ -1,7 +1,8 @@ use std::{collections::HashMap, fs}; use crate::encoding::{ - errors::EncodingError, evm::swap_encoder::builder::SwapEncoderBuilder, models::Chain, + errors::EncodingError, + evm::{constants::DEFAULT_EXECUTORS_JSON, swap_encoder::builder::SwapEncoderBuilder}, models::Chain, swap_encoder::SwapEncoder, }; @@ -15,14 +16,20 @@ pub struct SwapEncoderRegistry { impl SwapEncoderRegistry { /// Populates the registry with the `SwapEncoders` for the given blockchain by parsing the /// executors' addresses in the file at the given path. - pub fn new(executors_file_path: &str, blockchain: Chain) -> Result { - let config_str = fs::read_to_string(executors_file_path).map_err(|e| { - EncodingError::FatalError(format!( - "Error reading executors file from {}: {}", - executors_file_path, - e.to_string() - )) - })?; + pub fn new( + executors_file_path: Option<&str>, + blockchain: Chain, + ) -> Result { + let config_str = if let Some(path) = executors_file_path { + fs::read_to_string(path).map_err(|e| { + EncodingError::FatalError(format!( + "Error reading executors file from {:?}: {}", + executors_file_path, e + )) + })? + } else { + DEFAULT_EXECUTORS_JSON.to_string() + }; let config: HashMap> = serde_json::from_str(&config_str)?; let mut encoders = HashMap::new(); let executors = config