Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagofneto committed Oct 18, 2023
1 parent 04d93ba commit 5244956
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
13 changes: 9 additions & 4 deletions src/data_structures/eth_mpt.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ impl MPTImpl of MPTTrait {
}
};


let mut last_word_byte_len = rlp_byte_len % 8;
if last_word_byte_len == 0 {
last_word_byte_len = 8;
Expand Down Expand Up @@ -254,7 +253,9 @@ impl MPTImpl of MPTTrait {
loop {
if i == 16 {
let (value, _) = *l.at(16);
break Result::Ok((MPTNode::Branch((nibble_hashes.span(), value)), rlp_byte_len));
break Result::Ok(
(MPTNode::Branch((nibble_hashes.span(), value)), rlp_byte_len)
);
}

let (current_hash, _) = (*l.at(i));
Expand Down Expand Up @@ -299,8 +300,12 @@ impl MPTImpl of MPTTrait {
}


fn lazy_rlp_decode_branch_node(rlp: Words64, current_nibble: u8) -> Result<(MPTNode, usize), felt252> {
let (lazy_item, rlp_byte_len) = rlp_decode_list_lazy(rlp, array![current_nibble.into()].span())?;
fn lazy_rlp_decode_branch_node(
rlp: Words64, current_nibble: u8
) -> Result<(MPTNode, usize), felt252> {
let (lazy_item, rlp_byte_len) = rlp_decode_list_lazy(
rlp, array![current_nibble.into()].span()
)?;
match lazy_item {
RLPItem::Bytes(_) => Result::Err('Invalid RLP for node'),
RLPItem::List(l) => {
Expand Down
3 changes: 2 additions & 1 deletion src/data_structures/tests/test_eth_mpt.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ fn test_lazy_rlp_node_branch() {

let expected = 0xE7420366C27811B1DAB792630039C1CB6B504B4ED003E19E05B055663B68B020;

let (decoded, rlp_byte_len) = MPTTrait::lazy_rlp_decode_branch_node(rlp_node.span(), 0xa).unwrap();
let (decoded, rlp_byte_len) = MPTTrait::lazy_rlp_decode_branch_node(rlp_node.span(), 0xa)
.unwrap();
assert(rlp_byte_len == 66 * 8 + 4, 'Wrong RLP len');

let expected_node = MPTNode::LazyBranch(expected);
Expand Down
44 changes: 33 additions & 11 deletions src/encoding/tests/test_rlp.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,22 @@ fn test_rlp_lazy_decode_short_list() {
assert(res == expected_res, 'Wrong value indexes: 1');

let res = rlp_decode_list_lazy(arr.span(), array![0, 1, 2].span()).unwrap();
let mut expected_res = (RLPItem::List(array![
(array![0x893535].span(), 3), (array![0x42].span(), 1), (array![0x923845].span(), 3)
]
.span()), rlp_byte_len);
let mut expected_res = (
RLPItem::List(
array![
(array![0x893535].span(), 3), (array![0x42].span(), 1), (array![0x923845].span(), 3)
]
.span()
),
rlp_byte_len
);
assert(res == expected_res, 'Wrong value: indexes: 0, 1, 2');

let res = rlp_decode_list_lazy(arr.span(), array![0, 2].span()).unwrap();
let mut expected_res = (RLPItem::List(array![(array![0x893535].span(), 3), (array![0x923845].span(), 3)].span()), rlp_byte_len);
let mut expected_res = (
RLPItem::List(array![(array![0x893535].span(), 3), (array![0x923845].span(), 3)].span()),
rlp_byte_len
);
assert(res == expected_res, 'Wrong value: indexes: 0, 2');
}

Expand Down Expand Up @@ -475,14 +483,23 @@ fn test_rlp_lazy_decode_long_list() {
assert(res == expected_res, 'Wrong value indexes: 1');

let res = rlp_decode_list_lazy(arr.span(), array![0xa].span()).unwrap();
let expected_res = (RLPItem::List(array![(*expected_res_full.at(0xa), 32)].span()), rlp_byte_len);
let expected_res = (
RLPItem::List(array![(*expected_res_full.at(0xa), 32)].span()), rlp_byte_len
);
assert(res == expected_res, 'Wrong value indexes: 10');

let res = rlp_decode_list_lazy(arr.span(), array![0x5, 0x9, 0xf].span()).unwrap();
let expected_res = (RLPItem::List(array![
(*expected_res_full.at(0x5), 32), (*expected_res_full.at(0x9), 32), (*expected_res_full.at(0xf), 32)
]
.span()), rlp_byte_len);
let expected_res = (
RLPItem::List(
array![
(*expected_res_full.at(0x5), 32),
(*expected_res_full.at(0x9), 32),
(*expected_res_full.at(0xf), 32)
]
.span()
),
rlp_byte_len
);
assert(res == expected_res, 'Wrong value indexes: 5, 9, 15');
}

Expand Down Expand Up @@ -578,6 +595,11 @@ fn test_rlp_lazy_decode_list_long_string() {
assert(res == expected_res, 'Wrong value indexes: 1');

let res = rlp_decode_list_lazy(arr.span(), array![0, 1].span()).unwrap();
let expected_res = (RLPItem::List(array![(*expected_res_full.at(0), 32), (*expected_res_full.at(1), 56)].span()), rlp_byte_len);
let expected_res = (
RLPItem::List(
array![(*expected_res_full.at(0), 32), (*expected_res_full.at(1), 56)].span()
),
rlp_byte_len
);
assert(res == expected_res, 'Wrong value indexes: 0, 1');
}
3 changes: 1 addition & 2 deletions src/hashing/keccak.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn keccak_cairo_words64(words: Words64, last_word_bytes: usize) -> u256 {
if words.is_empty() {
return EMPTY_KECCAK;
}

let n = words.len();
let mut keccak_input = ArrayTrait::new();
let mut i: usize = 0;
Expand All @@ -30,5 +30,4 @@ fn keccak_cairo_words64(words: Words64, last_word_bytes: usize) -> u256 {
} else {
cairo_keccak(ref keccak_input, last, last_word_bytes)
}

}

0 comments on commit 5244956

Please sign in to comment.