diff --git a/core-primitives/utils/src/hex.rs b/core-primitives/utils/src/hex.rs index 6f3c63e68c..8543cba15f 100644 --- a/core-primitives/utils/src/hex.rs +++ b/core-primitives/utils/src/hex.rs @@ -57,10 +57,12 @@ pub fn hex_encode(data: &[u8]) -> String { /// Helper method for decoding hex. pub fn decode_hex>(message: T) -> Result> { - let mut message = message.as_ref(); - if message[..2] == [b'0', b'x'] { - message = &message[2..] - } + let message = message.as_ref(); + let message = match message { + [b'0', b'x', hex_value @ ..] => hex_value, + _ => message, + }; + let decoded_message = hex::decode(message).map_err(Error::Hex)?; Ok(decoded_message) } @@ -80,6 +82,26 @@ mod tests { assert_eq!(data, decoded_data); } + #[test] + fn hex_encode_decode_works_empty_input() { + let data = String::new(); + + let hex_encoded_data = hex_encode(&data.encode()); + let decoded_data = + String::decode(&mut decode_hex(hex_encoded_data).unwrap().as_slice()).unwrap(); + + assert_eq!(data, decoded_data); + } + + #[test] + fn hex_encode_decode_works_empty_input_for_decode() { + let data = String::new(); + + let decoded_data = decode_hex(&data).unwrap(); + + assert!(decoded_data.is_empty()); + } + #[test] fn to_hex_from_hex_works() { let data = "Hello World!".to_string(); diff --git a/sidechain/consensus/common/src/is_descendant_of_builder.rs b/sidechain/consensus/common/src/is_descendant_of_builder.rs index a6c8edc75f..5e13c6f69a 100644 --- a/sidechain/consensus/common/src/is_descendant_of_builder.rs +++ b/sidechain/consensus/common/src/is_descendant_of_builder.rs @@ -49,7 +49,7 @@ where // If the current hash is the head and the parent is the base, then we know that // this current hash is the descendant of the parent. Otherwise we can set the // head to the parent and find the lowest common ancestor between `head` - /// and `base` in the tree. + // and `base` in the tree. if current_hash == head { if current_parent_hash == base { return Ok(true)