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

chore(blockifier): refactor feature contract path getters for different paths #179

Merged
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
69 changes: 49 additions & 20 deletions crates/blockifier/src/test_utils/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ const SECURITY_TEST_CONTRACT_NAME: &str = "security_tests_contract";
const TEST_CONTRACT_NAME: &str = "test_contract";

// ERC20 contract is in a unique location.
const ERC20_CAIRO0_CONTRACT_SOURCE_PATH: &str =
"./ERC20/ERC20_Cairo0/ERC20_without_some_syscalls/ERC20/ERC20.cairo";
const ERC20_CAIRO0_CONTRACT_PATH: &str = "./ERC20/ERC20_Cairo0/ERC20_without_some_syscalls/ERC20/\
erc20_contract_without_some_syscalls_compiled.json";
const ERC20_CAIRO1_CONTRACT_SOURCE_PATH: &str = "./ERC20/ERC20_Cairo1/ERC20.cairo";
const ERC20_CAIRO1_CONTRACT_PATH: &str = "./ERC20/ERC20_Cairo1/erc20.casm.json";

/// Enum representing all feature contracts.
Expand Down Expand Up @@ -186,36 +189,62 @@ impl FeatureContract {
}
}

pub fn get_compiled_path(&self) -> String {
let cairo_version = self.cairo_version();
let contract_name = match self {
fn get_non_erc20_base_name(&self) -> &str {
match self {
Self::AccountWithLongValidate(_) => ACCOUNT_LONG_VALIDATE_NAME,
Self::AccountWithoutValidations(_) => ACCOUNT_WITHOUT_VALIDATIONS_NAME,
Self::Empty(_) => EMPTY_CONTRACT_NAME,
Self::FaultyAccount(_) => FAULTY_ACCOUNT_NAME,
Self::LegacyTestContract => LEGACY_CONTRACT_NAME,
Self::SecurityTests => SECURITY_TEST_CONTRACT_NAME,
Self::TestContract(_) => TEST_CONTRACT_NAME,
// ERC20 is a special case - not in the feature_contracts directory.
Self::ERC20(_) => {
return match cairo_version {
CairoVersion::Cairo0 => ERC20_CAIRO0_CONTRACT_PATH.into(),
CairoVersion::Cairo1 => ERC20_CAIRO1_CONTRACT_PATH.into(),
};
}
};
format!(
"feature_contracts/cairo{}/compiled/{}{}.json",
Self::ERC20(_) => unreachable!(),
}
}

pub fn get_source_path(&self) -> String {
// Special case: ERC20 contract in a different location.
if let Self::ERC20(cairo_version) = self {
match cairo_version {
CairoVersion::Cairo0 => "0",
CairoVersion::Cairo1 => "1",
},
contract_name,
CairoVersion::Cairo0 => ERC20_CAIRO0_CONTRACT_SOURCE_PATH,
CairoVersion::Cairo1 => ERC20_CAIRO1_CONTRACT_SOURCE_PATH,
}
.into()
} else {
format!(
"feature_contracts/cairo{}/{}.cairo",
match self.cairo_version() {
CairoVersion::Cairo0 => "0",
CairoVersion::Cairo1 => "1",
},
self.get_non_erc20_base_name()
)
}
}

pub fn get_compiled_path(&self) -> String {
// ERC20 is a special case - not in the feature_contracts directory.
if let Self::ERC20(cairo_version) = self {
match cairo_version {
CairoVersion::Cairo0 => "_compiled",
CairoVersion::Cairo1 => ".casm",
CairoVersion::Cairo0 => ERC20_CAIRO0_CONTRACT_PATH,
CairoVersion::Cairo1 => ERC20_CAIRO1_CONTRACT_PATH,
}
)
.into()
} else {
let cairo_version = self.cairo_version();
format!(
"feature_contracts/cairo{}/compiled/{}{}.json",
match cairo_version {
CairoVersion::Cairo0 => "0",
CairoVersion::Cairo1 => "1",
},
self.get_non_erc20_base_name(),
match cairo_version {
CairoVersion::Cairo0 => "_compiled",
CairoVersion::Cairo1 => ".casm",
}
)
}
}

/// Fetch PC locations from the compiled contract to compute the expected PC locations in the
Expand Down
Loading