Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
idiomatic changes to PodState (#10834)
Browse files Browse the repository at this point in the history
  • Loading branch information
debris authored and dvdplm committed Jul 3, 2019
1 parent 895574b commit 8d24b4e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 35 deletions.
16 changes: 2 additions & 14 deletions ethcore/src/pod_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

//! Account system expressed in Plain Old Data.

use std::fmt;
use std::collections::BTreeMap;
use itertools::Itertools;
use hash::{keccak};
Expand Down Expand Up @@ -53,7 +52,8 @@ pub struct PodAccount {
fn opt_bytes_to_hex<S>(opt_bytes: &Option<Bytes>, serializer: S) -> Result<S::Ok, S::Error>
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 {
Expand Down Expand Up @@ -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
/// if they are the same.
pub fn diff_pod(pre: Option<&PodAccount>, post: Option<&PodAccount>) -> Option<AccountDiff> {
Expand Down
31 changes: 10 additions & 21 deletions ethcore/src/pod_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -30,12 +28,6 @@ use ethjson;
pub struct PodState(BTreeMap<Address, PodAccount>);

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.
pub fn get(&self) -> &BTreeMap<Address, PodAccount> { &self.0 }

Expand Down Expand Up @@ -65,31 +57,28 @@ impl From<ethjson::spec::State> 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<BTreeMap<Address, PodAccount>> for PodState {
fn from(s: BTreeMap<Address, PodAccount>) -> 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()
}
}

#[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]
Expand All @@ -102,15 +91,15 @@ 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()),
code: Diff::Died(vec![]),
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()),
Expand Down

0 comments on commit 8d24b4e

Please sign in to comment.