Skip to content

Commit

Permalink
Alias mmr element and aggregator id types
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Jun 3, 2024
1 parent a96d02f commit 214a995
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 47 deletions.
11 changes: 6 additions & 5 deletions src/core/commitments_inbox.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion src/core/common.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
type MmrId = u128;
type MmrSize = u128;
type AggregatorId = u256;
6 changes: 4 additions & 2 deletions src/core/evm_facts_registry.cairo
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -115,6 +116,7 @@ trait IEVMFactsRegistry<TContractState> {
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;
Expand All @@ -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
};
Expand Down
52 changes: 26 additions & 26 deletions src/core/headers_store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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<TContractState> {
Expand All @@ -19,7 +19,7 @@ trait IHeadersStore<TContractState> {
// @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
Expand All @@ -36,7 +36,7 @@ trait IHeadersStore<TContractState> {
// @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
Expand All @@ -53,7 +53,7 @@ trait IHeadersStore<TContractState> {
fn verify_mmr_inclusion(
self: @TContractState,
index: MmrSize,
poseidon_blockhash: felt252,
poseidon_blockhash: MmrElement,
peaks: Peaks,
proof: Proof,
mmr_id: MmrId,
Expand All @@ -71,7 +71,7 @@ trait IHeadersStore<TContractState> {
fn verify_historical_mmr_inclusion(
self: @TContractState,
index: MmrSize,
poseidon_blockhash: felt252,
poseidon_blockhash: MmrElement,
peaks: Peaks,
proof: Proof,
mmr_id: MmrId,
Expand Down Expand Up @@ -108,9 +108,9 @@ trait IHeadersStore<TContractState> {
// @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
);

Expand All @@ -125,7 +125,7 @@ trait IHeadersStore<TContractState> {
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,
Expand Down Expand Up @@ -153,7 +153,7 @@ trait IHeadersStore<TContractState> {
#[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::{
Expand All @@ -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]
Expand All @@ -174,7 +174,7 @@ mod HeadersStore {
// MMR root = 0 means that MMR doesn't exist
mmr: LegacyMap::<MmrId, MMR>,
// 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::<u256, u256>,
}
Expand All @@ -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
}
Expand All @@ -208,15 +208,15 @@ mod HeadersStore {
struct ProcessedBatch {
block_start: u256,
block_end: u256,
new_root: felt252,
new_root: MmrElement,
new_size: MmrSize,
mmr_id: MmrId
}

#[derive(Drop, starknet::Event)]
struct BranchCreatedFromElement {
mmr_id: MmrId,
root: felt252,
root: MmrElement,
last_pos: MmrSize,
detached_from_mmr_id: MmrId,
mmr_index: MmrSize
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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))
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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');
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/core/tests/test_facts_registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions src/core/tests/test_headers_store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -282,7 +283,7 @@ fn test_create_branch_from_message() {
);
}

fn helper_create_mmr_with_items(mut items: Span<felt252>) -> MMR {
fn helper_create_mmr_with_items(mut items: Span<MmrElement>) -> MMR {
let mut mmr: MMR = Default::default();
let mut peaks = array![].span();
loop {
Expand Down
5 changes: 3 additions & 2 deletions src/remappers/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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<Words64>;
type MapperId = u128;
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/remappers/timestamp_remappers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -43,7 +43,7 @@ mod TimestampRemappers {
mapper_id: MapperId,
start_block: u256,
end_block: u256,
mmr_root: felt252,
mmr_root: MmrElement,
mmr_size: MmrSize
}

Expand All @@ -67,7 +67,7 @@ mod TimestampRemappers {
headers_store: ContractAddress,
mappers: LegacyMap::<MapperId, Mapper>,
mappers_mmrs: LegacyMap::<MapperId, MMR>,
mappers_mmrs_history: LegacyMap::<(MapperId, MmrSize), felt252>,
mappers_mmrs_history: LegacyMap::<(MapperId, MmrSize), MmrElement>,
}

#[constructor]
Expand Down

0 comments on commit 214a995

Please sign in to comment.