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
This commit is contained in:
Diana Carvalho
2025-02-06 12:39:19 +00:00
parent c791c93cb5
commit f5232f403e
4 changed files with 20 additions and 16 deletions

View File

@@ -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");

View File

@@ -3,7 +3,6 @@ use std::collections::HashMap;
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
evm::{ evm::{
constants::DEFAULT_EXECUTORS_FILE_PATH,
strategy_encoder::strategy_encoders::{ExecutorStrategyEncoder, SplitSwapStrategyEncoder}, strategy_encoder::strategy_encoders::{ExecutorStrategyEncoder, SplitSwapStrategyEncoder},
swap_encoder::swap_encoder_registry::SwapEncoderRegistry, swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
}, },
@@ -26,10 +25,7 @@ impl StrategyEncoderRegistry for EVMStrategyEncoderRegistry {
executors_file_path: Option<&str>, executors_file_path: Option<&str>,
signer_pk: Option<String>, signer_pk: Option<String>,
) -> Result<Self, EncodingError> { ) -> Result<Self, EncodingError> {
let swap_encoder_registry = SwapEncoderRegistry::new( let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, chain.clone())?;
executors_file_path.unwrap_or(DEFAULT_EXECUTORS_FILE_PATH),
chain.clone(),
)?;
let mut strategies: HashMap<String, Box<dyn StrategyEncoder>> = HashMap::new(); let mut strategies: HashMap<String, Box<dyn StrategyEncoder>> = HashMap::new();
strategies.insert( strategies.insert(

View File

@@ -506,7 +506,7 @@ mod tests {
fn get_swap_encoder_registry() -> SwapEncoderRegistry { fn get_swap_encoder_registry() -> SwapEncoderRegistry {
let eth_chain = eth_chain(); let eth_chain = eth_chain();
SwapEncoderRegistry::new("src/encoding/config/executor_addresses.json", eth_chain).unwrap() SwapEncoderRegistry::new(None, eth_chain).unwrap()
} }
#[test] #[test]

View File

@@ -1,7 +1,8 @@
use std::{collections::HashMap, fs}; use std::{collections::HashMap, fs};
use crate::encoding::{ 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, swap_encoder::SwapEncoder,
}; };
@@ -15,14 +16,20 @@ pub struct SwapEncoderRegistry {
impl SwapEncoderRegistry { impl SwapEncoderRegistry {
/// Populates the registry with the `SwapEncoders` for the given blockchain by parsing the /// Populates the registry with the `SwapEncoders` for the given blockchain by parsing the
/// executors' addresses in the file at the given path. /// executors' addresses in the file at the given path.
pub fn new(executors_file_path: &str, blockchain: Chain) -> Result<Self, EncodingError> { pub fn new(
let config_str = fs::read_to_string(executors_file_path).map_err(|e| { executors_file_path: Option<&str>,
EncodingError::FatalError(format!( blockchain: Chain,
"Error reading executors file from {}: {}", ) -> Result<Self, EncodingError> {
executors_file_path, let config_str = if let Some(path) = executors_file_path {
e.to_string() 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<String, HashMap<String, String>> = serde_json::from_str(&config_str)?; let config: HashMap<String, HashMap<String, String>> = serde_json::from_str(&config_str)?;
let mut encoders = HashMap::new(); let mut encoders = HashMap::new();
let executors = config let executors = config