From 3cca6c869ef97ded8269bbadd7044f11239c584f Mon Sep 17 00:00:00 2001 From: NikVolf Date: Wed, 29 Jun 2016 19:56:47 +0300 Subject: [PATCH] state diff serialization --- ethcore/src/pod_state.rs | 2 +- ethcore/src/types/account_diff.rs | 9 +++++---- ethcore/src/types/state_diff.rs | 11 +++++++---- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/ethcore/src/pod_state.rs b/ethcore/src/pod_state.rs index 76dac214b..d99344adb 100644 --- a/ethcore/src/pod_state.rs +++ b/ethcore/src/pod_state.rs @@ -72,7 +72,7 @@ impl fmt::Display for PodState { /// Calculate and return diff between `pre` state and `post` state. pub fn diff_pod(pre: &PodState, post: &PodState) -> StateDiff { - StateDiff(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))).collect()) + 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))).collect() } } #[cfg(test)] diff --git a/ethcore/src/types/account_diff.rs b/ethcore/src/types/account_diff.rs index 49fc51110..d37d3c86a 100644 --- a/ethcore/src/types/account_diff.rs +++ b/ethcore/src/types/account_diff.rs @@ -17,10 +17,11 @@ //! Diff between two accounts. use util::*; +use ipc::binary::{BinaryConvertError, BinaryConvertable}; -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, Binary)] /// Diff type for specifying a change (or not). -pub enum Diff where T: Eq { +pub enum Diff where T: Eq + BinaryConvertable { /// Both sides are the same. Same, /// Left (pre, source) side doesn't include value, right side (post, destination) does. @@ -31,7 +32,7 @@ pub enum Diff where T: Eq { Died(T), } -impl Diff where T: Eq { +impl Diff where T: Eq + BinaryConvertable { /// Construct new object with given `pre` and `post`. pub fn new(pre: T, post: T) -> Self { if pre == post { Diff::Same } else { Diff::Changed(pre, post) } } @@ -59,7 +60,7 @@ pub struct AccountDiff { } #[derive(Debug, PartialEq, Eq, Clone)] -/// Change in existance type. +/// Change in existance type. // TODO: include other types of change. pub enum Existance { /// Item came into existance. diff --git a/ethcore/src/types/state_diff.rs b/ethcore/src/types/state_diff.rs index 4257d5b07..9ddb92cdd 100644 --- a/ethcore/src/types/state_diff.rs +++ b/ethcore/src/types/state_diff.rs @@ -22,18 +22,21 @@ use account_diff::*; #[derive(Debug, PartialEq, Eq, Clone)] /// Expression for the delta between two system states. Encoded the /// delta of every altered account. -pub struct StateDiff (pub BTreeMap); +pub struct StateDiff { + /// Raw diff key-value + pub raw: BTreeMap +} impl StateDiff { /// Get the actual data. pub fn get(&self) -> &BTreeMap { - &self.0 + &self.raw } } impl fmt::Display for StateDiff { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - for (add, acc) in &self.0 { + for (add, acc) in &self.raw { try!(write!(f, "{} {}: {}", acc.existance(), add, acc)); } Ok(()) @@ -44,6 +47,6 @@ impl Deref for StateDiff { type Target = BTreeMap; fn deref(&self) -> &Self::Target { - &self.0 + &self.raw } }