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

Always predeclare Cairo0 and Cairo1 account classes #402

Merged
merged 5 commits into from
Mar 29, 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
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use std::str::FromStr;

use starknet_core::constants::{
CAIRO_0_ACCOUNT_CONTRACT_PATH, CAIRO_1_ACCOUNT_CONTRACT_SIERRA_PATH,
};
use starknet_rs_core::types::FieldElement;
use starknet_rs_core::utils::get_selector_from_name;
use starknet_types::contract_class::{Cairo0ContractClass, Cairo0Json, ContractClass};
use starknet_types::felt::Felt;
use starknet_types::traits::HashProducer;

use crate::constants::{CAIRO_0_ACCOUNT_CONTRACT_PATH, CAIRO_1_ACCOUNT_CONTRACT_SIERRA_PATH};
use crate::error::DevnetResult;

#[derive(clap::ValueEnum, Debug, Clone)]
pub enum AccountContractClassChoice {
Cairo0,
Cairo1,
}

impl AccountContractClassChoice {
pub(crate) fn get_class_wrapper(&self) -> Result<AccountClassWrapper, anyhow::Error> {
pub fn get_class_wrapper(&self) -> DevnetResult<AccountClassWrapper> {
Ok(match self {
AccountContractClassChoice::Cairo0 => {
let contract_class = Cairo0ContractClass::RawJson(Cairo0Json::raw_json_from_path(
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct AccountClassWrapper {
}

impl FromStr for AccountClassWrapper {
type Err = anyhow::Error;
type Err = crate::error::Error;

fn from_str(path_candidate: &str) -> Result<Self, Self::Err> {
// load artifact
Expand All @@ -72,7 +72,7 @@ impl FromStr for AccountClassWrapper {
"Not a valid Sierra account artifact; has __execute__: {has_execute}; has \
__validate__: {has_validate}"
);
return Err(anyhow::Error::msg(msg));
return Err(crate::error::Error::ContractClassLoadError(msg));
}

// generate the hash and return
Expand All @@ -85,13 +85,11 @@ impl FromStr for AccountClassWrapper {
#[cfg(test)]
mod tests {
use clap::ValueEnum;
use starknet_core::constants::{
CAIRO_0_ACCOUNT_CONTRACT_HASH, CAIRO_1_ACCOUNT_CONTRACT_SIERRA_HASH,
};
use starknet_types::felt::Felt;
use starknet_types::traits::HashProducer;

use super::AccountContractClassChoice;
use crate::constants::{CAIRO_0_ACCOUNT_CONTRACT_HASH, CAIRO_1_ACCOUNT_CONTRACT_SIERRA_HASH};
use crate::contract_class_choice::AccountClassWrapper;

#[test]
Expand Down
4 changes: 2 additions & 2 deletions crates/starknet-devnet-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub enum Error {
UnsupportedAction { msg: String },
#[error("Unexpected internal error: {msg}")]
UnexpectedInternalError { msg: String },
#[error("Failed to load ContractClass")]
ContractClassLoadError,
#[error("Failed to load ContractClass: {0}")]
ContractClassLoadError(String),
#[error("Deserialization error: {origin}")]
DeserializationError { origin: String },
#[error("Serialization error: {origin}")]
Expand Down
1 change: 1 addition & 0 deletions crates/starknet-devnet-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod account;
mod blocks;
pub mod constants;
pub mod contract_class_choice;
pub mod error;
pub mod messaging;
mod predeployed_accounts;
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet-devnet-core/src/predeployed_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl AccountGenerator for PredeployedAccounts {
&mut self,
number_of_accounts: u8,
class_hash: ClassHash,
contract_class: ContractClass,
contract_class: &ContractClass,
) -> DevnetResult<&Vec<Self::Acc>> {
let private_keys = self.generate_private_keys(number_of_accounts);

Expand Down
17 changes: 15 additions & 2 deletions crates/starknet-devnet-core/src/starknet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ use crate::constants::{
ETH_ERC20_CONTRACT_ADDRESS, ETH_ERC20_NAME, ETH_ERC20_SYMBOL, STRK_ERC20_CONTRACT_ADDRESS,
STRK_ERC20_NAME, STRK_ERC20_SYMBOL,
};
use crate::contract_class_choice::AccountContractClassChoice;
use crate::error::{DevnetResult, Error, TransactionValidationError};
use crate::messaging::MessagingBroker;
use crate::predeployed_accounts::PredeployedAccounts;
Expand Down Expand Up @@ -132,6 +133,18 @@ impl Starknet {
pub fn new(config: &StarknetConfig) -> DevnetResult<Self> {
let defaulter = StarknetDefaulter::new(config.fork_config.clone());
let mut state = StarknetState::new(defaulter);

marioiordanov marked this conversation as resolved.
Show resolved Hide resolved
// predeclare account classes
for account_class_choice in
[AccountContractClassChoice::Cairo0, AccountContractClassChoice::Cairo1]
{
let class_wrapper = account_class_choice.get_class_wrapper()?;
state.predeclare_contract_class(
class_wrapper.class_hash,
class_wrapper.contract_class,
)?;
}

// deploy udc, eth erc20 and strk erc20 contracts
let eth_erc20_fee_contract =
predeployed::create_erc20_at_address(ETH_ERC20_CONTRACT_ADDRESS)?;
Expand Down Expand Up @@ -167,7 +180,7 @@ impl Starknet {
let accounts = predeployed_accounts.generate_accounts(
config.total_accounts,
config.account_contract_class_hash,
config.account_contract_class.clone(),
&config.account_contract_class,
)?;
for account in accounts {
account.deploy(&mut state)?;
Expand Down Expand Up @@ -826,7 +839,7 @@ impl Starknet {
// starting block is reached in while loop.
if last_reached_block_hash == Felt::from(0) && reached_starting_block {
self.blocks.last_block_hash = None;
self.state = self.init_state.clone_historic(); // This will be refactored during the genesis block PR
self.state = self.init_state.clone_historic(); // This will be refactored during the genesis block PR
} else if reached_starting_block {
let current_block =
self.blocks.hash_to_block.get(&last_reached_block_hash).ok_or(Error::NoBlock)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet-devnet-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ pub trait AccountGenerator {
&mut self,
number_of_accounts: u8,
class_hash: ClassHash,
contract_class: ContractClass,
contract_class: &ContractClass,
) -> DevnetResult<&Vec<Self::Acc>>;
}
8 changes: 4 additions & 4 deletions crates/starknet-devnet/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use starknet_core::constants::{
DEVNET_DEFAULT_DATA_GAS_PRICE, DEVNET_DEFAULT_GAS_PRICE, DEVNET_DEFAULT_PORT,
DEVNET_DEFAULT_TIMEOUT, DEVNET_DEFAULT_TOTAL_ACCOUNTS,
};
use starknet_core::contract_class_choice::{AccountClassWrapper, AccountContractClassChoice};
use starknet_core::random_number_generator::generate_u32_random_number;
use starknet_core::starknet::starknet_config::{
DumpOn, ForkConfig, StarknetConfig, StateArchiveCapacity,
};
use starknet_types::chain_id::ChainId;

use crate::contract_class_choice::{AccountClassWrapper, AccountContractClassChoice};
use crate::initial_balance_wrapper::InitialBalanceWrapper;
use crate::ip_addr_wrapper::IpAddrWrapper;

Expand Down Expand Up @@ -277,7 +277,7 @@ mod tests {
get_first_line(&err.to_string()),
format!(
"error: invalid value '{custom_path}' for '--account-class-custom <PATH>': \
missing field `kind` at line 1 column 292"
Types error: missing field `kind` at line 1 column 292"
)
),
Ok(parsed) => panic!("Should have failed; got: {parsed:?}"),
Expand All @@ -293,8 +293,8 @@ mod tests {
get_first_line(&err.to_string()),
format!(
"error: invalid value '{custom_path}' for '--account-class-custom <PATH>': \
Not a valid Sierra account artifact; has __execute__: false; has \
__validate__: false"
Failed to load ContractClass: Not a valid Sierra account artifact; has \
__execute__: false; has __validate__: false"
)
),
Ok(parsed) => panic!("Should have failed; got: {parsed:?}"),
Expand Down
1 change: 0 additions & 1 deletion crates/starknet-devnet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use tracing::info;
use tracing_subscriber::EnvFilter;

mod cli;
mod contract_class_choice;
mod initial_balance_wrapper;
mod ip_addr_wrapper;

Expand Down
5 changes: 5 additions & 0 deletions crates/starknet-devnet/tests/test_account_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ mod test_account_selection {
can_deploy_new_account_test_body(&["--account-class", "cairo1"]).await;
}

#[tokio::test]
async fn can_deploy_new_cairo1_account_when_cairo0_selected() {
can_deploy_new_account_test_body(&["--account-class", "cairo0"]).await;
}

#[tokio::test]
async fn can_deploy_new_custom_account() {
can_deploy_new_account_test_body(&[
Expand Down