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

Commit

Permalink
dev: change loop syntaxes (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat authored Aug 8, 2024
1 parent 2ea8eea commit 2a27502
Show file tree
Hide file tree
Showing 24 changed files with 190 additions and 498 deletions.
33 changes: 10 additions & 23 deletions crates/contracts/src/kakarot_core/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,9 @@ pub mod KakarotCore {
self.Kakarot_uninitialized_account_class_hash.write(uninitialized_account_class_hash);
self.Kakarot_coinbase.write(coinbase);
self.Kakarot_block_gas_limit.write(block_gas_limit);
loop {
match eoas_to_deploy.pop_front() {
Option::Some(eoa_address) => self.deploy_externally_owned_account(*eoa_address),
Option::None => { break; },
};
}
for eoa_address in eoas_to_deploy {
self.deploy_externally_owned_account(*eoa_address);
};
}

#[abi(embed_v0)]
Expand Down Expand Up @@ -319,23 +316,13 @@ pub mod KakarotCore {

let mut accessed_storage_keys: Set<(EthAddress, u256)> = Default::default();

match tx.try_access_list() {
Option::Some(mut access_list) => {
loop {
match access_list.pop_front() {
Option::Some(access_list_item) => {
let AccessListItem { ethereum_address, storage_keys: _ } =
*access_list_item;
let storage_keys = access_list_item.to_storage_keys();

accessed_addresses.add(ethereum_address);
accessed_storage_keys.extend_from_span(storage_keys);
},
Option::None => { break; }
}
}
},
Option::None => {}
if let Option::Some(mut access_list) = tx.try_access_list() {
for access_list_item in access_list {
let AccessListItem { ethereum_address, storage_keys: _ } = *access_list_item;
let storage_keys = access_list_item.to_storage_keys();
accessed_addresses.add(ethereum_address);
accessed_storage_keys.extend_from_span(storage_keys);
}
};

let message = Message {
Expand Down
70 changes: 24 additions & 46 deletions crates/evm/src/backend/starknet_backend.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,11 @@ mod internals {
/// `Ok(())` if the commit was successful, otherwise an `EVMError`.
fn commit_accounts(ref state: State) -> Result<(), EVMError> {
let mut account_keys = state.accounts.keyset.to_span();
loop {
match account_keys.pop_front() {
Option::Some(evm_address) => {
let account = state.accounts.changes.get(*evm_address).deref();
commit(@account, ref state);
},
Option::None => { break Result::Ok(()); }
};
}
while let Option::Some(evm_address) = account_keys.pop_front() {
let account = state.accounts.changes.get(*evm_address).deref();
commit(@account, ref state);
};
return Result::Ok(());
}

/// Commits the account to Starknet by updating the account state if it
Expand Down Expand Up @@ -240,57 +236,39 @@ mod internals {
fn transfer_native_token(ref self: State) -> Result<(), EVMError> {
let kakarot_state = KakarotCore::unsafe_new_contract_state();
let native_token = kakarot_state.get_native_token();
loop {
match self.transfers.pop_front() {
Option::Some(transfer) => {
IERC20CamelDispatcher { contract_address: native_token }
.transferFrom(
transfer.sender.starknet, transfer.recipient.starknet, transfer.amount
);
},
Option::None => { break; }
}
while let Option::Some(transfer) = self.transfers.pop_front() {
IERC20CamelDispatcher { contract_address: native_token }
.transferFrom(
transfer.sender.starknet, transfer.recipient.starknet, transfer.amount
);
};
Result::Ok(())
}

/// Iterates through the list of events and emits them.
fn emit_events(ref self: State) -> Result<(), EVMError> {
loop {
match self.events.pop_front() {
Option::Some(event) => {
let mut keys = Default::default();
let mut data = Default::default();
Serde::<Array<u256>>::serialize(@event.keys, ref keys);
Serde::<Array<u8>>::serialize(@event.data, ref data);
emit_event_syscall(keys.span(), data.span()).unwrap_syscall();
},
Option::None => { break Result::Ok(()); }
}
}
while let Option::Some(event) = self.events.pop_front() {
let mut keys = Default::default();
let mut data = Default::default();
Serde::<Array<u256>>::serialize(@event.keys, ref keys);
Serde::<Array<u8>>::serialize(@event.data, ref data);
emit_event_syscall(keys.span(), data.span()).unwrap_syscall();
};
return Result::Ok(());
}

/// Commits storage changes to the KakarotCore contract by writing pending
/// state changes to Starknet Storage.
/// commit_storage MUST be called after commit_accounts.
fn commit_storage(ref self: State) -> Result<(), EVMError> {
let mut storage_keys = self.accounts_storage.keyset.to_span();
let result = loop {
match storage_keys.pop_front() {
Option::Some(state_key) => {
let (evm_address, key, value) = self
.accounts_storage
.changes
.get(*state_key)
.deref();
let mut account = self.get_account(evm_address);
IAccountDispatcher { contract_address: account.starknet_address() }
.write_storage(key, value);
},
Option::None => { break Result::Ok(()); }
}
while let Option::Some(state_key) = storage_keys.pop_front() {
let (evm_address, key, value) = self.accounts_storage.changes.get(*state_key).deref();
let mut account = self.get_account(evm_address);
IAccountDispatcher { contract_address: account.starknet_address() }
.write_storage(key, value);
};
result
Result::Ok(())
}
}

Expand Down
44 changes: 17 additions & 27 deletions crates/evm/src/gas.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,13 @@ fn calculate_intrinsic_gas_cost(tx: @EthereumTransaction) -> u128 {
let mut calldata = tx.calldata();
let calldata_len: usize = calldata.len();

loop {
match calldata.pop_front() {
Option::Some(data) => {
data_cost +=
if *data == 0 {
TRANSACTION_ZERO_DATA
} else {
TRANSACTION_NON_ZERO_DATA_INIT
};
},
Option::None => { break; },
}
for data in calldata {
data_cost +=
if *data == 0 {
TRANSACTION_ZERO_DATA
} else {
TRANSACTION_NON_ZERO_DATA_INIT
};
};

let create_cost = if target.is_none() {
Expand All @@ -223,21 +218,16 @@ fn calculate_intrinsic_gas_cost(tx: @EthereumTransaction) -> u128 {
0
};

let access_list_cost = match tx.try_access_list() {
Option::Some(mut access_list) => {
let mut access_list_cost = 0;
loop {
match access_list.pop_front() {
Option::Some(access_list_item) => {
let AccessListItem { ethereum_address: _, storage_keys } = access_list_item;
access_list_cost += ACCESS_LIST_ADDRESS
+ (ACCESS_LIST_STORAGE_KEY * (*storage_keys).len().into());
},
Option::None => { break access_list_cost; }
}
}
},
Option::None => { 0 }
let access_list_cost = if let Option::Some(mut access_list) = tx.try_access_list() {
let mut access_list_cost: u128 = 0;
for access_list_item in access_list {
let AccessListItem { ethereum_address: _, storage_keys } = *access_list_item;
access_list_cost += ACCESS_LIST_ADDRESS
+ (ACCESS_LIST_STORAGE_KEY * storage_keys.len().into());
};
access_list_cost
} else {
0
};

TRANSACTION_BASE_COST + data_cost + create_cost + access_list_cost
Expand Down
13 changes: 3 additions & 10 deletions crates/evm/src/instructions/duplication_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,16 @@ mod tests {
return;
}

loop {
if idx == to {
break;
}

while idx != to {
assert(stack.peek_at(idx).unwrap() == 0x00, 'should be zero');
idx += 1;
}
};
}

// push `n` number of `0x0` to the stack
fn push_zeros(ref stack: Stack, n: u8) {
let mut i = 0;
loop {
if i == n {
break;
}
while i != n {
stack.push(0x0).unwrap();
i += 1;
}
Expand Down
29 changes: 5 additions & 24 deletions crates/evm/src/instructions/environmental_information.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,10 @@ impl EnvironmentInformationImpl of EnvironmentInformationTrait {
// Fill the rest of the data to load with zeros
// TODO: optimize once we have dw-based exponentiation
let mut i = 32 - bytes_len;
loop {
if i == 0 {
break;
}
while i != 0 {
data_to_load *= 256;
i -= 1;
};

self.stack.push(data_to_load)
}

Expand Down Expand Up @@ -693,11 +689,7 @@ mod tests {
// bound bytes, 0's have been copied.
// Otherwise, the memory value would be 0, and we wouldn't be able to check it.
let mut i = 0;
loop {
if i == (size / 32) + 1 {
break;
}

while i != (size / 32) + 1 {
vm
.memory
.store(
Expand Down Expand Up @@ -834,11 +826,7 @@ mod tests {
let mut results: Array<u8> = u256_to_bytes_array(result);

let mut i = 0;
loop {
if (i == size) {
break;
}

while i != size {
// For out of bound bytes, 0s will be copied.
if (i + offset >= bytecode.len()) {
assert(*results[i] == 0, 'wrong data value');
Expand Down Expand Up @@ -1202,11 +1190,7 @@ mod tests {
let mut results: Array<u8> = ArrayTrait::new();

let mut i = 0;
loop {
if i == (size / 32) + 1 {
break;
}

while i != (size / 32) + 1 {
let result: u256 = vm.memory.load_internal(dest_offset + (i * 32)).into();
let result_span = u256_to_bytes_array(result).span();

Expand Down Expand Up @@ -1375,10 +1359,7 @@ mod tests {
setup_contracts_for_testing();

let mut i = 0;
loop {
if i == 0x10 {
break;
}
while i != 0x10 {
vm.stack.push(i.into()).expect('push failed');
// When
vm.exec_extcodehash().unwrap();
Expand Down
5 changes: 1 addition & 4 deletions crates/evm/src/instructions/push_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,7 @@ mod tests {

fn get_n_0xFF(mut n: u8) -> Span<u8> {
let mut array: Array<u8> = ArrayTrait::new();
loop {
if n == 0 {
break;
}
while n != 0 {
array.append(0xFF);
n -= 1;
};
Expand Down
10 changes: 2 additions & 8 deletions crates/evm/src/instructions/sha3.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ mod internal {
fn fill_array_with_memory_words(
ref self: VM, ref to_hash: Array<u64>, mut offset: u32, mut amount: u32
) -> u32 {
loop {
if amount == 0 {
break;
}
while amount != 0 {
let loaded = self.memory.load(offset);
let ((high_h, low_h), (high_l, low_l)) = loaded.split_into_u64_le();
to_hash.append(low_h);
Expand Down Expand Up @@ -439,10 +436,7 @@ mod tests {
vm.stack.push(0x00).expect('push failed');

let mut mem_dst: u32 = 0;
loop {
if mem_dst > 0x0C80 {
break;
}
while mem_dst <= 0x0C80 {
vm
.memory
.store(0xFAFAFAFA00000000000000000000000000000000000000000000000000000000, mem_dst);
Expand Down
Loading

0 comments on commit 2a27502

Please sign in to comment.