Skip to content

Commit

Permalink
Merge pull request #2 from sqrlfirst/feat/l1-headers
Browse files Browse the repository at this point in the history
Feat/l1 headers
  • Loading branch information
ametel01 authored May 15, 2024
2 parents e44f27c + d16455f commit 6d509ca
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 16 deletions.
135 changes: 125 additions & 10 deletions starknet/src/L1_headers_store/contract.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#[starknet::contract]
pub mod L1HeaderStore {
use fossil::L1_headers_store::interface::IL1HeadersStore;
use fossil::library::blockheader_rlp_extractor as extractor;
use core::clone::Clone;
use core::traits::Into;
use core::array::ArrayTrait;
use fossil::L1_headers_store::interface::IL1HeadersStore;
use fossil::library::blockheader_rlp_extractor::{
decode_parent_hash, decode_uncle_hash, decode_beneficiary, decode_state_root,
decode_transactions_root, decode_receipts_root, decode_difficulty, decode_base_fee,
decode_timestamp, decode_gas_used
};
use fossil::library::keccak_utils::keccak_words64;
use fossil::library::words64_utils::words64_to_u256;
use fossil::types::ProcessBlockOptions;
use fossil::types::Words64Sequence;
use starknet::{ContractAddress, EthAddress, get_caller_address};

#[storage]
Expand Down Expand Up @@ -52,17 +62,108 @@ pub mod L1HeaderStore {
option: ProcessBlockOptions,
block_number: u64,
block_header_rlp_bytes_len: usize,
block_header_bytes: Array<usize>,
block_header_rlp: Array<u64>,
) {}
) {
let child_block_parent_hash = self.get_parent_hash(block_number + 1);

let (block_header_rlp, len) = self
.validate_provided_header_rlp(
child_block_parent_hash,
block_number,
block_header_rlp_bytes_len,
block_header_rlp
);

let block_rlp = Words64Sequence {
values: block_header_rlp.span(), len_bytes: block_header_rlp_bytes_len,
};
let parent_hash = decode_parent_hash(block_rlp);
self.block_parent_hash.write(block_number, parent_hash);

match option {
ProcessBlockOptions::UncleHash => {
let field_value = decode_uncle_hash(block_rlp);
self.block_uncles_hash.write(block_number, field_value);
},
ProcessBlockOptions::Beneficiary => {
let field_value = decode_beneficiary(block_rlp);
self.block_beneficiary.write(block_number, field_value);
},
ProcessBlockOptions::StateRoot => {
let field_value = decode_state_root(block_rlp);
self.block_state_root.write(block_number, field_value);
},
ProcessBlockOptions::TxRoot => {
let field_value = decode_transactions_root(block_rlp);
self.block_transactions_root.write(block_number, field_value);
},
ProcessBlockOptions::ReceiptRoot => {
let field_value = decode_receipts_root(block_rlp);
self.block_receipts_root.write(block_number, field_value);
},
ProcessBlockOptions::Difficulty => {
let field_value = decode_difficulty(block_rlp);
self.block_difficulty.write(block_number, field_value);
},
ProcessBlockOptions::GasUsed => {
let field_value = decode_gas_used(block_rlp);
self.block_gas_used.write(block_number, field_value);
},
ProcessBlockOptions::TimeStamp => {
let field_value = decode_timestamp(block_rlp);
self.block_timestamp.write(block_number, field_value);
},
ProcessBlockOptions::BaseFee => {
let field_value = decode_base_fee(block_rlp);
self.block_base_fee.write(block_number, field_value);
},
_ => {}
}
}

fn process_till_block(
ref self: ContractState,
options_set: ProcessBlockOptions,
start_block_number: u64,
block_header_bytes: Array<usize>,
block_header_words: Array<u64>,
block_header_concat: Array<u64>,
) {}
block_header_concat: Array<usize>,
block_header_words: Array<Array<u64>>,
) {
assert!(
block_header_concat.len() == block_header_words.len(),
"L1HeaderStore: block_header_bytes and block_header_words must have the same length"
);
let mut parent_hash = self.get_parent_hash(start_block_number);

let mut current_index:u32 = 0;
let mut save_block_number = start_block_number + current_index.into();
while current_index < block_header_words.len() {

let (block_header_rlp_bytes, len) = self.validate_provided_header_rlp(
parent_hash,
save_block_number,
block_header_concat.at(current_index).clone(),
block_header_words.at(current_index).clone(),
);

current_index += 1;
save_block_number = start_block_number + current_index.into();
parent_hash = self.get_parent_hash(save_block_number);
if current_index == block_header_words.len() {
// Process the last block based on options and header data
self
.process_block(
options_set,
save_block_number - 1,
len,
block_header_rlp_bytes,
);
}
};

self.block_parent_hash.write(save_block_number, parent_hash);
}


fn get_initialized(self: @ContractState, block_number: u64) -> bool {
self.initialized.read()
}
Expand Down Expand Up @@ -112,7 +213,21 @@ pub mod L1HeaderStore {
child_block_parent_hash: u256,
block_number: u64,
block_header_rlp_bytes_len: usize,
block_header_rlp: Span<u64>
) {}
block_header_rlp: Array<u64>
) -> (Array<u64>, usize) {
let header_ints_sequence = Words64Sequence {
values: block_header_rlp.span(), len_bytes: block_header_rlp_bytes_len,
};

let provided_rlp_hash = keccak_words64(header_ints_sequence);
let provided_rlp_hash_u256 = words64_to_u256(provided_rlp_hash.values);

assert!(
child_block_parent_hash == provided_rlp_hash_u256,
"L1HeaderStore: hashes are not equal"
);

(block_header_rlp, block_header_rlp_bytes_len)
}
}
}
7 changes: 3 additions & 4 deletions starknet/src/L1_headers_store/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ pub trait IL1HeadersStore<TState> {
option: ProcessBlockOptions,
block_number: u64,
block_header_rlp_bytes_len: usize,
block_header_bytes: Array<usize>,
block_header_rlp: Array<u64>,
);
fn process_till_block(
ref self: TState,
options_set: ProcessBlockOptions,
start_block_number: u64,
block_header_bytes: Array<usize>,
block_header_words: Array<u64>,
block_header_concat: Array<u64>,
block_header_concat: Array<usize>,
block_header_words: Array<Array<u64>>,
);
fn get_initialized(self: @TState, block_number: u64) -> bool;
fn get_parent_hash(self: @TState, block_number: u64) -> u256;
Expand Down
2 changes: 1 addition & 1 deletion starknet/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod library {
mod bitshift;
pub mod blockheader_rlp_extractor;
mod keccak256;
mod keccak_utils;
pub mod keccak_utils;
mod math_utils;
mod merkle_patricia_utils;
pub mod rlp_utils;
Expand Down
2 changes: 1 addition & 1 deletion starknet/src/library/words64_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl EthAddressWords64 of Words64Trait<EthAddress> {
}
}

fn words64_to_u256(input: Span<u64>) -> u256 {
pub fn words64_to_u256(input: Span<u64>) -> u256 {
assert!(input.len() == 4, "input length must be less than or equal to 4");

let l0: u256 = BitShift::shl((*input.at(0)).into(), 192_u256);
Expand Down

0 comments on commit 6d509ca

Please sign in to comment.