Skip to content

Commit

Permalink
Fix decoding root hash from hex
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo committed Mar 3, 2023
1 parent a3cc583 commit c92a313
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 58 deletions.
122 changes: 90 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions core/lib/storage/src/chain/block/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,52 @@ impl TransactionItem {
}
}
}

/// Helper method for `find_block_by_height_or_hash`. It checks whether
/// provided string can be interpreted like a hash, and if so, returns the
/// hexadecimal string without prefix.
pub(crate) fn decode_hex_with_prefix(query: &str) -> Option<Vec<u8>> {
const HASH_STRING_SIZE: usize = 32 * 2; // 32 bytes, 2 symbols per byte.

let query = if let Some(query) = query.strip_prefix("0x") {
query
} else if let Some(query) = query.strip_prefix("sync-bl:") {
query
} else {
query
};

if query.len() != HASH_STRING_SIZE {
return None;
}

hex::decode(query).ok()
}

#[cfg(test)]
mod test {
use crate::chain::block::conversion::decode_hex_with_prefix;
use zksync_types::{H160, H256};

fn check_all_prefixes(value: String) -> bool {
let string_wit_0x_prefix = format!("0x{}", &value);
let string_wit_sync_bl_prefix = format!("sync-bl:{}", &value);
decode_hex_with_prefix(&string_wit_0x_prefix).is_some()
&& decode_hex_with_prefix(&string_wit_sync_bl_prefix).is_some()
&& decode_hex_with_prefix(&value).is_some()
}

#[test]
fn test_decode_hex() {
let correct_string = hex::encode(H256::random());
assert!(check_all_prefixes(correct_string));
let short_string = hex::encode(H160::random());
assert!(!check_all_prefixes(short_string));
let mut incorrect_string = hex::encode(H256::random());
// 'x' is impossible to use in hex string
incorrect_string.replace_range(10..11, "x");
assert!(!check_all_prefixes(incorrect_string));
let incorrect_string2 = "random_string".to_string();
assert!(!check_all_prefixes(incorrect_string2));
}
}
Loading

0 comments on commit c92a313

Please sign in to comment.