Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Feat: const fixed size arrays (#822)
Browse files Browse the repository at this point in the history
* use const fixed-size arrays in code

* use fixed-size array instead of array! when they are converted to span
  • Loading branch information
remybar authored Aug 8, 2024
1 parent 291a2be commit 5a0f044
Show file tree
Hide file tree
Showing 44 changed files with 647 additions and 871 deletions.
41 changes: 3 additions & 38 deletions crates/contracts/src/account_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub mod AccountContract {
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
NONCE_READ_ERROR, NONCE_WRITE_ERROR, KAKAROT_VALIDATION_FAILED
};
use contracts::kakarot_core::interface::{IKakarotCoreDispatcher, IKakarotCoreDispatcherTrait};
use core::integer;
Expand Down Expand Up @@ -81,6 +81,7 @@ pub mod AccountContract {

const VERSION: u32 = 000_001_000;


#[storage]
struct Storage {
Account_bytecode: ByteArray,
Expand Down Expand Up @@ -231,43 +232,7 @@ pub mod AccountContract {
let (success, return_data, gas_used) = if is_valid {
kakarot.eth_send_transaction(tx)
} else {
(
false,
array![
'K',
'a',
'k',
'a',
'r',
'o',
't',
':',
' ',
'e',
't',
'h',
' ',
'v',
'a',
'l',
'i',
'd',
'a',
't',
'i',
'o',
'n',
' ',
'f',
'a',
'i',
'l',
'e',
'd'
]
.span(),
0
)
(false, KAKAROT_VALIDATION_FAILED.span(), 0)
};
let return_data = serialize_bytes(return_data).span();

Expand Down
4 changes: 2 additions & 2 deletions crates/contracts/src/cairo1_helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ mod embeddable_impls {
};
match result {
Result::Ok((gas, output)) => (true, gas, output),
Result::Err(_) => (false, 0, array![].span())
Result::Err(_) => (false, 0, [].span())
}
}
}
Expand Down Expand Up @@ -188,7 +188,7 @@ mod embeddable_impls {
return (false, Zero::zero());
}
// Keccak output is little endian.
let point_hash_le = keccak_u256s_be_inputs(array![x, y].span());
let point_hash_le = keccak_u256s_be_inputs([x, y].span());
let point_hash = u256 {
low: core::integer::u128_byte_reverse(point_hash_le.high),
high: core::integer::u128_byte_reverse(point_hash_le.low)
Expand Down
35 changes: 35 additions & 0 deletions crates/contracts/src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,38 @@ pub const STORAGE_WRITE_ERROR: felt252 = 'CA: Storage Write Error';

pub const NONCE_READ_ERROR: felt252 = 'CA: Nonce Read Error';
pub const NONCE_WRITE_ERROR: felt252 = 'CA: Nonce Write Error';

pub const KAKAROT_VALIDATION_FAILED: [
u8
; 30] = [
'K',
'a',
'k',
'a',
'r',
'o',
't',
':',
' ',
'e',
't',
'h',
' ',
'v',
'a',
'l',
'i',
'd',
'a',
't',
'i',
'o',
'n',
' ',
'f',
'a',
'i',
'l',
'e',
'd'
];
2 changes: 1 addition & 1 deletion crates/contracts/src/kakarot_core/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ pub mod KakarotCore {
let to_starknet_address = self.compute_starknet_address(to_evm_address);
let to = Address { evm: to_evm_address, starknet: to_starknet_address };
let code = tx.calldata();
let calldata = array![].span();
let calldata = [].span();
(to, true, code, calldata)
},
};
Expand Down
25 changes: 10 additions & 15 deletions crates/contracts/src/test_data.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// }

pub(crate) fn deploy_counter_calldata() -> Span<u8> {
array![
[
0x60,
0x80,
0x60,
Expand Down Expand Up @@ -528,12 +528,11 @@ pub(crate) fn deploy_counter_calldata() -> Span<u8> {
0x16,
0x00,
0x33
]
.span()
].span()
}

pub(crate) fn counter_evm_bytecode() -> Span<u8> {
array![
[
0x60,
0x80,
0x60,
Expand Down Expand Up @@ -1007,8 +1006,7 @@ pub(crate) fn counter_evm_bytecode() -> Span<u8> {
0x16,
0x00,
0x33
]
.span()
].span()
}


Expand Down Expand Up @@ -1043,7 +1041,7 @@ pub(crate) fn counter_evm_bytecode() -> Span<u8> {
// }
// Remix compiler: 0.8.20+commit.a1b79de6
pub(crate) fn storage_evm_initcode() -> Span<u8> {
array![
[
0x60,
0x80,
0x60,
Expand Down Expand Up @@ -1396,12 +1394,11 @@ pub(crate) fn storage_evm_initcode() -> Span<u8> {
0x14,
0x00,
0x33
]
.span()
].span()
}

pub(crate) fn storage_evm_bytecode() -> Span<u8> {
array![
[
0x60,
0x80,
0x60,
Expand Down Expand Up @@ -1725,8 +1722,7 @@ pub(crate) fn storage_evm_bytecode() -> Span<u8> {
0x14,
0x00,
0x33
]
.span()
].span()
}


Expand All @@ -1735,7 +1731,7 @@ pub(crate) fn storage_evm_bytecode() -> Span<u8> {
// rlp decoding: [ '0x01', '0x', '0x3b9aca00', '0x1e8480',
// '0x0000006f746865725f65766d5f61646472657373', '0x', '0x371303c0', [] ]
pub(crate) fn eip_2930_rlp_encoded_counter_inc_tx() -> Span<u8> {
array![
[
1,
231,
1,
Expand Down Expand Up @@ -1777,6 +1773,5 @@ pub(crate) fn eip_2930_rlp_encoded_counter_inc_tx() -> Span<u8> {
3,
192,
192,
]
.span()
].span()
}
4 changes: 2 additions & 2 deletions crates/contracts/src/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub(crate) fn deploy_contract_account(evm_address: EthAddress, bytecode: Span<u8

fn deploy_eoa(eoa_address: EthAddress) -> IAccountDispatcher {
let kakarot_address = get_contract_address();
let calldata: Span<felt252> = array![kakarot_address.into(), eoa_address.into()].span();
let calldata: Span<felt252> = [kakarot_address.into(), eoa_address.into()].span();

let (starknet_address, _) = deploy_syscall(
UninitializedAccount::TEST_CLASS_HASH.try_into().unwrap(),
Expand Down Expand Up @@ -166,7 +166,7 @@ pub(crate) fn setup_contracts_for_testing() -> (
) {
let native_token = deploy_native_token();
let kakarot_core = deploy_kakarot_core(
native_token.contract_address, array![sequencer_evm_address()].span()
native_token.contract_address, [sequencer_evm_address()].span()
);

// We drop the first event of Kakarot Core, as it is the initializer from Ownable,
Expand Down
6 changes: 3 additions & 3 deletions crates/contracts/tests/test_contract_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use evm::test_utils::{ca_address, native_token};
#[test]
fn test_ca_deploy() {
setup_contracts_for_testing();
let ca_address = deploy_contract_account(ca_address(), array![].span());
let ca_address = deploy_contract_account(ca_address(), [].span());
let contract_account = IAccountDispatcher { contract_address: ca_address.starknet };

let initial_bytecode = contract_account.bytecode();
Expand All @@ -30,7 +30,7 @@ fn test_ca_bytecode() {
#[test]
fn test_ca_get_nonce() {
setup_contracts_for_testing();
let ca_address = deploy_contract_account(ca_address(), array![].span());
let ca_address = deploy_contract_account(ca_address(), [].span());
let contract_account = IAccountDispatcher { contract_address: ca_address.starknet };

let initial_nonce = contract_account.get_nonce();
Expand All @@ -48,7 +48,7 @@ fn test_ca_get_nonce() {
#[test]
fn test_ca_storage() {
setup_contracts_for_testing();
let ca_address = deploy_contract_account(ca_address(), array![].span());
let ca_address = deploy_contract_account(ca_address(), [].span());
let contract_account = IAccountDispatcher { contract_address: ca_address.starknet };

let storage_slot = 0x555;
Expand Down
8 changes: 3 additions & 5 deletions crates/contracts/tests/test_eoa.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn test___execute__a() {

// Then
// selector: function get()
let data_get_tx = array![0x6d, 0x4c, 0xe6, 0x3c].span();
let data_get_tx = [0x6d, 0x4c, 0xe6, 0x3c].span();

// check counter value is 0 before doing inc
let tx = call_transaction(chain_id(), Option::Some(other_evm_address()), data_get_tx);
Expand Down Expand Up @@ -169,7 +169,7 @@ fn test___validate__fail__to_address_not_kakarot_core() {
let call = Call {
to: other_starknet_address(),
selector: selector!("eth_send_transaction"),
calldata: array![].span()
calldata: [].span()
};

eoa_contract.__validate__(array![call]);
Expand Down Expand Up @@ -202,9 +202,7 @@ fn test___validate__fail__selector_not_eth_send_transaction() {
);

let call = Call {
to: kakarot_core.contract_address,
selector: selector!("eth_call"),
calldata: array![].span()
to: kakarot_core.contract_address, selector: selector!("eth_call"), calldata: [].span()
};

eoa_contract.__validate__(array![call]);
Expand Down
12 changes: 6 additions & 6 deletions crates/contracts/tests/test_kakarot_core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn test_eth_send_transaction_non_deploy_tx() {

// Then
// selector: function get()
let data_get_tx = array![0x6d, 0x4c, 0xe6, 0x3c].span();
let data_get_tx = [0x6d, 0x4c, 0xe6, 0x3c].span();

// check counter value is 0 before doing inc
let tx = contract_utils::call_transaction(
Expand All @@ -173,7 +173,7 @@ fn test_eth_send_transaction_non_deploy_tx() {
assert_eq!(return_data, u256_to_bytes_array(0).span());

// selector: function inc()
let data_increment_counter = array![0x37, 0x13, 0x03, 0xc0].span();
let data_increment_counter = [0x37, 0x13, 0x03, 0xc0].span();

// When
testing::set_contract_address(eoa);
Expand All @@ -194,7 +194,7 @@ fn test_eth_send_transaction_non_deploy_tx() {

// Then
// selector: function get()
let data_get_tx = array![0x6d, 0x4c, 0xe6, 0x3c].span();
let data_get_tx = [0x6d, 0x4c, 0xe6, 0x3c].span();

// check counter value is 1
let tx = contract_utils::call_transaction(
Expand Down Expand Up @@ -225,7 +225,7 @@ fn test_eth_call() {

let to = Option::Some(test_utils::other_evm_address());
// selector: function get()
let calldata = array![0x6d, 0x4c, 0xe6, 0x3c].span();
let calldata = [0x6d, 0x4c, 0xe6, 0x3c].span();

// When
let tx = contract_utils::call_transaction(chain_id(), to, calldata);
Expand Down Expand Up @@ -259,7 +259,7 @@ fn test_process_transaction() {
let gas_price = test_utils::gas_price();
let value = 0;
// selector: function get()
let calldata = array![0x6d, 0x4c, 0xe6, 0x3c].span();
let calldata = [0x6d, 0x4c, 0xe6, 0x3c].span();

let tx = EthereumTransaction::LegacyTransaction(
LegacyTransaction {
Expand Down Expand Up @@ -323,7 +323,7 @@ fn test_eth_send_transaction_deploy_tx() {
assert(bytecode == counter_evm_bytecode(), 'wrong bytecode');

// Check that the account was created and `get` returns 0.
let calldata = array![0x6d, 0x4c, 0xe6, 0x3c].span();
let calldata = [0x6d, 0x4c, 0xe6, 0x3c].span();
let to = Option::Some(expected_address);

// No need to set address back to eoa, as eth_call doesn't use the caller address.
Expand Down
2 changes: 1 addition & 1 deletion crates/contracts/tests/test_upgradeable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod MockContractUpgradeableV1 {
#[test]
fn test_upgradeable_update_contract() {
let (contract_address, _) = deploy_syscall(
MockContractUpgradeableV0::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
MockContractUpgradeableV0::TEST_CLASS_HASH.try_into().unwrap(), 0, [].span(), false
)
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions crates/evm/src/backend/starknet_backend.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn deploy(evm_address: EthAddress) -> Result<Address, EVMError> {
let mut kakarot_state = KakarotCore::unsafe_new_contract_state();
let uninitialized_account_class_hash = kakarot_state.uninitialized_account_class_hash();
let kakarot_address = get_contract_address();
let calldata: Span<felt252> = array![kakarot_address.into(), evm_address.into()].span();
let calldata: Span<felt252> = [kakarot_address.into(), evm_address.into()].span();

let (starknet_address, _) = deploy_syscall(
uninitialized_account_class_hash, evm_address.into(), calldata, true
Expand All @@ -62,7 +62,7 @@ fn get_bytecode(evm_address: EthAddress) -> Span<u8> {
let account = IAccountDispatcher { contract_address: starknet_address };
account.bytecode()
} else {
array![].span()
[].span()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/call_helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl CallHelpersImpl of CallHelpers {
ret_offset: usize,
ret_size: usize
) -> Result<(), EVMError> {
self.return_data = array![].span();
self.return_data = [].span();
if self.message().depth >= constants::STACK_MAX_DEPTH {
self.gas_left += gas;
return self.stack.push(0);
Expand Down
Loading

0 comments on commit 5a0f044

Please sign in to comment.