Skip to content

Commit

Permalink
add utils and update test
Browse files Browse the repository at this point in the history
  • Loading branch information
sqrlfirst committed May 22, 2024
1 parent 26edddb commit c4926fe
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 48 deletions.
8 changes: 8 additions & 0 deletions starknet/src/library/words64_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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
108 changes: 60 additions & 48 deletions starknet/tests/test_l1_header_store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ use fossil::library::blockheader_rlp_extractor::{
decode_transactions_root, decode_receipts_root, decode_difficulty, decode_base_fee,
decode_timestamp, decode_gas_used
};
use fossil::library::words64_utils::split_u256_to_u64_array_no_span;
use fossil::types::Words64Sequence;
use fossil::types::ProcessBlockOptions;
use super::test_utils::{setup, testdata};

use fossil::testing::proofs;
use super::test_utils::setup;

pub fn get_rlp() -> Words64Sequence {
let rlp = Words64Sequence {
Expand All @@ -28,17 +30,18 @@ pub fn get_rlp() -> Words64Sequence {
.span(),
len_bytes: 104
};
rlp
}


#[test]
fn receive_from_l1_success_test() {
let dsp = setup();

let block = testdata::blocks::BLOCK_0();
let block = proofs::blocks::BLOCK_0();
let parent_hash: u256 = 0xfbacb363819451babc6e7596aa48af6c223e40e8b0ad975e372347df5d60ba0f;

dsp.store.receive_from_l1(dsp.proxy.contract_address, parent_hash, block.number);
dsp.store.receive_from_l1(parent_hash, block.number);

assert_eq!(dsp.store.get_parent_hash(block.number), parent_hash);
}
Expand All @@ -48,71 +51,74 @@ fn receive_from_l1_success_test() {
fn receive_from_l1_fail_wrong_caller_test() {
let dsp = setup();

let block = testdata::blocks::BLOCK_0();
let block = proofs::blocks::BLOCK_0();
let parent_hash: u256 = 0xfbacb363819451babc6e7596aa48af6c223e40e8b0ad975e372347df5d60ba0f;

dsp.store.receive_from_l1(dsp.store.contract_address, parent_hash, block.number);
dsp.store.receive_from_l1(parent_hash, block.number);
}

#[test]
fn process_block_success_uncle_hash_test() {
let dsp = setup();

let block = testdata::blocks::BLOCK_0();
let block = proofs::blocks::BLOCK_0();
let rlp = get_rlp();
let data = decode_uncle_hash(rlp);
let data_arr = split_u256_to_u64_array_no_span(data);

dsp
.store
.process_block(
ProcessBlockOptions::UncleHash,
block.number,
4, // block_header_rlp_bytes_len: usize,
data // block_header_rlp: Array<u64>,
data_arr // block_header_rlp: Array<u64>,
);

let uncle_hash: u256 = dsp.store.get_uncles_hash(block.number); // u256

assert_eq!(uncle_hash, data);
}

#[test]
fn process_block_success_beneficiary_test() {
let dsp = setup();
// #[test]
// fn process_block_success_beneficiary_test() {
// let dsp = setup();

let block = testdata::blocks::BLOCK_0();
let rlp = get_rlp();
let data = decode_beneficiary(rlp);
// let block = proofs::blocks::BLOCK_0();
// let rlp = get_rlp();
// let data = decode_beneficiary(rlp);
// let data_arr = split_u256_to_u64_array_no_span(data); // TODO ETHAddress

dsp
.store
.process_block(
ProcessBlockOptions::Beneficiary,
block.number,
4, // block_header_rlp_bytes_len: usize ,
data // block_header_rlp: Array<u64>,
);
// dsp
// .store
// .process_block(
// ProcessBlockOptions::Beneficiary,
// block.number,
// 4, // block_header_rlp_bytes_len: usize ,
// data_arr // block_header_rlp: Array<u64>,
// );

let beneficiary: EthAddress = dsp.store.get_beneficiary(block.number);
// let beneficiary: EthAddress = dsp.store.get_beneficiary(block.number);

assert_eq!(beneficiary, data);
}
// assert_eq!(beneficiary, data);
// }

#[test]
fn process_block_success_state_root_test() {
let dsp = setup();

let block = testdata::blocks::BLOCK_0();
let block = proofs::blocks::BLOCK_0();
let rlp = get_rlp();
let data = decode_state_root(rlp);
let data_arr = split_u256_to_u64_array_no_span(data);

dsp
.store
.process_block(
ProcessBlockOptions::StateRoot,
block.number,
4, // block_header_rlp_bytes_len: usize ,
data // block_header_rlp: Array<u64>,
data_arr // block_header_rlp: Array<u64>,
);

let state_root: u256 = dsp.store.get_state_root(block.number);
Expand All @@ -124,17 +130,18 @@ fn process_block_success_state_root_test() {
fn process_block_success_transactions_root_test() {
let dsp = setup();

let block = testdata::blocks::BLOCK_0();
let block = proofs::blocks::BLOCK_0();
let rlp = get_rlp();
let data = decode_transactions_root(rlp);
let data = decode_transactions_root(rlp); // u256
let data_arr = split_u256_to_u64_array_no_span(data);

dsp
.store
.process_block(
ProcessBlockOptions::TxRoot,
block.number,
4, // block_header_rlp_bytes_len: usize ,
data // block_header_rlp: Array<u64>,
data_arr // block_header_rlp: Array<u64>,
);

let transactions_root: u256 = dsp.store.get_transactions_root(block.number);
Expand All @@ -146,17 +153,18 @@ fn process_block_success_transactions_root_test() {
fn process_block_success_receipts_root_test() {
let dsp = setup();

let block = testdata::blocks::BLOCK_0();
let block = proofs::blocks::BLOCK_0();
let rlp = get_rlp();
let data = decode_receipts_root(rlp);
let data_arr = split_u256_to_u64_array_no_span(data);

dsp
.store
.process_block(
ProcessBlockOptions::ReceiptRoot,
block.number,
4, // block_header_rlp_bytes_len: usize ,
data // block_header_rlp: Array<u64>,
data_arr // block_header_rlp: Array<u64>,
);

let receipts_root: u256 = dsp.store.get_receipts_root(block.number);
Expand All @@ -168,17 +176,18 @@ fn process_block_success_receipts_root_test() {
fn process_block_success_difficulty_test() {
let dsp = setup();

let block = testdata::blocks::BLOCK_0();
let block = proofs::blocks::BLOCK_0();
let rlp = get_rlp();
let data = decode_difficulty(rlp);
let data_arr = array![data];

dsp
.store
.process_block(
ProcessBlockOptions::Difficulty,
block.number,
1, // block_header_rlp_bytes_len: usize ,
data // block_header_rlp: Array<u64>,
data_arr // block_header_rlp: Array<u64>,
);

let difficulty: u64 = dsp.store.get_difficulty(block.number);
Expand All @@ -190,17 +199,18 @@ fn process_block_success_difficulty_test() {
fn process_block_gas_used_test() {
let dsp = setup();

let block = testdata::blocks::BLOCK_0();
let block = proofs::blocks::BLOCK_0();
let rlp = get_rlp();
let data = decode_gas_used(rlp);
let data_arr = array![data];

dsp
.store
.process_block(
ProcessBlockOptions::GasUsed,
block.number,
1, // block_header_rlp_bytes_len: usize ,
data // block_header_rlp: Array<u64>,
data_arr // block_header_rlp: Array<u64>,
);

let gas_used: u64 = dsp.store.get_gas_used(block.number);
Expand All @@ -212,17 +222,18 @@ fn process_block_gas_used_test() {
fn process_block_success_timestamp_test() {
let dsp = setup();

let block = testdata::blocks::BLOCK_0();
let block = proofs::blocks::BLOCK_0();
let rlp = get_rlp();
let data = decode_timestamp(rlp);
let data_arr = array![data];

dsp
.store
.process_block(
ProcessBlockOptions::TimeStamp,
block.number,
1, // block_header_rlp_bytes_len: usize ,
data // block_header_rlp: Array<u64>,
data_arr // block_header_rlp: Array<u64>,
);

let timestamp: u64 = dsp.store.get_timestamp(block.number);
Expand All @@ -234,17 +245,18 @@ fn process_block_success_timestamp_test() {
fn process_block_success_base_fee_test() {
let dsp = setup();

let block = testdata::blocks::BLOCK_0();
let block = proofs::blocks::BLOCK_0();
let rlp = get_rlp();
let data = decode_base_fee(rlp);
let data_arr = array![data];

dsp
.store
.process_block(
ProcessBlockOptions::BaseFee,
block.number,
1, // block_header_rlp_bytes_len: usize ,
data // block_header_rlp: Array<u64>,
data_arr // block_header_rlp: Array<u64>,
);

let base_fee: u64 = dsp.store.get_base_fee(block.number);
Expand All @@ -255,31 +267,31 @@ fn process_block_success_base_fee_test() {
#[test]
#[should_panic]
fn process_block_cannot_validate_header_rlp_test() {
let dsp = setup();
// TODO
// let dsp = setup();
// // TODO
assert!(false)
}

#[test]
fn process_till_block_success_test() {
let dsp = setup();
// TODO
// let dsp = setup();
// // TODO
assert!(true)
}

#[test]
#[should_panic]
fn process_till_block_fail_wrong_block_headers_length_test() {
let dsp = setup();
// TODO
// let dsp = setup();
// // TODO
assert!(false)
}

#[test]
#[should_panic]
fn process_till_block_fail_wrong_block_headers_test() {
let dsp = setup();
// TODO
// let dsp = setup();
// // TODO
assert!(false)
}

Expand Down

0 comments on commit c4926fe

Please sign in to comment.