Skip to content

Commit

Permalink
ensure ozvotes / evm_slot_value strategies expose the SingleSlotProof…
Browse files Browse the repository at this point in the history
… external functions
  • Loading branch information
pscott committed Oct 17, 2024
1 parent 86e78d3 commit 46b80c5
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 0 deletions.
1 change: 1 addition & 0 deletions starknet/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,6 @@ mod external {
mod herodotus;
}

#[cfg(test)]
mod tests;

3 changes: 3 additions & 0 deletions starknet/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,4 +53,5 @@ mod setup {

mod utils {
mod strategy_trait;
mod single_slot_proof;
}
21 changes: 21 additions & 0 deletions starknet/src/tests/mocks/facts_registry.cairo
Original file line number Diff line number Diff line change
@@ -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<ContractState> {
fn get_storage(
self: @ContractState,
block: u256,
account: felt252,
slot: u256,
mpt_proof: Span<Words64>
) -> u256 {
return 1;
}
}
}
22 changes: 22 additions & 0 deletions starknet/src/tests/mocks/timestamp_remappers.cairo
Original file line number Diff line number Diff line change
@@ -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<ContractState> {
fn get_closest_l1_block_number(
self: @ContractState, tree: BinarySearchTree, timestamp: u256
) -> Result<Option<u256>, 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;
}
}
}
33 changes: 33 additions & 0 deletions starknet/src/tests/utils/single_slot_proof.cairo
Original file line number Diff line number Diff line change
@@ -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<BinarySearchTree> {
fn default() -> BinarySearchTree {
BinarySearchTree {
mapper_id: 1,
last_pos: 1,
peaks: array![].span(),
proofs: array![].span(),
left_neighbor: Option::None,
}
}
}
30 changes: 30 additions & 0 deletions starknet/src/voting_strategies/evm_slot_value.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
30 changes: 30 additions & 0 deletions starknet/src/voting_strategies/oz_votes_storage_proof.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 46b80c5

Please sign in to comment.