Skip to content

Commit

Permalink
refactor!: change the rlp codec of Hex (#1382)
Browse files Browse the repository at this point in the history
* refactor!: change the rlp codec of `Hex`

* fix e2e test
  • Loading branch information
KaoImin authored Aug 30, 2023
1 parent 2877191 commit 6548ea0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 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
39 changes: 20 additions & 19 deletions tests/e2e/src/create_test_data/createTestDataManage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import erc20 from "./ERC20.json";
const basePath = "./src/test_data_temp_file";
const option = { timeout: 1000 * 30 };
const web3 = new Web3(
new Web3.providers.HttpProvider(Config.getIns().axonRpc.url, option)
new Web3.providers.HttpProvider(Config.getIns().axonRpc.url, option),
);
const accountFrom = web3.eth.accounts.privateKeyToAccount(
Config.getIns().hexPrivateKey
Config.getIns().hexPrivateKey,
);
const transactionInfo = {
contractAddress: "",
Expand All @@ -27,6 +27,7 @@ const filterIds = {
filter_id_2: "",
filter_id_3: "",
};
const hexPrefix = "0x";
const createTestDataMange = {
async savejson(filePath, data) {
const dataStr = JSON.stringify(data, null, 4);
Expand Down Expand Up @@ -66,10 +67,8 @@ const createTestDataMange = {
transactionInfo.transactionIndex = receipt.transactionIndex;
transactionInfo.accountAddress = accountFrom.address;
transactionInfo.hexBlockNumber = `0x${receipt.blockNumber.toString(16)}`;
transactionInfo.topic1 =
"0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0";
transactionInfo.topic2 =
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
transactionInfo.topic1 = "0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0";
transactionInfo.topic2 = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
await this.savejson(`${basePath}/testData_1.json`, transactionInfo);
},
async writeFilterIds(filterIdIndex, id) {
Expand Down Expand Up @@ -115,6 +114,7 @@ const createTestDataMange = {
fs.mkdirSync(`${basePath}/`);
}
},

async sendRawTestTx() {
const toAddress = Config.getIns().acount2;
const nonce = (await web3.eth.getTransactionCount(accountFrom.address)) + 1;
Expand All @@ -126,17 +126,18 @@ const createTestDataMange = {
value: web3.utils.toHex(web3.utils.toWei("1", "ether")),
};
// eslint-disable-next-line global-require
const { Common } = require('@ethereumjs/common');
const {Transaction} = require('@ethereumjs/tx');
const common = Common.custom({ chainId: web3.utils.toHex(Config.getIns().axonRpc.chainId)});
const tx = Transaction.fromTxData(txObject, {common});
const privateKey = Buffer.from(Config.getIns().hexPrivateKey.substring(2), 'hex');
const { Common } = require("@ethereumjs/common");
const { Transaction } = require("@ethereumjs/tx");
const common = Common.custom({ chainId: web3.utils.toHex(Config.getIns().axonRpc.chainId) });
const tx = Transaction.fromTxData(txObject, { common });
const privateKey = Buffer.from(Config.getIns().hexPrivateKey.substring(2), "hex");
const signedTx = tx.sign(privateKey);
const serializedTx = signedTx.serialize();
return serializedTx.toString("hex");
return hexPrefix + serializedTx.toString("hex");
},

async sendPreEip155RawTestTx() {
const toAddress = Config.getIns().acount2;
const toAddress = Config.getIns().aount2;
const nonce = (await web3.eth.getTransactionCount(accountFrom.address)) + 1;
const txObject = {
nonce: web3.utils.toHex(nonce),
Expand All @@ -146,14 +147,14 @@ const createTestDataMange = {
value: web3.utils.toHex(web3.utils.toWei("1", "ether")),
};
// eslint-disable-next-line global-require
const { Chain, Common, Hardfork } = require('@ethereumjs/common');
const {Transaction} = require('@ethereumjs/tx');
const common = new Common({chain: Chain.Mainnet, hardfork: Hardfork.TangerineWhistle});
const tx = Transaction.fromTxData(txObject,{common});
const privateKey = Buffer.from(Config.getIns().hexPrivateKey.substring(2), 'hex');
const { Chain, Common, Hardfork } = require("@ethereumjs/common");
const { Transaction } = require("@ethereumjs/tx");
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.TangerineWhistle });
const tx = Transaction.fromTxData(txObject, { common });
const privateKey = Buffer.from(Config.getIns().hexPrivateKey.substring(2), "hex");
const signedTx = tx.sign(privateKey);
const serializedTx = signedTx.serialize();
return serializedTx.toString("hex");
return hexPrefix + serializedTx.toString("hex");
},
};
export default createTestDataMange;
6 changes: 3 additions & 3 deletions tests/e2e/src/web3_sha3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("web3_sha3", () => {
const param1 = await page.$(goto.pageIds.param1Id);
await testType.type("1"); // 0: none params 1: common params to request 2: more params
await param1.type("");
await goto.check(page, "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");
await goto.check(page, "-32603");
});

/**
Expand Down Expand Up @@ -56,7 +56,7 @@ describe("web3_sha3", () => {
await testType.type("1");
const param1 = await page.$(goto.pageIds.param1Id);
await param1.type("123456");
await goto.check(page, "0x6adf031833174bbe4c85eafe59ddb54e6584648c2c962c6f94791ab49caa0ad4");
await goto.check(page, "-32603");
});

/**
Expand All @@ -67,7 +67,7 @@ describe("web3_sha3", () => {
await testType.type("1");
const param1 = await page.$(goto.pageIds.param1Id);
await param1.type("68656c6c6f20776f726c64");
await goto.check(page, "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad");
await goto.check(page, "-32603");
});

/**
Expand Down

0 comments on commit 6548ea0

Please sign in to comment.