Skip to content

Commit

Permalink
dev: reorganize codebase for edition bump (#883)
Browse files Browse the repository at this point in the history
* bump utils to 2024_07

* remove unused imports

bump evm to 2024_07

bump contracts to 2024_07

bump snforge_utils to 2024_07

finalize work

fmt

remove unused imports in tests

* sync with 2.8.2

* remove unused imports

* fix build

* fix tests

* fmt
  • Loading branch information
enitrat authored Sep 9, 2024
1 parent b0e990b commit 10ad45b
Show file tree
Hide file tree
Showing 83 changed files with 1,253 additions and 1,664 deletions.
2 changes: 1 addition & 1 deletion crates/alexandria_data_structures/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod vec;
pub mod vec;
4 changes: 2 additions & 2 deletions crates/alexandria_data_structures/src/vec.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ impl VecIndex<V, T, +VecTrait<V, T>> of Index<V, usize> {
}

pub struct Felt252Vec<T> {
items: Felt252Dict<T>,
len: usize,
pub items: Felt252Dict<T>,
pub len: usize,
}

impl DefaultFeltVec<T, +Drop<T>, +Copy<T>, +Felt252DictValue<T>> of Default<Felt252Vec<T>> {
Expand Down
2 changes: 1 addition & 1 deletion crates/contracts/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "contracts"
version = "0.1.0"
edition = "2023_10"
edition = "2024_07"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

Expand Down
43 changes: 17 additions & 26 deletions crates/contracts/src/account_contract.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use core::starknet::account::{Call};
//! The generic account that is deployed by Kakarot Core before being "specialized" into an
//! Externally Owned Account or a Contract Account This aims at having only one class hash for all
//! the contracts deployed by Kakarot, thus enforcing a unique and consistent address mapping Eth
//! Address <=> Starknet Address

use core::starknet::{ContractAddress, EthAddress, ClassHash};
use core::starknet::account::{Call};
use core::starknet::{EthAddress, ClassHash, ContractAddress};

#[derive(Copy, Drop, Serde, Debug)]
pub struct OutsideExecution {
Expand Down Expand Up @@ -49,38 +49,29 @@ pub mod AccountContract {
use contracts::components::ownable::IOwnable;
use contracts::components::ownable::ownable_component::InternalTrait;
use contracts::components::ownable::ownable_component;
use contracts::errors::{
BYTECODE_READ_ERROR, BYTECODE_WRITE_ERROR, STORAGE_READ_ERROR, STORAGE_WRITE_ERROR,
NONCE_READ_ERROR, NONCE_WRITE_ERROR, KAKAROT_VALIDATION_FAILED, KAKAROT_REENTRANCY
};
use contracts::errors::{KAKAROT_VALIDATION_FAILED, KAKAROT_REENTRANCY};
use contracts::kakarot_core::interface::{IKakarotCoreDispatcher, IKakarotCoreDispatcherTrait};
use contracts::storage::StorageBytecode;
use core::integer;
use core::num::traits::Bounded;
use core::num::traits::zero::Zero;
use core::panic_with_felt252;
use core::starknet::SyscallResultTrait;
use core::starknet::account::{Call};
use core::starknet::secp256_trait::Signature;
use core::starknet::storage::{
Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess,
StoragePointerWriteAccess
};
use core::starknet::storage_access::{storage_base_address_from_felt252, StorageBaseAddress};
use core::starknet::syscalls::{call_contract_syscall, replace_class_syscall};
use core::starknet::{
ContractAddress, EthAddress, ClassHash, VALIDATED, get_caller_address, get_contract_address,
get_tx_info, Store, get_block_timestamp
EthAddress, ClassHash, VALIDATED, get_caller_address, get_contract_address, get_tx_info,
get_block_timestamp
};
use core::traits::TryInto;
use openzeppelin::token::erc20::interface::{IERC20CamelDispatcher, IERC20CamelDispatcherTrait};
use super::{IAccountLibraryDispatcher, IAccountDispatcherTrait, OutsideExecution};
use utils::constants::{POW_2_32};
use utils::eth_transaction::EthereumTransactionTrait;
use utils::eth_transaction::{EthTransactionTrait, TransactionMetadata};
use utils::helpers::SpanExtTrait;
use utils::helpers::{ByteArrayExTrait, ResultExTrait};
use utils::math::OverflowingMul;
use utils::serialization::{deserialize_signature, deserialize_bytes, serialize_bytes};

// Add ownable component
Expand All @@ -95,30 +86,30 @@ pub mod AccountContract {

#[storage]
pub(crate) struct Storage {
Account_bytecode: StorageBytecode,
pub(crate) Account_bytecode: StorageBytecode,
pub(crate) Account_bytecode_len: u32,
Account_storage: Map<u256, u256>,
Account_is_initialized: bool,
Account_nonce: u64,
Account_implementation: ClassHash,
Account_evm_address: EthAddress,
Account_code_hash: u256,
pub(crate) Account_storage: Map<u256, u256>,
pub(crate) Account_is_initialized: bool,
pub(crate) Account_nonce: u64,
pub(crate) Account_implementation: ClassHash,
pub(crate) Account_evm_address: EthAddress,
pub(crate) Account_code_hash: u256,
#[substorage(v0)]
ownable: ownable_component::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
pub enum Event {
transaction_executed: TransactionExecuted,
OwnableEvent: ownable_component::Event
}

#[derive(Drop, starknet::Event, Debug)]
struct TransactionExecuted {
response: Span<felt252>,
success: bool,
gas_used: u128
pub struct TransactionExecuted {
pub response: Span<felt252>,
pub success: bool,
pub gas_used: u128
}

#[constructor]
Expand Down
33 changes: 18 additions & 15 deletions crates/contracts/src/cairo1_helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,27 @@ pub trait IHelpers<T> {
}


mod embeddable_impls {
pub mod embeddable_impls {
use core::keccak::{cairo_keccak, keccak_u256s_be_inputs};
use core::num::traits::Zero;
use core::starknet::EthAddress;
use core::starknet::eth_signature::{
Signature, verify_eth_signature, public_key_point_to_eth_address
use core::starknet::eth_signature::{verify_eth_signature};
use core::starknet::secp256_trait::{
Signature, recover_public_key, Secp256PointTrait, is_valid_signature
};
use core::starknet::secp256_trait::{recover_public_key, Secp256PointTrait, is_valid_signature};
use core::starknet::secp256_trait::{Secp256Trait};
use core::starknet::secp256k1::Secp256k1Point;
use core::starknet::secp256r1::{secp256r1_new_syscall, Secp256r1Point};
use core::starknet::secp256r1::{Secp256r1Point};
use core::traits::Into;
use core::{starknet, starknet::SyscallResultTrait};
use evm::errors::EVMError;
use evm::precompiles::blake2f::Blake2f;
use evm::precompiles::ec_add::EcAdd;
use evm::precompiles::ec_mul::EcMul;
use evm::precompiles::ec_recover::EcRecover;
use evm::precompiles::identity::Identity;
use evm::precompiles::modexp::ModExp;
use evm::precompiles::sha256::Sha256;
use evm::precompiles::Blake2f;
use evm::precompiles::EcAdd;
use evm::precompiles::EcMul;
use evm::precompiles::EcRecover;
use evm::precompiles::Identity;
use evm::precompiles::ModExp;
use evm::precompiles::Sha256;
use utils::helpers::U256Trait;


Expand Down Expand Up @@ -209,7 +210,9 @@ mod embeddable_impls {
fn verify_signature_secp256r1(
self: @TContractState, msg_hash: u256, r: u256, s: u256, x: u256, y: u256
) -> bool {
let maybe_public_key: Option<Secp256r1Point> = secp256r1_new_syscall(x, y)
let maybe_public_key: Option<Secp256r1Point> = Secp256Trait::secp256_ec_new_syscall(
x, y
)
.unwrap_syscall();
let public_key = match maybe_public_key {
Option::Some(public_key) => public_key,
Expand All @@ -227,8 +230,8 @@ pub mod Cairo1Helpers {
struct Storage {}

#[abi(embed_v0)]
impl Precompiles = super::embeddable_impls::Precompiles<ContractState>;
pub impl Precompiles = super::embeddable_impls::Precompiles<ContractState>;

#[abi(embed_v0)]
impl Helpers = super::embeddable_impls::Helpers<ContractState>;
pub impl Helpers = super::embeddable_impls::Helpers<ContractState>;
}
16 changes: 8 additions & 8 deletions crates/contracts/src/components/ownable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ pub trait IOwnable<TContractState> {
#[starknet::component]
pub mod ownable_component {
use core::num::traits::Zero;
use core::starknet::ContractAddress;
use core::starknet::get_caller_address;
use core::starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess};
use core::starknet::{get_caller_address, ContractAddress};
use super::Errors;

#[storage]
struct Storage {
Ownable_owner: ContractAddress
pub struct Storage {
pub Ownable_owner: ContractAddress
}

#[event]
Expand All @@ -34,14 +34,14 @@ pub mod ownable_component {
}

#[derive(Drop, starknet::Event)]
struct OwnershipTransferred {
previous_owner: ContractAddress,
new_owner: ContractAddress,
pub struct OwnershipTransferred {
pub previous_owner: ContractAddress,
pub new_owner: ContractAddress,
}


#[embeddable_as(Ownable)]
impl OwnableImpl<
pub impl OwnableImpl<
TContractState, +HasComponent<TContractState>
> of super::IOwnable<ComponentState<TContractState>> {
fn owner(self: @ComponentState<TContractState>) -> ContractAddress {
Expand Down
6 changes: 3 additions & 3 deletions crates/contracts/src/components/upgradeable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ pub trait IUpgradeable<TContractState> {

#[starknet::component]
pub mod upgradeable_component {
use core::starknet::ClassHash;
use core::starknet::syscalls::{replace_class_syscall};
use core::starknet::{get_caller_address, ClassHash};


#[storage]
struct Storage {}
pub struct Storage {}

#[event]
#[derive(Drop, starknet::Event)]
Expand All @@ -27,7 +27,7 @@ pub mod upgradeable_component {
}

#[embeddable_as(Upgradeable)]
impl UpgradeableImpl<
pub impl UpgradeableImpl<
TContractState, +HasComponent<TContractState>
> of super::IUpgradeable<ComponentState<TContractState>> {
fn upgrade_contract(
Expand Down
8 changes: 5 additions & 3 deletions crates/contracts/src/kakarot_core.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod interface;
mod kakarot;
use interface::IKakarotCore;

use kakarot::KakarotCore;
pub use interface::{
IKakarotCore, IKakarotCoreDispatcher, IKakarotCoreDispatcherTrait,
IExtendedKakarotCoreDispatcher, IExtendedKakarotCoreDispatcherTrait
};
pub use kakarot::KakarotCore;
60 changes: 26 additions & 34 deletions crates/contracts/src/kakarot_core/kakarot.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::starknet::{ContractAddress, EthAddress, ClassHash};

const INVOKE_ETH_CALL_FORBIDDEN: felt252 = 'KKT: Cannot invoke eth_call';


Expand All @@ -9,36 +7,30 @@ pub mod KakarotCore {
use contracts::components::ownable::{ownable_component};
use contracts::components::upgradeable::{IUpgradeable, upgradeable_component};
use contracts::kakarot_core::interface::IKakarotCore;
use core::num::traits::{Zero, CheckedAdd, CheckedSub, CheckedMul};
use core::starknet::SyscallResultTrait;
use core::num::traits::{Zero, CheckedSub};
use core::starknet::event::EventEmitter;
use core::starknet::storage::{
Map, StorageMapReadAccess, StorageMapWriteAccess, StoragePointerReadAccess,
StoragePointerWriteAccess
};
use core::starknet::syscalls::deploy_syscall;
use core::starknet::{
EthAddress, ContractAddress, ClassHash, get_tx_info, get_contract_address,
get_caller_address
};
use evm::backend::starknet_backend;
use evm::errors::{EVMError, ensure, EVMErrorTrait,};
use evm::gas;
use evm::interpreter::{EVMTrait};
use evm::model::account::{Account, AccountTrait};
use evm::model::account::AccountTrait;
use evm::model::{
Transfer, Message, Environment, TransactionResult, TransactionResultTrait, ExecutionSummary,
ExecutionSummaryTrait, Address, AddressTrait
Message, TransactionResult, TransactionResultTrait, ExecutionSummaryTrait, Address
};
use evm::precompiles::eth_precompile_addresses;
use evm::state::{State, StateTrait};
use super::{INVOKE_ETH_CALL_FORBIDDEN};
use evm::state::StateTrait;
use evm::{EVMTrait};
use utils::address::compute_contract_address;
use utils::constants;
use utils::eth_transaction::AccessListItemTrait;
use utils::eth_transaction::{EthereumTransaction, EthereumTransactionTrait, AccessListItem};
use utils::helpers::{compute_starknet_address, EthAddressExTrait};
use utils::rlp::RLPTrait;
use utils::helpers::compute_starknet_address;
use utils::set::{Set, SetTrait};

component!(path: ownable_component, storage: ownable, event: OwnableEvent);
Expand All @@ -54,14 +46,14 @@ pub mod KakarotCore {

#[storage]
pub struct Storage {
Kakarot_evm_to_starknet_address: Map::<EthAddress, ContractAddress>,
Kakarot_uninitialized_account_class_hash: ClassHash,
Kakarot_account_contract_class_hash: ClassHash,
Kakarot_native_token_address: ContractAddress,
Kakarot_coinbase: EthAddress,
Kakarot_base_fee: u128,
Kakarot_prev_randao: u256,
Kakarot_block_gas_limit: u128,
pub Kakarot_evm_to_starknet_address: Map::<EthAddress, ContractAddress>,
pub Kakarot_uninitialized_account_class_hash: ClassHash,
pub Kakarot_account_contract_class_hash: ClassHash,
pub Kakarot_native_token_address: ContractAddress,
pub Kakarot_coinbase: EthAddress,
pub Kakarot_base_fee: u128,
pub Kakarot_prev_randao: u256,
pub Kakarot_block_gas_limit: u128,
// Components
#[substorage(v0)]
ownable: ownable_component::Storage,
Expand All @@ -71,7 +63,7 @@ pub mod KakarotCore {

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
pub enum Event {
OwnableEvent: ownable_component::Event,
UpgradeableEvent: upgradeable_component::Event,
AccountDeployed: AccountDeployed,
Expand All @@ -80,24 +72,24 @@ pub mod KakarotCore {
}

#[derive(Copy, Drop, starknet::Event)]
struct AccountDeployed {
pub struct AccountDeployed {
#[key]
evm_address: EthAddress,
pub evm_address: EthAddress,
#[key]
starknet_address: ContractAddress,
pub starknet_address: ContractAddress,
}

#[derive(Copy, Drop, starknet::Event)]
struct AccountClassHashChange {
old_class_hash: ClassHash,
new_class_hash: ClassHash,
pub struct AccountClassHashChange {
pub old_class_hash: ClassHash,
pub new_class_hash: ClassHash,
}


#[derive(Copy, Drop, starknet::Event)]
struct EOAClassHashChange {
old_class_hash: ClassHash,
new_class_hash: ClassHash,
pub struct EOAClassHashChange {
pub old_class_hash: ClassHash,
pub new_class_hash: ClassHash,
}


Expand Down Expand Up @@ -126,7 +118,7 @@ pub mod KakarotCore {
}

#[abi(embed_v0)]
impl KakarotCoreImpl of IKakarotCore<ContractState> {
pub impl KakarotCoreImpl of IKakarotCore<ContractState> {
fn set_native_token(ref self: ContractState, native_token: ContractAddress) {
self.ownable.assert_only_owner();
self.Kakarot_native_token_address.write(native_token);
Expand Down Expand Up @@ -250,7 +242,7 @@ pub mod KakarotCore {
}

#[generate_trait]
impl KakarotCoreInternalImpl of KakarotCoreInternal {
pub impl KakarotCoreInternalImpl of KakarotCoreInternal {
fn is_view(self: @ContractState) -> bool {
let tx_info = get_tx_info().unbox();

Expand Down
Loading

0 comments on commit 10ad45b

Please sign in to comment.