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

feat: simplify defition of env-var aliases #1579

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 src/bin/run_with_importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn run(config: RunWithImporterConfig) -> anyhow::Result<()> {
// config
rpc_config.clone(),
rpc_config.rpc_server,
rpc_config.executor.chain_id.into(),
rpc_config.executor.executor_chain_id.into(),
)
.await;
GlobalState::shutdown_from(TASK_NAME, "rpc server finished unexpectedly");
Expand Down
4 changes: 2 additions & 2 deletions src/eth/executor/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Evm {
handler.set_instruction_table(instructions);

// configure revm
let chain_id = config.chain_id;
let chain_id = config.executor_chain_id;
let mut evm = RevmEvm::builder()
.with_external_context(())
.with_db(RevmSession::new(storage, config))
Expand Down Expand Up @@ -238,7 +238,7 @@ impl Database for RevmSession {
// warn if the loaded account is the `to` account and it does not have a bytecode
if let Some(ref to_address) = self.input.to {
if account.bytecode.is_none() && &address == to_address && self.input.is_contract_call() {
if self.config.reject_not_contract {
if self.config.executor_reject_not_contract {
return Err(StratusError::TransactionAccountNotContract { address: *to_address });
} else {
tracing::warn!(%address, "evm to_account is not a contract because does not have bytecode");
Expand Down
10 changes: 5 additions & 5 deletions src/eth/executor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ impl Evms {
evm_tx
};

let tx_parallel = match config.strategy {
let tx_parallel = match config.executor_strategy {
ExecutorStrategy::Serial => spawn_evms("evm-tx-unused", 1), // should not really be used if strategy is serial, but keep 1 for fallback
ExecutorStrategy::Paralell => spawn_evms("evm-tx-parallel", config.num_evms),
ExecutorStrategy::Paralell => spawn_evms("evm-tx-parallel", config.executor_evms),
};
let tx_serial = spawn_evms("evm-tx-serial", 1);
let tx_external = spawn_evms("evm-tx-external", 1);
let call_present = spawn_evms("evm-call-present", max(config.num_evms / 2, 1));
let call_past = spawn_evms("evm-call-past", max(config.num_evms / 4, 1));
let call_present = spawn_evms("evm-call-present", max(config.executor_evms / 2, 1));
let call_past = spawn_evms("evm-call-past", max(config.executor_evms / 4, 1));

Evms {
tx_parallel,
Expand Down Expand Up @@ -370,7 +370,7 @@ impl Executor {
// execute according to the strategy
const INFINITE_ATTEMPTS: usize = usize::MAX;

let tx_execution = match self.config.strategy {
let tx_execution = match self.config.executor_strategy {
// Executes transactions in serial mode:
// * Uses a Mutex, so a new transactions starts executing only after the previous one is executed and persisted.
// * Without a Mutex, conflict can happen because the next transactions starts executing before the previous one is saved.
Expand Down
23 changes: 14 additions & 9 deletions src/eth/executor/executor_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@ use crate::eth::storage::StratusStorage;
#[derive(Parser, DebugAsJson, Clone, serde::Serialize)]
pub struct ExecutorConfig {
/// Chain ID of the network.
#[arg(long = "chain-id", env = "CHAIN_ID")]
pub chain_id: u64,
#[arg(long = "executor-chain-id", alias = "chain-id", env = "EXECUTOR_CHAIN_ID")]
pub executor_chain_id: u64,

/// Number of EVM instances to run.
///
/// TODO: should be configured for each kind of EvmRoute instead of being a single value.
#[arg(long = "evms", env = "EVMS")]
pub num_evms: usize,
#[arg(long = "executor-evms", alias = "evms", env = "EXECUTOR_EVMS")]
pub executor_evms: usize,

/// EVM execution strategy.
#[arg(long = "strategy", env = "STRATEGY", default_value = "serial")]
pub strategy: ExecutorStrategy,
#[arg(long = "executor-strategy", alias = "strategy", env = "EXECUTOR_STRATEGY", default_value = "serial")]
pub executor_strategy: ExecutorStrategy,

/// Should reject contract transactions and calls to accounts that are not contracts?
#[arg(long = "reject-not-contract", env = "REJECT_NOT_CONTRACT", default_value = "true")]
pub reject_not_contract: bool,
#[arg(
long = "executor-reject-not-contract",
alias = "reject-not-contract",
env = "EXECUTOR_REJECT_NOT_CONTRACT",
default_value = "true"
)]
pub executor_reject_not_contract: bool,
}

impl ExecutorConfig {
Expand All @@ -36,7 +41,7 @@ impl ExecutorConfig {
/// Note: Should be called only after async runtime is initialized.
pub fn init(&self, storage: Arc<StratusStorage>, miner: Arc<Miner>) -> Arc<Executor> {
let mut config = self.clone();
config.num_evms = max(config.num_evms, 1);
config.executor_evms = max(config.executor_evms, 1);
tracing::info!(?config, "creating executor");

let executor = Executor::new(storage, miner, config);
Expand Down
26 changes: 18 additions & 8 deletions src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ where
where
T: clap::Parser + WithCommonConfig + Debug,
{
// translate renamed environment variables because clap does not support multiple aliases for env-vars
if let Ok(value) = env::var("TRACING_COLLECTOR_URL") {
env::set_var("TRACING_URL", value);
}
if let Ok(value) = env::var("LOG_FORMAT") {
env::set_var("TRACING_LOG_FORMAT", value);
}
// .dotfile support
load_dotenv();

// apply env-var aliases
env_alias("EXECUTOR_CHAIN_ID", "CHAIN_ID");
env_alias("EXECUTOR_EVMS", "EVMS");
env_alias("EXECUTOR_EVMS", "NUM_EVMS");
env_alias("EXECUTOR_REJECT_NOT_CONTRACT", "REJECT_NOT_CONTRACT");
env_alias("EXECUTOR_STRATEGY", "STRATEGY");
env_alias("TRACING_LOG_FORMAT", "LOG_FORMAT");
env_alias("TRACING_URL", "TRACING_COLLECTOR_URL");

// parse configuration
load_dotenv();
let config = T::parse();
let common = config.common();

Expand Down Expand Up @@ -78,6 +81,13 @@ where
}
}

/// Translates an aliased environment variable to a canonical one.
fn env_alias(canonical: &'static str, alias: &'static str) {
if let Ok(value) = env::var(alias) {
env::set_var(canonical, value);
}
}

// -----------------------------------------------------------------------------
// Global state
// -----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn run(config: StratusConfig) -> anyhow::Result<()> {
// config
config.clone(),
config.rpc_server,
config.executor.chain_id.into(),
config.executor.executor_chain_id.into(),
)
.await?;

Expand Down
4 changes: 2 additions & 2 deletions tests/test_import_external_snapshot_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ pub fn init_config_and_data(
) {
// init config
let mut global_services = GlobalServices::<IntegrationTestConfig>::init();
global_services.config.executor.chain_id = 2009;
global_services.config.executor.num_evms = 8;
global_services.config.executor.executor_chain_id = 2009;
global_services.config.executor.executor_evms = 8;

// init block data
let block_json = fs::read_to_string(format!("tests/fixtures/snapshots/{}/block.json", block_number)).unwrap();
Expand Down