Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
KaoImin committed Aug 29, 2023
1 parent 0526fd7 commit 010f6a4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions protocol/src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ethers_core::utils::parse_checksummed;
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
use serde::{Deserialize as _, Deserializer, Serializer};

use crate::types::{Address, Bytes, DBBytes, Hex, Key256Bits, TypesError, H160, U256};
use crate::types::{Address, Bytes, DBBytes, Hex, Key256Bits, TypesError, H160, U256, HEX_PREFIX};
use crate::ProtocolResult;

static CHARS: &[u8] = b"0123456789abcdef";
Expand Down Expand Up @@ -59,13 +59,14 @@ impl Decodable for Address {

impl Encodable for Hex {
fn rlp_append(&self, s: &mut RlpStream) {
s.begin_list(1).append(&self.as_string());
s.begin_list(1).append(&self.as_string_trim0x());
}
}

impl Decodable for Hex {
fn decode(r: &Rlp) -> Result<Self, DecoderError> {
let s: String = r.val_at(0)?;
let s = HEX_PREFIX.to_string() + &s;
Hex::from_str(s.as_str()).map_err(|_| DecoderError::Custom("hex check"))
}
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub type Hash = H256;
pub type MerkleRoot = Hash;

const ADDRESS_LEN: usize = 20;
const HEX_PREFIX: &str = "0x";
pub(crate) const HEX_PREFIX: &str = "0x";

pub const NIL_DATA: H256 = H256([
0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0,
Expand Down Expand Up @@ -489,7 +489,7 @@ mod tests {
let res = Hex::from_str("0x").unwrap();
assert!(res.is_empty());

assert!(Hex::from_str("123456").is_ok());
assert!(Hex::from_str("123456").is_err());
assert!(Hex::from_str("0x123f").is_ok());
}

Expand Down

0 comments on commit 010f6a4

Please sign in to comment.