Skip to content

Commit

Permalink
remove the three tuple storage packing since it's only used in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Mar 18, 2024
1 parent 88ed52b commit 59a8cb8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
14 changes: 10 additions & 4 deletions src/execution_state.cairo
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -10,11 +10,17 @@ pub struct ExecutionState {

pub(crate) impl ExecutionStateStorePacking of StorePacking<ExecutionState, felt252> {
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() }
}
}
29 changes: 29 additions & 0 deletions src/execution_state_test.cairo
Original file line number Diff line number Diff line change
@@ -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 }
);
}
13 changes: 0 additions & 13 deletions src/utils/u64_tuple_storage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 1 addition & 18 deletions src/utils/u64_tuple_storage_test.cairo
Original file line number Diff line number Diff line change
@@ -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<
Expand All @@ -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));
}

0 comments on commit 59a8cb8

Please sign in to comment.