Skip to content

Commit da96616

Browse files
authored
Refactored and further tested storage of Result and Option. (#8583)
1 parent 9010f8c commit da96616

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

corelib/src/starknet/storage_access.cairo

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -681,13 +681,16 @@ impl TupleNextStore<
681681
}
682682
}
683683

684+
const RESULT_OK_INDICATOR: felt252 = 0;
685+
const RESULT_ERR_INDICATOR: felt252 = 1;
686+
684687
impl ResultStore<T, E, +Store<T>, +Store<E>, +Drop<T>, +Drop<E>> of Store<Result<T, E>> {
685688
#[inline]
686689
fn read(address_domain: u32, base: StorageBaseAddress) -> SyscallResult<Result<T, E>> {
687690
let idx = Store::<felt252>::read(address_domain, base)?;
688-
if idx == 0 {
691+
if idx == RESULT_OK_INDICATOR {
689692
starknet::SyscallResult::Ok(Ok(Store::read_at_offset(address_domain, base, 1_u8)?))
690-
} else if idx == 1 {
693+
} else if idx == RESULT_ERR_INDICATOR {
691694
starknet::SyscallResult::Ok(Err(Store::read_at_offset(address_domain, base, 1_u8)?))
692695
} else {
693696
starknet::SyscallResult::Err(array!['Incorrect index:'])
@@ -700,11 +703,11 @@ impl ResultStore<T, E, +Store<T>, +Store<E>, +Drop<T>, +Drop<E>> of Store<Result
700703
) -> SyscallResult<()> {
701704
match value {
702705
Ok(x) => {
703-
Store::write(address_domain, base, 0)?;
706+
Store::write(address_domain, base, RESULT_OK_INDICATOR)?;
704707
Store::write_at_offset(address_domain, base, 1_u8, x)?;
705708
},
706709
Err(x) => {
707-
Store::write(address_domain, base, 1)?;
710+
Store::write(address_domain, base, RESULT_ERR_INDICATOR)?;
708711
Store::write_at_offset(address_domain, base, 1_u8, x)?;
709712
},
710713
}
@@ -716,11 +719,11 @@ impl ResultStore<T, E, +Store<T>, +Store<E>, +Drop<T>, +Drop<E>> of Store<Result
716719
address_domain: u32, base: StorageBaseAddress, offset: u8,
717720
) -> SyscallResult<Result<T, E>> {
718721
let idx = Store::<felt252>::read_at_offset(address_domain, base, offset)?;
719-
if idx == 0 {
722+
if idx == RESULT_OK_INDICATOR {
720723
starknet::SyscallResult::Ok(
721724
Ok(Store::read_at_offset(address_domain, base, offset + 1_u8)?),
722725
)
723-
} else if idx == 1 {
726+
} else if idx == RESULT_ERR_INDICATOR {
724727
starknet::SyscallResult::Ok(
725728
Err(Store::read_at_offset(address_domain, base, offset + 1_u8)?),
726729
)
@@ -735,11 +738,11 @@ impl ResultStore<T, E, +Store<T>, +Store<E>, +Drop<T>, +Drop<E>> of Store<Result
735738
) -> SyscallResult<()> {
736739
match value {
737740
Ok(x) => {
738-
Store::write_at_offset(address_domain, base, offset, 0)?;
741+
Store::write_at_offset(address_domain, base, offset, RESULT_OK_INDICATOR)?;
739742
Store::write_at_offset(address_domain, base, offset + 1_u8, x)?;
740743
},
741744
Err(x) => {
742-
Store::write_at_offset(address_domain, base, offset, 0)?;
745+
Store::write_at_offset(address_domain, base, offset, RESULT_ERR_INDICATOR)?;
743746
Store::write_at_offset(address_domain, base, offset + 1_u8, x)?;
744747
},
745748
}
@@ -752,13 +755,16 @@ impl ResultStore<T, E, +Store<T>, +Store<E>, +Drop<T>, +Drop<E>> of Store<Result
752755
}
753756
}
754757

758+
const OPTION_NONE_INDICATOR: felt252 = 0;
759+
const OPTION_SOME_INDICATOR: felt252 = 1;
760+
755761
impl OptionStore<T, +Store<T>, +Drop<T>> of Store<Option<T>> {
756762
#[inline]
757763
fn read(address_domain: u32, base: StorageBaseAddress) -> SyscallResult<Option<T>> {
758764
let idx = Store::<felt252>::read(address_domain, base)?;
759-
if idx == 1 {
765+
if idx == OPTION_SOME_INDICATOR {
760766
starknet::SyscallResult::Ok(Some(Store::read_at_offset(address_domain, base, 1_u8)?))
761-
} else if idx == 0 {
767+
} else if idx == OPTION_NONE_INDICATOR {
762768
starknet::SyscallResult::Ok(None)
763769
} else {
764770
starknet::SyscallResult::Err(array!['Incorrect index:'])
@@ -769,10 +775,10 @@ impl OptionStore<T, +Store<T>, +Drop<T>> of Store<Option<T>> {
769775
fn write(address_domain: u32, base: StorageBaseAddress, value: Option<T>) -> SyscallResult<()> {
770776
match value {
771777
Some(x) => {
772-
Store::write(address_domain, base, 1)?;
778+
Store::write(address_domain, base, OPTION_SOME_INDICATOR)?;
773779
Store::write_at_offset(address_domain, base, 1_u8, x)?;
774780
},
775-
None(_) => { Store::write(address_domain, base, 0)?; },
781+
None(_) => { Store::write(address_domain, base, OPTION_NONE_INDICATOR)?; },
776782
}
777783
starknet::SyscallResult::Ok(())
778784
}
@@ -782,11 +788,11 @@ impl OptionStore<T, +Store<T>, +Drop<T>> of Store<Option<T>> {
782788
address_domain: u32, base: StorageBaseAddress, offset: u8,
783789
) -> SyscallResult<Option<T>> {
784790
let idx = Store::<felt252>::read_at_offset(address_domain, base, offset)?;
785-
if idx == 1 {
791+
if idx == OPTION_SOME_INDICATOR {
786792
starknet::SyscallResult::Ok(
787793
Some(Store::read_at_offset(address_domain, base, offset + 1_u8)?),
788794
)
789-
} else if idx == 0 {
795+
} else if idx == OPTION_NONE_INDICATOR {
790796
starknet::SyscallResult::Ok(None)
791797
} else {
792798
starknet::SyscallResult::Err(array!['Incorrect index:'])
@@ -799,10 +805,12 @@ impl OptionStore<T, +Store<T>, +Drop<T>> of Store<Option<T>> {
799805
) -> SyscallResult<()> {
800806
match value {
801807
Some(x) => {
802-
Store::write_at_offset(address_domain, base, offset, 1)?;
808+
Store::write_at_offset(address_domain, base, offset, OPTION_SOME_INDICATOR)?;
803809
Store::write_at_offset(address_domain, base, offset + 1_u8, x)?;
804810
},
805-
None(_x) => { Store::write_at_offset(address_domain, base, offset, 0)?; },
811+
None(_x) => {
812+
Store::write_at_offset(address_domain, base, offset, OPTION_NONE_INDICATOR)?;
813+
},
806814
}
807815
starknet::SyscallResult::Ok(())
808816
}

crates/cairo-lang-starknet/cairo_level_tests/storage_access.cairo

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ mod test_contract {
109109
pub vecs: super::Vecs,
110110
pub queryable_enum: super::QueryableEnum,
111111
pub option_value: Option<u32>,
112+
pub offset_option_value: (felt252, Option<u32>),
113+
pub result_value: Result<u32, u32>,
114+
pub offset_result_value: (felt252, Result<u32, u32>),
112115
}
113116
}
114117

@@ -263,6 +266,27 @@ fn test_enum_sub_pointers() {
263266
assert_eq!(ptr.high.read(), 0);
264267
}
265268

269+
#[test]
270+
fn test_option_and_result_base_access() {
271+
let mut state = test_contract::contract_state_for_testing();
272+
state.option_value.write(Some(101_u32));
273+
state.offset_option_value.write((202_felt252, Some(102_u32)));
274+
state.result_value.write(Ok(103_u32));
275+
state.offset_result_value.write((204_felt252, Ok(104_u32)));
276+
assert_eq!((@state).option_value.read(), Some(101));
277+
assert_eq!((@state).offset_option_value.read(), (202_felt252, Some(102_u32)));
278+
assert_eq!((@state).result_value.read(), Ok(103));
279+
assert_eq!((@state).offset_result_value.read(), (204_felt252, Ok(104_u32)));
280+
state.option_value.write(None);
281+
state.offset_option_value.write((205_felt252, None));
282+
state.result_value.write(Err(106_u32));
283+
state.offset_result_value.write((207_felt252, Err(107_u32)));
284+
assert_eq!((@state).option_value.read(), None);
285+
assert_eq!((@state).offset_option_value.read(), (205_felt252, None));
286+
assert_eq!((@state).result_value.read(), Err(106));
287+
assert_eq!((@state).offset_result_value.read(), (207_felt252, Err(107_u32)));
288+
}
289+
266290
#[test]
267291
fn test_option_sub_pointers() {
268292
let mut state = test_contract::contract_state_for_testing();

0 commit comments

Comments
 (0)