Skip to content

Commit

Permalink
Use while loops
Browse files Browse the repository at this point in the history
  • Loading branch information
b-j-roberts committed Apr 3, 2024
1 parent 39ccd99 commit 788bd75
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions src/bytes/src/storage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl BytesStore of Store<Bytes> {
/// * 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)
}

Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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
}

0 comments on commit 788bd75

Please sign in to comment.