Skip to content

Commit

Permalink
fix: finalize_global returns Result<(), EVMError>
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Oct 19, 2023
1 parent df8fb81 commit a9b2f39
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
14 changes: 7 additions & 7 deletions crates/contracts/src/contract_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! KakarotCore's storage.

use alexandria_storage::list::{List, ListTrait};
use evm::errors::{EVMError, READ_SYSCALL_FAILED};
use evm::errors::{EVMError, READ_SYSCALL_FAILED, WRITE_SYSCALL_FAILED};
use hash::{HashStateTrait, HashStateExTrait};
use poseidon::PoseidonTrait;
use starknet::{
Expand Down Expand Up @@ -52,7 +52,7 @@ impl ContractAccountImpl of ContractAccountTrait {
let nonce: u64 = Store::<u64>::read(0, storage_address)
.map_err(EVMError::SyscallFailed(READ_SYSCALL_FAILED))?;
Store::<u64>::write(0, storage_address, nonce + 1)
.map_err(EVMError::SyscallFailed(READ_SYSCALL_FAILED))
.map_err(EVMError::SyscallFailed(WRITE_SYSCALL_FAILED))
}

/// Returns the balance of a contract account.
Expand All @@ -78,7 +78,7 @@ impl ContractAccountImpl of ContractAccountTrait {
selector!("contract_account_balance"), array![self.evm_address.into()].span()
);
Store::<u256>::write(0, storage_address, balance)
.map_err(EVMError::SyscallFailed(READ_SYSCALL_FAILED))
.map_err(EVMError::SyscallFailed(WRITE_SYSCALL_FAILED))
}

/// Returns the value stored at a `u256` key inside the Contract Account storage.
Expand Down Expand Up @@ -108,7 +108,7 @@ impl ContractAccountImpl of ContractAccountTrait {
array![self.evm_address.into(), key.low.into(), key.high.into()].span()
);
Store::<u256>::write(0, storage_address, value)
.map_err(EVMError::SyscallFailed(READ_SYSCALL_FAILED))
.map_err(EVMError::SyscallFailed(WRITE_SYSCALL_FAILED))
}

/// Stores the EVM bytecode of a contract account in Kakarot Core's contract storage. The bytecode is first packed
Expand Down Expand Up @@ -138,15 +138,15 @@ impl ContractAccountImpl of ContractAccountTrait {
>::write(
0, storage_base_address_from_felt252(pending_word_addr), packed_bytecode.pending_word
)
.map_err(EVMError::SyscallFailed(READ_SYSCALL_FAILED))?;
.map_err(EVMError::SyscallFailed(WRITE_SYSCALL_FAILED))?;
Store::<
usize
>::write(
0,
storage_base_address_from_felt252(pending_word_len_addr),
packed_bytecode.pending_word_len
)
.map_err(EVMError::SyscallFailed(READ_SYSCALL_FAILED))?;
.map_err(EVMError::SyscallFailed(WRITE_SYSCALL_FAILED))?;
//TODO(eni) PR Alexandria so that from_span returns SyscallResult
stored_list.from_span(packed_bytecode.data.span());
Result::Ok(())
Expand Down Expand Up @@ -220,7 +220,7 @@ impl ContractAccountImpl of ContractAccountTrait {
array![self.evm_address.into(), offset.into()].span()
);
Store::<bool>::write(0, data_address, true)
.map_err(EVMError::SyscallFailed(READ_SYSCALL_FAILED))
.map_err(EVMError::SyscallFailed(WRITE_SYSCALL_FAILED))
}
}

1 change: 1 addition & 0 deletions crates/evm/src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const WRITE_IN_STATIC_CONTEXT: felt252 = 'KKT: WriteInStaticContext';

// STARKNET_SYSCALLS
const READ_SYSCALL_FAILED: felt252 = 'KKT: read syscall failed';
const WRITE_SYSCALL_FAILED: felt252 = 'KKT: write syscall failed';

#[derive(Drop, Copy, PartialEq)]
enum EVMError {
Expand Down
21 changes: 15 additions & 6 deletions crates/evm/src/storage_journal.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use nullable::{match_nullable, FromNullableResult};
use starknet::{StorageBaseAddress, Store, storage_base_address_from_felt252};
use utils::helpers::ArrayExtTrait;
use utils::helpers::{ArrayExtTrait, ResultExTrait};
use utils::traits::{StorageBaseAddressPartialEq, StorageBaseAddressIntoFelt252};
use evm::errors::{EVMError, WRITE_SYSCALL_FAILED, READ_SYSCALL_FAILED};
/// The Journal tracks the changes applied to storage during the execution of a transaction.
/// Local changes tracks the changes applied inside a single execution context.
/// Global changes tracks the changes applied in the entire transaction.
Expand Down Expand Up @@ -63,7 +64,7 @@ impl JournalImpl of JournalTrait {

/// Finalizes the global changes in the journal by writing them to the storage to be stored permanently onchain.
/// Global changes are relative the the execution of an entire transaction. `finalize_global` must be called upon finishing the transaction.
fn finalize_global(ref self: Journal) {
fn finalize_global(ref self: Journal) -> Result<(), EVMError> {
let mut global_keys = self.global_keys.span();
loop {
match global_keys.pop_front() {
Expand All @@ -74,14 +75,22 @@ impl JournalImpl of JournalTrait {
FromNullableResult::Null => {},
FromNullableResult::NotNull(value) => {
let value = value.unbox();
Store::write(0, key, value);
match Store::write(0, key, value) {
Result::Ok(()) => {},
Result::Err(error) => {
break Result::Err(
EVMError::SyscallFailed(WRITE_SYSCALL_FAILED)
);
}
};
}
};
},
Option::None => { break; }
}
};
Option::None => { break Result::Ok(()); }
};
}?;
self.global_keys = Default::default();
Result::Ok(());
}

fn clear_local(ref self: Journal) {
Expand Down

0 comments on commit a9b2f39

Please sign in to comment.