Skip to content

Commit

Permalink
trim left zero for expected value
Browse files Browse the repository at this point in the history
Signed-off-by: Naohiro Yoshida <naohiro.yoshida@datachain.jp>
  • Loading branch information
Naohiro Yoshida committed Feb 25, 2024
1 parent a9f7da3 commit a2ae5c3
Showing 1 changed file with 94 additions and 2 deletions.
96 changes: 94 additions & 2 deletions light-client/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ pub fn verify_proof(
) -> Result<(), Error> {
let value = verify(root, proof, key)?;

let expected_value = expected_value.as_ref().map(|e| rlp::encode(e).to_vec());
let expected_value = expected_value
.as_ref()
.map(|e| rlp::encode(&trim_left_zero(e).to_vec()).to_vec());
if value != expected_value {
return Err(Error::UnexpectedStateValue(
*root,
Expand All @@ -71,6 +73,17 @@ fn verify(root: &Hash, proof: &[Vec<u8>], key: &[u8]) -> Result<Option<Vec<u8>>,
trie.get(&keccak_256(key)).map_err(Error::TrieError)
}

fn trim_left_zero(value: &[u8]) -> &[u8] {
let mut pos = 0;
for v in value.iter() {
if *v != 0 {
break;
}
pos += 1;
}
&value[pos..]
}

#[cfg(test)]
mod test {
use alloc::borrow::ToOwned as _;
Expand All @@ -81,7 +94,7 @@ mod test {

use crate::commitment::{
calculate_ibc_commitment_storage_key, decode_eip1184_rlp_proof, resolve_account,
verify_proof,
trim_left_zero, verify_proof,
};
use crate::misc::{Account, Hash};

Expand Down Expand Up @@ -267,4 +280,83 @@ mod test {
unreachable!("{:?}", e);
}
}

#[test]
fn test_verify_with_left_zero() {
let storage_root: Hash = [
168, 105, 232, 135, 230, 245, 248, 79, 115, 67, 172, 247, 33, 207, 55, 158, 86, 149,
132, 227, 147, 238, 198, 180, 47, 29, 218, 84, 139, 33, 181, 95,
];
let key: Hash = [
169, 21, 106, 163, 236, 80, 251, 216, 119, 110, 40, 43, 67, 76, 199, 200, 217, 131, 89,
39, 240, 116, 104, 135, 170, 105, 69, 65, 171, 60, 50, 220,
];
let proof = vec![
vec![
249, 1, 145, 160, 237, 65, 114, 78, 179, 29, 45, 55, 177, 252, 192, 57, 61, 189,
254, 163, 46, 173, 183, 234, 50, 175, 31, 88, 20, 182, 117, 26, 53, 131, 20, 244,
128, 128, 160, 217, 87, 240, 147, 120, 91, 216, 55, 83, 73, 206, 238, 1, 102, 181,
50, 2, 74, 71, 64, 197, 237, 68, 169, 76, 26, 239, 37, 153, 92, 220, 113, 160, 216,
212, 96, 41, 186, 243, 127, 252, 208, 26, 34, 20, 186, 30, 191, 52, 2, 220, 19,
242, 76, 78, 226, 14, 172, 57, 53, 201, 47, 11, 15, 153, 160, 167, 92, 79, 208, 96,
165, 234, 64, 80, 127, 165, 227, 164, 155, 99, 128, 30, 139, 93, 87, 118, 169, 233,
241, 254, 76, 96, 65, 218, 188, 52, 252, 160, 238, 223, 128, 255, 50, 42, 169, 98,
225, 114, 201, 135, 113, 214, 235, 46, 158, 215, 179, 18, 28, 2, 42, 207, 120, 188,
138, 162, 63, 132, 117, 1, 160, 251, 56, 65, 100, 82, 159, 84, 95, 43, 67, 84, 246,
177, 74, 231, 199, 242, 53, 159, 97, 153, 197, 36, 123, 105, 107, 102, 136, 128,
190, 59, 88, 160, 67, 76, 154, 154, 112, 58, 176, 167, 174, 126, 79, 134, 194, 208,
154, 245, 161, 106, 236, 125, 64, 136, 202, 72, 61, 70, 170, 12, 109, 132, 68, 213,
160, 175, 251, 37, 136, 180, 2, 187, 116, 68, 53, 156, 120, 112, 109, 23, 255, 14,
160, 160, 225, 226, 77, 8, 255, 178, 118, 39, 248, 88, 56, 217, 113, 160, 209, 8,
0, 140, 126, 171, 172, 12, 93, 82, 67, 64, 234, 3, 152, 165, 245, 137, 166, 131,
218, 2, 177, 29, 84, 166, 186, 8, 42, 245, 54, 145, 160, 126, 86, 61, 131, 89, 22,
160, 114, 81, 76, 246, 11, 153, 34, 100, 184, 198, 43, 2, 180, 83, 193, 165, 54,
37, 212, 204, 215, 49, 39, 116, 222, 160, 126, 251, 244, 122, 135, 211, 3, 252, 89,
162, 34, 15, 235, 205, 93, 159, 171, 6, 213, 189, 213, 223, 17, 136, 199, 254, 127,
150, 153, 242, 158, 7, 160, 0, 53, 8, 236, 247, 236, 248, 247, 225, 155, 146, 46,
95, 246, 225, 44, 111, 43, 92, 252, 212, 58, 208, 125, 204, 149, 39, 112, 188, 138,
177, 107, 128, 128, 128,
],
vec![
248, 113, 128, 128, 128, 128, 128, 160, 90, 69, 107, 219, 253, 138, 22, 197, 123,
169, 48, 208, 29, 244, 54, 64, 26, 33, 180, 155, 153, 221, 168, 40, 98, 182, 91,
178, 43, 170, 115, 153, 128, 128, 160, 148, 29, 172, 220, 20, 75, 103, 125, 205,
59, 18, 1, 190, 50, 143, 224, 94, 172, 183, 211, 109, 156, 45, 19, 41, 85, 73, 3,
225, 115, 167, 54, 128, 128, 128, 128, 128, 128, 160, 55, 161, 114, 79, 30, 112,
182, 164, 75, 220, 84, 123, 141, 161, 31, 21, 13, 106, 247, 163, 245, 230, 99, 156,
152, 95, 61, 127, 120, 150, 38, 56, 128,
],
vec![
248, 81, 128, 128, 160, 16, 199, 4, 245, 162, 194, 163, 175, 45, 227, 201, 130, 93,
89, 14, 146, 105, 114, 253, 197, 180, 187, 144, 5, 16, 54, 62, 149, 197, 213, 157,
198, 128, 128, 128, 128, 128, 160, 252, 84, 179, 32, 171, 137, 126, 173, 198, 115,
136, 179, 5, 108, 247, 113, 181, 223, 103, 47, 138, 160, 241, 123, 74, 243, 254,
87, 209, 203, 77, 195, 128, 128, 128, 128, 128, 128, 128, 128,
],
vec![
248, 65, 159, 60, 3, 201, 167, 144, 148, 99, 114, 173, 5, 157, 47, 119, 81, 218,
54, 108, 248, 45, 205, 32, 185, 55, 231, 180, 251, 129, 223, 7, 32, 85, 160, 159,
111, 171, 245, 30, 2, 82, 177, 191, 87, 208, 208, 160, 11, 160, 220, 75, 0, 135,
232, 65, 160, 38, 209, 95, 135, 192, 247, 187, 186, 85, 132,
],
];
let expected_value = Some(vec![
0, 111, 171, 245, 30, 2, 82, 177, 191, 87, 208, 208, 160, 11, 160, 220, 75, 0, 135,
232, 65, 160, 38, 209, 95, 135, 192, 247, 187, 186, 85, 132,
]);
if let Err(e) = verify_proof(&storage_root, &proof, key.as_slice(), &expected_value) {
unreachable!("{:?}", e);
}
}

#[test]
fn test_trim_left_zero() {
assert_eq!(trim_left_zero(&[1, 2, 3, 4]), &vec![1, 2, 3, 4]);
assert_eq!(trim_left_zero(&[1, 2, 3, 0]), &vec![1, 2, 3, 0]);
assert_eq!(trim_left_zero(&[0, 2, 3, 0]), &vec![2, 3, 0]);
assert_eq!(trim_left_zero(&[0, 0, 3, 0]), &vec![3, 0]);
assert_eq!(trim_left_zero(&[0, 0, 0, 4]), &vec![4]);
assert!(trim_left_zero(&[0, 0, 0, 0]).is_empty());
assert!(trim_left_zero(&[]).is_empty());
}
}

0 comments on commit a2ae5c3

Please sign in to comment.