From 1ff9f6b04b8e4ab25d4523b54918bd51875745aa Mon Sep 17 00:00:00 2001 From: Ori Ziv Date: Mon, 10 Jun 2024 17:30:55 +0300 Subject: [PATCH] Fixed implementation of Option and Result Store. --- corelib/src/starknet/storage_access.cairo | 14 +++++++------- tests/bug_samples/issue5764.cairo | 9 +++++++++ tests/bug_samples/lib.cairo | 1 + 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 tests/bug_samples/issue5764.cairo diff --git a/corelib/src/starknet/storage_access.cairo b/corelib/src/starknet/storage_access.cairo index ad8836792e0..8c0460d3558 100644 --- a/corelib/src/starknet/storage_access.cairo +++ b/corelib/src/starknet/storage_access.cairo @@ -587,12 +587,12 @@ impl ResultStore, +Store, +Drop, +Drop> of Store SyscallResult<()> { match value { Result::Ok(x) => { - Store::write(address_domain, base, 0)?; - Store::write_at_offset(address_domain, base, 1_u8, x)?; + Store::write_at_offset(address_domain, base, offset, 0)?; + Store::write_at_offset(address_domain, base, offset + 1_u8, x)?; }, Result::Err(x) => { - Store::write(address_domain, base, 1)?; - Store::write_at_offset(address_domain, base, 1_u8, x)?; + Store::write_at_offset(address_domain, base, offset, 0)?; + Store::write_at_offset(address_domain, base, offset + 1_u8, x)?; } }; starknet::SyscallResult::Ok(()) @@ -649,10 +649,10 @@ impl OptionStore, +Drop,> of Store> { ) -> SyscallResult<()> { match value { Option::Some(x) => { - Store::write(address_domain, base, 1)?; - Store::write_at_offset(address_domain, base, 1_u8, x)?; + Store::write_at_offset(address_domain, base, offset, 1)?; + Store::write_at_offset(address_domain, base, offset + 1_u8, x)?; }, - Option::None(_x) => { Store::write(address_domain, base, 0)?; } + Option::None(_x) => { Store::write_at_offset(address_domain, base, offset, 0)?; } }; starknet::SyscallResult::Ok(()) } diff --git a/tests/bug_samples/issue5764.cairo b/tests/bug_samples/issue5764.cairo new file mode 100644 index 00000000000..39925c26741 --- /dev/null +++ b/tests/bug_samples/issue5764.cairo @@ -0,0 +1,9 @@ +use core::starknet::SyscallResultTrait; + +#[test] +fn test_store_opt_and_result_at_offset() { + let base = starknet::storage_access::storage_base_address_const::<1000>(); + let v = (Option::Some(1_u256), Result::<_, u128>::Ok(2_u8), Option::Some(3_u64)); + starknet::Store::write(0, base, v).unwrap_syscall(); + assert_eq!(starknet::Store::read(0, base), Result::Ok(v)); +} diff --git a/tests/bug_samples/lib.cairo b/tests/bug_samples/lib.cairo index 50dc46b7375..c8d221d2ba3 100644 --- a/tests/bug_samples/lib.cairo +++ b/tests/bug_samples/lib.cairo @@ -40,6 +40,7 @@ mod issue4318; mod issue4380; mod issue4897; mod issue4937; +mod issue5764; mod loop_break_in_match; mod loop_only_change; mod partial_param_local;