From 788bd751e605f3c9162108d8ef3598a238273122 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Wed, 3 Apr 2024 16:47:03 -0500 Subject: [PATCH] Use while loops --- src/bytes/src/storage.cairo | 72 ++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/src/bytes/src/storage.cairo b/src/bytes/src/storage.cairo index 4c2ee664..2357950f 100644 --- a/src/bytes/src/storage.cairo +++ b/src/bytes/src/storage.cairo @@ -49,7 +49,7 @@ impl BytesStore of Store { /// * The short string `Bytes` is used as the capacity argument of the sponge /// construction (domain separation). fn inner_bytes_pointer(address: StorageAddress, chunk: felt252) -> StorageBaseAddress { - let (r, _, _) = core::poseidon::hades_permutation(address.into(), chunk, 'Bytes'_felt252); + let (r, _, _) = core::poseidon::hades_permutation(address.into(), chunk, 'Bytes'); storage_base_address_from_felt252(r) } @@ -69,20 +69,24 @@ fn inner_read_bytes(address_domain: u32, address: StorageAddress) -> SyscallResu let mut chunk_base = inner_bytes_pointer(address, chunk); let mut index_in_chunk = 0_u8; let mut result: Bytes = BytesTrait::new_empty(); - loop { - if remaining_full_words == 0 { - break Result::Ok(()); - } + let mut error = Result::Ok(()); + while remaining_full_words != 0 { let value = match starknet::syscalls::storage_read_syscall( address_domain, storage_address_from_base_and_offset(chunk_base, index_in_chunk) ) { Result::Ok(value) => value, - Result::Err(err) => { break Result::Err(err); }, + Result::Err(err) => { + error = Result::Err(err); + break; + }, }; let value: u128 = match value.try_into() { Option::Some(x) => x, - Option::None => { break Result::Err(array!['Invalid inner value']); }, + Option::None => { + error = Result::Err(array!['Invalid inner value']); + break; + }, }; result.data.append(value); remaining_full_words -= 1; @@ -96,7 +100,10 @@ fn inner_read_bytes(address_domain: u32, address: StorageAddress) -> SyscallResu 0 }, }; - }?; + }; + if error.is_err() { + return SyscallResult::Err(error.unwrap_err()); + } if last_word_len != 0 { let last_word = starknet::syscalls::storage_read_syscall( address_domain, storage_address_from_base_and_offset(chunk_base, index_in_chunk) @@ -119,29 +126,30 @@ fn inner_write_bytes( let mut chunk = 0; let mut chunk_base = inner_bytes_pointer(address, chunk); let mut index_in_chunk = 0_u8; - loop { - let curr_value = match words.pop_front() { - Option::Some(x) => x, - Option::None => { break Result::Ok(()); }, - }; - match starknet::syscalls::storage_write_syscall( - address_domain, - storage_address_from_base_and_offset(chunk_base, index_in_chunk), - (*curr_value).into() - ) { - Result::Ok(_) => {}, - Result::Err(err) => { break Result::Err(err); }, - }; - index_in_chunk = match core::integer::u8_overflowing_add(index_in_chunk, 1) { - Result::Ok(x) => x, - Result::Err(_) => { - // After writing 256 `uint128`s `index_in_chunk` will overflow and we move to the - // next chunk. - chunk += 1; - chunk_base = inner_bytes_pointer(address, chunk); - 0 - }, + let mut result = Result::Ok(()); + while let Option::Some(curr_value) = words + .pop_front() { + match starknet::syscalls::storage_write_syscall( + address_domain, + storage_address_from_base_and_offset(chunk_base, index_in_chunk), + (*curr_value).into() + ) { + Result::Ok(_) => {}, + Result::Err(err) => { + result = Result::Err(err); + break; + }, + }; + index_in_chunk = match core::integer::u8_overflowing_add(index_in_chunk, 1) { + Result::Ok(x) => x, + Result::Err(_) => { + // After writing 256 `uint128`s `index_in_chunk` will overflow and we move to the + // next chunk. + chunk += 1; + chunk_base = inner_bytes_pointer(address, chunk); + 0 + }, + }; }; - }?; - Result::Ok(()) + result }