Skip to content

Commit

Permalink
refactor!: change the rlp codec of Hex
Browse files Browse the repository at this point in the history
  • Loading branch information
KaoImin committed Aug 29, 2023
1 parent 2877191 commit 02d4e51
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
6 changes: 3 additions & 3 deletions core/run/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ const TESTCASES: &[TestCase] = &[
config_file: "config.toml",
chain_spec_file: "specs/single_node/chain-spec.toml",
input_genesis_hash: "0x2043f690fc6e086c6940a083072a82dee16c18a4c4afaf6f4e1c7a585fae2543",
genesis_state_root: "0x47bd35cdb8bd43da1d5ce85ad77fd68de4d654fc2677ac36bd3e18cfda0ff136",
genesis_state_root: "0x601bd874d41eb9adb32021ee3ab934e0481065c58abfe7e757e33fb01be18dd5",
genesis_receipts_root: "0x8544b530238201f1620b139861a6841040b37f78f8bdae8736ef5cec474e979b",
},
TestCase {
chain_name: "multi_nodes",
config_file: "nodes/node_1.toml",
chain_spec_file: "specs/multi_nodes/chain-spec.toml",
input_genesis_hash: "0x5e5c47725bb1face59965a326b1d69e1ada1892da2e2f53c4520ed5da3d88d59",
genesis_state_root: "0x305f218b6deb1af9c59ac3fc1620f378e55d1d16f189ffc13a44debc6e16646c",
genesis_state_root: "0x6dfa5c13c07a2c22d8696488a5edbee502157d58f771d02cb9c24dd49b844491",
genesis_receipts_root: "0x8544b530238201f1620b139861a6841040b37f78f8bdae8736ef5cec474e979b",
},
TestCase {
chain_name: "multi_nodes_short_epoch_len",
config_file: "nodes/node_1.toml",
chain_spec_file: "specs/multi_nodes_short_epoch_len/chain-spec.toml",
input_genesis_hash: "0x2043f690fc6e086c6940a083072a82dee16c18a4c4afaf6f4e1c7a585fae2543",
genesis_state_root: "0xcbf9d771e6f7d4e8cf8946d7cce7489d60daadda9faecea8692d26735faf72fc",
genesis_state_root: "0x2a69b31d0d9a2391b6299f75a780d58acf411ec3e41374a8b829824a1850e73f",
genesis_receipts_root: "0x8544b530238201f1620b139861a6841040b37f78f8bdae8736ef5cec474e979b",
},
];
Expand Down
17 changes: 8 additions & 9 deletions protocol/src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ pub mod executor;
pub mod receipt;
pub mod transaction;

use std::str::FromStr;

pub use transaction::truncate_slice;

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, HEX_PREFIX, U256};
use crate::types::{Address, Bytes, DBBytes, Hex, Key256Bits, TypesError, H160, U256};
use crate::ProtocolResult;

static CHARS: &[u8] = b"0123456789abcdef";
Expand Down Expand Up @@ -59,15 +57,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_ref());
}
}

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"))
let b: Vec<u8> = r.val_at(0)?;
Ok(Hex::encode(b))
}
}

Expand Down Expand Up @@ -212,7 +209,9 @@ mod tests {
#[test]
fn test_hex_rlp() {
let origin = Hex::random();
let raw = rlp::encode(&origin);
assert_eq!(rlp::decode::<Hex>(&raw).unwrap(), origin)
let raw = origin.rlp_bytes();
let decode = <Hex as Decodable>::decode(&Rlp::new(raw.as_ref())).unwrap();

assert_eq!(origin, decode);
}
}
2 changes: 1 addition & 1 deletion 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;
pub(crate) const HEX_PREFIX: &str = "0x";
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

0 comments on commit 02d4e51

Please sign in to comment.