diff --git a/src/execution_state.cairo b/src/execution_state.cairo index 49f79b4..0eb9a90 100644 --- a/src/execution_state.cairo +++ b/src/execution_state.cairo @@ -1,4 +1,4 @@ -use governance::utils::u64_tuple_storage::{ThreeU64TupleStorePacking}; +use governance::utils::u64_tuple_storage::{TwoU64TupleStorePacking}; use starknet::storage_access::{StorePacking}; #[derive(Copy, Drop, Serde, PartialEq, Debug)] @@ -10,11 +10,17 @@ pub struct ExecutionState { pub(crate) impl ExecutionStateStorePacking of StorePacking { fn pack(value: ExecutionState) -> felt252 { - ThreeU64TupleStorePacking::pack((value.created, value.executed, value.canceled)) + u256 { + low: TwoU64TupleStorePacking::pack((value.created, value.executed)), + high: value.canceled.into() + } + .try_into() + .unwrap() } fn unpack(value: felt252) -> ExecutionState { - let (created, executed, canceled) = ThreeU64TupleStorePacking::unpack(value); - ExecutionState { created, executed, canceled } + let u256_value: u256 = value.into(); + let (created, executed) = TwoU64TupleStorePacking::unpack(u256_value.low); + ExecutionState { created, executed, canceled: (u256_value.high).try_into().unwrap() } } } diff --git a/src/execution_state_test.cairo b/src/execution_state_test.cairo new file mode 100644 index 0000000..9ae9d27 --- /dev/null +++ b/src/execution_state_test.cairo @@ -0,0 +1,29 @@ +use governance::utils::u64_tuple_storage::{ThreeU64TupleStorePacking, TwoU64TupleStorePacking}; +use governance::utils::u64_tuple_storage_test::{assert_pack_unpack}; +use starknet::storage_access::{StorePacking}; + +#[test] +fn test_three_tuple_storage_forward_back() { + assert_pack_unpack(ExecutionState { created: 123, executed: 234, canceled: 345 }); + assert_pack_unpack(ExecutionState { created: 0, executed: 0, canceled: 0 }); + + assert_pack_unpack( + ExecutionState { + created: 0xffffffffffffffff, executed: 0xffffffffffffffff, canceled: 0xffffffffffffffff + } + ); + + assert_pack_unpack( + ExecutionState { created: 0xffffffffffffffff, executed: 0xffffffffffffffff, canceled: 0 } + ); + assert_pack_unpack(ExecutionState { created: 0xffffffffffffffff, executed: 0, canceled: 0 }); + + assert_pack_unpack( + ExecutionState { created: 0xffffffffffffffff, executed: 0, canceled: 0xffffffffffffffff } + ); + assert_pack_unpack(ExecutionState { created: 0, executed: 0, canceled: 0xffffffffffffffff }); + + assert_pack_unpack( + ExecutionState { created: 0, executed: 0xffffffffffffffff, canceled: 0xffffffffffffffff } + ); +} diff --git a/src/utils/u64_tuple_storage.cairo b/src/utils/u64_tuple_storage.cairo index 78ec9f3..e6d7ee6 100644 --- a/src/utils/u64_tuple_storage.cairo +++ b/src/utils/u64_tuple_storage.cairo @@ -3,19 +3,6 @@ use starknet::storage_access::{StorePacking}; const TWO_POW_64: u128 = 0x10000000000000000; -pub(crate) impl ThreeU64TupleStorePacking of StorePacking<(u64, u64, u64), felt252> { - fn pack(value: (u64, u64, u64)) -> felt252 { - let (a, b, c) = value; - u256 { low: TwoU64TupleStorePacking::pack((a, b)), high: c.into() }.try_into().unwrap() - } - - fn unpack(value: felt252) -> (u64, u64, u64) { - let u256_value: u256 = value.into(); - let (a, b) = TwoU64TupleStorePacking::unpack(u256_value.low); - (a, b, (u256_value.high).try_into().unwrap()) - } -} - pub(crate) impl TwoU64TupleStorePacking of StorePacking<(u64, u64), u128> { fn pack(value: (u64, u64)) -> u128 { let (a, b) = value; diff --git a/src/utils/u64_tuple_storage_test.cairo b/src/utils/u64_tuple_storage_test.cairo index 8f8c2f2..e8178f2 100644 --- a/src/utils/u64_tuple_storage_test.cairo +++ b/src/utils/u64_tuple_storage_test.cairo @@ -1,4 +1,4 @@ -use governance::utils::u64_tuple_storage::{ThreeU64TupleStorePacking, TwoU64TupleStorePacking}; +use governance::utils::u64_tuple_storage::{TwoU64TupleStorePacking}; use starknet::storage_access::{StorePacking}; pub(crate) fn assert_pack_unpack< @@ -17,20 +17,3 @@ fn test_two_tuple_storage_forward_back() { assert_pack_unpack((0xffffffffffffffff_u64, 0_u64)); assert_pack_unpack((0_u64, 0xffffffffffffffff_u64)); } - -#[test] -fn test_three_tuple_storage_forward_back() { - assert_pack_unpack((123_u64, 234_u64, 345_u64)); - assert_pack_unpack((0_u64, 0_u64, 0_u64)); - - assert_pack_unpack((0xffffffffffffffff_u64, 0xffffffffffffffff_u64, 0xffffffffffffffff_u64)); - - assert_pack_unpack((0xffffffffffffffff_u64, 0xffffffffffffffff_u64, 0_u64)); - assert_pack_unpack((0xffffffffffffffff_u64, 0_u64, 0_u64)); - assert_pack_unpack((0_u64, 0_u64, 0_u64)); - - assert_pack_unpack((0xffffffffffffffff_u64, 0_u64, 0xffffffffffffffff_u64)); - assert_pack_unpack((0_u64, 0_u64, 0xffffffffffffffff_u64)); - - assert_pack_unpack((0_u64, 0xffffffffffffffff_u64, 0xffffffffffffffff_u64)); -}