From 214a995d18b9788d8b0ecc025249477534451594 Mon Sep 17 00:00:00 2001 From: Filip Krawczyk Date: Mon, 3 Jun 2024 16:31:27 +0200 Subject: [PATCH] Alias mmr element and aggregator id types --- src/core/commitments_inbox.cairo | 11 ++--- src/core/common.cairo | 2 +- src/core/evm_facts_registry.cairo | 6 ++- src/core/headers_store.cairo | 52 ++++++++++++------------ src/core/tests/test_facts_registry.cairo | 3 +- src/core/tests/test_headers_store.cairo | 11 ++--- src/remappers/interface.cairo | 5 ++- src/remappers/timestamp_remappers.cairo | 8 ++-- 8 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/core/commitments_inbox.cairo b/src/core/commitments_inbox.cairo index ac2c79f..fa7327a 100644 --- a/src/core/commitments_inbox.cairo +++ b/src/core/commitments_inbox.cairo @@ -50,7 +50,8 @@ mod CommitmentsInbox { use herodotus_eth_starknet::core::headers_store::{ IHeadersStoreDispatcherTrait, IHeadersStoreDispatcher }; - use herodotus_eth_starknet::core::common::{MmrId, MmrSize}; + use herodotus_eth_starknet::core::common::{MmrId, AggregatorId}; + use cairo_lib::data_structures::mmr::mmr::{MmrSize, MmrElement}; #[storage] struct Storage { @@ -87,9 +88,9 @@ mod CommitmentsInbox { #[derive(Drop, starknet::Event)] struct MMRReceived { - root: felt252, + root: MmrElement, last_pos: MmrSize, - aggregator_id: u256 + aggregator_id: AggregatorId } #[constructor] @@ -210,9 +211,9 @@ mod CommitmentsInbox { fn receive_mmr( ref self: ContractState, from_address: felt252, - root: felt252, + root: MmrElement, last_pos: MmrSize, - aggregator_id: u256, + aggregator_id: AggregatorId, mmr_id: MmrId ) { assert(from_address == self.l1_message_sender.read().into(), 'ONLY_L1_MESSAGE_SENDER'); diff --git a/src/core/common.cairo b/src/core/common.cairo index 2d4781b..da00ea0 100644 --- a/src/core/common.cairo +++ b/src/core/common.cairo @@ -1,2 +1,2 @@ type MmrId = u128; -type MmrSize = u128; +type AggregatorId = u256; diff --git a/src/core/evm_facts_registry.cairo b/src/core/evm_facts_registry.cairo index b0dbdd5..7f91eda 100644 --- a/src/core/evm_facts_registry.cairo +++ b/src/core/evm_facts_registry.cairo @@ -1,8 +1,9 @@ use starknet::ContractAddress; +use cairo_lib::data_structures::mmr::mmr::MmrSize; use cairo_lib::data_structures::mmr::proof::Proof; use cairo_lib::data_structures::mmr::peaks::Peaks; use cairo_lib::utils::types::words64::Words64; -use herodotus_eth_starknet::core::common::{MmrId, MmrSize}; +use herodotus_eth_starknet::core::common::MmrId; #[derive(Drop, Serde)] enum AccountField { @@ -115,6 +116,7 @@ trait IEVMFactsRegistry { mod EVMFactsRegistry { use starknet::ContractAddress; use super::AccountField; + use cairo_lib::data_structures::mmr::mmr::MmrSize; use cairo_lib::data_structures::mmr::proof::Proof; use cairo_lib::data_structures::mmr::peaks::Peaks; use cairo_lib::hashing::poseidon::hash_words64; @@ -123,7 +125,7 @@ mod EVMFactsRegistry { use cairo_lib::utils::types::words64::{ Words64, Words64Trait, reverse_endianness_u64, bytes_used_u64 }; - use herodotus_eth_starknet::core::common::{MmrId, MmrSize}; + use herodotus_eth_starknet::core::common::MmrId; use herodotus_eth_starknet::core::headers_store::{ IHeadersStoreDispatcherTrait, IHeadersStoreDispatcher }; diff --git a/src/core/headers_store.cairo b/src/core/headers_store.cairo index 95ccd45..7bc1a6d 100644 --- a/src/core/headers_store.cairo +++ b/src/core/headers_store.cairo @@ -2,8 +2,8 @@ use starknet::ContractAddress; use cairo_lib::data_structures::mmr::peaks::Peaks; use cairo_lib::data_structures::mmr::proof::Proof; use cairo_lib::utils::types::words64::Words64; -use cairo_lib::data_structures::mmr::mmr::MMR; -use herodotus_eth_starknet::core::common::{MmrId, MmrSize}; +use cairo_lib::data_structures::mmr::mmr::{MMR, MmrSize, MmrElement}; +use herodotus_eth_starknet::core::common::{MmrId, AggregatorId}; #[starknet::interface] trait IHeadersStore { @@ -19,7 +19,7 @@ trait IHeadersStore { // @notice Returns the root of the MMR with a given id // @param mmr_id The id of the MMR // @return The root of the MMR with the given id - fn get_mmr_root(self: @TContractState, mmr_id: MmrId) -> felt252; + fn get_mmr_root(self: @TContractState, mmr_id: MmrId) -> MmrElement; // @notice Returns the size of the MMR with a given id // @param mmr_id The id of the MMR @@ -36,7 +36,7 @@ trait IHeadersStore { // @param mmr_id The id of the MMR // @param size The size of the MMR // @return The root of the MMR with the given id and size - fn get_historical_root(self: @TContractState, mmr_id: MmrId, size: MmrSize) -> felt252; + fn get_historical_root(self: @TContractState, mmr_id: MmrId, size: MmrSize) -> MmrElement; // @notice Receives a parent blockhash and the corresponding block number from L1 and saves it // @dev This function can only be called by the CommitmentsInbox contract @@ -53,7 +53,7 @@ trait IHeadersStore { fn verify_mmr_inclusion( self: @TContractState, index: MmrSize, - poseidon_blockhash: felt252, + poseidon_blockhash: MmrElement, peaks: Peaks, proof: Proof, mmr_id: MmrId, @@ -71,7 +71,7 @@ trait IHeadersStore { fn verify_historical_mmr_inclusion( self: @TContractState, index: MmrSize, - poseidon_blockhash: felt252, + poseidon_blockhash: MmrElement, peaks: Peaks, proof: Proof, mmr_id: MmrId, @@ -108,9 +108,9 @@ trait IHeadersStore { // @dev This function can only be called by the CommitmentsInbox contract fn create_branch_from_message( ref self: TContractState, - root: felt252, + root: MmrElement, last_pos: MmrSize, - aggregator_id: u256, + aggregator_id: AggregatorId, new_mmr_id: MmrId ); @@ -125,7 +125,7 @@ trait IHeadersStore { fn create_branch_single_element( ref self: TContractState, index: MmrSize, - initial_poseidon_blockhash: felt252, + initial_poseidon_blockhash: MmrElement, peaks: Peaks, proof: Proof, mmr_id: MmrId, @@ -153,7 +153,7 @@ trait IHeadersStore { #[starknet::contract] mod HeadersStore { use starknet::{ContractAddress, get_caller_address}; - use cairo_lib::data_structures::mmr::mmr::{MMR, MMRTrait}; + use cairo_lib::data_structures::mmr::mmr::{MMR, MMRTrait, MmrSize, MmrElement}; use cairo_lib::data_structures::mmr::peaks::Peaks; use cairo_lib::data_structures::mmr::proof::Proof; use cairo_lib::utils::types::words64::{ @@ -163,9 +163,9 @@ mod HeadersStore { use cairo_lib::hashing::poseidon::hash_words64; use cairo_lib::utils::bitwise::reverse_endianness_u256; use cairo_lib::encoding::rlp::{RLPItem, rlp_decode_list_lazy}; - use herodotus_eth_starknet::core::common::{MmrId, MmrSize}; + use herodotus_eth_starknet::core::common::{MmrId, AggregatorId}; - const MMR_INITIAL_ROOT: felt252 = + const MMR_INITIAL_ROOT: MmrElement = 0x6759138078831011e3bc0b4a135af21c008dda64586363531697207fb5a2bae; #[storage] @@ -174,7 +174,7 @@ mod HeadersStore { // MMR root = 0 means that MMR doesn't exist mmr: LegacyMap::, // MMR root = 0 means that MMR doesn't exist - mmr_history: LegacyMap::<(MmrId, MmrSize), felt252>, + mmr_history: LegacyMap::<(MmrId, MmrSize), MmrElement>, // block_number => parent blockhash received_blocks: LegacyMap::, } @@ -199,7 +199,7 @@ mod HeadersStore { #[derive(Drop, starknet::Event)] struct ProcessedBlock { block_number: u256, - new_root: felt252, + new_root: MmrElement, new_size: MmrSize, mmr_id: MmrId } @@ -208,7 +208,7 @@ mod HeadersStore { struct ProcessedBatch { block_start: u256, block_end: u256, - new_root: felt252, + new_root: MmrElement, new_size: MmrSize, mmr_id: MmrId } @@ -216,7 +216,7 @@ mod HeadersStore { #[derive(Drop, starknet::Event)] struct BranchCreatedFromElement { mmr_id: MmrId, - root: felt252, + root: MmrElement, last_pos: MmrSize, detached_from_mmr_id: MmrId, mmr_index: MmrSize @@ -225,15 +225,15 @@ mod HeadersStore { #[derive(Drop, starknet::Event)] struct BranchCreatedFromL1 { mmr_id: MmrId, - root: felt252, + root: MmrElement, last_pos: MmrSize, - aggregator_id: u256 + aggregator_id: AggregatorId } #[derive(Drop, starknet::Event)] struct BranchCreatedClone { mmr_id: MmrId, - root: felt252, + root: MmrElement, last_pos: MmrSize, detached_from_mmr_id: MmrId } @@ -257,7 +257,7 @@ mod HeadersStore { } // @inheritdoc IHeadersStore - fn get_mmr_root(self: @ContractState, mmr_id: MmrId) -> felt252 { + fn get_mmr_root(self: @ContractState, mmr_id: MmrId) -> MmrElement { self.mmr.read(mmr_id).root } @@ -272,7 +272,7 @@ mod HeadersStore { } // @inheritdoc IHeadersStore - fn get_historical_root(self: @ContractState, mmr_id: MmrId, size: MmrSize) -> felt252 { + fn get_historical_root(self: @ContractState, mmr_id: MmrId, size: MmrSize) -> MmrElement { self.mmr_history.read((mmr_id, size)) } @@ -434,7 +434,7 @@ mod HeadersStore { fn verify_mmr_inclusion( self: @ContractState, index: MmrSize, - poseidon_blockhash: felt252, + poseidon_blockhash: MmrElement, peaks: Peaks, proof: Proof, mmr_id: MmrId, @@ -449,7 +449,7 @@ mod HeadersStore { fn verify_historical_mmr_inclusion( self: @ContractState, index: MmrSize, - poseidon_blockhash: felt252, + poseidon_blockhash: MmrElement, peaks: Peaks, proof: Proof, mmr_id: MmrId, @@ -466,9 +466,9 @@ mod HeadersStore { // @inheritdoc IHeadersStore fn create_branch_from_message( ref self: ContractState, - root: felt252, + root: MmrElement, last_pos: MmrSize, - aggregator_id: u256, + aggregator_id: AggregatorId, new_mmr_id: MmrId ) { assert(new_mmr_id != 0, 'NEW_MMR_ID_0_NOT_ALLOWED'); @@ -495,7 +495,7 @@ mod HeadersStore { fn create_branch_single_element( ref self: ContractState, index: MmrSize, - initial_poseidon_blockhash: felt252, + initial_poseidon_blockhash: MmrElement, peaks: Peaks, proof: Proof, mmr_id: MmrId, diff --git a/src/core/tests/test_facts_registry.cairo b/src/core/tests/test_facts_registry.cairo index 45ce0ec..0ed52e4 100644 --- a/src/core/tests/test_facts_registry.cairo +++ b/src/core/tests/test_facts_registry.cairo @@ -6,11 +6,10 @@ use herodotus_eth_starknet::core::headers_store::{ use herodotus_eth_starknet::core::evm_facts_registry::{ IEVMFactsRegistryDispatcherTrait, IEVMFactsRegistryDispatcher, AccountField }; -use herodotus_eth_starknet::core::common::MmrSize; use starknet::ContractAddress; use cairo_lib::utils::types::words64::Words64; use cairo_lib::hashing::poseidon::{hash_words64, PoseidonHasher}; -use cairo_lib::data_structures::mmr::{proof::Proof, peaks::Peaks}; +use cairo_lib::data_structures::mmr::{proof::Proof, peaks::Peaks, mmr::MmrSize}; const COMMITMENTS_INBOX_ADDRESS: felt252 = 0x123; const TEST_MMR_ROOT: felt252 = 0x37a31db9c80c54ec632f04f7984155dc43591a3f8c891adfbf34e75331e0eec; diff --git a/src/core/tests/test_headers_store.cairo b/src/core/tests/test_headers_store.cairo index 608e583..9921d6f 100644 --- a/src/core/tests/test_headers_store.cairo +++ b/src/core/tests/test_headers_store.cairo @@ -5,16 +5,17 @@ use herodotus_eth_starknet::core::headers_store::{ IHeadersStoreDispatcherTrait, IHeadersStoreDispatcher, IHeadersStoreSafeDispatcherTrait, IHeadersStoreSafeDispatcher }; -use herodotus_eth_starknet::core::common::{MmrSize, MmrId}; +use herodotus_eth_starknet::core::common::MmrId; use starknet::ContractAddress; use cairo_lib::utils::types::words64::Words64; -use cairo_lib::data_structures::mmr::mmr::{MMR, MMRTrait}; +use cairo_lib::data_structures::mmr::mmr::{MMR, MMRTrait, MmrSize, MmrElement}; use debug::PrintTrait; const COMMITMENTS_INBOX_ADDRESS: felt252 = 0x123; -const MMR_INITIAL_ELEMENT: felt252 = +const MMR_INITIAL_ELEMENT: MmrElement = 0x02241b3b7f1c4b9cf63e670785891de91f7237b1388f6635c1898ae397ad32dd; -const MMR_INITIAL_ROOT: felt252 = 0x6759138078831011e3bc0b4a135af21c008dda64586363531697207fb5a2bae; +const MMR_INITIAL_ROOT: MmrElement = + 0x6759138078831011e3bc0b4a135af21c008dda64586363531697207fb5a2bae; fn helper_create_headers_store() -> (IHeadersStoreDispatcher, ContractAddress) { let contract = declare("HeadersStore").unwrap(); @@ -282,7 +283,7 @@ fn test_create_branch_from_message() { ); } -fn helper_create_mmr_with_items(mut items: Span) -> MMR { +fn helper_create_mmr_with_items(mut items: Span) -> MMR { let mut mmr: MMR = Default::default(); let mut peaks = array![].span(); loop { diff --git a/src/remappers/interface.cairo b/src/remappers/interface.cairo index ad59fbc..c0b5c90 100644 --- a/src/remappers/interface.cairo +++ b/src/remappers/interface.cairo @@ -4,10 +4,11 @@ // Interface types // +use cairo_lib::data_structures::mmr::mmr::{MmrSize, MmrElement}; use cairo_lib::data_structures::mmr::proof::Proof; use cairo_lib::data_structures::mmr::peaks::Peaks; use cairo_lib::utils::types::words64::Words64; -use herodotus_eth_starknet::core::common::{MmrId, MmrSize}; +use herodotus_eth_starknet::core::common::MmrId; type Headers = Span; type MapperId = u128; @@ -17,7 +18,7 @@ struct OriginElement { tree_id: MmrId, last_pos: MmrSize, leaf_idx: MmrSize, - leaf_value: felt252, + leaf_value: MmrElement, inclusion_proof: Proof, peaks: Peaks, header: Words64 diff --git a/src/remappers/timestamp_remappers.cairo b/src/remappers/timestamp_remappers.cairo index fba7967..0b6c514 100644 --- a/src/remappers/timestamp_remappers.cairo +++ b/src/remappers/timestamp_remappers.cairo @@ -6,14 +6,14 @@ #[starknet::contract] mod TimestampRemappers { - use herodotus_eth_starknet::core::common::{MmrId, MmrSize}; + use herodotus_eth_starknet::core::common::MmrId; use herodotus_eth_starknet::remappers::interface::{ ITimestampRemappers, Headers, OriginElement, Proof, Peaks, Words64, ProofElement, BinarySearchTree, MapperId }; use starknet::ContractAddress; use cairo_lib::hashing::poseidon::{PoseidonHasher, hash_words64}; - use cairo_lib::data_structures::mmr::mmr::{MMR, MMRTrait}; + use cairo_lib::data_structures::mmr::mmr::{MMR, MMRTrait, MmrSize, MmrElement}; use cairo_lib::data_structures::mmr::utils::{leaf_index_to_mmr_index}; use cairo_lib::encoding::rlp::{RLPItem, rlp_decode_list_lazy}; use cairo_lib::utils::types::words64::{reverse_endianness_u64, bytes_used_u64}; @@ -43,7 +43,7 @@ mod TimestampRemappers { mapper_id: MapperId, start_block: u256, end_block: u256, - mmr_root: felt252, + mmr_root: MmrElement, mmr_size: MmrSize } @@ -67,7 +67,7 @@ mod TimestampRemappers { headers_store: ContractAddress, mappers: LegacyMap::, mappers_mmrs: LegacyMap::, - mappers_mmrs_history: LegacyMap::<(MapperId, MmrSize), felt252>, + mappers_mmrs_history: LegacyMap::<(MapperId, MmrSize), MmrElement>, } #[constructor]