Skip to content

Commit

Permalink
feat: pack calldata for v0 txs (#717)
Browse files Browse the repository at this point in the history
* feat: pack calldata for v0 txs

* add feature(not(v0))

* fix checks

* add import under feature flag
  • Loading branch information
enitrat authored Jun 6, 2024
1 parent 6caa613 commit 10d1135
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
10 changes: 5 additions & 5 deletions crates/ef-testing/src/evm_sequencer/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ pub mod kkrt_account {
}

/// Splits a byte array into 31-byte chunks and converts each chunk to a StarkFelt.
pub fn split_bytecode_to_starkfelt(bytecode: &[u8]) -> impl Iterator<Item = StarkFelt> + '_ {
bytecode.chunks(31).filter_map(|bytes| {
let f = FieldElement::from_byte_slice_be(bytes);
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()
})
}
Expand Down Expand Up @@ -121,12 +121,12 @@ mod tests {
use reth_primitives::Bytes;

#[test]
fn test_split_bytecode_to_starkfelt() {
fn test_pack_byte_array_to_starkfelt_array() {
// Given
let bytes = Bytes::from([0x01, 0x02, 0x03, 0x04, 0x05]);

// When
let result: Vec<_> = split_bytecode_to_starkfelt(&bytes).collect();
let result: Vec<_> = pack_byte_array_to_starkfelt_array(&bytes).collect();

// Then
assert_eq!(result, vec![StarkFelt::from(0x0102030405u64)]);
Expand Down
4 changes: 2 additions & 2 deletions crates/ef-testing/src/evm_sequencer/account/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use starknet_api::core::PatriciaKey;
use starknet_api::{core::Nonce, hash::StarkFelt, state::StorageKey, StarknetApiError};
use starknet_crypto::FieldElement;

use super::{split_bytecode_to_starkfelt, KakarotAccount};
use super::{pack_byte_array_to_starkfelt_array, KakarotAccount};
use crate::evm_sequencer::constants::storage_variables::{
ACCOUNT_BYTECODE_LEN, ACCOUNT_EVM_ADDRESS, ACCOUNT_IS_INITIALIZED,
ACCOUNT_JUMPDESTS_INITIALIZED, ACCOUNT_NONCE, ACCOUNT_STORAGE, ACCOUNT_VALID_JUMPDESTS,
Expand Down Expand Up @@ -42,7 +42,7 @@ impl KakarotAccount {
storage.append(&mut vec![starknet_storage!(ACCOUNT_NONCE, nonce)]);

// Initialize the bytecode storage var.
let mut bytecode_storage = split_bytecode_to_starkfelt(code)
let mut bytecode_storage = pack_byte_array_to_starkfelt_array(code)
.enumerate()
.map(|(i, bytes)| (StorageKey::from(i as u32), bytes))
.collect();
Expand Down
4 changes: 2 additions & 2 deletions crates/ef-testing/src/evm_sequencer/account/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use starknet_api::{
};

use super::KakarotAccount;
use super::{inner_byte_array_pointer, split_bytecode_to_starkfelt};
use super::{inner_byte_array_pointer, pack_byte_array_to_starkfelt_array};
use crate::evm_sequencer::constants::storage_variables::{
ACCOUNT_EVM_ADDRESS, ACCOUNT_IS_INITIALIZED, ACCOUNT_NONCE, ACCOUNT_STORAGE,
};
Expand All @@ -32,7 +32,7 @@ fn prepare_bytearray_storage(code: &Bytes) -> Vec<(StorageKey, StarkFelt)> {
let bytecode_base_address = get_storage_var_address(ACCOUNT_BYTECODE, &[]);
let mut bytearray = vec![(bytecode_base_address, StarkFelt::from(code.len() as u128))];

let bytecode_storage: Vec<_> = split_bytecode_to_starkfelt(code)
let bytecode_storage: Vec<_> = pack_byte_array_to_starkfelt_array(code)
.enumerate()
.map(|(index, b)| {
let offset = index % 256;
Expand Down
2 changes: 1 addition & 1 deletion crates/ef-testing/src/evm_sequencer/evm_state/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ mod tests {
transaction: reth_primitives::Transaction::Legacy(TxLegacy {
chain_id: Some(CHAIN_ID),
gas_limit: 1_000_000,
to: reth_primitives::TransactionKind::Call(*TEST_CONTRACT_ADDRESS),
to: reth_primitives::TxKind::Call(*TEST_CONTRACT_ADDRESS),
..Default::default()
}),
};
Expand Down
20 changes: 19 additions & 1 deletion crates/ef-testing/src/evm_sequencer/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,25 @@ pub fn to_broadcasted_starknet_transaction(
let mut bytes = BytesMut::new();
transaction.transaction.encode_without_signature(&mut bytes);

let mut calldata: Vec<_> = bytes.into_iter().map(FieldElement::from).collect();
let mut calldata: Vec<FieldElement> = {
// Pack the calldata in 31-byte chunks.
#[cfg(feature = "v0")]
{
use crate::evm_sequencer::account::pack_byte_array_to_starkfelt_array;
let bytes_u8: Vec<u8> = bytes.into_iter().collect();
let bytes_len = bytes_u8.len();
let packed_data: Vec<_> = pack_byte_array_to_starkfelt_array(bytes_u8.as_slice())
.map(FieldElement::from)
.collect();
std::iter::once(FieldElement::from(bytes_len))
.chain(packed_data)
.collect()
}
#[cfg(not(feature = "v0"))]
{
bytes.into_iter().map(FieldElement::from).collect()
}
};

let mut execute_calldata = {
#[cfg(feature = "v0")]
Expand Down

0 comments on commit 10d1135

Please sign in to comment.