Skip to content

Commit

Permalink
update deps to have prints in cairo vm (#725)
Browse files Browse the repository at this point in the history
* update dep to have prints in cairo vm

* fix make file

* Update crates/ef-testing/src/evm_sequencer/evm_state/v0.rs
  • Loading branch information
tcoratger authored Jul 16, 2024
1 parent 45b8528 commit a38a32e
Show file tree
Hide file tree
Showing 21 changed files with 696 additions and 631 deletions.
639 changes: 396 additions & 243 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ revm-interpreter = { version = "6.0.0", default-features = false }
revm-primitives = { version = "5.0.0", default-features = false }

# Starknet deps
cairo-lang-casm = "2.6.3"
cairo-lang-starknet = "2.6.3"
cairo-lang-utils = "2.6.3"
cairo-lang-starknet-classes = "2.6.3"
cairo-lang-casm = "2.7.0-rc.2"
cairo-lang-starknet = "2.7.0-rc.2"
cairo-lang-utils = "2.7.0-rc.2"
cairo-lang-starknet-classes = "2.7.0-rc.2"
cairo-vm = "0.9.2"
blockifier = { package = "blockifier", git = "https://github.com/kkrt-labs/blockifier.git", rev = "6003d36", default-features = false, features = [
blockifier = { package = "blockifier", git = "https://github.com/kkrt-labs/blockifier.git", rev = "3cb0634", default-features = false, features = [
"testing",
] }
starknet = "0.6.0"
starknet-crypto = "0.6.0"
starknet_api = "0.12.0-dev.0"
starknet = "0.11.0"
starknet-crypto = "0.7.1"
starknet_api = "0.13.0-rc.0"

# Other
async-trait = "0.1.80"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ setup-kakarot-v0: clean-kakarot-v0
@curl -sL -o kakarot-build.zip "$(KKRT_V0_BUILD_ARTIFACT_URL)"
unzip -o kakarot-build.zip -d build/v0
mv build/v0/build/* build/v0
mv build/v0/fixtures/ERC20.json build/common/
mv build/v0/ERC20.json build/common/
rm -f kakarot-build.zip

setup-kakarot-v1: clean-kakarot-v1
Expand Down
49 changes: 21 additions & 28 deletions crates/ef-testing/src/evm_sequencer/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ pub mod v0;
pub mod v1;

use starknet::core::utils::cairo_short_string_to_felt;
use starknet_api::{core::Nonce, hash::StarkFelt, state::StorageKey};
use starknet_crypto::{poseidon_permute_comp, FieldElement};
use starknet_api::{core::Nonce, state::StorageKey};
use starknet_crypto::{poseidon_permute_comp, Felt};

#[macro_export]
macro_rules! starknet_storage {
($storage_var: expr, $felt: expr) => {
(
get_storage_var_address($storage_var, &[]),
StarkFelt::from($felt),
Felt::from($felt),
)
};
($storage_var: expr, [$($key: expr),*], $felt: expr) => {
{
let args = vec![$($key),*];
(
get_storage_var_address($storage_var, &args),
StarkFelt::from($felt),
Felt::from($felt),
)
}
};
Expand All @@ -32,21 +32,21 @@ macro_rules! starknet_storage {
#[allow(dead_code)]
#[derive(Debug, Clone, Default)]
pub struct KakarotAccount {
pub(crate) evm_address: StarkFelt,
pub(crate) evm_address: Felt,
pub(crate) nonce: Nonce,
pub(crate) storage: Vec<(StorageKey, StarkFelt)>,
pub(crate) storage: Vec<(StorageKey, Felt)>,
}

impl KakarotAccount {
pub const fn evm_address(&self) -> &StarkFelt {
pub const fn evm_address(&self) -> &Felt {
&self.evm_address
}

pub const fn nonce(&self) -> &Nonce {
&self.nonce
}

pub fn storage(&self) -> &[(StorageKey, StarkFelt)] {
pub fn storage(&self) -> &[(StorageKey, Felt)] {
self.storage.as_slice()
}
}
Expand All @@ -63,7 +63,8 @@ pub enum AccountType {
pub mod kkrt_account {
use super::KakarotAccount;
use reth_primitives::{Address, Bytes, U256};
use starknet_api::{core::Nonce, hash::StarkFelt, StarknetApiError};
use starknet::core::types::Felt;
use starknet_api::{core::Nonce, StarknetApiError};

impl KakarotAccount {
pub fn new(
Expand All @@ -73,20 +74,17 @@ pub mod kkrt_account {
_evm_storage: &[(U256, U256)],
) -> Result<Self, StarknetApiError> {
Ok(Self {
evm_address: StarkFelt::default(),
evm_address: Felt::default(),
nonce: Nonce::default(),
storage: vec![],
})
}
}
}

/// Splits a byte array into 31-byte chunks and converts each chunk to a StarkFelt.
pub fn pack_byte_array_to_starkfelt_array(bytes: &[u8]) -> impl Iterator<Item = StarkFelt> + '_ {
bytes.chunks(31).filter_map(|chunk_bytes| {
let f = FieldElement::from_byte_slice_be(chunk_bytes);
f.map(StarkFelt::from).ok()
})
/// Splits a byte array into 31-byte chunks and converts each chunk to a Felt.
pub fn pack_byte_array_to_starkfelt_array(bytes: &[u8]) -> impl Iterator<Item = Felt> + '_ {
bytes.chunks(31).map(Felt::from_bytes_be_slice)
}

/// Computes the inner pointer of a byte array in storage.
Expand All @@ -102,10 +100,7 @@ pub fn pack_byte_array_to_starkfelt_array(bytes: &[u8]) -> impl Iterator<Item =
///
/// # Returns
/// The inner pointer of the byte array.
pub fn inner_byte_array_pointer(
base_address: FieldElement,
storage_segment: FieldElement,
) -> FieldElement {
pub fn inner_byte_array_pointer(base_address: Felt, storage_segment: Felt) -> Felt {
let suffix = cairo_short_string_to_felt("ByteArray").unwrap();
let mut state = [base_address, storage_segment, suffix];
poseidon_permute_comp(&mut state);
Expand All @@ -129,25 +124,23 @@ mod tests {
let result: Vec<_> = pack_byte_array_to_starkfelt_array(&bytes).collect();

// Then
assert_eq!(result, vec![StarkFelt::from(0x0102030405u64)]);
assert_eq!(result, vec![Felt::from(0x0102030405u64)]);
}

#[test]
fn test_inner_byte_array_pointer() {
// Given
let base_address: StarkFelt = get_storage_var_address(ACCOUNT_BYTECODE, &[]).into();
let chunk = FieldElement::ZERO;
let base_address: Felt = get_storage_var_address(ACCOUNT_BYTECODE, &[]).into();
let chunk = Felt::ZERO;

// When
let result = inner_byte_array_pointer(base_address.into(), chunk);
let result = inner_byte_array_pointer(base_address, chunk);

// Then
assert_eq!(
result,
FieldElement::from_hex_be(
"0x030dc4fd6786155d4743a0f56ea73bea9521eba2552a2ca5080b830ad047907a"
)
.unwrap()
Felt::from_hex("0x030dc4fd6786155d4743a0f56ea73bea9521eba2552a2ca5080b830ad047907a")
.unwrap()
);
}
}
19 changes: 8 additions & 11 deletions crates/ef-testing/src/evm_sequencer/account/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use reth_primitives::{Address, U256};
use revm_interpreter::analysis::to_analysed;
use revm_primitives::{Bytecode, Bytes};
use starknet_api::core::PatriciaKey;
use starknet_api::{core::Nonce, hash::StarkFelt, state::StorageKey, StarknetApiError};
use starknet_crypto::FieldElement;
use starknet_api::{core::Nonce, state::StorageKey, StarknetApiError};
use starknet_crypto::Felt;

use super::{pack_byte_array_to_starkfelt_array, KakarotAccount};
use crate::evm_sequencer::constants::storage_variables::{
Expand All @@ -21,7 +21,7 @@ impl KakarotAccount {
nonce: U256,
evm_storage: &[(U256, U256)],
) -> Result<Self, StarknetApiError> {
let nonce = StarkFelt::from(TryInto::<u128>::try_into(nonce).map_err(|err| {
let nonce = Felt::from(TryInto::<u128>::try_into(nonce).map_err(|err| {
StarknetApiError::OutOfRange {
string: err.to_string(),
}
Expand Down Expand Up @@ -62,25 +62,22 @@ impl KakarotAccount {
};

let jumdpests_storage_address = get_storage_var_address(ACCOUNT_VALID_JUMPDESTS, &[]);
let jumdpests_storage_address = FieldElement::from(*jumdpests_storage_address.0.key());
let jumdpests_storage_address = *jumdpests_storage_address.0.key();
valid_jumpdests.into_iter().for_each(|index| {
storage.push((
StorageKey(
PatriciaKey::try_from(StarkFelt::from(
jumdpests_storage_address + index.into(),
))
.unwrap(),
PatriciaKey::try_from(jumdpests_storage_address + Felt::from(index)).unwrap(),
),
StarkFelt::ONE,
Felt::ONE,
))
});

// Initialize the storage vars.
let mut evm_storage_storage: Vec<(StorageKey, StarkFelt)> = evm_storage
let mut evm_storage_storage: Vec<(StorageKey, Felt)> = evm_storage
.iter()
.flat_map(|(k, v)| {
let keys = split_u256(*k).map(Into::into);
let values = split_u256(*v).map(Into::<StarkFelt>::into);
let values = split_u256(*v).map(Into::<Felt>::into);
let low_key = get_storage_var_address(ACCOUNT_STORAGE, &keys);
let high_key = next_storage_key(&low_key).unwrap(); // can fail only if low is the max key
vec![(low_key, values[0]), (high_key, values[1])]
Expand Down
31 changes: 15 additions & 16 deletions crates/ef-testing/src/evm_sequencer/account/v1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use blockifier::abi::abi_utils::get_storage_var_address;
use reth_primitives::{Address, Bytes, U256};
use starknet::core::types::Felt;
use starknet_api::{
core::{Nonce, PatriciaKey},
hash::StarkFelt,
state::StorageKey,
StarknetApiError,
};
Expand All @@ -28,20 +28,19 @@ use crate::{
/// - The address storing the length of the array.
/// - The chunk index.
/// - The short string `ByteArray`.
fn prepare_bytearray_storage(code: &Bytes) -> Vec<(StorageKey, StarkFelt)> {
fn prepare_bytearray_storage(code: &Bytes) -> Vec<(StorageKey, Felt)> {
let bytecode_base_address = get_storage_var_address(ACCOUNT_BYTECODE, &[]);
let mut bytearray = vec![(bytecode_base_address, StarkFelt::from(code.len() as u128))];
let mut bytearray = vec![(bytecode_base_address, Felt::from(code.len() as u128))];

let bytecode_storage: Vec<_> = pack_byte_array_to_starkfelt_array(code)
.enumerate()
.map(|(index, b)| {
let offset = index % 256;
let index = index / 256;
let key =
inner_byte_array_pointer((*bytecode_base_address.0.key()).into(), index.into());
let key = inner_byte_array_pointer(*bytecode_base_address.0.key(), index.into());
(
offset_storage_key(
StorageKey(PatriciaKey::try_from(StarkFelt::from(key)).unwrap()),
StorageKey(PatriciaKey::try_from(key).unwrap()),
offset as i64,
),
b,
Expand All @@ -60,7 +59,7 @@ impl KakarotAccount {
nonce: U256,
evm_storage: &[(U256, U256)],
) -> Result<Self, StarknetApiError> {
let nonce = StarkFelt::from(TryInto::<u128>::try_into(nonce).map_err(|err| {
let nonce = Felt::from(TryInto::<u128>::try_into(nonce).map_err(|err| {
StarknetApiError::OutOfRange {
string: err.to_string(),
}
Expand All @@ -85,11 +84,11 @@ impl KakarotAccount {
storage.append(&mut bytecode_storage);

// Initialize the storage vars.
let mut evm_storage_storage: Vec<(StorageKey, StarkFelt)> = evm_storage
let mut evm_storage_storage: Vec<(StorageKey, Felt)> = evm_storage
.iter()
.flat_map(|(k, v)| {
let keys = split_u256(*k).map(Into::into);
let values = split_u256(*v).map(Into::<StarkFelt>::into);
let values = split_u256(*v).map(Into::<Felt>::into);
let low_key = compute_storage_base_address(ACCOUNT_STORAGE, &keys);
let high_key = offset_storage_key(low_key, 1);
vec![(low_key, values[0]), (high_key, values[1])]
Expand All @@ -108,7 +107,7 @@ impl KakarotAccount {
#[cfg(test)]
mod tests {
use super::*;
use starknet::core::types::FieldElement;
use starknet::core::types::Felt;

#[test]
fn test_prepare_bytearray_storage() {
Expand All @@ -121,19 +120,19 @@ mod tests {

// Then
let expected_result = vec![
(bytecode_base_address, StarkFelt::from(code.len() as u128)),
(bytecode_base_address, Felt::from(code.len() as u128)),
(
offset_storage_key(
StorageKey(
PatriciaKey::try_from(StarkFelt::from(inner_byte_array_pointer(
(*bytecode_base_address.0.key()).into(),
FieldElement::ZERO,
)))
PatriciaKey::try_from(inner_byte_array_pointer(
*bytecode_base_address.0.key(),
Felt::ZERO,
))
.unwrap(),
),
0,
),
StarkFelt::from(0x0102030405u64),
Felt::from(0x0102030405u64),
),
];

Expand Down
28 changes: 13 additions & 15 deletions crates/ef-testing/src/evm_sequencer/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use reth_primitives::alloy_primitives::{address, Address};
use serde::de::DeserializeOwned;
use starknet::core::types::contract::legacy::LegacyContractClass;
use starknet::core::types::contract::CompiledClass;
use starknet_api::felt;
use starknet_api::{
contract_address,
core::{ClassHash, ContractAddress, PatriciaKey},
hash::{StarkFelt, StarkHash},
patricia_key,
};

Expand Down Expand Up @@ -48,16 +48,14 @@ lazy_static! {
// Main addresses
pub static ref ETH_FEE_TOKEN_ADDRESS: ContractAddress = contract_address!("0x049D36570D4e46f48e99674bd3fcc84644DdD6b96F7C741B1562B82f9e004dC7");
pub static ref STRK_FEE_TOKEN_ADDRESS: ContractAddress = contract_address!("0xCa14007Eff0dB1f8135f4C25B34De49AB0d42766");
pub static ref KAKAROT_ADDRESS: ContractAddress =
ContractAddress(TryInto::<PatriciaKey>::try_into(StarkFelt::from(1_u8)).unwrap());
pub static ref KAKAROT_OWNER_ADDRESS: ContractAddress =
ContractAddress(TryInto::<PatriciaKey>::try_into(StarkFelt::from(2_u8)).unwrap());
pub static ref KAKAROT_ADDRESS: ContractAddress = ContractAddress(1_u128.into());
pub static ref KAKAROT_OWNER_ADDRESS: ContractAddress = ContractAddress(2_u128.into());

pub static ref FEE_TOKEN_CLASS: LegacyContractClass = load_contract_class("../../build/common/ERC20.json").expect("Failed to load FeeToken contract class");
pub static ref FEE_TOKEN_CLASS_HASH: ClassHash = ClassHash(FEE_TOKEN_CLASS.class_hash().unwrap().into());
pub static ref FEE_TOKEN_CLASS_HASH: ClassHash = ClassHash(FEE_TOKEN_CLASS.class_hash().unwrap());

pub static ref CAIRO1_HELPERS_CLASS: CompiledClass = load_contract_class("../../build/common/cairo1_helpers.json").expect("Failed to load precompiles contract class");
pub static ref CAIRO1_HELPERS_CLASS_HASH: ClassHash = ClassHash(CAIRO1_HELPERS_CLASS.class_hash().unwrap().into());
pub static ref CAIRO1_HELPERS_CLASS_HASH: ClassHash = ClassHash(CAIRO1_HELPERS_CLASS.class_hash().unwrap());

}

Expand All @@ -69,9 +67,9 @@ lazy_static! {
pub static ref UNINITIALIZED_ACCOUNT_CLASS: LegacyContractClass = load_contract_class("../../build/v0/uninitialized_account.json").expect("Failed to load uninitialized account c contract class");

// Main class hashes
pub static ref KAKAROT_CLASS_HASH: ClassHash = ClassHash(KAKAROT_CLASS.class_hash().unwrap().into());
pub static ref ACCOUNT_CONTRACT_CLASS_HASH: ClassHash = ClassHash(ACCOUNT_CONTRACT_CLASS.class_hash().unwrap().into());
pub static ref UNINITIALIZED_ACCOUNT_CLASS_HASH: ClassHash = ClassHash(UNINITIALIZED_ACCOUNT_CLASS.class_hash().unwrap().into());
pub static ref KAKAROT_CLASS_HASH: ClassHash = ClassHash(KAKAROT_CLASS.class_hash().unwrap());
pub static ref ACCOUNT_CONTRACT_CLASS_HASH: ClassHash = ClassHash(ACCOUNT_CONTRACT_CLASS.class_hash().unwrap());
pub static ref UNINITIALIZED_ACCOUNT_CLASS_HASH: ClassHash = ClassHash(UNINITIALIZED_ACCOUNT_CLASS.class_hash().unwrap());
}

#[cfg(feature = "v1")]
Expand All @@ -82,9 +80,9 @@ lazy_static! {
pub static ref UNINITIALIZED_ACCOUNT_CLASS: CompiledClass = load_contract_class("../../build/v1/contracts_UninitializedAccount.compiled_contract_class.json").expect("Failed to load uninitialized account c contract class");

// Main class hashes
pub static ref KAKAROT_CLASS_HASH: ClassHash = ClassHash(KAKAROT_CLASS.class_hash().unwrap().into());
pub static ref ACCOUNT_CONTRACT_CLASS_HASH: ClassHash = ClassHash(ACCOUNT_CONTRACT_CLASS.class_hash().unwrap().into());
pub static ref UNINITIALIZED_ACCOUNT_CLASS_HASH: ClassHash = ClassHash(UNINITIALIZED_ACCOUNT_CLASS.class_hash().unwrap().into());
pub static ref KAKAROT_CLASS_HASH: ClassHash = ClassHash(KAKAROT_CLASS.class_hash().unwrap());
pub static ref ACCOUNT_CONTRACT_CLASS_HASH: ClassHash = ClassHash(ACCOUNT_CONTRACT_CLASS.class_hash().unwrap());
pub static ref UNINITIALIZED_ACCOUNT_CLASS_HASH: ClassHash = ClassHash(UNINITIALIZED_ACCOUNT_CLASS.class_hash().unwrap());
pub static ref PROXY_CLASS_HASH: ClassHash = *UNINITIALIZED_ACCOUNT_CLASS_HASH;
}

Expand All @@ -110,8 +108,8 @@ pub mod storage_variables {
pub const ACCOUNT_KAKAROT_ADDRESS: &str = "Account_kakarot_address";
pub const ACCOUNT_IMPLEMENTATION: &str = "Account_implementation";
pub const ACCOUNT_CAIRO1_HELPERS_CLASS: &str = "Account_cairo1_helpers_class_hash";
pub const ACCOUNT_VALID_JUMPDESTS : &str = "Account_valid_jumpdests";
pub const ACCOUNT_JUMPDESTS_INITIALIZED : &str = "Account_jumpdests_initialized";
pub const ACCOUNT_VALID_JUMPDESTS: &str = "Account_valid_jumpdests";
pub const ACCOUNT_JUMPDESTS_INITIALIZED: &str = "Account_jumpdests_initialized";

pub const KAKAROT_COINBASE: &str = "Kakarot_coinbase";
pub const KAKAROT_BASE_FEE: &str = "Kakarot_base_fee";
Expand Down
Loading

0 comments on commit a38a32e

Please sign in to comment.