Skip to content

Commit

Permalink
use PathBuf for system contract paths
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgar2017 committed Jan 16, 2025
1 parent 99be488 commit d7e8a95
Showing 1 changed file with 124 additions and 59 deletions.
183 changes: 124 additions & 59 deletions compiler_tester/src/vm/eravm/system_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,87 +36,110 @@ pub struct SystemContracts {

impl SystemContracts {
/// The empty contract implementation path.
const PATH_EMPTY_CONTRACT: &'static str =
r"era-contracts\system-contracts\contracts\EmptyContract.sol:EmptyContract";
const PATH_EMPTY_CONTRACT: (&'static str, &'static str) = (
"era-contracts/system-contracts/contracts/EmptyContract.sol",
"EmptyContract",
);

/// The default account abstraction contract implementation path.
const PATH_DEFAULT_AA: &'static str =
r"era-contracts\system-contracts\contracts\DefaultAccount.sol:DefaultAccount";
const PATH_DEFAULT_AA: (&'static str, &'static str) = (
"era-contracts/system-contracts/contracts/DefaultAccount.sol",
"DefaultAccount",
);

/// The EVM emulator system contract implementation path.
const PATH_EVM_EMULATOR: &'static str =
r"era-contracts\system-contracts\contracts\EvmEmulator.yul";
"era-contracts/system-contracts/contracts/EvmEmulator.yul";

/// The `keccak256` system contract implementation path.
const PATH_KECCAK256: &'static str =
r"era-contracts\system-contracts\contracts\precompiles\Keccak256.yul";
"era-contracts/system-contracts/contracts/precompiles/Keccak256.yul";

/// The `ecrecover` system contract implementation path.
const PATH_ECRECOVER: &'static str =
r"era-contracts\system-contracts\contracts\precompiles\Ecrecover.yul";
"era-contracts/system-contracts/contracts/precompiles/Ecrecover.yul";

/// The `sha256` system contract implementation path.
const PATH_SHA256: &'static str =
r"era-contracts\system-contracts\contracts\precompiles\SHA256.yul";
"era-contracts/system-contracts/contracts/precompiles/SHA256.yul";

/// The `identity` system contract implementation path.
const PATH_IDENTITY: &'static str = r"tests\solidity\simple\system\identity.sol:Identity";
const PATH_IDENTITY: (&'static str, &'static str) =
("tests/solidity/simple/system/identity.sol", "Identity");

/// The `ecadd` system contract implementation path.
const PATH_ECADD: &'static str =
r"era-contracts\system-contracts\contracts\precompiles\EcAdd.yul";
"era-contracts/system-contracts/contracts/precompiles/EcAdd.yul";

/// The `ecmul` system contract implementation path.
const PATH_ECMUL: &'static str =
r"era-contracts\system-contracts\contracts\precompiles\EcMul.yul";
"era-contracts/system-contracts/contracts/precompiles/EcMul.yul";

/// The account code storage system contract implementation path.
const PATH_ACCOUNT_CODE_STORAGE: &'static str =
r"era-contracts\system-contracts\contracts\AccountCodeStorage.sol:AccountCodeStorage";
const PATH_ACCOUNT_CODE_STORAGE: (&'static str, &'static str) = (
"era-contracts/system-contracts/contracts/AccountCodeStorage.sol",
"AccountCodeStorage",
);

/// The contract deployer system contract implementation path.
const PATH_CONTRACT_DEPLOYER: &'static str =
r"era-contracts\system-contracts\contracts\ContractDeployer.sol:ContractDeployer";
const PATH_CONTRACT_DEPLOYER: (&'static str, &'static str) = (
"era-contracts/system-contracts/contracts/ContractDeployer.sol",
"ContractDeploye",
);

/// The nonce holder system contract implementation path.
const PATH_NONCE_HOLDER: &'static str =
r"era-contracts\system-contracts\contracts\NonceHolder.sol:NonceHolder";
const PATH_NONCE_HOLDER: (&'static str, &'static str) = (
"era-contracts/system-contracts/contracts/NonceHolder.sol",
"NonceHolde",
);

/// The knows codes storage system contract implementation path.
const PATH_KNOWN_CODES_STORAGE: &'static str =
r"era-contracts\system-contracts\contracts\KnownCodesStorage.sol:KnownCodesStorage";
const PATH_KNOWN_CODES_STORAGE: (&'static str, &'static str) = (
"era-contracts/system-contracts/contracts/KnownCodesStorage.sol",
"KnownCodesStorage",
);

/// The immutable simulator system contract implementation path.
const PATH_IMMUTABLE_SIMULATOR: &'static str =
r"era-contracts\system-contracts\contracts\ImmutableSimulator.sol:ImmutableSimulator";
const PATH_IMMUTABLE_SIMULATOR: (&'static str, &'static str) = (
"era-contracts/system-contracts/contracts/ImmutableSimulator.sol",
"ImmutableSimulato",
);

/// The L1-messenger system contract implementation path.
const PATH_L1_MESSENGER: &'static str =
r"era-contracts\system-contracts\contracts\L1Messenger.sol:L1Messenger";
const PATH_L1_MESSENGER: (&'static str, &'static str) = (
"era-contracts/system-contracts/contracts/L1Messenger.sol",
"L1Messenge",
);

/// The `msg.value` simulator system contract implementation path.
const PATH_MSG_VALUE_SIMULATOR: &'static str =
r"era-contracts\system-contracts\contracts\MsgValueSimulator.sol:MsgValueSimulator";
const PATH_MSG_VALUE_SIMULATOR: (&'static str, &'static str) = (
"era-contracts/system-contracts/contracts/MsgValueSimulator.sol",
"MsgValueSimulato",
);

/// The system context system contract implementation path.
const PATH_SYSTEM_CONTEXT: &'static str =
r"era-contracts\system-contracts\contracts\SystemContext.sol:SystemContext";
const PATH_SYSTEM_CONTEXT: (&'static str, &'static str) = (
"era-contracts/system-contracts/contracts/SystemContext.sol",
"SystemContext",
);

/// The event writer system contract implementation path.
const PATH_EVENT_WRITER: &'static str =
r"era-contracts\system-contracts\contracts\EventWriter.yul";
"era-contracts/system-contracts/contracts/EventWriter.yul";

/// The code oracle system contract implementation path.
const PATH_CODE_ORACLE: &'static str =
r"era-contracts\system-contracts\contracts\precompiles/CodeOracle.yul";
"era-contracts/system-contracts/contracts/precompiles/CodeOracle.yul";

/// The base token system contract implementation path.
const PATH_BASE_TOKEN: &'static str =
r"era-contracts\system-contracts\contracts\L2BaseToken.sol:L2BaseToken";
const PATH_BASE_TOKEN: (&'static str, &'static str) = (
"era-contracts/system-contracts/contracts/BaseToken.sol",
"BaseToken",
);

/// The EVM gas manager system contract implementation path.
const PATH_EVM_GAS_MANAGER: &'static str =
r"era-contracts\system-contracts\contracts\EvmGasManager.yul";
"era-contracts/system-contracts/contracts/EvmGasManager.yul";

///
/// Loads or builds the system contracts.
Expand Down Expand Up @@ -157,104 +180,128 @@ impl SystemContracts {
let yul_system_contracts = [
(
web3::types::Address::from_low_u64_be(zkevm_opcode_defs::ADDRESS_KECCAK256.into()),
Self::PATH_KECCAK256,
Self::PATH_KECCAK256.to_owned(),
),
(
web3::types::Address::from_low_u64_be(zkevm_opcode_defs::ADDRESS_ECRECOVER.into()),
Self::PATH_ECRECOVER,
Self::PATH_ECRECOVER.to_owned(),
),
(
web3::types::Address::from_low_u64_be(zkevm_opcode_defs::ADDRESS_SHA256.into()),
Self::PATH_SHA256,
Self::PATH_SHA256.to_owned(),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::system_params::ADDRESS_ECADD.into(),
),
Self::PATH_ECADD,
Self::PATH_ECADD.to_owned(),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::system_params::ADDRESS_ECMUL.into(),
),
Self::PATH_ECMUL,
Self::PATH_ECMUL.to_owned(),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::ADDRESS_EVENT_WRITER.into(),
),
Self::PATH_EVENT_WRITER,
Self::PATH_EVENT_WRITER.to_owned(),
),
(
web3::types::Address::from_low_u64_be(0x8012),
Self::PATH_CODE_ORACLE,
Self::PATH_CODE_ORACLE.to_owned(),
),
(
web3::types::Address::from_low_u64_be(ADDRESS_EVM_GAS_MANAGER.into()),
Self::PATH_EVM_GAS_MANAGER,
Self::PATH_EVM_GAS_MANAGER.to_owned(),
),
];

let solidity_system_contracts = vec![
(web3::types::Address::zero(), Self::PATH_EMPTY_CONTRACT),
(
web3::types::Address::zero(),
Self::normalize_path(
Self::PATH_EMPTY_CONTRACT.0,
Some(Self::PATH_EMPTY_CONTRACT.1),
),
),
(
web3::types::Address::from_low_u64_be(zkevm_opcode_defs::ADDRESS_IDENTITY.into()),
Self::PATH_IDENTITY,
Self::normalize_path(Self::PATH_IDENTITY.0, None),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::ADDRESS_ACCOUNT_CODE_STORAGE.into(),
),
Self::PATH_ACCOUNT_CODE_STORAGE,
Self::normalize_path(
Self::PATH_ACCOUNT_CODE_STORAGE.0,
Some(Self::PATH_ACCOUNT_CODE_STORAGE.1),
),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::ADDRESS_NONCE_HOLDER.into(),
),
Self::PATH_NONCE_HOLDER,
Self::normalize_path(Self::PATH_NONCE_HOLDER.0, Some(Self::PATH_NONCE_HOLDER.1)),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::ADDRESS_KNOWN_CODES_STORAGE.into(),
),
Self::PATH_KNOWN_CODES_STORAGE,
Self::normalize_path(
Self::PATH_KNOWN_CODES_STORAGE.0,
Some(Self::PATH_KNOWN_CODES_STORAGE.1),
),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::ADDRESS_IMMUTABLE_SIMULATOR.into(),
),
Self::PATH_IMMUTABLE_SIMULATOR,
Self::normalize_path(
Self::PATH_IMMUTABLE_SIMULATOR.0,
Some(Self::PATH_IMMUTABLE_SIMULATOR.1),
),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::ADDRESS_CONTRACT_DEPLOYER.into(),
),
Self::PATH_CONTRACT_DEPLOYER,
Self::normalize_path(
Self::PATH_CONTRACT_DEPLOYER.0,
Some(Self::PATH_CONTRACT_DEPLOYER.1),
),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::ADDRESS_L1_MESSENGER.into(),
),
Self::PATH_L1_MESSENGER,
Self::normalize_path(Self::PATH_L1_MESSENGER.0, Some(Self::PATH_L1_MESSENGER.1)),
),
(
web3::types::Address::from_low_u64_be(zkevm_opcode_defs::ADDRESS_MSG_VALUE.into()),
Self::PATH_MSG_VALUE_SIMULATOR,
Self::normalize_path(
Self::PATH_MSG_VALUE_SIMULATOR.0,
Some(Self::PATH_MSG_VALUE_SIMULATOR.1),
),
),
(
web3::types::Address::from_low_u64_be(
zkevm_opcode_defs::ADDRESS_SYSTEM_CONTEXT.into(),
),
Self::PATH_SYSTEM_CONTEXT,
Self::normalize_path(
Self::PATH_SYSTEM_CONTEXT.0,
Some(Self::PATH_SYSTEM_CONTEXT.1),
),
),
(
web3::types::Address::from_low_u64_be(zkevm_opcode_defs::ADDRESS_ETH_TOKEN.into()),
Self::PATH_BASE_TOKEN,
Self::normalize_path(Self::PATH_BASE_TOKEN.0, Some(Self::PATH_BASE_TOKEN.1)),
),
];

let mut yul_file_paths = Vec::with_capacity(yul_system_contracts.len() + 1);
for (_, path) in yul_system_contracts.into_iter() {
for (_, path) in yul_system_contracts.iter() {
yul_file_paths.push(path.to_owned());
}
yul_file_paths.push(Self::PATH_EVM_EMULATOR.to_owned());
Expand Down Expand Up @@ -317,12 +364,19 @@ impl SystemContracts {
debug_config,
)?);

let default_aa = builds.remove(Self::PATH_DEFAULT_AA).ok_or_else(|| {
anyhow::anyhow!("The default AA code not found in the compiler build artifacts")
})?;
let evm_emulator = builds.remove(Self::PATH_EVM_EMULATOR).ok_or_else(|| {
anyhow::anyhow!("The EVM emulator code not found in the compiler build artifacts")
})?;
let default_aa = builds
.remove(
Self::normalize_path(Self::PATH_DEFAULT_AA.0, Some(Self::PATH_DEFAULT_AA.1))
.as_str(),
)
.ok_or_else(|| {
anyhow::anyhow!("The default AA code not found in the compiler build artifacts")
})?;
let evm_emulator = builds
.remove(Self::normalize_path(Self::PATH_EVM_EMULATOR, None).as_str())
.ok_or_else(|| {
anyhow::anyhow!("The EVM emulator code not found in the compiler build artifacts")
})?;

let mut system_contracts =
Vec::with_capacity(solidity_system_contracts.len() + yul_system_contracts.len());
Expand All @@ -332,7 +386,7 @@ impl SystemContracts {
let mut deployed_contracts = Vec::with_capacity(system_contracts.len());
for (address, path) in system_contracts.into_iter() {
let build = builds
.remove(path)
.remove(path.as_str())
.unwrap_or_else(|| panic!("System contract `{path}` not found in the builds"));
deployed_contracts.push((address, build));
}
Expand Down Expand Up @@ -393,6 +447,17 @@ impl SystemContracts {
Ok(())
}

///
/// Returns a unified path for a system contract.
///
fn normalize_path(path: &str, name: Option<&str>) -> String {
let contract_name = era_compiler_common::ContractName::new(
path.to_string(),
name.map(|name| name.to_string()),
);
contract_name.full_path
}

///
/// Compiles the system contracts.
///
Expand Down

0 comments on commit d7e8a95

Please sign in to comment.