Skip to content

Commit

Permalink
fix: create collision state (#959)
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat authored Sep 18, 2024
1 parent 06bc1c0 commit 4b4139b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 33 deletions.
2 changes: 1 addition & 1 deletion crates/evm/src/backend/starknet_backend.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn deploy(evm_address: EthAddress) -> Result<Address, EVMError> {
// Unlike CAs, there is not check for the existence of an EOA prealably to calling
// `EOATrait::deploy` - therefore, we need to check that there is no collision.
let mut is_deployed = evm_address.is_deployed();
ensure(!is_deployed, EVMError::DeployError(EOA_EXISTS))?;
ensure(!is_deployed, EVMError::Collision)?;

let mut kakarot_state = KakarotCore::unsafe_new_contract_state();
let uninitialized_account_class_hash = kakarot_state.uninitialized_account_class_hash();
Expand Down
4 changes: 2 additions & 2 deletions crates/evm/src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub enum EVMError {
InvalidParameter: felt252,
InvalidOpcode: u8,
WriteInStaticContext,
DeployError: felt252,
Collision,
OutOfGas,
Assertion,
DepthLimit,
Expand All @@ -78,7 +78,7 @@ pub impl EVMErrorImpl of EVMErrorTrait {
// TODO: refactor with dynamic strings once supported
EVMError::InvalidOpcode => 'invalid opcode'.into(),
EVMError::WriteInStaticContext => 'write protection',
EVMError::DeployError(error_message) => error_message,
EVMError::Collision => 'create collision'.into(),
EVMError::OutOfGas => 'out of gas'.into(),
EVMError::Assertion => 'assertion failed'.into(),
EVMError::DepthLimit => 'max call depth exceeded'.into(),
Expand Down
16 changes: 10 additions & 6 deletions crates/evm/src/interpreter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use evm::instructions::{
use evm::model::account::{Account, AccountTrait};
use evm::model::vm::{VM, VMTrait};
use evm::model::{
Message, Environment, Transfer, ExecutionSummary, ExecutionSummaryTrait, ExecutionResult,
ExecutionResultTrait, ExecutionResultStatus, AddressTrait, TransactionResult, Address
Message, Environment, Transfer, ExecutionSummary, ExecutionResult, ExecutionResultTrait,
ExecutionResultStatus, AddressTrait, TransactionResult, Address
};
use evm::precompiles::Precompiles;
use evm::precompiles::eth_precompile_addresses;
Expand Down Expand Up @@ -170,7 +170,7 @@ pub impl EVMImpl of EVMTrait {
// };

TransactionResult {
success: summary.is_success(),
success: summary.status == ExecutionResultStatus::Success,
return_data: summary.return_data,
gas_used: total_gas_used,
state: summary.state,
Expand All @@ -185,9 +185,13 @@ pub impl EVMImpl of EVMTrait {
let result = if is_deploy_tx {
// Check collision
if target_account.has_code_or_nonce() {
return ExecutionSummaryTrait::exceptional_failure(
EVMError::DeployError(CONTRACT_ACCOUNT_EXISTS).to_bytes(),
);
return ExecutionSummary {
status: ExecutionResultStatus::Exception,
return_data: EVMError::Collision.to_bytes(),
gas_left: 0,
state: env.state,
gas_refund: 0
};
}

let mut result = Self::process_create_message(message, ref env);
Expand Down
24 changes: 0 additions & 24 deletions crates/evm/src/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,6 @@ pub struct ExecutionSummary {
pub gas_refund: u64
}

#[generate_trait]
pub impl ExecutionSummaryImpl of ExecutionSummaryTrait {
fn exceptional_failure(error: Span<u8>) -> ExecutionSummary {
ExecutionSummary {
status: ExecutionResultStatus::Exception,
return_data: error,
gas_left: 0,
state: Default::default(),
gas_refund: 0
}
}

fn is_success(self: @ExecutionSummary) -> bool {
*self.status == ExecutionResultStatus::Success
}

fn is_exception(self: @ExecutionSummary) -> bool {
*self.status == ExecutionResultStatus::Exception
}

fn is_revert(self: @ExecutionSummary) -> bool {
*self.status == ExecutionResultStatus::Revert
}
}

pub struct TransactionResult {
pub success: bool,
Expand Down

0 comments on commit 4b4139b

Please sign in to comment.