Skip to content

Commit

Permalink
follow review opinion
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason committed Aug 29, 2023
1 parent 9c37ae5 commit 63712ca
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 30 deletions.
5 changes: 1 addition & 4 deletions core/consensus/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ fn _get_random_bytes(len: usize) -> Bytes {
}

fn _mock_pub_key() -> Hex {
Hex::from_string(
"0x026c184a9016f6f71a234c86b141621f38b68c78602ab06768db4d83682c616004".to_owned(),
)
.unwrap()
Hex::from_str("0x026c184a9016f6f71a234c86b141621f38b68c78602ab06768db4d83682c616004").unwrap()
}

fn _mock_validators(len: usize) -> Vec<Validator> {
Expand Down
6 changes: 3 additions & 3 deletions core/consensus/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ pub fn convert_hex_to_bls_pubkeys(hex: Hex) -> ProtocolResult<BlsPublicKey> {

#[cfg(test)]
mod tests {
use std::str::FromStr;

use super::*;
use protocol::codec::hex_decode;

Expand Down Expand Up @@ -253,8 +255,6 @@ mod tests {
#[test]
fn test_convert_from_hex() {
let hex_str = "0xa694f4e48a5a173b61731998f8f1204342dc5c8eb1e32cdae37415c20d11ae035ddac4a39f105e9c2d4d3691024d385d";
assert!(
convert_hex_to_bls_pubkeys(Hex::from_string(String::from(hex_str)).unwrap()).is_ok()
);
assert!(convert_hex_to_bls_pubkeys(Hex::from_str(hex_str).unwrap()).is_ok());
}
}
9 changes: 6 additions & 3 deletions protocol/src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub mod executor;
pub mod receipt;
pub mod transaction;

use std::str::FromStr;

pub use transaction::truncate_slice;

use ethers_core::utils::parse_checksummed;
Expand Down Expand Up @@ -57,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_trim0x());
s.begin_list(1).append(&self.as_string());
}
}

impl Decodable for Hex {
fn decode(r: &Rlp) -> Result<Self, DecoderError> {
Hex::from_string(r.val_at(0)?).map_err(|_| DecoderError::Custom("hex check"))
let s: String = r.val_at(0)?;
Hex::from_str(s.as_str()).map_err(|_| DecoderError::Custom("hex check"))
}
}

Expand Down Expand Up @@ -188,7 +191,7 @@ mod tests {
impl Hex {
fn random() -> Self {
let data = (0..128).map(|_| random()).collect::<Vec<u8>>();
Self::from_string(hex_encode(data)).unwrap()
Hex::encode(data)
}
}

Expand Down
9 changes: 6 additions & 3 deletions protocol/src/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ mod tests {
Block, BlockVersion, ConsensusConfig, Header, Hex, Metadata, MetadataVersion, ProposeCount,
RichBlock, ValidatorExtend, H160,
};
use std::time::{SystemTime, UNIX_EPOCH};
use std::{
str::FromStr,
time::{SystemTime, UNIX_EPOCH},
};

pub fn time_now() -> u64 {
SystemTime::now()
Expand Down Expand Up @@ -328,8 +331,8 @@ mod tests {
version: MetadataVersion::new(0, 1000000000),
epoch: 0,
verifier_list: vec![ValidatorExtend {
bls_pub_key: Hex::from_string("0x04102947214862a503c73904deb5818298a186d68c7907bb609583192a7de6331493835e5b8281f4d9ee705537c0e765580e06f86ddce5867812fceb42eecefd209f0eddd0389d6b7b0100f00fb119ef9ab23826c6ea09aadcc76fa6cea6a32724".to_string()).unwrap(),
pub_key: Hex::from_string("0x02ef0cb0d7bc6c18b4bea1f5908d9106522b35ab3c399369605d4242525bda7e60".to_string()).unwrap(),
bls_pub_key: Hex::from_str("0x04102947214862a503c73904deb5818298a186d68c7907bb609583192a7de6331493835e5b8281f4d9ee705537c0e765580e06f86ddce5867812fceb42eecefd209f0eddd0389d6b7b0100f00fb119ef9ab23826c6ea09aadcc76fa6cea6a32724").unwrap(),
pub_key: Hex::from_str("0x02ef0cb0d7bc6c18b4bea1f5908d9106522b35ab3c399369605d4242525bda7e60").unwrap(),
address: H160::default(),
propose_weight: 1,
vote_weight: 1,
Expand Down
34 changes: 17 additions & 17 deletions protocol/src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use zeroize::Zeroizing;
use std::cmp::Ordering;
use std::{fmt, str::FromStr};

use bytes::BytesMut;
use faster_hex::withpfx_lowercase;
use ophelia::{PublicKey, UncompressedPublicKey};
use overlord::DurationConfig;
Expand All @@ -18,15 +17,14 @@ use common_crypto::Secp256k1PublicKey;
use common_hasher::keccak256;

use crate::codec::{deserialize_address, hex_decode, hex_encode, serialize_uint};
use crate::types::{BlockNumber, Bytes, TypesError};
use crate::types::{BlockNumber, Bytes, BytesMut, TypesError};
use crate::{ProtocolError, ProtocolResult};

pub type Hash = H256;
pub type MerkleRoot = Hash;

const ADDRESS_LEN: usize = 20;
const HEX_PREFIX: &str = "0x";
const HEX_PREFIX_UPPER: &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 @@ -99,16 +97,6 @@ impl Hex {
Ok(Bytes::from(hex_decode(s)?))
}

pub fn from_string(s: String) -> ProtocolResult<Self> {
let s = if Self::is_prefixed(s.as_str()) {
s
} else {
HEX_PREFIX.to_string() + &s
};

Ok(Hex(hex_decode(&s[2..])?.into()))
}

pub fn as_string(&self) -> String {
HEX_PREFIX.to_string() + &hex_encode(self.0.as_ref())
}
Expand All @@ -122,7 +110,7 @@ impl Hex {
}

fn is_prefixed(s: &str) -> bool {
s.starts_with(HEX_PREFIX) || s.starts_with(HEX_PREFIX_UPPER)
s.starts_with(HEX_PREFIX)
}
}

Expand All @@ -132,6 +120,18 @@ impl Default for Hex {
}
}

impl FromStr for Hex {
type Err = ProtocolError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if !Self::is_prefixed(s) {
return Err(TypesError::HexPrefix.into());
}

Ok(Hex(hex_decode(&s[2..])?.into()))
}
}

impl Serialize for Hex {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -481,18 +481,18 @@ mod tests {
#[test]
fn test_hex_decode() {
let hex = String::from("0x");
let res = Hex::from_string(hex.clone()).unwrap();
let res = Hex::from_str(&hex).unwrap();
assert!(res.is_empty());

let res = Hex::decode(hex).unwrap();
assert!(res.is_empty());

let hex = String::from("123456");
let _ = Hex::from_string(hex.clone()).unwrap();
let _ = Hex::from_str(&hex).unwrap();
let _ = Hex::decode(hex).unwrap();

let hex = String::from("0x123f");
let _ = Hex::from_string(hex.clone()).unwrap();
let _ = Hex::from_str(&hex).unwrap();
let _ = Hex::decode(hex).unwrap();
}

Expand Down

0 comments on commit 63712ca

Please sign in to comment.