From 48129bc6e2ae3d6875fa2498678086c86d8fb2a8 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 3 Jul 2019 15:39:29 +0800 Subject: [PATCH] idiomatic changes to PodState --- ethcore/src/pod_account.rs | 16 ++-------------- ethcore/src/pod_state.rs | 31 ++++++++++--------------------- 2 files changed, 12 insertions(+), 35 deletions(-) diff --git a/ethcore/src/pod_account.rs b/ethcore/src/pod_account.rs index 087577b02c6..196d627b124 100644 --- a/ethcore/src/pod_account.rs +++ b/ethcore/src/pod_account.rs @@ -16,7 +16,6 @@ //! Account system expressed in Plain Old Data. -use std::fmt; use std::collections::BTreeMap; use itertools::Itertools; use hash::{keccak}; @@ -53,7 +52,8 @@ pub struct PodAccount { fn opt_bytes_to_hex(opt_bytes: &Option, serializer: S) -> Result where S: Serializer { - serializer.collect_str(&format_args!("0x{}",opt_bytes.as_ref().map_or("".to_string(), |b|b.to_hex()))) + let readable = opt_bytes.as_ref().map(|b| b.to_hex()).unwrap_or_default(); + serializer.collect_str(&format_args!("0x{}", readable)) } impl PodAccount { @@ -124,18 +124,6 @@ impl From for PodAccount { } } -impl fmt::Display for PodAccount { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "(bal={}; nonce={}; code={} bytes, #{}; storage={} items)", - self.balance, - self.nonce, - self.code.as_ref().map_or(0, |c| c.len()), - self.code.as_ref().map_or_else(H256::zero, |c| keccak(c)), - self.storage.len(), - ) - } -} - /// Determine difference between two optionally existant `Account`s. Returns None /// if they are the same. pub fn diff_pod(pre: Option<&PodAccount>, post: Option<&PodAccount>) -> Option { diff --git a/ethcore/src/pod_state.rs b/ethcore/src/pod_state.rs index 1c5ecc8400e..406bcc05dc4 100644 --- a/ethcore/src/pod_state.rs +++ b/ethcore/src/pod_state.rs @@ -16,9 +16,7 @@ //! State of all accounts in the system expressed in Plain Old Data. -use std::fmt; use std::collections::BTreeMap; -use itertools::Itertools; use ethereum_types::{H256, Address}; use triehash::sec_trie_root; use pod_account::{self, PodAccount}; @@ -30,12 +28,6 @@ use ethjson; pub struct PodState(BTreeMap); impl PodState { - /// Contruct a new object from the `m`. - pub fn new() -> PodState { Default::default() } - - /// Contruct a new object from the `m`. - pub fn from(m: BTreeMap) -> PodState { PodState(m) } - /// Get the underlying map. pub fn get(&self) -> &BTreeMap { &self.0 } @@ -65,21 +57,18 @@ impl From for PodState { } } -impl fmt::Display for PodState { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - for (add, acc) in &self.0 { - writeln!(f, "{} => {}", add, acc)?; - } - Ok(()) +impl From> for PodState { + fn from(s: BTreeMap) -> Self { + PodState(s) } } /// Calculate and return diff between `pre` state and `post` state. pub fn diff_pod(pre: &PodState, post: &PodState) -> StateDiff { StateDiff { - raw: pre.get().keys() - .merge(post.get().keys()) - .filter_map(|acc| pod_account::diff_pod(pre.get().get(acc), post.get().get(acc)).map(|d| (acc.clone(), d))) + raw: pre.0.keys() + .chain(post.0.keys()) + .filter_map(|acc| pod_account::diff_pod(pre.0.get(acc), post.0.get(acc)).map(|d| (*acc, d))) .collect() } } @@ -87,9 +76,9 @@ pub fn diff_pod(pre: &PodState, post: &PodState) -> StateDiff { #[cfg(test)] mod test { use std::collections::BTreeMap; - use types::state_diff::*; - use types::account_diff::*; use pod_account::PodAccount; + use types::account_diff::{AccountDiff, Diff}; + use types::state_diff::StateDiff; use super::{PodState, Address}; #[test] @@ -102,7 +91,7 @@ mod test { storage: map![], } ]); - assert_eq!(super::diff_pod(&a, &PodState::new()), StateDiff { raw: map![ + assert_eq!(super::diff_pod(&a, &PodState::default()), StateDiff { raw: map![ Address::from_low_u64_be(1) => AccountDiff{ balance: Diff::Died(69.into()), nonce: Diff::Died(0.into()), @@ -110,7 +99,7 @@ mod test { storage: map![], } ]}); - assert_eq!(super::diff_pod(&PodState::new(), &a), StateDiff{ raw: map![ + assert_eq!(super::diff_pod(&PodState::default(), &a), StateDiff{ raw: map![ Address::from_low_u64_be(1) => AccountDiff{ balance: Diff::Born(69.into()), nonce: Diff::Born(0.into()),