Skip to content

Commit

Permalink
add SingleSlotProofImpl as embeddable
Browse files Browse the repository at this point in the history
  • Loading branch information
pscott committed Oct 17, 2024
1 parent 7b21f43 commit 250b40c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 90 deletions.
28 changes: 28 additions & 0 deletions starknet/src/interfaces/i_single_slot_proof.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use sx::external::herodotus::BinarySearchTree;

/// Optional trait that execution strategies can decide to implement.
#[starknet::interface]
trait ISingleSlotProof<TContractState> {
/// Queries the Timestamp Remapper contract for the closest L1 block number that occurred before
/// the given timestamp and then caches the result. If the queried timestamp is less than the earliest
/// timestamp or larger than the latest timestamp in the mapper then the transaction will revert.
/// This function should be used to cache a remapped timestamp before it's used when calling the
/// `get_storage_slot` function with the same timestamp.
///
/// # Arguments
///
/// * `timestamp` - The timestamp at which to query.
/// * `tree` - The tree proof required to query the remapper.
fn cache_timestamp(ref self: TContractState, timestamp: u32, tree: BinarySearchTree);

/// View function exposing the cached remapped timestamps. Reverts if the timestamp is not cached.
///
/// # Arguments
///
/// * `timestamp` - The timestamp to query.
///
/// # Returns
///
/// * `u256` - The cached L1 block number corresponding to the timestamp.
fn cached_timestamps(self: @TContractState, timestamp: u32) -> u256;
}
5 changes: 5 additions & 0 deletions starknet/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ mod interfaces {
IVotingStrategy, IVotingStrategyDispatcher, IVotingStrategyDispatcherTrait
};

mod i_single_slot_proof;
use i_single_slot_proof::{
ISingleSlotProof, ISingleSlotProofDispatcher, ISingleSlotProofDispatcherTrait
};

mod i_space;
use i_space::{ISpace, ISpaceDispatcher, ISpaceDispatcherTrait};

Expand Down
6 changes: 6 additions & 0 deletions starknet/src/utils/single_slot_proof.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod SingleSlotProofComponent {
ITimestampRemappersDispatcherTrait, IEVMFactsRegistryDispatcher,
IEVMFactsRegistryDispatcherTrait
};
use sx::interfaces::i_single_slot_proof::ISingleSlotProof;

#[storage]
struct Storage {
Expand Down Expand Up @@ -46,7 +47,12 @@ mod SingleSlotProofComponent {

slot_value
}
}

#[embeddable_as(SingleSlotProofImpl)]
impl SimpleQuorum<
TContractState, +HasComponent<TContractState>
> of ISingleSlotProof<ComponentState<TContractState>> {
fn cache_timestamp(
ref self: ComponentState<TContractState>, timestamp: u32, tree: BinarySearchTree
) {
Expand Down
32 changes: 2 additions & 30 deletions starknet/src/voting_strategies/evm_slot_value.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mod EvmSlotValueVotingStrategy {
path: SingleSlotProofComponent, storage: single_slot_proof, event: SingleSlotProofEvent
);

#[abi(embed_v0)]
impl SingleSlotProofImpl = SingleSlotProofComponent::SingleSlotProofImpl<ContractState>;
impl SingleSlotProofInternalImpl = SingleSlotProofComponent::InternalImpl<ContractState>;

#[storage]
Expand Down Expand Up @@ -74,36 +76,6 @@ mod EvmSlotValueVotingStrategy {
}
}

#[generate_trait]
impl SingleSlotProofImpl of SingleSlotProofTrait {
/// Queries the Timestamp Remapper contract for the closest L1 block number that occurred before
/// the given timestamp and then caches the result. If the queried timestamp is less than the earliest
/// timestamp or larger than the latest timestamp in the mapper then the transaction will revert.
/// This function should be used to cache a remapped timestamp before it's used when calling the
/// `get_storage_slot` function with the same timestamp.
///
/// # Arguments
///
/// * `timestamp` - The timestamp at which to query.
/// * `tree` - The tree proof required to query the remapper.
fn cache_timestamp(ref self: ContractState, timestamp: u32, tree: BinarySearchTree) {
self.single_slot_proof.cache_timestamp(timestamp, tree);
}

/// View function exposing the cached remapped timestamps. Reverts if the timestamp is not cached.
///
/// # Arguments
///
/// * `timestamp` - The timestamp to query.
///
/// # Returns
///
/// * `u256` - The cached L1 block number corresponding to the timestamp.
fn cached_timestamps(self: @ContractState, timestamp: u32) -> u256 {
self.single_slot_proof.cached_timestamps(timestamp)
}
}

#[generate_trait]
impl InternalImpl of InternalTrait {
fn get_mapping_slot_key(mapping_key: u256, slot_index: u256) -> u256 {
Expand Down
33 changes: 3 additions & 30 deletions starknet/src/voting_strategies/oz_votes_storage_proof.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ mod OZVotesStorageProofVotingStrategy {
path: SingleSlotProofComponent, storage: single_slot_proof, event: SingleSlotProofEvent
);

#[abi(embed_v0)]
impl SingleSlotProofImpl =
SingleSlotProofComponent::SingleSlotProofImpl<ContractState>;
impl SingleSlotProofInternalImpl = SingleSlotProofComponent::InternalImpl<ContractState>;


Expand Down Expand Up @@ -96,36 +99,6 @@ mod OZVotesStorageProofVotingStrategy {
}
}

#[generate_trait]
impl SingleSlotProofImpl of SingleSlotProofTrait {
/// Queries the Timestamp Remapper contract for the closest L1 block number that occurred before
/// the given timestamp and then caches the result. If the queried timestamp is less than the earliest
/// timestamp or larger than the latest timestamp in the mapper then the transaction will revert.
/// This function should be used to cache a remapped timestamp before it's used when calling the
/// `get_storage_slot` function with the same timestamp.
///
/// # Arguments
///
/// * `timestamp` - The timestamp at which to query.
/// * `tree` - The tree proof required to query the remapper.
fn cache_timestamp(ref self: ContractState, timestamp: u32, tree: BinarySearchTree) {
self.single_slot_proof.cache_timestamp(timestamp, tree);
}

/// View function exposing the cached remapped timestamps. Reverts if the timestamp is not cached.
///
/// # Arguments
///
/// * `timestamp` - The timestamp to query.
///
/// # Returns
///
/// * `u256` - The cached L1 block number corresponding to the timestamp.
fn cached_timestamps(self: @ContractState, timestamp: u32) -> u256 {
self.single_slot_proof.cached_timestamps(timestamp)
}
}

#[generate_trait]
impl InternalImpl of InternalTrait {
fn final_checkpoint_slot_key(mapping_key: u256, slot_index: u256, offset: u32) -> u256 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ mod OZVotesTrace208StorageProofVotingStrategy {
path: SingleSlotProofComponent, storage: single_slot_proof, event: SingleSlotProofEvent
);

#[abi(embed_v0)]
impl SingleSlotProofImpl =
SingleSlotProofComponent::SingleSlotProofImpl<ContractState>;
impl SingleSlotProofInternalImpl = SingleSlotProofComponent::InternalImpl<ContractState>;

#[storage]
Expand Down Expand Up @@ -93,36 +96,6 @@ mod OZVotesTrace208StorageProofVotingStrategy {
}
}

#[generate_trait]
impl SingleSlotProofImpl of SingleSlotProofTrait {
/// Queries the Timestamp Remapper contract for the closest L1 block number that occurred before
/// the given timestamp and then caches the result. If the queried timestamp is less than the earliest
/// timestamp or larger than the latest timestamp in the mapper then the transaction will revert.
/// This function should be used to cache a remapped timestamp before it's used when calling the
/// `get_storage_slot` function with the same timestamp.
///
/// # Arguments
///
/// * `timestamp` - The timestamp at which to query.
/// * `tree` - The tree proof required to query the remapper.
fn cache_timestamp(ref self: ContractState, timestamp: u32, tree: BinarySearchTree) {
self.single_slot_proof.cache_timestamp(timestamp, tree);
}

/// View function exposing the cached remapped timestamps. Reverts if the timestamp is not cached.
///
/// # Arguments
///
/// * `timestamp` - The timestamp to query.
///
/// # Returns
///
/// * `u256` - The cached L1 block number corresponding to the timestamp.
fn cached_timestamps(self: @ContractState, timestamp: u32) -> u256 {
self.single_slot_proof.cached_timestamps(timestamp)
}
}

#[generate_trait]
impl InternalImpl of InternalTrait {
fn final_checkpoint_slot_key(mapping_key: u256, slot_index: u256, offset: u32) -> u256 {
Expand Down

0 comments on commit 250b40c

Please sign in to comment.