Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Aug 11, 2024
1 parent 7fcbafc commit a3fbeba
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 148 deletions.
25 changes: 14 additions & 11 deletions starknet/src/L1_headers_store/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ pub mod L1HeaderStore {
use fossil::types::ProcessBlockOptions;
use fossil::types::Words64Sequence;
use openzeppelin_access::ownable::OwnableComponent;
use starknet::storage::Map;
// *************************************************************************
// IMPORTS
// *************************************************************************
// Core lib imports
// Core lib imports
use openzeppelin_access::ownable::ownable::OwnableComponent::InternalTrait;
use openzeppelin_upgrades::UpgradeableComponent;
use openzeppelin_upgrades::interface::IUpgradeable;
use starknet::storage::Map;
use starknet::{ContractAddress, EthAddress, get_caller_address, ClassHash};

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
Expand All @@ -37,7 +37,7 @@ pub mod L1HeaderStore {
impl OwnableMixinImpl = OwnableComponent::OwnableMixinImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

// Upgradeable
// Upgradeable
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

// *************************************************************************
Expand Down Expand Up @@ -80,7 +80,7 @@ pub mod L1HeaderStore {
// CONSTRUCTOR
// *************************************************************************
/// Contract Constructor.
///
///
/// # Arguments
/// * `l1_messages_origin` - The address of L1 Message Proxy.
/// * `owner` - .
Expand All @@ -103,7 +103,7 @@ pub mod L1HeaderStore {
#[abi(embed_v0)]
impl L1HeaderStoreImpl of IL1HeadersStore<ContractState> {
/// Receives `block_number` and `parent_hash` from L1 Message Proxy for processing.
///
///
/// # Arguments
/// * `parent_hash` - reference to the hash of the previous block's header.
/// * `block_number` - unique identifier for each block.
Expand All @@ -124,7 +124,7 @@ pub mod L1HeaderStore {
}

/// Change L1 Message Proxy address. (Only Owner)
///
///
/// # Arguments
/// * `l1_messages_origin` - The address of L1 Message Proxy.
fn change_l1_messages_origin(
Expand Down Expand Up @@ -208,10 +208,12 @@ pub mod L1HeaderStore {
// *************************************************************************
#[generate_trait]
impl Private of PrivateTrait {
/// Validates the provided RLP-encoded block header `block_header_rlp` against the expected parent hash `child_block_parent_hash`.
///
/// Validates the provided RLP-encoded block header `block_header_rlp` against the expected
/// parent hash `child_block_parent_hash`.
///
/// # Arguments
/// * `child_block_parent_hash` - The expected parent hash of the child block. The parent hash of the next block i.e The hash of block `block_number`
/// * `child_block_parent_hash` - The expected parent hash of the child block. The parent
/// hash of the next block i.e The hash of block `block_number`
/// * `block_number` - The block number of the child block.
/// * `block_header_rlp_bytes_len` - The length of the RLP-encoded block header.
/// * `block_header_rlp` - An array of u64 representing the RLP-encoded block header.
Expand All @@ -220,9 +222,10 @@ pub mod L1HeaderStore {
/// A tuple containing:
/// * `block_header_rlp` The RLP-encoded block header.
/// * `block_header_rlp_bytes_len` The length of the RLP-encoded block header.
///
///
/// # Panics
/// This function panics if the calculated hash from `block_header_rlp` which is `provided_rlp_hash_u256`
/// This function panics if the calculated hash from `block_header_rlp` which is
/// `provided_rlp_hash_u256`
/// does not match the expected parent hash `child_block_parent_hash`.
fn validate_provided_header_rlp(
self: @ContractState,
Expand Down
12 changes: 6 additions & 6 deletions starknet/src/L1_messages_proxy/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod L1MessagesProxy {
// *************************************************************************
// IMPORTS
// *************************************************************************
// Core lib imports
// Core lib imports
use starknet::{ContractAddress, EthAddress, ClassHash};

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
Expand All @@ -25,7 +25,7 @@ pub mod L1MessagesProxy {
impl OwnableMixinImpl = OwnableComponent::OwnableMixinImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

// Upgradeable
// Upgradeable
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

/// Struct to receive message (parentHash_, blockNumber_) from L1.
Expand Down Expand Up @@ -67,7 +67,7 @@ pub mod L1MessagesProxy {
// CONSTRUCTOR
// *************************************************************************
/// Contract Constructor.
///
///
/// # Arguments
/// * `l1_messages_sender` - The address of the L1 solidity contract.
/// * `owner` - The owner address.
Expand All @@ -84,7 +84,7 @@ pub mod L1MessagesProxy {
// L1 HANDLER FUNCTION
// *************************************************************************
/// Receive `message` from L1 which is deserialized as L1Payload.
///
///
/// # Arguments
/// * `from_address` - The contract address on L1 to receive messages from.
/// * `data` - The payload from L1, an array of 'parent_hash' and 'block_number'.
Expand Down Expand Up @@ -114,7 +114,7 @@ pub mod L1MessagesProxy {
#[abi(embed_v0)]
impl L1MessagesProxyImpl of IL1MessagesProxy<ContractState> {
/// Set the Header Store Address. (Only Owner)
///
///
/// # Arguments
/// * `l1_headers_store_address` - The address of the header store cairo contract.
fn set_l1_headers_store(
Expand All @@ -127,7 +127,7 @@ pub mod L1MessagesProxy {
}

/// Change contract address. (Only Owner)
///
///
/// # Arguments
/// * `l1_messages_sender` - The address of the L1 solidity contract.
/// * `l1_headers_store_address` - The address of the header store cairo contract.
Expand Down
59 changes: 37 additions & 22 deletions starknet/src/fact_registry/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pub mod FactRegistry {
use openzeppelin_access::ownable::OwnableComponent;
use openzeppelin_upgrades::UpgradeableComponent;
use openzeppelin_upgrades::interface::IUpgradeable;
// Core lib imports
use starknet::{ContractAddress, EthAddress, contract_address_const, ClassHash};
use starknet::storage::Map;
// Core lib imports
use starknet::{ContractAddress, EthAddress, contract_address_const, ClassHash};

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: UpgradeableComponent, storage: upgradeable, event: upgradeableEvent);
Expand All @@ -32,7 +32,7 @@ pub mod FactRegistry {
impl OwnableMixinImpl = OwnableComponent::OwnableMixinImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

// Upgradeable
// Upgradeable
impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>;

const SUCCESS: felt252 = 'Proof verified successfully';
Expand Down Expand Up @@ -72,7 +72,7 @@ pub mod FactRegistry {
// CONSTRUCTOR
// *************************************************************************
/// Contract Constructor.
///
///
/// # Arguments
/// * `l1_headers_store_addr` - The address of L1 Header Store contract.
#[constructor]
Expand All @@ -93,7 +93,8 @@ pub mod FactRegistry {
// Implementation of IFactRegistry interface
#[abi(embed_v0)]
impl FactRegistry of IFactRegistry<ContractState> {
/// Verifies the account information for a given Ethereum address at a given block using a provided state root proof and stores the verified value.
/// Verifies the account information for a given Ethereum address at a given block using a
/// provided state root proof and stores the verified value.
///
/// # Arguments
/// * `option` - An enum specifying which part of the account state to verify and store.
Expand All @@ -105,10 +106,13 @@ pub mod FactRegistry {
/// # Panics
/// - The state root for the given block is not found.
/// - The account is not found in the proof verification.
///
/// This function takes an account address, block number, proof sizes, and concatenated proofs to verify the account state.
/// It retrieves the state root for the given block, reconstructs the proof, and verifies it.
/// Depending on the `option` provided, it stores the corresponding verified values (code hash, balance, nonce, storage hash)
///
/// This function takes an account address, block number, proof sizes, and concatenated
/// proofs to verify the account state.
/// It retrieves the state root for the given block, reconstructs the proof, and verifies
/// it.
/// Depending on the `option` provided, it stores the corresponding verified values (code
/// hash, balance, nonce, storage hash)
/// in the contract state.
fn prove_account(
ref self: ContractState,
Expand Down Expand Up @@ -152,23 +156,27 @@ pub mod FactRegistry {
}
}

/// Retrieves the storage hash value at a given slot for an Ethereum account and block number given a state root proof.
/// Retrieves the storage hash value at a given slot for an Ethereum account and block
/// number given a state root proof.
///
/// # Arguments
/// * `block` - The block number at which to fetch the storage value.
/// * `account` - The Ethereum address of the account whose storage is being queried.
/// * `slot` - The storage slot whose value is being retrieved.
/// * `proof_sizes_bytes` - An array of sizes for the proof elements in bytes.
/// * `proofs_concat` - An array containing the concatenated proofs for storage verification.
/// * `proofs_concat` - An array containing the concatenated proofs for storage
/// verification.
///
/// # Returns
/// * `Words64Sequence` - A sequence of 64-bit words containing the storage value at the given slot, or empty if the proof is invalid.
/// * `Words64Sequence` - A sequence of 64-bit words containing the storage value at the
/// given slot, or empty if the proof is invalid.
///
/// # Panics
/// Panics if the storage hash for the given account and block is not found.
///
/// This function is responsible for retrieving the storage value at a specific slot for an Ethereum account and block number
/// using StarkNet state root proofs. It verifies the proof and returns the storage value if the proof is valid.
///
/// This function is responsible for retrieving the storage value at a specific slot for an
/// Ethereum account and block number using StarkNet state root proofs. It verifies the
/// proof and returns the storage value if the proof is valid.
fn prove_storage(
ref self: ContractState,
block: u64,
Expand Down Expand Up @@ -203,17 +211,21 @@ pub mod FactRegistry {
}
}

/// Retrieves the storage value at a given slot for an Ethereum account and block number as an unsigned 256-bit integer.
/// Retrieves the storage value at a given slot for an Ethereum account and block number as
/// an unsigned 256-bit integer.
///
/// # Arguments
/// - `block`: The block number for which the storage value is to be retrieved.
/// - `account`: The Ethereum address of the account.
/// - `slot`: The storage slot for which the value is to be retrieved.
/// - `proof_sizes_bytes`: An array containing the sizes (in bytes) of the individual proofs in the concatenated proof.
/// - `proof_sizes_bytes`: An array containing the sizes (in bytes) of the individual proofs
/// in the concatenated proof.
/// - `proofs_concat`: An array containing the concatenated proof data.
///
/// This function is a wrapper around `get_storage` that specifically returns the storage value as an unsigned 256-bit integer.
/// It is useful when working with storage values that represent unsigned integers, such as balances or other numerical values.
/// This function is a wrapper around `get_storage` that specifically returns the storage
/// value as an unsigned 256-bit integer.
/// It is useful when working with storage values that represent unsigned integers, such as
/// balances or other numerical values.
///
/// Returns:
/// * `u256` - The storage value at the given slot.
Expand Down Expand Up @@ -308,11 +320,13 @@ pub mod FactRegistry {
// *************************************************************************
#[generate_trait]
impl Private of PrivateTrait {
/// Reconstructs a list of integer sequences from a concatenated byte sequence and an array of byte sizes.
/// Reconstructs a list of integer sequences from a concatenated byte sequence and an array
/// of byte sizes.
///
/// # Arguments
/// * `sequence`: A span of 64-bit words representing the concatenated byte sequence.
/// * `sizes_bytes`: A span of sizes (in bytes) for each integer sequence in the concatenated byte sequence.
/// * `sizes_bytes`: A span of sizes (in bytes) for each integer sequence in the
/// concatenated byte sequence.
///
/// Returns:
/// * `Array<Words64Sequence>` - The reconstructed sequences.
Expand Down Expand Up @@ -372,7 +386,8 @@ pub mod FactRegistry {
/// # Returns
/// * ` Array<Words64Sequence>` - each values extracted from the RLP list.
///
/// This function iterates through the provided `rlp_list` and extracts data from the `words64`
/// This function iterates through the provided `rlp_list` and extracts data from the
/// `words64`
/// sequence based on the position and length of each `RLPItem` in the list. The extracted
/// data is appended to an array and returned.
fn extract_list_values(
Expand Down
48 changes: 23 additions & 25 deletions starknet/src/library/array_utils.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// The following code was taken from the Alexandria library and added as internal library to
// make auditing easier. The original code can be found at https://github.com/keep-starknet-strange/alexandria/blob/main/src/data_structures/src/array_ext.cairo
// The following code was taken from the Alexandria library and added as internal library to
// make auditing easier. The original code can be found at
// https://github.com/keep-starknet-strange/alexandria/blob/main/src/data_structures/src/array_ext.cairo

pub trait ArrayTraitExt<T> {
fn append_all(ref self: Array<T>, ref arr: Array<T>);
Expand Down Expand Up @@ -213,14 +214,13 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
Option::Some(item) => *item,
Option::None => { return Option::None; },
};
while let Option::Some(item) = self
.pop_front() {
if *item < min {
index_of_min = index + 1;
min = *item;
}
index += 1;
};
while let Option::Some(item) = self.pop_front() {
if *item < min {
index_of_min = index + 1;
min = *item;
}
index += 1;
};
Option::Some(index_of_min)
}

Expand All @@ -244,14 +244,13 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
Option::Some(item) => *item,
Option::None => { return Option::None; },
};
while let Option::Some(item) = self
.pop_front() {
if *item > max {
index_of_max = index + 1;
max = *item
}
index += 1;
};
while let Option::Some(item) = self.pop_front() {
if *item > max {
index_of_max = index + 1;
max = *item
}
index += 1;
};
Option::Some(index_of_max)
}

Expand All @@ -263,13 +262,12 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
let mut last_value = self.pop_front().unwrap();
let mut ret = array![*last_value];

while let Option::Some(v) = self
.pop_front() {
if (last_value != v) {
last_value = v;
ret.append(*v);
}
};
while let Option::Some(v) = self.pop_front() {
if (last_value != v) {
last_value = v;
ret.append(*v);
}
};

ret
}
Expand Down
9 changes: 5 additions & 4 deletions starknet/src/library/bitshift.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// The following code was taken from the Alexandria library and added as internal library to
// make auditing easier. The original code can be found at https://github.com/keep-starknet-strange/alexandria/blob/main/src/math/src/lib.cairo
use core::num::traits::{Bounded, OverflowingMul,WideMul};
// The following code was taken from the Alexandria library and added as internal library to
// make auditing easier. The original code can be found at
// https://github.com/keep-starknet-strange/alexandria/blob/main/src/math/src/lib.cairo
use core::num::traits::{Bounded, OverflowingMul, WideMul};
use fossil::library::math_utils::pow;

pub trait BitShift<T> {
Expand Down Expand Up @@ -62,7 +63,7 @@ pub impl U128BitShift of BitShift<u128> {
pub impl U256BitShift of BitShift<u256> {
fn shl(x: u256, n: u256) -> u256 {
let res = WideMul::wide_mul(x, pow(2, n));
u256{low: res.limb0, high: res.limb1}
u256 { low: res.limb0, high: res.limb1 }
}

fn shr(x: u256, n: u256) -> u256 {
Expand Down
Loading

0 comments on commit a3fbeba

Please sign in to comment.