diff --git a/src/execution_state.cairo b/src/execution_state.cairo index 0eb9a90..946ddbc 100644 --- a/src/execution_state.cairo +++ b/src/execution_state.cairo @@ -1,6 +1,20 @@ -use governance::utils::u64_tuple_storage::{TwoU64TupleStorePacking}; use starknet::storage_access::{StorePacking}; +const TWO_POW_64: u128 = 0x10000000000000000; +const TWO_POW_64_DIVISOR: NonZero = 0x10000000000000000; + +impl TwoU64TupleStorePacking of StorePacking<(u64, u64), u128> { + fn pack(value: (u64, u64)) -> u128 { + let (a, b) = value; + a.into() + (b.into() * TWO_POW_64) + } + + fn unpack(value: u128) -> (u64, u64) { + let (q, r) = DivRem::div_rem(value, TWO_POW_64_DIVISOR); + (r.try_into().unwrap(), q.try_into().unwrap()) + } +} + #[derive(Copy, Drop, Serde, PartialEq, Debug)] pub struct ExecutionState { pub created: u64, @@ -21,6 +35,6 @@ pub(crate) impl ExecutionStateStorePacking of StorePacking ExecutionState { let u256_value: u256 = value.into(); let (created, executed) = TwoU64TupleStorePacking::unpack(u256_value.low); - ExecutionState { created, executed, canceled: (u256_value.high).try_into().unwrap() } + ExecutionState { created, executed, canceled: u256_value.high.try_into().unwrap() } } } diff --git a/src/execution_state_test.cairo b/src/execution_state_test.cairo index 9ae9d27..70f5610 100644 --- a/src/execution_state_test.cairo +++ b/src/execution_state_test.cairo @@ -1,7 +1,14 @@ -use governance::utils::u64_tuple_storage::{ThreeU64TupleStorePacking, TwoU64TupleStorePacking}; -use governance::utils::u64_tuple_storage_test::{assert_pack_unpack}; +use governance::execution_state::{ExecutionState}; use starknet::storage_access::{StorePacking}; +pub(crate) fn assert_pack_unpack< + T, U, +StorePacking, +PartialEq, +core::fmt::Debug, +Drop, +Copy +>( + x: T +) { + assert_eq!(x, StorePacking::::unpack(StorePacking::::pack(x))); +} + #[test] fn test_three_tuple_storage_forward_back() { assert_pack_unpack(ExecutionState { created: 123, executed: 234, canceled: 345 }); @@ -26,4 +33,5 @@ fn test_three_tuple_storage_forward_back() { assert_pack_unpack( ExecutionState { created: 0, executed: 0xffffffffffffffff, canceled: 0xffffffffffffffff } ); + assert_pack_unpack(ExecutionState { created: 0, executed: 0xffffffffffffffff, canceled: 0 }); } diff --git a/src/governor.cairo b/src/governor.cairo index 560bb38..9e9a994 100644 --- a/src/governor.cairo +++ b/src/governor.cairo @@ -71,7 +71,6 @@ pub trait IGovernor { // Get the proposal info for the given proposal id. fn get_proposal(self: @TContractState, id: felt252) -> ProposalInfo; - // Configure the delay and the window for call execution. This must be self-called via #queue. fn configure(ref self: TContractState, config: Config); diff --git a/src/lib.cairo b/src/lib.cairo index 49b63fa..286e6ad 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -7,6 +7,8 @@ pub mod call_trait; #[cfg(test)] pub(crate) mod call_trait_test; pub mod execution_state; +#[cfg(test)] +pub(crate) mod execution_state_test; pub mod governor; #[cfg(test)] pub(crate) mod governor_test; @@ -18,9 +20,6 @@ pub(crate) mod interfaces { } pub(crate) mod utils { pub(crate) mod exp2; - pub(crate) mod u64_tuple_storage; - #[cfg(test)] - pub(crate) mod u64_tuple_storage_test; } #[cfg(test)] diff --git a/src/staker_test.cairo b/src/staker_test.cairo index 7a26363..f0059d6 100644 --- a/src/staker_test.cairo +++ b/src/staker_test.cairo @@ -3,6 +3,7 @@ use core::num::traits::zero::{Zero}; use core::option::{OptionTrait}; use core::result::{Result, ResultTrait}; use core::traits::{TryInto}; +use governance::execution_state_test::{assert_pack_unpack}; use governance::interfaces::erc20::{IERC20Dispatcher, IERC20DispatcherTrait}; use governance::staker::{ @@ -10,7 +11,6 @@ use governance::staker::{ Staker::{DelegatedSnapshotStorePacking, DelegatedSnapshot}, }; use governance::test::test_token::{TestToken, deploy as deploy_token}; -use governance::utils::u64_tuple_storage_test::{assert_pack_unpack}; use starknet::testing::{set_contract_address, set_block_timestamp, pop_log}; use starknet::{ get_contract_address, syscalls::deploy_syscall, ClassHash, contract_address_const, diff --git a/src/utils/u64_tuple_storage.cairo b/src/utils/u64_tuple_storage.cairo deleted file mode 100644 index 613e894..0000000 --- a/src/utils/u64_tuple_storage.cairo +++ /dev/null @@ -1,16 +0,0 @@ -use starknet::storage_access::{StorePacking}; - -const TWO_POW_64: u128 = 0x10000000000000000; -const TWO_POW_64_DIVISOR: NonZero = 0x10000000000000000; - -pub(crate) impl TwoU64TupleStorePacking of StorePacking<(u64, u64), u128> { - fn pack(value: (u64, u64)) -> u128 { - let (a, b) = value; - a.into() + (b.into() * TWO_POW_64) - } - - fn unpack(value: u128) -> (u64, u64) { - let (q, r) = DivRem::div_rem(value, TWO_POW_64_DIVISOR); - (r.try_into().unwrap(), q.try_into().unwrap()) - } -} diff --git a/src/utils/u64_tuple_storage_test.cairo b/src/utils/u64_tuple_storage_test.cairo deleted file mode 100644 index e8178f2..0000000 --- a/src/utils/u64_tuple_storage_test.cairo +++ /dev/null @@ -1,19 +0,0 @@ -use governance::utils::u64_tuple_storage::{TwoU64TupleStorePacking}; -use starknet::storage_access::{StorePacking}; - -pub(crate) fn assert_pack_unpack< - T, U, +StorePacking, +PartialEq, +core::fmt::Debug, +Drop, +Copy ->( - x: T -) { - assert_eq!(x, StorePacking::::unpack(StorePacking::::pack(x))); -} - -#[test] -fn test_two_tuple_storage_forward_back() { - assert_pack_unpack((123_u64, 234_u64)); - assert_pack_unpack((0_u64, 0_u64)); - assert_pack_unpack((0xffffffffffffffff_u64, 0xffffffffffffffff_u64)); - assert_pack_unpack((0xffffffffffffffff_u64, 0_u64)); - assert_pack_unpack((0_u64, 0xffffffffffffffff_u64)); -}