Skip to content

Commit

Permalink
Merge branch 'main' into bug/rlpdecodedefault
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentash committed Nov 21, 2023
2 parents 3b89dfb + 9c481e6 commit 126cf49
Show file tree
Hide file tree
Showing 27 changed files with 366 additions and 241 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gas_reports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Set up Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: 2.3.1
scarb-version: 2.4.0-rc2

- name: Run compare_snapshot script
id: run-script
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gas_snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Set up Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: 2.3.1
scarb-version: 2.4.0-rc2

- name: Generate gas snapshot
run: python scripts/gen_snapshot.py
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: 2.3.1
scarb-version: 2.4.0-rc2
- run: scarb fmt --check
- run: scarb build
- run: scarb test
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.3.1
scarb 2.4.0-rc2
4 changes: 2 additions & 2 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ lint:
- [email protected]
- [email protected]
- [email protected]
- [email protected].32
- [email protected].37
- git-diff-check
- [email protected]
- [email protected]
- [email protected]
- prettier@3.0.3
- prettier@3.1.0
- [email protected]
- [email protected]
- [email protected]
Expand Down
4 changes: 2 additions & 2 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ version = 1

[[package]]
name = "alexandria_storage"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=0682b1e096fe40b2bbab69863e63f2c88ca06c77#0682b1e096fe40b2bbab69863e63f2c88ca06c77"
version = "0.3.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=01a7690dc25d19a086f525b8ce66aa505c8e7527#01a7690dc25d19a086f525b8ce66aa505c8e7527"

[[package]]
name = "contracts"
Expand Down
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ members = ["crates/*"]
[workspace.package]
description = "Kakarot is an (zk)-Ethereum Virtual Machine implementation written in Cairo."
documentation = "https://www.kakarot.org/"
cairo-version = "2.3.1"
cairo-version = "2.4.0-rc2"
version = "0.1.0"
readme = "README.md"
repository = "https://github.com/kkrt-labs/kakarot-ssj/"
license-file = "LICENSE"

[workspace.dependencies]
starknet = "2.3.1"
starknet = "2.4.0-rc2"

[workspace.tool.fmt]
sort-module-level-items = true
2 changes: 1 addition & 1 deletion crates/contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ starknet.workspace = true
evm = { path = "../evm" }
openzeppelin = { path = "../openzeppelin" }
utils = { path = "../utils" }
alexandria_storage = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "0682b1e096fe40b2bbab69863e63f2c88ca06c77" }
alexandria_storage = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "01a7690dc25d19a086f525b8ce66aa505c8e7527" }

[tool]
fmt.workspace = true
Expand Down
56 changes: 21 additions & 35 deletions crates/contracts/src/contract_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ trait IContractAccount<TContractState> {

fn nonce(self: @TContractState) -> u64;
fn set_nonce(ref self: TContractState, new_nonce: u64);
fn increment_nonce(ref self: TContractState);
// ***
// JUMP
// Records of valid jumps in the context of jump opcodes
Expand Down Expand Up @@ -76,7 +75,7 @@ mod ContractAccount {
use super::IContractAccount;
use utils::helpers::{ByteArrayExTrait, ResultExTrait};
use utils::storage::{compute_storage_base_address};
use utils::traits::{StorageBaseAddressIntoFelt252, StoreBytes31};
use utils::traits::{StorageBaseAddressIntoFelt252};

component!(path: upgradeable_component, storage: upgradeable, event: UpgradeableEvent);

Expand All @@ -89,6 +88,7 @@ mod ContractAccount {
// evm_address, kakarot_core_address will be set by account/account.cairo::constructor
evm_address: EthAddress,
kakarot_core_address: ContractAddress,
contract_account_bytecode: List<bytes31>,
#[substorage(v0)]
upgradeable: upgradeable_component::Storage,
}
Expand Down Expand Up @@ -116,21 +116,14 @@ mod ContractAccount {
// We start loading the full 31-byte words of bytecode data at address
// `data_address`. The `pending_word` and `pending_word_len` are stored at
// address `data_address-2` and `data_address-1` respectively.
//TODO(eni) replace with ListTrait::new() once merged in alexandria
let list_len = Store::<usize>::read(0, data_address).expect(BYTECODE_READ_ERROR);
let mut stored_list: List<bytes31> = List {
address_domain: 0,
base: data_address,
len: list_len,
storage_size: Store::<bytes31>::size()
};
let mut stored_list: List<bytes31> = ListTrait::fetch(0, data_address)
.expect('failed to fetch bytecode');
let pending_word_addr: felt252 = data_address.into() - 2;
let pending_word_len_addr: felt252 = pending_word_addr + 1;

// Read the `ByteArray` in the contract storage.
let bytecode = ByteArray {
//TODO(eni) PR alexandria to make List methods return SyscallResult
data: stored_list.array(),
data: stored_list.array().expect('failed to fetch bytecode'),
pending_word: Store::<
felt252
>::read(0, storage_base_address_from_felt252(pending_word_addr))
Expand All @@ -144,21 +137,12 @@ mod ContractAccount {
}

fn set_bytecode(ref self: ContractState, bytecode: Span<u8>) {
self.assert_only_kakarot_core();
let packed_bytecode: ByteArray = ByteArrayExTrait::from_bytes(bytecode);
// data_address is h(h(sn_keccak("contract_account_bytecode")), evm_address)
let data_address = storage_base_address_from_felt252(
selector!("contract_account_bytecode")
);
let data_address = self.contract_account_bytecode.address();
// We start storing the full 31-byte words of bytecode data at address
// `data_address`. The `pending_word` and `pending_word_len` are stored at
// address `data_address-2` and `data_address-1` respectively.
//TODO(eni) replace with ListTrait::new() once merged in alexandria
let mut stored_list: List<bytes31> = List {
address_domain: 0,
base: data_address,
len: 0,
storage_size: Store::<bytes31>::size()
};
let pending_word_addr: felt252 = data_address.into() - 2;
let pending_word_len_addr: felt252 = pending_word_addr + 1;

Expand All @@ -179,8 +163,8 @@ mod ContractAccount {
packed_bytecode.pending_word_len
)
.expect(BYTECODE_WRITE_ERROR);
//TODO(eni) PR Alexandria so that from_span returns SyscallResult
stored_list.from_span(packed_bytecode.data.span());
let mut stored_list: List<bytes31> = ListTrait::new(0, data_address);
stored_list.append_span(packed_bytecode.data.span());
}

fn storage_at(self: @ContractState, key: u256) -> u256 {
Expand All @@ -192,6 +176,7 @@ mod ContractAccount {
}

fn set_storage_at(ref self: ContractState, key: u256, value: u256) {
self.assert_only_kakarot_core();
let storage_address = compute_storage_base_address(
selector!("contract_account_storage_keys"),
array![key.low.into(), key.high.into()].span()
Expand All @@ -208,32 +193,26 @@ mod ContractAccount {


fn set_nonce(ref self: ContractState, new_nonce: u64) {
self.assert_only_kakarot_core();
let storage_address: StorageBaseAddress = storage_base_address_from_felt252(
selector!("contract_account_nonce")
);
Store::<u64>::write(0, storage_address, new_nonce).expect(NONCE_WRITE_ERROR)
}


fn increment_nonce(ref self: ContractState) {
let storage_address: StorageBaseAddress = storage_base_address_from_felt252(
selector!("contract_account_nonce")
);
let nonce = Store::<u64>::read(0, storage_address).expect(NONCE_READ_ERROR);
Store::<u64>::write(0, storage_address, nonce + 1).expect(NONCE_WRITE_ERROR)
}

fn is_false_positive_jumpdest(self: @ContractState, offset: usize) -> bool {
panic_with_felt252('unimplemented')
}


fn set_false_positive_jumpdest(ref self: ContractState, offset: usize) {
self.assert_only_kakarot_core();
panic_with_felt252('unimplemented')
}

fn selfdestruct(ref self: ContractState) {
//TODO add access control
self.assert_only_kakarot_core();
self.set_nonce(0);
self.evm_address.write(0.try_into().unwrap());
self.set_bytecode(array![].span());
Expand All @@ -243,11 +222,18 @@ mod ContractAccount {


fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
self.assert_only_kakarot_core();
self.upgradeable.upgrade_contract(new_class_hash);
}
}

#[generate_trait]
impl PrivateImpl of PrivateTrait {
fn assert_only_kakarot_core(self: @ContractState) {
assert(
get_caller_address() == self.kakarot_core_address.read(),
'Caller not Kakarot Core address'
);
self.upgradeable.upgrade_contract(new_class_hash);
}
}
}
31 changes: 22 additions & 9 deletions crates/contracts/src/eoa.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use starknet::{ContractAddress, EthAddress, ClassHash};
trait IExternallyOwnedAccount<TContractState> {
fn kakarot_core_address(self: @TContractState) -> ContractAddress;
fn evm_address(self: @TContractState) -> EthAddress;
fn chain_id(self: @TContractState) -> u128;

/// Upgrade the ExternallyOwnedAccount smart contract
/// Using replace_class_syscall
fn upgrade(ref self: TContractState, new_class_hash: ClassHash);
fn set_chain_id(ref self: TContractState, chain_id: u128);
}

#[starknet::contract]
Expand All @@ -29,6 +31,7 @@ mod ExternallyOwnedAccount {
struct Storage {
evm_address: EthAddress,
kakarot_core_address: ContractAddress,
chain_id: u128,
#[substorage(v0)]
upgradeable: upgradeable_component::Storage,
}
Expand All @@ -40,13 +43,6 @@ mod ExternallyOwnedAccount {
UpgradeableEvent: upgradeable_component::Event,
}

#[constructor]
fn constructor(
ref self: ContractState, kakarot_address: ContractAddress, evm_address: EthAddress
) {
self.kakarot_core_address.write(kakarot_address);
self.evm_address.write(evm_address);
}

#[abi(embed_v0)]
impl ExternallyOwnedAccount of super::IExternallyOwnedAccount<ContractState> {
Expand All @@ -57,12 +53,22 @@ mod ExternallyOwnedAccount {
self.evm_address.read()
}

// TODO: make this function reachable from an external invoke call
fn chain_id(self: @ContractState) -> u128 {
self.chain_id.read()
}
// TODO: add some security methods to make sure that only some specific upgrades can be made ( low priority )
fn upgrade(ref self: ContractState, new_class_hash: ClassHash) {
assert(get_caller_address() == get_contract_address(), 'Caller not contract address');
self.assert_only_self();
self.upgradeable.upgrade_contract(new_class_hash);
}

fn set_chain_id(ref self: ContractState, chain_id: u128) {
assert(
get_caller_address() == self.kakarot_core_address.read(),
'Caller not kakarot address'
);
self.chain_id.write(chain_id);
}
}

#[abi(embed_v0)]
Expand Down Expand Up @@ -104,4 +110,11 @@ mod ExternallyOwnedAccount {
array![]
}
}

#[generate_trait]
impl PrivateImpl of PrivateTrait {
fn assert_only_self(self: @ContractState) {
assert(get_caller_address() == get_contract_address(), 'Caller not self');
}
}
}
1 change: 0 additions & 1 deletion crates/contracts/src/kakarot_core/interface.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use contracts::kakarot_core::kakarot::StoredAccountType;
use evm::model::AccountType;
use starknet::{ContractAddress, EthAddress, ClassHash};
use utils::traits::ByteArraySerde;

#[starknet::interface]
trait IKakarotCore<TContractState> {
Expand Down
1 change: 0 additions & 1 deletion crates/contracts/src/kakarot_core/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ mod KakarotCore {
use super::{INVOKE_ETH_CALL_FORBIDDEN};
use super::{StoredAccountType};
use utils::helpers::{compute_starknet_address};
use utils::traits::{ByteArraySerde};

component!(path: ownable_component, storage: ownable, event: OwnableEvent);
component!(path: upgradeable_component, storage: upgradeable, event: UpgradeableEvent);
Expand Down
Loading

0 comments on commit 126cf49

Please sign in to comment.