From 717e87b1c30befb618c8424073bb47fc034135c1 Mon Sep 17 00:00:00 2001 From: Niklas Date: Mon, 27 Jan 2020 17:01:38 +0100 Subject: [PATCH 1/4] fix: export hardcoded sync format --- ethcore/spec/src/spec.rs | 7 ++++--- ethcore/types/Cargo.toml | 4 +--- ethcore/types/src/encoded.rs | 9 +++++++++ ethcore/types/src/lib.rs | 6 ++---- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/ethcore/spec/src/spec.rs b/ethcore/spec/src/spec.rs index 74d0696e13f..6dd0b393ce2 100644 --- a/ethcore/spec/src/spec.rs +++ b/ethcore/spec/src/spec.rs @@ -250,9 +250,10 @@ impl From for SpecHardcodedSync { impl fmt::Display for SpecHardcodedSync { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!(f, "{{")?; - writeln!(f, r#"header": "{:?},"#, self.header)?; - writeln!(f, r#"total_difficulty": "{:?},"#, self.total_difficulty)?; - writeln!(f, r#"chts": {:#?}"#, self.chts.iter().map(|x| format!(r#"{:?}"#, x)).collect::>())?; + writeln!(f, r#""header": "{}","#, self.header)?; + writeln!(f, r#""totalDifficulty": "{:?}""#, self.total_difficulty)?; + // TODO: #11415 - fix trailing comma for CHTs + writeln!(f, r#""CHTs": {:#?}"#, self.chts.iter().map(|x| format!("{:?}", x)).collect::>())?; writeln!(f, "}}") } } diff --git a/ethcore/types/Cargo.toml b/ethcore/types/Cargo.toml index f1002b14a11..5b423948608 100644 --- a/ethcore/types/Cargo.toml +++ b/ethcore/types/Cargo.toml @@ -18,11 +18,9 @@ parity-snappy = "0.1" patricia-trie-ethereum = { path = "../../util/patricia-trie-ethereum" } rlp = "0.4.0" rlp_derive = { path = "../../util/rlp-derive" } +rustc-hex = "1.0" unexpected = { path = "../../util/unexpected" } vm = { path = "../vm"} -[dev-dependencies] -rustc-hex = "1.0" - [features] test-helpers = [] diff --git a/ethcore/types/src/encoded.rs b/ethcore/types/src/encoded.rs index 1cf418742cb..dbe96f24684 100644 --- a/ethcore/types/src/encoded.rs +++ b/ethcore/types/src/encoded.rs @@ -23,12 +23,15 @@ //! When the entirety of the object is needed, it's better to upgrade it to a fully //! decoded object where parts like the hash can be saved. +use std::fmt; + use block::Block as FullBlock; use ethereum_types::{H256, Bloom, U256, Address}; use hash::keccak; use header::{Header as FullHeader}; use parity_util_mem::MallocSizeOf; use rlp::{self, Rlp, RlpStream}; +use rustc_hex::ToHex; use transaction::UnverifiedTransaction; use views::{self, BlockView, HeaderView, BodyView}; use BlockNumber; @@ -60,6 +63,12 @@ impl Header { pub fn into_inner(self) -> Vec { self.0 } } +impl fmt::Display for Header { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.0.to_hex()) + } +} + // forwarders to borrowed view. impl Header { /// Returns the header hash. diff --git a/ethcore/types/src/lib.rs b/ethcore/types/src/lib.rs index 07bd7b12774..2683b6a614d 100644 --- a/ethcore/types/src/lib.rs +++ b/ethcore/types/src/lib.rs @@ -42,8 +42,9 @@ extern crate derive_more; extern crate keccak_hash as hash; extern crate parity_bytes as bytes; extern crate patricia_trie_ethereum as ethtrie; -extern crate rlp; extern crate parity_snappy; +extern crate rlp; +extern crate rustc_hex; extern crate unexpected; #[macro_use] @@ -54,9 +55,6 @@ extern crate parity_util_mem as malloc_size_of; #[macro_use] pub mod views; -#[cfg(test)] -extern crate rustc_hex; - pub mod account_diff; pub mod ancestry_action; pub mod basic_account; From b0bfd187990e0626cd03752da4f74841940ab45b Mon Sep 17 00:00:00 2001 From: Niklas Date: Tue, 28 Jan 2020 10:23:03 +0100 Subject: [PATCH 2/4] address grumbles --- Cargo.lock | 2 +- ethcore/spec/src/spec.rs | 2 +- ethcore/types/Cargo.toml | 2 +- ethcore/types/src/encoded.rs | 11 ++++------- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 582f6dabce8..e4f8cb70336 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -658,7 +658,7 @@ dependencies = [ "patricia-trie-ethereum 0.1.0", "rlp 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "rlp_derive 0.1.0", - "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "unexpected 0.1.0", "vm 0.1.0", ] diff --git a/ethcore/spec/src/spec.rs b/ethcore/spec/src/spec.rs index 6dd0b393ce2..b1e7ae99a1d 100644 --- a/ethcore/spec/src/spec.rs +++ b/ethcore/spec/src/spec.rs @@ -250,7 +250,7 @@ impl From for SpecHardcodedSync { impl fmt::Display for SpecHardcodedSync { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!(f, "{{")?; - writeln!(f, r#""header": "{}","#, self.header)?; + writeln!(f, r#""header": "{}","#, self.header.to_hex())?; writeln!(f, r#""totalDifficulty": "{:?}""#, self.total_difficulty)?; // TODO: #11415 - fix trailing comma for CHTs writeln!(f, r#""CHTs": {:#?}"#, self.chts.iter().map(|x| format!("{:?}", x)).collect::>())?; diff --git a/ethcore/types/Cargo.toml b/ethcore/types/Cargo.toml index 5b423948608..e4ca00f6899 100644 --- a/ethcore/types/Cargo.toml +++ b/ethcore/types/Cargo.toml @@ -18,7 +18,7 @@ parity-snappy = "0.1" patricia-trie-ethereum = { path = "../../util/patricia-trie-ethereum" } rlp = "0.4.0" rlp_derive = { path = "../../util/rlp-derive" } -rustc-hex = "1.0" +rustc-hex = "2.0" unexpected = { path = "../../util/unexpected" } vm = { path = "../vm"} diff --git a/ethcore/types/src/encoded.rs b/ethcore/types/src/encoded.rs index dbe96f24684..be0d580b964 100644 --- a/ethcore/types/src/encoded.rs +++ b/ethcore/types/src/encoded.rs @@ -23,12 +23,10 @@ //! When the entirety of the object is needed, it's better to upgrade it to a fully //! decoded object where parts like the hash can be saved. -use std::fmt; - use block::Block as FullBlock; use ethereum_types::{H256, Bloom, U256, Address}; use hash::keccak; -use header::{Header as FullHeader}; +use header::Header as FullHeader; use parity_util_mem::MallocSizeOf; use rlp::{self, Rlp, RlpStream}; use rustc_hex::ToHex; @@ -61,11 +59,10 @@ impl Header { /// Consume the view and return the raw bytes. pub fn into_inner(self) -> Vec { self.0 } -} -impl fmt::Display for Header { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.0.to_hex()) + /// Get the hexadecimal representation of the header + pub fn to_hex(&self) -> String { + self.0.to_hex() } } From 9ceda9964a1011ce18be1c4cd2863db08582022b Mon Sep 17 00:00:00 2001 From: Niklas Date: Tue, 28 Jan 2020 11:15:58 +0100 Subject: [PATCH 3/4] make tests compile with rustc_hex 2.0 --- ethcore/types/src/header.rs | 14 +++++++------- ethcore/types/src/receipt.rs | 18 ++++++++++-------- ethcore/types/src/transaction/transaction.rs | 12 ++++++------ ethcore/types/src/views/block.rs | 2 +- ethcore/types/src/views/body.rs | 2 +- ethcore/types/src/views/header.rs | 6 +++--- ethcore/types/src/views/transaction.rs | 4 ++-- 7 files changed, 30 insertions(+), 28 deletions(-) diff --git a/ethcore/types/src/header.rs b/ethcore/types/src/header.rs index 87fe9a5348e..a36dca43587 100644 --- a/ethcore/types/src/header.rs +++ b/ethcore/types/src/header.rs @@ -377,11 +377,11 @@ mod tests { #[test] fn test_header_seal_fields() { // that's rlp of block header created with ethash engine. - let header_rlp = "f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23".from_hex().unwrap(); - let mix_hash = "a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd".from_hex().unwrap(); - let mix_hash_decoded = "a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd".from_hex().unwrap(); - let nonce = "88ab4e252a7e8c2a23".from_hex().unwrap(); - let nonce_decoded = "ab4e252a7e8c2a23".from_hex().unwrap(); + let header_rlp: Vec = "f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23".from_hex().unwrap(); + let mix_hash: Vec = "a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd".from_hex().unwrap(); + let mix_hash_decoded: Vec = "a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd".from_hex().unwrap(); + let nonce: Vec = "88ab4e252a7e8c2a23".from_hex().unwrap(); + let nonce_decoded: Vec = "ab4e252a7e8c2a23".from_hex().unwrap(); let header: Header = rlp::decode(&header_rlp).expect("error decoding header"); let seal_fields = header.seal.clone(); @@ -398,7 +398,7 @@ mod tests { #[test] fn decode_and_encode_header() { // that's rlp of block header created with ethash engine. - let header_rlp = "f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23".from_hex().unwrap(); + let header_rlp: Vec = "f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23".from_hex().unwrap(); let header: Header = rlp::decode(&header_rlp).expect("error decoding header"); let encoded_header = rlp::encode(&header); @@ -410,7 +410,7 @@ mod tests { fn reject_header_with_large_timestamp() { // that's rlp of block header created with ethash engine. // The encoding contains a large timestamp (295147905179352825856) - let header_rlp = "f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d891000000000000000000080a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23".from_hex().unwrap(); + let header_rlp: Vec = "f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d891000000000000000000080a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23".from_hex().unwrap(); // This should fail decoding timestamp let header: Result = rlp::decode(&header_rlp); diff --git a/ethcore/types/src/receipt.rs b/ethcore/types/src/receipt.rs index 82c4763b172..b630c048a32 100644 --- a/ethcore/types/src/receipt.rs +++ b/ethcore/types/src/receipt.rs @@ -170,13 +170,15 @@ pub struct LocalizedReceipt { #[cfg(test)] mod tests { + use std::str::FromStr; + use super::{Receipt, TransactionOutcome, Address, H256}; use log_entry::LogEntry; - use std::str::FromStr; + use rustc_hex::FromHex; #[test] fn test_no_state_root() { - let expected = ::rustc_hex::FromHex::from_hex("f9014183040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let expected: Vec = FromHex::from_hex("f9014183040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap(); let r = Receipt::new( TransactionOutcome::Unknown, 0x40cae.into(), @@ -191,7 +193,7 @@ mod tests { #[test] fn test_basic() { - let expected = ::rustc_hex::FromHex::from_hex("f90162a02f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee83040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let expected: Vec = FromHex::from_hex("f90162a02f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee83040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap(); let r = Receipt::new( TransactionOutcome::StateRoot(H256::from_str("2f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee").unwrap()), 0x40cae.into(), @@ -201,15 +203,15 @@ mod tests { data: vec![0u8; 32] }] ); - let encoded = ::rlp::encode(&r); + let encoded = rlp::encode(&r); assert_eq!(&encoded[..], &expected[..]); - let decoded: Receipt = ::rlp::decode(&encoded).expect("decoding receipt failed"); + let decoded: Receipt = rlp::decode(&encoded).expect("decoding receipt failed"); assert_eq!(decoded, r); } #[test] fn test_status_code() { - let expected = ::rustc_hex::FromHex::from_hex("f901428083040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let expected: Vec = FromHex::from_hex("f901428083040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap(); let r = Receipt::new( TransactionOutcome::StatusCode(0), 0x40cae.into(), @@ -219,9 +221,9 @@ mod tests { data: vec![0u8; 32] }] ); - let encoded = ::rlp::encode(&r); + let encoded = rlp::encode(&r); assert_eq!(&encoded[..], &expected[..]); - let decoded: Receipt = ::rlp::decode(&encoded).expect("decoding receipt failed"); + let decoded: Receipt = rlp::decode(&encoded).expect("decoding receipt failed"); assert_eq!(decoded, r); } } diff --git a/ethcore/types/src/transaction/transaction.rs b/ethcore/types/src/transaction/transaction.rs index c59050f1144..0fe4bb67aa4 100644 --- a/ethcore/types/src/transaction/transaction.rs +++ b/ethcore/types/src/transaction/transaction.rs @@ -548,14 +548,16 @@ impl From for PendingTransaction { #[cfg(test)] mod tests { + use std::str::FromStr; + use super::*; use ethereum_types::{U256, Address}; use hash::keccak; - use std::str::FromStr; + use rustc_hex::FromHex; #[test] fn sender_test() { - let bytes = ::rustc_hex::FromHex::from_hex("f85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804").unwrap(); + let bytes: Vec = FromHex::from_hex("f85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804").unwrap(); let t: UnverifiedTransaction = rlp::decode(&bytes).expect("decoding UnverifiedTransaction failed"); assert_eq!(t.data, b""); assert_eq!(t.gas, U256::from(0x5208u64)); @@ -675,11 +677,9 @@ mod tests { #[test] fn should_agree_with_vitalik() { - use rustc_hex::FromHex; - let test_vector = |tx_data: &str, address: &'static str| { - let signed = rlp::decode(&FromHex::from_hex(tx_data).unwrap()).expect("decoding tx data failed"); - let signed = SignedTransaction::new(signed).unwrap(); + let bytes = rlp::decode(&tx_data.from_hex::>().unwrap()).expect("decoding tx data failed"); + let signed = SignedTransaction::new(bytes).unwrap(); assert_eq!(signed.sender(), Address::from_str(&address[2..]).unwrap()); println!("chainid: {:?}", signed.chain_id()); }; diff --git a/ethcore/types/src/views/block.rs b/ethcore/types/src/views/block.rs index b9e23ab3b4f..5666a10318d 100644 --- a/ethcore/types/src/views/block.rs +++ b/ethcore/types/src/views/block.rs @@ -183,7 +183,7 @@ mod tests { #[test] fn test_block_view() { // that's rlp of block created with ethash engine. - let rlp = "f90261f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23f862f86002018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d01801ba03a347e72953c860f32b1eb2c78a680d8734b2ea08085d949d729479796f218d5a047ea6239d9e31ccac8af3366f5ca37184d26e7646e3191a3aeb81c4cf74de500c0".from_hex().unwrap(); + let rlp: Vec = "f90261f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23f862f86002018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d01801ba03a347e72953c860f32b1eb2c78a680d8734b2ea08085d949d729479796f218d5a047ea6239d9e31ccac8af3366f5ca37184d26e7646e3191a3aeb81c4cf74de500c0".from_hex().unwrap(); let view = view!(BlockView, &rlp); assert_eq!(view.hash(), H256::from_str("2c9747e804293bd3f1a986484343f23bc88fd5be75dfe9d5c2860aff61e6f259").unwrap()); diff --git a/ethcore/types/src/views/body.rs b/ethcore/types/src/views/body.rs index f8840f8d3d3..dbc6e880136 100644 --- a/ethcore/types/src/views/body.rs +++ b/ethcore/types/src/views/body.rs @@ -167,7 +167,7 @@ mod tests { #[test] fn test_block_view() { // that's rlp of block created with ethash engine. - let rlp = "f90261f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23f862f86002018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d01801ba03a347e72953c860f32b1eb2c78a680d8734b2ea08085d949d729479796f218d5a047ea6239d9e31ccac8af3366f5ca37184d26e7646e3191a3aeb81c4cf74de500c0".from_hex().unwrap(); + let rlp: Vec = "f90261f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23f862f86002018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d01801ba03a347e72953c860f32b1eb2c78a680d8734b2ea08085d949d729479796f218d5a047ea6239d9e31ccac8af3366f5ca37184d26e7646e3191a3aeb81c4cf74de500c0".from_hex().unwrap(); let body = block_to_body(&rlp); let view = view!(BodyView, &body); assert_eq!(view.transactions_count(), 1); diff --git a/ethcore/types/src/views/header.rs b/ethcore/types/src/views/header.rs index cb148f5649a..0e52dd8b1b8 100644 --- a/ethcore/types/src/views/header.rs +++ b/ethcore/types/src/views/header.rs @@ -127,9 +127,9 @@ mod tests { #[test] fn test_header_view() { // that's rlp of block header created with ethash engine. - let rlp = "f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23".from_hex().unwrap(); - let mix_hash = "a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd".from_hex().unwrap(); - let nonce = "88ab4e252a7e8c2a23".from_hex().unwrap(); + let rlp: Vec = "f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23".from_hex().unwrap(); + let mix_hash: Vec = "a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd".from_hex().unwrap(); + let nonce: Vec = "88ab4e252a7e8c2a23".from_hex().unwrap(); let view = view!(HeaderView, &rlp); assert_eq!(view.hash(), H256::from_str("2c9747e804293bd3f1a986484343f23bc88fd5be75dfe9d5c2860aff61e6f259").unwrap()); diff --git a/ethcore/types/src/views/transaction.rs b/ethcore/types/src/views/transaction.rs index ef3dd6c5bc4..67fcd31f1b8 100644 --- a/ethcore/types/src/views/transaction.rs +++ b/ethcore/types/src/views/transaction.rs @@ -90,14 +90,14 @@ mod tests { #[test] fn test_transaction_view() { - let rlp = "f87c80018261a894095e7baea6a6c7c4c2dfeb977efac326af552d870a9d00000000000000000000000000000000000000000000000000000000001ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804".from_hex().unwrap(); + let rlp: Vec = "f87c80018261a894095e7baea6a6c7c4c2dfeb977efac326af552d870a9d00000000000000000000000000000000000000000000000000000000001ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804".from_hex().unwrap(); let view = view!(TransactionView, &rlp); assert_eq!(view.nonce(), 0.into()); assert_eq!(view.gas_price(), 1.into()); assert_eq!(view.gas(), 0x61a8.into()); assert_eq!(view.value(), 0xa.into()); - assert_eq!(view.data(), "0000000000000000000000000000000000000000000000000000000000".from_hex().unwrap()); + assert_eq!(view.data(), "0000000000000000000000000000000000000000000000000000000000".from_hex::>().unwrap()); assert_eq!(view.r(), "48b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353".into()); assert_eq!(view.s(), "efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804".into()); assert_eq!(view.v(), 0x1b); From 99816abd57b216610451cb747c097e23aeae8bb2 Mon Sep 17 00:00:00 2001 From: Niklas Date: Tue, 28 Jan 2020 11:50:11 +0100 Subject: [PATCH 4/4] fix grumbles: impl LowerHex for encoded Header --- ethcore/spec/src/spec.rs | 2 +- ethcore/types/Cargo.toml | 4 +++- ethcore/types/src/encoded.rs | 11 +++++++---- ethcore/types/src/lib.rs | 4 +++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/ethcore/spec/src/spec.rs b/ethcore/spec/src/spec.rs index b1e7ae99a1d..1116fc626a5 100644 --- a/ethcore/spec/src/spec.rs +++ b/ethcore/spec/src/spec.rs @@ -250,7 +250,7 @@ impl From for SpecHardcodedSync { impl fmt::Display for SpecHardcodedSync { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!(f, "{{")?; - writeln!(f, r#""header": "{}","#, self.header.to_hex())?; + writeln!(f, r#""header": "{:x}","#, self.header)?; writeln!(f, r#""totalDifficulty": "{:?}""#, self.total_difficulty)?; // TODO: #11415 - fix trailing comma for CHTs writeln!(f, r#""CHTs": {:#?}"#, self.chts.iter().map(|x| format!("{:?}", x)).collect::>())?; diff --git a/ethcore/types/Cargo.toml b/ethcore/types/Cargo.toml index e4ca00f6899..9f2ae77fb84 100644 --- a/ethcore/types/Cargo.toml +++ b/ethcore/types/Cargo.toml @@ -18,9 +18,11 @@ parity-snappy = "0.1" patricia-trie-ethereum = { path = "../../util/patricia-trie-ethereum" } rlp = "0.4.0" rlp_derive = { path = "../../util/rlp-derive" } -rustc-hex = "2.0" unexpected = { path = "../../util/unexpected" } vm = { path = "../vm"} +[dev-dependencies] +rustc-hex = "2.0" + [features] test-helpers = [] diff --git a/ethcore/types/src/encoded.rs b/ethcore/types/src/encoded.rs index be0d580b964..a7c7b5c0cc1 100644 --- a/ethcore/types/src/encoded.rs +++ b/ethcore/types/src/encoded.rs @@ -29,7 +29,6 @@ use hash::keccak; use header::Header as FullHeader; use parity_util_mem::MallocSizeOf; use rlp::{self, Rlp, RlpStream}; -use rustc_hex::ToHex; use transaction::UnverifiedTransaction; use views::{self, BlockView, HeaderView, BodyView}; use BlockNumber; @@ -59,10 +58,14 @@ impl Header { /// Consume the view and return the raw bytes. pub fn into_inner(self) -> Vec { self.0 } +} - /// Get the hexadecimal representation of the header - pub fn to_hex(&self) -> String { - self.0.to_hex() +impl std::fmt::LowerHex for Header { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for byte in &self.0 { + write!(f, "{:02x}", byte)?; + } + Ok(()) } } diff --git a/ethcore/types/src/lib.rs b/ethcore/types/src/lib.rs index 2683b6a614d..5b664e9dc20 100644 --- a/ethcore/types/src/lib.rs +++ b/ethcore/types/src/lib.rs @@ -44,7 +44,6 @@ extern crate parity_bytes as bytes; extern crate patricia_trie_ethereum as ethtrie; extern crate parity_snappy; extern crate rlp; -extern crate rustc_hex; extern crate unexpected; #[macro_use] @@ -52,6 +51,9 @@ extern crate rlp_derive; extern crate parity_util_mem; extern crate parity_util_mem as malloc_size_of; +#[cfg(test)] +extern crate rustc_hex; + #[macro_use] pub mod views;