idiomatic changes to PodState (#10834)

This commit is contained in:
Marek Kotewicz 2019-07-03 16:57:50 +08:00 committed by David
parent 895574b774
commit 8d24b4e804
2 changed files with 12 additions and 35 deletions

View File

@ -16,7 +16,6 @@
//! Account system expressed in Plain Old Data. //! Account system expressed in Plain Old Data.
use std::fmt;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use itertools::Itertools; use itertools::Itertools;
use hash::{keccak}; use hash::{keccak};
@ -53,7 +52,8 @@ pub struct PodAccount {
fn opt_bytes_to_hex<S>(opt_bytes: &Option<Bytes>, serializer: S) -> Result<S::Ok, S::Error> fn opt_bytes_to_hex<S>(opt_bytes: &Option<Bytes>, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer 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 { impl PodAccount {
@ -124,18 +124,6 @@ impl From<ethjson::spec::Account> 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 /// Determine difference between two optionally existant `Account`s. Returns None
/// if they are the same. /// if they are the same.
pub fn diff_pod(pre: Option<&PodAccount>, post: Option<&PodAccount>) -> Option<AccountDiff> { pub fn diff_pod(pre: Option<&PodAccount>, post: Option<&PodAccount>) -> Option<AccountDiff> {

View File

@ -16,9 +16,7 @@
//! State of all accounts in the system expressed in Plain Old Data. //! State of all accounts in the system expressed in Plain Old Data.
use std::fmt;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use itertools::Itertools;
use ethereum_types::{H256, Address}; use ethereum_types::{H256, Address};
use triehash::sec_trie_root; use triehash::sec_trie_root;
use pod_account::{self, PodAccount}; use pod_account::{self, PodAccount};
@ -30,12 +28,6 @@ use ethjson;
pub struct PodState(BTreeMap<Address, PodAccount>); pub struct PodState(BTreeMap<Address, PodAccount>);
impl PodState { 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<Address, PodAccount>) -> PodState { PodState(m) }
/// Get the underlying map. /// Get the underlying map.
pub fn get(&self) -> &BTreeMap<Address, PodAccount> { &self.0 } pub fn get(&self) -> &BTreeMap<Address, PodAccount> { &self.0 }
@ -65,21 +57,18 @@ impl From<ethjson::spec::State> for PodState {
} }
} }
impl fmt::Display for PodState { impl From<BTreeMap<Address, PodAccount>> for PodState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn from(s: BTreeMap<Address, PodAccount>) -> Self {
for (add, acc) in &self.0 { PodState(s)
writeln!(f, "{} => {}", add, acc)?;
}
Ok(())
} }
} }
/// Calculate and return diff between `pre` state and `post` state. /// Calculate and return diff between `pre` state and `post` state.
pub fn diff_pod(pre: &PodState, post: &PodState) -> StateDiff { pub fn diff_pod(pre: &PodState, post: &PodState) -> StateDiff {
StateDiff { StateDiff {
raw: pre.get().keys() raw: pre.0.keys()
.merge(post.get().keys()) .chain(post.0.keys())
.filter_map(|acc| pod_account::diff_pod(pre.get().get(acc), post.get().get(acc)).map(|d| (acc.clone(), d))) .filter_map(|acc| pod_account::diff_pod(pre.0.get(acc), post.0.get(acc)).map(|d| (*acc, d)))
.collect() .collect()
} }
} }
@ -87,9 +76,9 @@ pub fn diff_pod(pre: &PodState, post: &PodState) -> StateDiff {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::collections::BTreeMap; use std::collections::BTreeMap;
use types::state_diff::*;
use types::account_diff::*;
use pod_account::PodAccount; use pod_account::PodAccount;
use types::account_diff::{AccountDiff, Diff};
use types::state_diff::StateDiff;
use super::{PodState, Address}; use super::{PodState, Address};
#[test] #[test]
@ -102,7 +91,7 @@ mod test {
storage: map![], 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{ Address::from_low_u64_be(1) => AccountDiff{
balance: Diff::Died(69.into()), balance: Diff::Died(69.into()),
nonce: Diff::Died(0.into()), nonce: Diff::Died(0.into()),
@ -110,7 +99,7 @@ mod test {
storage: map![], 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{ Address::from_low_u64_be(1) => AccountDiff{
balance: Diff::Born(69.into()), balance: Diff::Born(69.into()),
nonce: Diff::Born(0.into()), nonce: Diff::Born(0.into()),