Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagofneto committed Oct 17, 2023
1 parent 9ca7589 commit e1fe792
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 50 deletions.
6 changes: 4 additions & 2 deletions src/data_structures/eth_mpt.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ impl MPTImpl of MPTTrait {
let decoded = if proof_index != proof_len - 1 && node.len() > 9 {
let current_nibble = (key / key_pow2) & 0xf;
// Unwrap impossible to fail, as we are masking with 0xf, meaning the result is always a nibble
match MPTTrait::lazy_rlp_decode_branch_node(node, current_nibble.try_into().unwrap()) {
match MPTTrait::lazy_rlp_decode_branch_node(
node, current_nibble.try_into().unwrap()
) {
Result::Ok(decoded) => decoded,
Result::Err(e) => {
break Result::Err(e);
Expand Down Expand Up @@ -272,7 +274,7 @@ impl MPTImpl of MPTTrait {
}
}


fn lazy_rlp_decode_branch_node(rlp: Words64, current_nibble: u8) -> Result<MPTNode, felt252> {
let hash_words = rlp_decode_list_lazy(rlp, array![current_nibble.into()].span())?;
match (*hash_words.at(0)).try_into() {
Expand Down
23 changes: 16 additions & 7 deletions src/encoding/rlp.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,15 @@ fn rlp_decode_list_lazy(input: Words64, lazy: Span<usize>) -> Result<Span<Words6
let list_prefix: u32 = (*input.at(0) & 0xff).try_into().unwrap();
let list_type = RLPTypeTrait::from_byte(list_prefix.try_into().unwrap()).unwrap();
let (mut current_input_index, len) = match list_type {
RLPType::String(()) => { return Result::Err('Not a list'); },
RLPType::StringShort(()) => { return Result::Err('Not a list'); },
RLPType::StringLong(()) => { return Result::Err('Not a list'); },
RLPType::String(()) => {
return Result::Err('Not a list');
},
RLPType::StringShort(()) => {
return Result::Err('Not a list');
},
RLPType::StringLong(()) => {
return Result::Err('Not a list');
},
RLPType::ListShort(()) => (1, list_prefix - 0xc0),
RLPType::ListLong(()) => {
let len_len = list_prefix - 0xf7;
Expand All @@ -171,7 +177,7 @@ fn rlp_decode_list_lazy(input: Words64, lazy: Span<usize>) -> Result<Span<Words6
if output.len() == lazy.len() {
break Result::Ok(output.span());
}

if current_input_index >= len {
break Result::Err('Too many items to decode');
}
Expand All @@ -197,12 +203,15 @@ fn rlp_decode_list_lazy(input: Words64, lazy: Span<usize>) -> Result<Span<Words6
let current_word = (current_input_index + 1) / 8;
let current_word_offset = 7 - ((current_input_index + 1) % 8);

let len_span = input.slice_le(current_word * 8 + current_word_offset, len_len.try_into().unwrap());
let len_span = input
.slice_le(current_word * 8 + current_word_offset, len_len.try_into().unwrap());
// Enough to store 4.29 GB (fits in u32)
assert(len_span.len() == 1 && *len_span.at(0) <= 0xffffffff, 'Len of len too big');

// len fits in 32 bits, confirmed by previous assertion
let len: u32 = reverse_endianness_u64(*len_span.at(0), Option::Some(len_len.try_into().unwrap()))
let len: u32 = reverse_endianness_u64(
*len_span.at(0), Option::Some(len_len.try_into().unwrap())
)
.try_into()
.unwrap();

Expand All @@ -215,7 +224,7 @@ fn rlp_decode_list_lazy(input: Words64, lazy: Span<usize>) -> Result<Span<Words6
panic_with_felt252('Recursive list not supported')
}
};

current_input_index += item_start_skip.try_into().unwrap();
if span_contains(lazy, lazy_index) {
let current_word = current_input_index / 8;
Expand Down
62 changes: 21 additions & 41 deletions src/encoding/tests/test_rlp.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,18 @@ fn test_rlp_lazy_decode_short_list() {
assert(res.is_empty(), 'Wrong value indexes: empty');

let res = rlp_decode_list_lazy(arr.span(), array![1].span()).unwrap();
let expected_res = array![
array![0x42].span()
].span();
let expected_res = array![array![0x42].span()].span();
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 = array![
array![0x893535].span(), array![0x42].span(), array![0x923845].span()
].span();
]
.span();
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 = array![
array![0x893535].span(), array![0x923845].span()
].span();
let mut expected_res = array![array![0x893535].span(), array![0x923845].span()].span();
assert(res == expected_res, 'Wrong value: indexes: 0, 2');
}

Expand Down Expand Up @@ -419,27 +416,22 @@ fn test_rlp_lazy_decode_long_list() {
assert(res.is_empty(), 'Wrong value indexes: empty');

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

let res = rlp_decode_list_lazy(arr.span(), array![1].span()).unwrap();
let expected_res = array![
*expected_res_full.at(1)
].span();
let expected_res = array![*expected_res_full.at(1)].span();
assert(res == expected_res, 'Wrong value indexes: 1');

let res = rlp_decode_list_lazy(arr.span(), array![0xa].span()).unwrap();
let expected_res = array![
*expected_res_full.at(0xa)
].span();
let expected_res = array![*expected_res_full.at(0xa)].span();
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 = array![
*expected_res_full.at(0x5), *expected_res_full.at(0x9), *expected_res_full.at(0xf)
].span();
]
.span();
assert(res == expected_res, 'Wrong value indexes: 5, 9, 15');
}

Expand All @@ -465,12 +457,8 @@ fn test_rlp_decode_list_long_string() {
assert(len == 1 + (0xf8 - 0xf7) + 0x5b, 'Wrong len');

let expected_res = array![
array![
0x57b94f7235e356ac,
0x1d27207f0b03476f,
0x3aab1f4760f75aaf,
0xed276fa9c2173ae5,
].span(),
array![0x57b94f7235e356ac, 0x1d27207f0b03476f, 0x3aab1f4760f75aaf, 0xed276fa9c2173ae5,]
.span(),
array![
0xada3968dadf338d4,
0x661865fe3827777e,
Expand All @@ -479,7 +467,8 @@ fn test_rlp_decode_list_long_string() {
0x4756674547654546,
0x6535476567456764,
0xfa77645733566377,
].span(),
]
.span(),
];
let expected_item = RLPItem::List(expected_res.span());
assert(res == expected_item, 'Wrong value');
Expand All @@ -504,12 +493,8 @@ fn test_rlp_lazy_decode_list_long_string() {
];

let expected_res_full = array![
array![
0x57b94f7235e356ac,
0x1d27207f0b03476f,
0x3aab1f4760f75aaf,
0xed276fa9c2173ae5,
].span(),
array![0x57b94f7235e356ac, 0x1d27207f0b03476f, 0x3aab1f4760f75aaf, 0xed276fa9c2173ae5,]
.span(),
array![
0xada3968dadf338d4,
0x661865fe3827777e,
Expand All @@ -518,27 +503,22 @@ fn test_rlp_lazy_decode_list_long_string() {
0x4756674547654546,
0x6535476567456764,
0xfa77645733566377,
].span(),
]
.span(),
];

let res = rlp_decode_list_lazy(arr.span(), array![].span()).unwrap();
assert(res.is_empty(), 'Wrong value indexes: empty');

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

let res = rlp_decode_list_lazy(arr.span(), array![1].span()).unwrap();
let expected_res = array![
*expected_res_full.at(1)
].span();
let expected_res = array![*expected_res_full.at(1)].span();
assert(res == expected_res, 'Wrong value indexes: 1');

let res = rlp_decode_list_lazy(arr.span(), array![0, 1].span()).unwrap();
let expected_res = array![
*expected_res_full.at(0), *expected_res_full.at(1)
].span();
let expected_res = array![*expected_res_full.at(0), *expected_res_full.at(1)].span();
assert(res == expected_res, 'Wrong value indexes: 0, 1');
}

0 comments on commit e1fe792

Please sign in to comment.