Skip to content

Commit 0526fd7

Browse files
author
Eason
committed
follow review opinion
1 parent 9c37ae5 commit 0526fd7

7 files changed

Lines changed: 54 additions & 58 deletions

File tree

core/api/src/jsonrpc/impl/node.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use std::path::PathBuf;
22
use std::sync::atomic::{AtomicBool, Ordering};
33
use std::sync::Arc;
44

5-
use jsonrpsee::core::Error;
6-
75
use core_consensus::SYNC_STATUS;
86
use protocol::lazy::CHAIN_ID;
97
use protocol::types::{Hash, Hasher, Hex, H160, H256, U256};
@@ -116,9 +114,7 @@ impl AxonNodeRpcServer for NodeRpcImpl {
116114
}
117115

118116
fn sha3(&self, data: Hex) -> RpcResult<Hash> {
119-
let decode_data =
120-
Hex::decode(data.as_string()).map_err(|e| Error::Custom(e.to_string()))?;
121-
Ok(Hasher::digest(decode_data.as_ref()))
117+
Ok(Hasher::digest(data.as_ref()))
122118
}
123119
}
124120

core/api/src/jsonrpc/web3_types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt;
1+
use std::{fmt, str::FromStr};
22

33
use either::Either;
44
use serde::de::{Error, MapAccess, Visitor};
@@ -455,13 +455,13 @@ impl<'a> Visitor<'a> for BlockIdVisitor {
455455
}
456456
"blockHash" => {
457457
let value: String = visitor.next_value()?;
458-
let raw_value = Hex::decode(value.clone())
458+
let raw_value = Hex::from_str(&value)
459459
.map_err(|e| Error::custom(format!("Invalid hex code: {}", e)))?;
460460
if raw_value.len() != 32 {
461461
return Err(Error::custom(format!("Invalid block hash: {}", value)));
462462
} else {
463463
let mut v = [0u8; 32];
464-
v.copy_from_slice(&raw_value);
464+
v.copy_from_slice(raw_value.as_ref());
465465
block_hash = Some(v.into());
466466
break;
467467
}
@@ -899,7 +899,7 @@ mod tests {
899899
#[test]
900900
fn test_web3_transaction_json() {
901901
// https://etherscan.io/getRawTx?tx=0x07c7388b03ab8403deeaefc551efbc632f8531f04dc9993a274dbba9bbb98cbf
902-
let tx = Hex::decode("0x02f902f801728405f5e1008509898edcf78302ffb8943fc91a3afd70395cd496c647d5a6cc9d4b2b7fad8802c68af0bb140000b902843593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000006480c64700000000000000000000000000000000000000000000000000000000000000020b080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000002c68af0bb1400000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000002c68af0bb1400000000000000000000000000000000000000000004a715ce36374beaa635218d9700000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000c3681a720605bd6f8fe9a2fabff6a7cdecdc605dc080a0d253ee687ab2d9734a5073d64a0ba26bc3bc1cf4582005137bba05ef88616ea89e8ba79925267b17403fdf3ab47641b4aa52322dc385429cc92a7003c5d7c2".into()).unwrap();
902+
let tx = Hex::from_str("0x02f902f801728405f5e1008509898edcf78302ffb8943fc91a3afd70395cd496c647d5a6cc9d4b2b7fad8802c68af0bb140000b902843593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000006480c64700000000000000000000000000000000000000000000000000000000000000020b080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000002c68af0bb1400000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000002c68af0bb1400000000000000000000000000000000000000000004a715ce36374beaa635218d9700000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000c3681a720605bd6f8fe9a2fabff6a7cdecdc605dc080a0d253ee687ab2d9734a5073d64a0ba26bc3bc1cf4582005137bba05ef88616ea89e8ba79925267b17403fdf3ab47641b4aa52322dc385429cc92a7003c5d7c2").unwrap();
903903
let tx = UnverifiedTransaction::decode(tx).unwrap();
904904
let tx = SignedTransaction::from_unverified(tx, None).unwrap();
905905
let tx_json = serde_json::to_value(Web3Transaction::from(tx)).unwrap();

core/consensus/src/tests/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ fn _get_random_bytes(len: usize) -> Bytes {
9090
}
9191

9292
fn _mock_pub_key() -> Hex {
93-
Hex::from_string(
94-
"0x026c184a9016f6f71a234c86b141621f38b68c78602ab06768db4d83682c616004".to_owned(),
95-
)
96-
.unwrap()
93+
Hex::from_str("0x026c184a9016f6f71a234c86b141621f38b68c78602ab06768db4d83682c616004").unwrap()
9794
}
9895

9996
fn _mock_validators(len: usize) -> Vec<Validator> {

core/consensus/src/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ pub fn convert_hex_to_bls_pubkeys(hex: Hex) -> ProtocolResult<BlsPublicKey> {
189189

190190
#[cfg(test)]
191191
mod tests {
192+
use std::str::FromStr;
193+
192194
use super::*;
193195
use protocol::codec::hex_decode;
194196

@@ -253,8 +255,6 @@ mod tests {
253255
#[test]
254256
fn test_convert_from_hex() {
255257
let hex_str = "0xa694f4e48a5a173b61731998f8f1204342dc5c8eb1e32cdae37415c20d11ae035ddac4a39f105e9c2d4d3691024d385d";
256-
assert!(
257-
convert_hex_to_bls_pubkeys(Hex::from_string(String::from(hex_str)).unwrap()).is_ok()
258-
);
258+
assert!(convert_hex_to_bls_pubkeys(Hex::from_str(hex_str).unwrap()).is_ok());
259259
}
260260
}

protocol/src/codec/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ pub mod executor;
44
pub mod receipt;
55
pub mod transaction;
66

7+
use std::str::FromStr;
8+
79
pub use transaction::truncate_slice;
810

911
use ethers_core::utils::parse_checksummed;
@@ -57,13 +59,14 @@ impl Decodable for Address {
5759

5860
impl Encodable for Hex {
5961
fn rlp_append(&self, s: &mut RlpStream) {
60-
s.begin_list(1).append(&self.as_string_trim0x());
62+
s.begin_list(1).append(&self.as_string());
6163
}
6264
}
6365

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

@@ -188,7 +191,7 @@ mod tests {
188191
impl Hex {
189192
fn random() -> Self {
190193
let data = (0..128).map(|_| random()).collect::<Vec<u8>>();
191-
Self::from_string(hex_encode(data)).unwrap()
194+
Hex::encode(data)
192195
}
193196
}
194197

protocol/src/types/block.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,10 @@ mod tests {
272272
Block, BlockVersion, ConsensusConfig, Header, Hex, Metadata, MetadataVersion, ProposeCount,
273273
RichBlock, ValidatorExtend, H160,
274274
};
275-
use std::time::{SystemTime, UNIX_EPOCH};
275+
use std::{
276+
str::FromStr,
277+
time::{SystemTime, UNIX_EPOCH},
278+
};
276279

277280
pub fn time_now() -> u64 {
278281
SystemTime::now()
@@ -328,8 +331,8 @@ mod tests {
328331
version: MetadataVersion::new(0, 1000000000),
329332
epoch: 0,
330333
verifier_list: vec![ValidatorExtend {
331-
bls_pub_key: Hex::from_string("0x04102947214862a503c73904deb5818298a186d68c7907bb609583192a7de6331493835e5b8281f4d9ee705537c0e765580e06f86ddce5867812fceb42eecefd209f0eddd0389d6b7b0100f00fb119ef9ab23826c6ea09aadcc76fa6cea6a32724".to_string()).unwrap(),
332-
pub_key: Hex::from_string("0x02ef0cb0d7bc6c18b4bea1f5908d9106522b35ab3c399369605d4242525bda7e60".to_string()).unwrap(),
334+
bls_pub_key: Hex::from_str("0x04102947214862a503c73904deb5818298a186d68c7907bb609583192a7de6331493835e5b8281f4d9ee705537c0e765580e06f86ddce5867812fceb42eecefd209f0eddd0389d6b7b0100f00fb119ef9ab23826c6ea09aadcc76fa6cea6a32724").unwrap(),
335+
pub_key: Hex::from_str("0x02ef0cb0d7bc6c18b4bea1f5908d9106522b35ab3c399369605d4242525bda7e60").unwrap(),
333336
address: H160::default(),
334337
propose_weight: 1,
335338
vote_weight: 1,

protocol/src/types/primitive.rs

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use zeroize::Zeroizing;
77
use std::cmp::Ordering;
88
use std::{fmt, str::FromStr};
99

10-
use bytes::BytesMut;
1110
use faster_hex::withpfx_lowercase;
1211
use ophelia::{PublicKey, UncompressedPublicKey};
1312
use overlord::DurationConfig;
@@ -18,15 +17,14 @@ use common_crypto::Secp256k1PublicKey;
1817
use common_hasher::keccak256;
1918

2019
use crate::codec::{deserialize_address, hex_decode, hex_encode, serialize_uint};
21-
use crate::types::{BlockNumber, Bytes, TypesError};
20+
use crate::types::{BlockNumber, Bytes, BytesMut, TypesError};
2221
use crate::{ProtocolError, ProtocolResult};
2322

2423
pub type Hash = H256;
2524
pub type MerkleRoot = Hash;
2625

2726
const ADDRESS_LEN: usize = 20;
2827
const HEX_PREFIX: &str = "0x";
29-
const HEX_PREFIX_UPPER: &str = "0X";
3028

3129
pub const NIL_DATA: H256 = H256([
3230
0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0,
@@ -85,28 +83,12 @@ impl Hex {
8583
self.0.is_empty()
8684
}
8785

88-
pub fn encode<T: AsRef<[u8]>>(src: T) -> Self {
89-
Hex(BytesMut::from(src.as_ref()).freeze())
90-
}
91-
92-
pub fn decode(s: String) -> ProtocolResult<Bytes> {
93-
let s = if Self::is_prefixed(s.as_str()) {
94-
&s[2..]
95-
} else {
96-
s.as_str()
97-
};
98-
99-
Ok(Bytes::from(hex_decode(s)?))
86+
pub fn len(&self) -> usize {
87+
self.0.len()
10088
}
10189

102-
pub fn from_string(s: String) -> ProtocolResult<Self> {
103-
let s = if Self::is_prefixed(s.as_str()) {
104-
s
105-
} else {
106-
HEX_PREFIX.to_string() + &s
107-
};
108-
109-
Ok(Hex(hex_decode(&s[2..])?.into()))
90+
pub fn encode<T: AsRef<[u8]>>(src: T) -> Self {
91+
Hex(BytesMut::from(src.as_ref()).freeze())
11092
}
11193

11294
pub fn as_string(&self) -> String {
@@ -122,7 +104,7 @@ impl Hex {
122104
}
123105

124106
fn is_prefixed(s: &str) -> bool {
125-
s.starts_with(HEX_PREFIX) || s.starts_with(HEX_PREFIX_UPPER)
107+
s.starts_with(HEX_PREFIX)
126108
}
127109
}
128110

@@ -132,6 +114,30 @@ impl Default for Hex {
132114
}
133115
}
134116

117+
impl AsRef<[u8]> for Hex {
118+
fn as_ref(&self) -> &[u8] {
119+
&self.0
120+
}
121+
}
122+
123+
impl FromStr for Hex {
124+
type Err = ProtocolError;
125+
126+
fn from_str(s: &str) -> Result<Self, Self::Err> {
127+
if !Self::is_prefixed(s) {
128+
return Err(TypesError::HexPrefix.into());
129+
}
130+
131+
Ok(Hex(hex_decode(&s[2..])?.into()))
132+
}
133+
}
134+
135+
impl From<Hex> for Bytes {
136+
fn from(bytes: Hex) -> Self {
137+
bytes.0
138+
}
139+
}
140+
135141
impl Serialize for Hex {
136142
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
137143
where
@@ -480,20 +486,11 @@ mod tests {
480486

481487
#[test]
482488
fn test_hex_decode() {
483-
let hex = String::from("0x");
484-
let res = Hex::from_string(hex.clone()).unwrap();
485-
assert!(res.is_empty());
486-
487-
let res = Hex::decode(hex).unwrap();
489+
let res = Hex::from_str("0x").unwrap();
488490
assert!(res.is_empty());
489491

490-
let hex = String::from("123456");
491-
let _ = Hex::from_string(hex.clone()).unwrap();
492-
let _ = Hex::decode(hex).unwrap();
493-
494-
let hex = String::from("0x123f");
495-
let _ = Hex::from_string(hex.clone()).unwrap();
496-
let _ = Hex::decode(hex).unwrap();
492+
assert!(Hex::from_str("123456").is_ok());
493+
assert!(Hex::from_str("0x123f").is_ok());
497494
}
498495

499496
#[test]

0 commit comments

Comments
 (0)