From 46b80c5e7fa405f7e86be026dda56e5a8d135d11 Mon Sep 17 00:00:00 2001 From: Scott Piriou <30843220+pscott@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:43:51 +0200 Subject: [PATCH] ensure ozvotes / evm_slot_value strategies expose the SingleSlotProof external functions --- starknet/src/lib.cairo | 1 + starknet/src/tests.cairo | 3 ++ starknet/src/tests/mocks/facts_registry.cairo | 21 ++++++++++++ .../src/tests/mocks/timestamp_remappers.cairo | 22 +++++++++++++ .../src/tests/utils/single_slot_proof.cairo | 33 +++++++++++++++++++ .../voting_strategies/evm_slot_value.cairo | 30 +++++++++++++++++ .../oz_votes_storage_proof.cairo | 30 +++++++++++++++++ .../oz_votes_trace_208_storage_proof.cairo | 30 +++++++++++++++++ 8 files changed, 170 insertions(+) create mode 100644 starknet/src/tests/mocks/facts_registry.cairo create mode 100644 starknet/src/tests/mocks/timestamp_remappers.cairo create mode 100644 starknet/src/tests/utils/single_slot_proof.cairo diff --git a/starknet/src/lib.cairo b/starknet/src/lib.cairo index a014c7636..81483908e 100644 --- a/starknet/src/lib.cairo +++ b/starknet/src/lib.cairo @@ -166,5 +166,6 @@ mod external { mod herodotus; } +#[cfg(test)] mod tests; diff --git a/starknet/src/tests.cairo b/starknet/src/tests.cairo index 4edaa337c..438e0f25b 100644 --- a/starknet/src/tests.cairo +++ b/starknet/src/tests.cairo @@ -36,9 +36,11 @@ mod space { mod mocks { mod erc20_votes_preset; mod executor; + mod facts_registry; mod no_voting_power; mod proposal_validation_always_fail; mod space_v2; + mod timestamp_remappers; mod vanilla_authenticator; mod vanilla_execution_strategy; mod vanilla_proposal_validation; @@ -51,4 +53,5 @@ mod setup { mod utils { mod strategy_trait; + mod single_slot_proof; } diff --git a/starknet/src/tests/mocks/facts_registry.cairo b/starknet/src/tests/mocks/facts_registry.cairo new file mode 100644 index 000000000..f8e2e99d8 --- /dev/null +++ b/starknet/src/tests/mocks/facts_registry.cairo @@ -0,0 +1,21 @@ +#[starknet::contract] +mod MockFactsRegistry { + use sx::external::herodotus::IEVMFactsRegistry; + use sx::external::herodotus::{BinarySearchTree, MapperId, Words64}; + + #[storage] + struct Storage {} + + #[abi(embed_v0)] + impl FactsRegistry of IEVMFactsRegistry { + fn get_storage( + self: @ContractState, + block: u256, + account: felt252, + slot: u256, + mpt_proof: Span + ) -> u256 { + return 1; + } + } +} diff --git a/starknet/src/tests/mocks/timestamp_remappers.cairo b/starknet/src/tests/mocks/timestamp_remappers.cairo new file mode 100644 index 000000000..f0d058a9d --- /dev/null +++ b/starknet/src/tests/mocks/timestamp_remappers.cairo @@ -0,0 +1,22 @@ +#[starknet::contract] +mod MockTimestampRemappers { + use sx::external::herodotus::ITimestampRemappers; + use sx::external::herodotus::{BinarySearchTree, MapperId}; + + #[storage] + struct Storage {} + + #[abi(embed_v0)] + impl TimestampRemappers of ITimestampRemappers { + fn get_closest_l1_block_number( + self: @ContractState, tree: BinarySearchTree, timestamp: u256 + ) -> Result, felt252> { + return Result::Ok(Option::Some(1)); + } + + // Getter for the last timestamp of a given mapper. + fn get_last_mapper_timestamp(self: @ContractState, mapper_id: MapperId) -> u256 { + return 1; + } + } +} diff --git a/starknet/src/tests/utils/single_slot_proof.cairo b/starknet/src/tests/utils/single_slot_proof.cairo new file mode 100644 index 000000000..edf2fb719 --- /dev/null +++ b/starknet/src/tests/utils/single_slot_proof.cairo @@ -0,0 +1,33 @@ +use sx::tests::mocks::{ + timestamp_remappers::MockTimestampRemappers, facts_registry::MockFactsRegistry +}; +use starknet::ContractAddress; +use sx::external::herodotus::BinarySearchTree; + +fn deploy_timestamp_remappers() -> ContractAddress { + let (contract_address, _) = starknet::syscalls::deploy_syscall( + MockTimestampRemappers::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false, + ) + .unwrap(); + contract_address +} + +fn deploy_facts_registry() -> ContractAddress { + let (contract_address, _) = starknet::syscalls::deploy_syscall( + MockFactsRegistry::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false, + ) + .unwrap(); + contract_address +} + +impl DefaultBinarySearchTree of Default { + fn default() -> BinarySearchTree { + BinarySearchTree { + mapper_id: 1, + last_pos: 1, + peaks: array![].span(), + proofs: array![].span(), + left_neighbor: Option::None, + } + } +} diff --git a/starknet/src/voting_strategies/evm_slot_value.cairo b/starknet/src/voting_strategies/evm_slot_value.cairo index 5f12361b2..4781298bf 100644 --- a/starknet/src/voting_strategies/evm_slot_value.cairo +++ b/starknet/src/voting_strategies/evm_slot_value.cairo @@ -97,6 +97,36 @@ mod EvmSlotValueVotingStrategy { #[cfg(test)] mod tests { use super::EvmSlotValueVotingStrategy; + use sx::interfaces::{ + ISingleSlotProof, ISingleSlotProofDispatcher, ISingleSlotProofDispatcherTrait + }; + use sx::tests::mocks::timestamp_remappers::MockTimestampRemappers; + use sx::tests::mocks::facts_registry::MockFactsRegistry; + use sx::external::herodotus::BinarySearchTree; + use sx::tests::utils::single_slot_proof::{ + deploy_timestamp_remappers, deploy_facts_registry, DefaultBinarySearchTree + }; + + #[test] + #[available_gas(10000000)] + fn ensure_ssp_is_exposed() { + let constructor_calldata = array![ + deploy_timestamp_remappers().into(), deploy_facts_registry().into() + ]; + let (contract_address, _) = starknet::syscalls::deploy_syscall( + EvmSlotValueVotingStrategy::TEST_CLASS_HASH.try_into().unwrap(), + 0, + constructor_calldata.span(), + false, + ) + .unwrap(); + + let ssp = ISingleSlotProofDispatcher { contract_address }; + let tt = 1337; + ssp.cache_timestamp(tt, DefaultBinarySearchTree::default()); + + assert(ssp.cached_timestamps(tt) == 1, 'Timestamp not cached'); + } #[test] #[available_gas(10000000)] diff --git a/starknet/src/voting_strategies/oz_votes_storage_proof.cairo b/starknet/src/voting_strategies/oz_votes_storage_proof.cairo index 8a9a17369..d87678ecc 100644 --- a/starknet/src/voting_strategies/oz_votes_storage_proof.cairo +++ b/starknet/src/voting_strategies/oz_votes_storage_proof.cairo @@ -142,6 +142,36 @@ mod OZVotesStorageProofVotingStrategy { #[cfg(test)] mod tests { use super::OZVotesStorageProofVotingStrategy; + use sx::interfaces::{ + ISingleSlotProof, ISingleSlotProofDispatcher, ISingleSlotProofDispatcherTrait + }; + use sx::tests::mocks::timestamp_remappers::MockTimestampRemappers; + use sx::tests::mocks::facts_registry::MockFactsRegistry; + use sx::external::herodotus::BinarySearchTree; + use sx::tests::utils::single_slot_proof::{ + deploy_timestamp_remappers, deploy_facts_registry, DefaultBinarySearchTree + }; + + #[test] + #[available_gas(10000000)] + fn ensure_ssp_is_exposed() { + let constructor_calldata = array![ + deploy_timestamp_remappers().into(), deploy_facts_registry().into() + ]; + let (contract_address, _) = starknet::syscalls::deploy_syscall( + OZVotesStorageProofVotingStrategy::TEST_CLASS_HASH.try_into().unwrap(), + 0, + constructor_calldata.span(), + false, + ) + .unwrap(); + + let ssp = ISingleSlotProofDispatcher { contract_address }; + let tt = 1337; + ssp.cache_timestamp(tt, DefaultBinarySearchTree::default()); + + assert(ssp.cached_timestamps(tt) == 1, 'Timestamp not cached'); + } #[test] #[available_gas(10000000)] diff --git a/starknet/src/voting_strategies/oz_votes_trace_208_storage_proof.cairo b/starknet/src/voting_strategies/oz_votes_trace_208_storage_proof.cairo index 822987c8b..d60cc911e 100644 --- a/starknet/src/voting_strategies/oz_votes_trace_208_storage_proof.cairo +++ b/starknet/src/voting_strategies/oz_votes_trace_208_storage_proof.cairo @@ -139,6 +139,36 @@ mod OZVotesTrace208StorageProofVotingStrategy { #[cfg(test)] mod tests { use super::OZVotesTrace208StorageProofVotingStrategy; + use sx::interfaces::{ + ISingleSlotProof, ISingleSlotProofDispatcher, ISingleSlotProofDispatcherTrait + }; + use sx::tests::mocks::timestamp_remappers::MockTimestampRemappers; + use sx::tests::mocks::facts_registry::MockFactsRegistry; + use sx::external::herodotus::BinarySearchTree; + use sx::tests::utils::single_slot_proof::{ + deploy_timestamp_remappers, deploy_facts_registry, DefaultBinarySearchTree + }; + + #[test] + #[available_gas(10000000)] + fn ensure_ssp_is_exposed() { + let constructor_calldata = array![ + deploy_timestamp_remappers().into(), deploy_facts_registry().into() + ]; + let (contract_address, _) = starknet::syscalls::deploy_syscall( + OZVotesTrace208StorageProofVotingStrategy::TEST_CLASS_HASH.try_into().unwrap(), + 0, + constructor_calldata.span(), + false, + ) + .unwrap(); + + let ssp = ISingleSlotProofDispatcher { contract_address }; + let tt = 1337; + ssp.cache_timestamp(tt, DefaultBinarySearchTree::default()); + + assert(ssp.cached_timestamps(tt) == 1, 'Timestamp not cached'); + } #[test] #[available_gas(10000000)]