Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/l1 headers test #3

Merged
merged 15 commits into from
Jun 3, 2024
7 changes: 4 additions & 3 deletions starknet/src/L1_headers_store/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub mod L1HeaderStore {
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 parent_hash = self.get_parent_hash(start_block_number + 1);

let mut current_index: u32 = 0;
let mut save_block_number = start_block_number + current_index.into();
Expand All @@ -142,8 +142,9 @@ pub mod L1HeaderStore {
);

current_index += 1;
save_block_number = start_block_number + current_index.into();
parent_hash = self.get_parent_hash(save_block_number);
save_block_number += 1;
parent_hash = self.get_parent_hash(save_block_number + 1);

if current_index == block_header_words.len() {
// Process the last block based on options and header data
self
Expand Down
1 change: 1 addition & 0 deletions starknet/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod library {

#[cfg(test)]
pub mod testing {
pub mod rlp;
pub mod proofs {
pub mod account;
pub mod blocks;
Expand Down
28 changes: 8 additions & 20 deletions starknet/src/library/blockheader_rlp_extractor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn decode_uncle_hash(block_rlp: Words64Sequence) -> u256 {
}

pub fn decode_beneficiary(block_rlp: Words64Sequence) -> EthAddress {
extract_data(block_rlp, BENEFICIARY_START, 20).from_words64()
extract_data(block_rlp, BENEFICIARY_START, 24).from_words64()
}

pub fn decode_state_root(block_rlp: Words64Sequence) -> u256 {
Expand All @@ -50,58 +50,46 @@ pub fn decode_difficulty(block_rlp: Words64Sequence) -> u64 {
let rlp_items = to_rlp_array(block_rlp);
assert!(rlp_items.len() > decoder::DIFFICULTY, "Block RLP is too short");
let data = *rlp_items.at(decoder::DIFFICULTY);
let difficulty_rlp_element = extract_data(
block_rlp, data.position, data.position.into() + data.length
);
let difficulty_rlp_element = extract_data(block_rlp, data.position, data.length);
*difficulty_rlp_element.values.at(0)
}

pub fn decode_block_number(block_rlp: Words64Sequence) -> u64 {
let rlp_items = to_rlp_array(block_rlp);
assert!(rlp_items.len() > decoder::BLOCK_NUMBER, "Block RLP is too short");
assert!(rlp_items.len() > decoder::BLOCK_NUMBER, "Block RLP is too short"); // I'm not sure tha
let data = *rlp_items.at(decoder::BLOCK_NUMBER);
let block_number_rlp_element = extract_data(
block_rlp, data.position, data.position.into() + data.length
);
let block_number_rlp_element = extract_data(block_rlp, data.position, data.length);
*block_number_rlp_element.values.at(0)
}

pub fn decode_gas_limit(block_rlp: Words64Sequence) -> u64 {
let rlp_items = to_rlp_array(block_rlp);
assert!(rlp_items.len() > decoder::GAS_LIMIT, "Block RLP is too short");
let data = *rlp_items.at(decoder::GAS_LIMIT);
let gas_limit_rlp_element = extract_data(
block_rlp, data.position, data.position.into() + data.length
);
let gas_limit_rlp_element = extract_data(block_rlp, data.position, data.length);
*gas_limit_rlp_element.values.at(0)
}

pub fn decode_gas_used(block_rlp: Words64Sequence) -> u64 {
let rlp_items = to_rlp_array(block_rlp);
assert!(rlp_items.len() > decoder::GAS_USED, "Block RLP is too short");
let data = *rlp_items.at(decoder::GAS_USED);
let gas_used_rlp_element = extract_data(
block_rlp, data.position, data.position.into() + data.length
);
let gas_used_rlp_element = extract_data(block_rlp, data.position, data.length);
*gas_used_rlp_element.values.at(0)
}

pub fn decode_timestamp(block_rlp: Words64Sequence) -> u64 {
let rlp_items = to_rlp_array(block_rlp);
assert!(rlp_items.len() > decoder::TIMESTAMP, "Block RLP is too short");
let data = *rlp_items.at(decoder::TIMESTAMP);
let timestamp_rlp_element = extract_data(
block_rlp, data.position, data.position.into() + data.length
);
let timestamp_rlp_element = extract_data(block_rlp, data.position, data.length);
*timestamp_rlp_element.values.at(0)
}

pub fn decode_base_fee(block_rlp: Words64Sequence) -> u64 {
let rlp_items = to_rlp_array(block_rlp);
assert!(rlp_items.len() > decoder::BASE_FEE, "Block RLP is too short");
let data = *rlp_items.at(decoder::BASE_FEE);
let base_fee_rlp_element = extract_data(
block_rlp, data.position, data.position.into() + data.length
);
let base_fee_rlp_element = extract_data(block_rlp, data.position, data.length);
*base_fee_rlp_element.values.at(0)
}
8 changes: 8 additions & 0 deletions starknet/src/library/words64_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ pub fn split_u256_to_u64_array(value: u256) -> Span<u64> {
return array![l0, l1, l2, l3].span();
}

pub fn split_u256_to_u64_array_no_span(value: u256) -> Array<u64> {
let l0: u64 = (BitShift::shr(value, 192) & U64_MASK).try_into().unwrap();
let l1: u64 = (BitShift::shr(value, 128) & U64_MASK).try_into().unwrap();
let l2: u64 = (BitShift::shr(value, 64) & U64_MASK).try_into().unwrap();
let l3: u64 = (value & U64_MASK).try_into().unwrap();
return array![l0, l1, l2, l3];
}

pub fn words64_to_nibbles(input: Words64Sequence, skip_nibbles: usize) -> Array<u64> {
let (_, remainder) = u32_safe_divmod(input.len_bytes * 2, 16);
let mut acc = array![];
Expand Down
Loading
Loading