Skip to content

Commit

Permalink
update based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 committed Oct 3, 2023
1 parent 89aed45 commit b740ba2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 105 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ on:

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 30
runs-on: ubuntu-latest-16-cores
timeout-minutes: 120
steps:
- uses: actions/checkout@v3
- name: Load compiled kakarot contracts
Expand Down
32 changes: 10 additions & 22 deletions crates/ef-testing/src/evm_sequencer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use self::constants::{
FEE_TOKEN_ADDRESS, KAKAROT_ADDRESS, KAKAROT_CLASS, KAKAROT_CLASS_HASH, KAKAROT_OWNER_ADDRESS,
PROXY_CLASS, PROXY_CLASS_HASH,
};
use self::utils::{class_hash_to_starkfelt, contract_address_to_starkfelt};

pub(crate) struct KakarotSequencer(Sequencer<State>);

Expand All @@ -29,31 +28,20 @@ impl KakarotSequencer {

pub fn initialize(mut self) -> StateResult<Self> {
let storage = vec![
(
get_storage_var_address("Ownable_owner", &[]).unwrap(), // safe unwrap: var is ASCII
contract_address_to_starkfelt(&KAKAROT_OWNER_ADDRESS),
),
(
get_storage_var_address("native_token_address", &[]).unwrap(), // safe unwrap: var is ASCII
contract_address_to_starkfelt(&FEE_TOKEN_ADDRESS),
),
(
get_storage_var_address("contract_account_class_hash", &[]).unwrap(),
class_hash_to_starkfelt(&CONTRACT_ACCOUNT_CLASS_HASH),
),
(
get_storage_var_address("externally_owned_account_class_hash", &[]).unwrap(),
class_hash_to_starkfelt(&EOA_CLASS_HASH),
),
(
get_storage_var_address("account_proxy_class_hash", &[]).unwrap(),
class_hash_to_starkfelt(&PROXY_CLASS_HASH),
),
("Ownable_owner", *KAKAROT_OWNER_ADDRESS.0.key()),
("native_token_address", *FEE_TOKEN_ADDRESS.0.key()),
("contract_account_class_hash", CONTRACT_ACCOUNT_CLASS_HASH.0),
("externally_owned_account_class_hash", EOA_CLASS_HASH.0),
("account_proxy_class_hash", PROXY_CLASS_HASH.0),
];

// Write all the storage vars to the sequencer state.
for (k, v) in storage {
(&mut self.0.state).set_storage_at(*KAKAROT_ADDRESS, k, v);
(&mut self.0.state).set_storage_at(
*KAKAROT_ADDRESS,
get_storage_var_address(k, &[]).unwrap(), // safe unwrap: all vars are ASCII
v,
);
}

// Write the kakarot class and class hash.
Expand Down
93 changes: 38 additions & 55 deletions crates/ef-testing/src/evm_sequencer/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ use super::constants::{
PROXY_CLASS_HASH,
};
use super::types::FeltSequencer;
use super::utils::{
class_hash_to_starkfelt, compute_starknet_address, contract_address_to_starkfelt,
split_bytecode_to_starkfelt, split_u256,
};
use super::utils::{compute_starknet_address, split_bytecode_to_starkfelt, split_u256};
use super::KakarotSequencer;

pub trait InitializeEvmState {
fn initialize_contract(
pub trait EvmState {
fn setup_account(
&mut self,
evm_address: &Address,
bytecode: &Bytes,
Expand All @@ -31,8 +28,8 @@ pub trait InitializeEvmState {
fn fund(&mut self, evm_address: &Address, balance: U256) -> StateResult<()>;
}

impl InitializeEvmState for KakarotSequencer {
fn initialize_contract(
impl EvmState for KakarotSequencer {
fn setup_account(
&mut self,
evm_address: &Address,
bytecode: &Bytes,
Expand All @@ -48,80 +45,66 @@ impl InitializeEvmState for KakarotSequencer {
let evm_address = Into::<FeltSequencer>::into(*evm_address).into();

let mut storage = vec![
(("evm_address", vec![]), evm_address),
(("is_initialized_", vec![]), StarkFelt::from(1u8)),
(("Ownable_owner", vec![]), *KAKAROT_ADDRESS.0.key()),
(
get_storage_var_address("evm_address", &[]).unwrap(), // safe unwrap: var is ASCII
evm_address,
),
(
get_storage_var_address("is_initialized_", &[]).unwrap(), // safe unwrap: var is ASCII
StarkFelt::from(1u8),
),
(
get_storage_var_address("Ownable_owner", &[]).unwrap(), // safe unwrap: var is ASCII
contract_address_to_starkfelt(&KAKAROT_ADDRESS),
),
(
get_storage_var_address("bytecode_len_", &[]).unwrap(), // safe unwrap: var is ASCII
("bytecode_len_", vec![]),
StarkFelt::from(bytecode.len() as u32),
),
(
get_storage_var_address("kakarot_address", &[]).unwrap(), // safe unwrap: var is ASCII
contract_address_to_starkfelt(&KAKAROT_ADDRESS),
),
(
get_storage_var_address("nonce", &[]).unwrap(), // safe unwrap: var is ASCII
nonce,
),
(
get_storage_var_address("_implementation", &[]).unwrap(), // safe unwrap: var is ASCII
if bytecode.is_empty() && evm_storage.is_empty() {
class_hash_to_starkfelt(&EOA_CLASS_HASH)
} else {
class_hash_to_starkfelt(&CONTRACT_ACCOUNT_CLASS_HASH)
},
),
(("kakarot_address", vec![]), *KAKAROT_ADDRESS.0.key()),
];

let starknet_address = starknet_address.try_into()?;
// Initialize the implementation and nonce based on account type.
if bytecode.is_empty() && evm_storage.is_empty() {
storage.push((("_implementation", vec![]), EOA_CLASS_HASH.0));
self.0.state.set_nonce(starknet_address, Nonce(nonce));
} else {
storage.append(&mut vec![
(("nonce", vec![]), nonce),
(("_implementation", vec![]), CONTRACT_ACCOUNT_CLASS_HASH.0),
]);
}

// Initialize the bytecode storage var.
let bytecode_storage = &mut split_bytecode_to_starkfelt(bytecode)
.into_iter()
.enumerate()
.map(|(i, bytes)| {
(
get_storage_var_address("bytecode_", &[StarkFelt::from(i as u32)]).unwrap(), // safe unwrap: var is ASCII
bytes,
)
})
.map(|(i, bytes)| (("bytecode_", vec![StarkFelt::from(i as u32)]), bytes))
.collect();
storage.append(bytecode_storage);

// Initialize the storage vars.
let evm_storage_storage = &mut evm_storage
.iter()
.flat_map(|(k, v)| {
let keys =
get_uint256_storage_var_addresses("storage_", &split_u256(*k).map(Into::into))
.unwrap();
let keys = split_u256(*k).map(Into::into);
let values = split_u256(*v).map(Into::into);
vec![(keys.0, values[0]), (keys.1, values[1])]
vec![
(("storage_", vec![keys[0]]), values[0]),
(("storage_", vec![keys[1]]), values[1]),
]
})
.collect();
storage.append(evm_storage_storage);

// Write all the storage vars to the sequencer state.
let starknet_address = starknet_address.try_into()?;
for (k, v) in storage {
(&mut self.0.state).set_storage_at(starknet_address, k, v);
for ((var, keys), v) in storage {
(&mut self.0.state).set_storage_at(
starknet_address,
get_storage_var_address(var, &keys).unwrap(), // safe unwrap: all vars are ASCII
v,
);
}

// Set up the contract class hash and nonce.
self.0.state.set_nonce(starknet_address, Nonce(nonce));
// Set up the contract class hash.
(&mut self.0.state).set_class_hash_at(starknet_address, *PROXY_CLASS_HASH)?;

// Add the address to the Kakarot evm to starknet mapping
let evm_starknet_address_mapping_storage = (
get_storage_var_address("evm_to_starknet_address", &[evm_address]).unwrap(), // safe unwrap: var is ASCII
contract_address_to_starkfelt(&starknet_address),
*starknet_address.0.key(),
);
(&mut self.0.state).set_storage_at(
*KAKAROT_ADDRESS,
Expand Down Expand Up @@ -223,10 +206,10 @@ mod tests {
let bytecode = Bytes::from(vec![96, 1, 96, 0, 85]); // PUSH 01 PUSH 00 SSTORE
let nonce = U256::from(0);
sequencer
.initialize_contract(&TEST_CONTRACT_ADDRESS, &bytecode, nonce, vec![])
.setup_account(&TEST_CONTRACT_ADDRESS, &bytecode, nonce, vec![])
.unwrap();
sequencer
.initialize_contract(&PUBLIC_KEY, &Bytes::default(), U256::from(0), vec![])
.setup_account(&PUBLIC_KEY, &Bytes::default(), U256::from(0), vec![])
.unwrap();
sequencer.0.execute(transaction).unwrap();

Expand Down
25 changes: 7 additions & 18 deletions crates/ef-testing/src/evm_sequencer/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use starknet::{
},
macros::selector,
};
use starknet_api::{
core::{ClassHash, ContractAddress},
hash::StarkFelt,
};
use starknet_api::hash::StarkFelt;

pub fn compute_starknet_address(evm_address: &Address) -> FeltSequencer {
let evm_address: FeltSequencer = (*evm_address).into();
Expand Down Expand Up @@ -46,14 +43,6 @@ pub(crate) fn split_u256(value: U256) -> [u128; 2] {
]
}

pub(crate) fn contract_address_to_starkfelt(contract_address: &ContractAddress) -> StarkFelt {
*contract_address.0.key()
}

pub(crate) fn class_hash_to_starkfelt(class_hash: &ClassHash) -> StarkFelt {
class_hash.0
}

pub fn bytes_to_felt_vec(bytes: &Bytes) -> Vec<FieldElement> {
bytes.to_vec().into_iter().map(FieldElement::from).collect()
}
Expand All @@ -74,12 +63,12 @@ pub(crate) fn to_broadcasted_starknet_transaction(
let mut calldata = bytes_to_felt_vec(bytes);

let mut execute_calldata: Vec<FieldElement> = vec![
FieldElement::ONE, // call array length
contract_address_to_starkfelt(&KAKAROT_ADDRESS).into(), // contract address
selector!("eth_send_transaction"), // selector
FieldElement::ZERO, // data offset
FieldElement::from(calldata.len()), // data length
FieldElement::from(calldata.len()), // calldata length
FieldElement::ONE, // call array length
(*KAKAROT_ADDRESS.0.key()).into(), // contract address
selector!("eth_send_transaction"), // selector
FieldElement::ZERO, // data offset
FieldElement::from(calldata.len()), // data length
FieldElement::from(calldata.len()), // calldata length
];
execute_calldata.append(&mut calldata);

Expand Down
9 changes: 1 addition & 8 deletions crates/sequencer/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ impl StarknetTransaction {
}
}

const PREFIX_INVOKE: FieldElement = FieldElement::from_mont([
18443034532770911073,
18446744073709551615,
18446744073709551615,
513398556346534256,
]);

fn compute_transaction_hash(
sender_address: FieldElement,
calldata: &[FieldElement],
Expand All @@ -81,7 +74,7 @@ fn compute_transaction_hash(
nonce: FieldElement,
) -> FieldElement {
compute_hash_on_elements(&[
PREFIX_INVOKE,
FieldElement::from_byte_slice_be(b"invoke").unwrap(),
FieldElement::ONE,
sender_address,
FieldElement::ZERO, // entry_point_selector
Expand Down

0 comments on commit b740ba2

Please sign in to comment.