Skip to content

Commit

Permalink
Merge pull request #15 from yoshidan/trim_left_zero_from_value
Browse files Browse the repository at this point in the history
Trim left zero from expected value

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Feb 27, 2024
2 parents 4e408c5 + bef26b3 commit 35ffc69
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion crates/ibc/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<const SYNC_COMMITTEE_SIZE: usize, const EXECUTION_PAYLOAD_TREE_DEPTH: usize
.verify_membership(
H256::from_slice(root.as_bytes()),
key.as_bytes(),
rlp::encode(&value).as_ref(),
rlp::encode(&trim_left_zero(&value)).as_ref(),
proof.clone(),
)
.map_err(|e| ClientError::ClientSpecific {
Expand Down Expand Up @@ -824,6 +824,17 @@ fn maybe_consensus_state(
}
}

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

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -978,4 +989,15 @@ mod tests {
panic!("expected error");
}
}

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

0 comments on commit 35ffc69

Please sign in to comment.