Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change the inner type of Hex #1371

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions core/api/src/jsonrpc/impl/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use jsonrpsee::core::Error;

use core_consensus::SYNC_STATUS;
use protocol::lazy::CHAIN_ID;
use protocol::types::{Hash, Hasher, Hex, H160, H256, U256};
Expand Down Expand Up @@ -116,9 +114,7 @@ impl AxonNodeRpcServer for NodeRpcImpl {
}

fn sha3(&self, data: Hex) -> RpcResult<Hash> {
let decode_data =
Hex::decode(data.as_string()).map_err(|e| Error::Custom(e.to_string()))?;
Ok(Hasher::digest(decode_data.as_ref()))
Ok(Hasher::digest(data.as_ref()))
}
}

Expand Down
8 changes: 4 additions & 4 deletions core/api/src/jsonrpc/web3_types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::{fmt, str::FromStr};

use either::Either;
use serde::de::{Error, MapAccess, Visitor};
Expand Down Expand Up @@ -455,13 +455,13 @@ impl<'a> Visitor<'a> for BlockIdVisitor {
}
"blockHash" => {
let value: String = visitor.next_value()?;
let raw_value = Hex::decode(value.clone())
let raw_value = Hex::from_str(&value)
.map_err(|e| Error::custom(format!("Invalid hex code: {}", e)))?;
if raw_value.len() != 32 {
return Err(Error::custom(format!("Invalid block hash: {}", value)));
} else {
let mut v = [0u8; 32];
v.copy_from_slice(&raw_value);
v.copy_from_slice(raw_value.as_ref());
block_hash = Some(v.into());
break;
}
Expand Down Expand Up @@ -899,7 +899,7 @@ mod tests {
#[test]
fn test_web3_transaction_json() {
// https://etherscan.io/getRawTx?tx=0x07c7388b03ab8403deeaefc551efbc632f8531f04dc9993a274dbba9bbb98cbf
let tx = Hex::decode("0x02f902f801728405f5e1008509898edcf78302ffb8943fc91a3afd70395cd496c647d5a6cc9d4b2b7fad8802c68af0bb140000b902843593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000006480c64700000000000000000000000000000000000000000000000000000000000000020b080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000002c68af0bb1400000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000002c68af0bb1400000000000000000000000000000000000000000004a715ce36374beaa635218d9700000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000c3681a720605bd6f8fe9a2fabff6a7cdecdc605dc080a0d253ee687ab2d9734a5073d64a0ba26bc3bc1cf4582005137bba05ef88616ea89e8ba79925267b17403fdf3ab47641b4aa52322dc385429cc92a7003c5d7c2".into()).unwrap();
let tx = Hex::from_str("0x02f902f801728405f5e1008509898edcf78302ffb8943fc91a3afd70395cd496c647d5a6cc9d4b2b7fad8802c68af0bb140000b902843593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000006480c64700000000000000000000000000000000000000000000000000000000000000020b080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000002c68af0bb1400000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000002c68af0bb1400000000000000000000000000000000000000000004a715ce36374beaa635218d9700000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000c3681a720605bd6f8fe9a2fabff6a7cdecdc605dc080a0d253ee687ab2d9734a5073d64a0ba26bc3bc1cf4582005137bba05ef88616ea89e8ba79925267b17403fdf3ab47641b4aa52322dc385429cc92a7003c5d7c2").unwrap();
let tx = UnverifiedTransaction::decode(tx).unwrap();
let tx = SignedTransaction::from_unverified(tx, None).unwrap();
let tx_json = serde_json::to_value(Web3Transaction::from(tx)).unwrap();
Expand Down
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());
yangby-cryptape marked this conversation as resolved.
Show resolved Hide resolved
}
}

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
Loading