From 599915b52b78391ff4c508e8148aeb72f3933905 Mon Sep 17 00:00:00 2001 From: TAdev0 Date: Tue, 23 Apr 2024 17:26:15 +0200 Subject: [PATCH 1/2] is_created --- crates/evm/src/interpreter.cairo | 3 +++ crates/evm/src/model/account.cairo | 11 +++++++++-- .../test_instructions/test_memory_operations.cairo | 8 +++++--- crates/evm/src/tests/test_model.cairo | 1 + crates/evm/src/tests/test_state.cairo | 12 ++++++++---- crates/evm/src/tests/test_utils.cairo | 3 ++- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/crates/evm/src/interpreter.cairo b/crates/evm/src/interpreter.cairo index fe7708b6a..ae0959e23 100644 --- a/crates/evm/src/interpreter.cairo +++ b/crates/evm/src/interpreter.cairo @@ -35,6 +35,7 @@ impl EVMImpl of EVMTrait { EVMError::DeployError(CONTRACT_ACCOUNT_EXISTS).to_bytes(), ); } + let mut result = EVMTrait::process_create_message(message, ref env); if result.success { result.return_data = message.target.evm.to_bytes().span(); @@ -65,6 +66,8 @@ impl EVMImpl of EVMTrait { // Increment nonce of target target_account.set_nonce(1); + // Set the target as created + target_account.set_created(true); target_account.address = message.target; env.state.set_account(target_account); diff --git a/crates/evm/src/model/account.cairo b/crates/evm/src/model/account.cairo index f5d59548b..392b490e5 100644 --- a/crates/evm/src/model/account.cairo +++ b/crates/evm/src/model/account.cairo @@ -30,6 +30,7 @@ impl AccountBuilderImpl of AccountBuilderTrait { nonce: 0, balance: 0, selfdestruct: false, + is_created: false } } } @@ -65,7 +66,6 @@ impl AccountBuilderImpl of AccountBuilderTrait { } } - #[derive(Copy, Drop, PartialEq)] struct Account { address: Address, @@ -73,6 +73,7 @@ struct Account { nonce: u64, balance: u256, selfdestruct: bool, + is_created: bool, } #[generate_trait] @@ -137,8 +138,14 @@ impl AccountImpl of AccountTrait { return !(*self.code).is_empty() || *self.nonce != 0; } + #[inline(always)] fn is_created(self: @Account) -> bool { - panic!("unimplemented is created") + *self.is_created + } + + #[inline(always)] + fn set_created(ref self: Account, is_created: bool) { + self.is_created = is_created; } #[inline(always)] diff --git a/crates/evm/src/tests/test_instructions/test_memory_operations.cairo b/crates/evm/src/tests/test_instructions/test_memory_operations.cairo index ed74e023a..f723d37fe 100644 --- a/crates/evm/src/tests/test_instructions/test_memory_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_memory_operations.cairo @@ -517,7 +517,8 @@ fn test_exec_sload_from_storage() { code: array![0xab, 0xcd, 0xef].span(), nonce: 1, balance: 0, - selfdestruct: false + selfdestruct: false, + is_created: false, }; let key: u256 = 0x100000000000000000000000000000001; let value: u256 = 0xABDE1E11A5; @@ -586,7 +587,8 @@ fn test_exec_sstore_on_contract_account_alive() { code: array![].span(), nonce: 1, balance: 0, - selfdestruct: false + selfdestruct: false, + is_created: false, }; vm.env.state.set_account(account); assert!(vm.env.state.is_account_alive(account.address.evm)); @@ -626,7 +628,7 @@ fn test_exec_sstore_finalized() { // Deploys the contract account to be able to commit storage changes. let ca_address = deploy_contract_account(vm.message().target.evm, array![].span()); let account = Account { - address: ca_address, code: array![].span(), nonce: 1, balance: 0, selfdestruct: false + address: ca_address, code: array![].span(), nonce: 1, balance: 0, selfdestruct: false, is_created: false, }; let key: u256 = 0x100000000000000000000000000000001; let value: u256 = 0xABDE1E11A5; diff --git a/crates/evm/src/tests/test_model.cairo b/crates/evm/src/tests/test_model.cairo index c40e30223..ed26fee23 100644 --- a/crates/evm/src/tests/test_model.cairo +++ b/crates/evm/src/tests/test_model.cairo @@ -223,6 +223,7 @@ fn test_account_commit_undeployed() { code: array![0x69].span(), balance: 0, selfdestruct: false, + is_created: false, }; account.nonce = 420; account.code = array![0x1].span(); diff --git a/crates/evm/src/tests/test_state.cairo b/crates/evm/src/tests/test_state.cairo index 48c895caa..1d948e091 100644 --- a/crates/evm/src/tests/test_state.cairo +++ b/crates/evm/src/tests/test_state.cairo @@ -100,7 +100,8 @@ mod test_state { code: Default::default().span(), nonce: 0, balance: 0, - selfdestruct: false + selfdestruct: false, + is_created: false, }; let account = state.get_account(evm_address); @@ -125,7 +126,8 @@ mod test_state { code: array![0xab, 0xcd, 0xef].span(), nonce: 1, balance: 420, - selfdestruct: false + selfdestruct: false, + is_created: false, }; state.set_account(expected_account); @@ -153,7 +155,8 @@ mod test_state { code: array![0xab, 0xcd, 0xef].span(), nonce: 1, balance: 420, - selfdestruct: false + selfdestruct: false, + is_created: false, }; let account = state.get_account(evm_address); @@ -190,7 +193,8 @@ mod test_state { code: array![0xab, 0xcd, 0xef].span(), nonce: 1, balance: 0, - selfdestruct: false + selfdestruct: false, + is_created: false, }; IAccountDispatcher { contract_address: account.starknet_address() } .write_storage(key, value); diff --git a/crates/evm/src/tests/test_utils.cairo b/crates/evm/src/tests/test_utils.cairo index 93866c6a8..8cff069ba 100644 --- a/crates/evm/src/tests/test_utils.cairo +++ b/crates/evm/src/tests/test_utils.cairo @@ -253,7 +253,8 @@ fn initialize_contract_account( code: array![0xab, 0xcd, 0xef].span(), nonce: 1, balance: 0, - selfdestruct: false + selfdestruct: false, + is_created: false, }; let mut i = 0; loop { From 1d671b9a99d19dfc1e6115e706db31ebfaebd5b4 Mon Sep 17 00:00:00 2001 From: TAdev0 Date: Tue, 23 Apr 2024 17:31:36 +0200 Subject: [PATCH 2/2] fmt --- .../tests/test_instructions/test_memory_operations.cairo | 7 ++++++- crates/evm/src/tests/test_state.cairo | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/evm/src/tests/test_instructions/test_memory_operations.cairo b/crates/evm/src/tests/test_instructions/test_memory_operations.cairo index f723d37fe..c8acbcd14 100644 --- a/crates/evm/src/tests/test_instructions/test_memory_operations.cairo +++ b/crates/evm/src/tests/test_instructions/test_memory_operations.cairo @@ -628,7 +628,12 @@ fn test_exec_sstore_finalized() { // Deploys the contract account to be able to commit storage changes. let ca_address = deploy_contract_account(vm.message().target.evm, array![].span()); let account = Account { - address: ca_address, code: array![].span(), nonce: 1, balance: 0, selfdestruct: false, is_created: false, + address: ca_address, + code: array![].span(), + nonce: 1, + balance: 0, + selfdestruct: false, + is_created: false, }; let key: u256 = 0x100000000000000000000000000000001; let value: u256 = 0xABDE1E11A5; diff --git a/crates/evm/src/tests/test_state.cairo b/crates/evm/src/tests/test_state.cairo index 1d948e091..50517bf39 100644 --- a/crates/evm/src/tests/test_state.cairo +++ b/crates/evm/src/tests/test_state.cairo @@ -193,7 +193,7 @@ mod test_state { code: array![0xab, 0xcd, 0xef].span(), nonce: 1, balance: 0, - selfdestruct: false, + selfdestruct: false, is_created: false, }; IAccountDispatcher { contract_address: account.starknet_address() }