state diff serialization

This commit is contained in:
NikVolf 2016-06-29 19:56:47 +03:00
parent 9aef8ba063
commit 3cca6c869e
3 changed files with 13 additions and 9 deletions

View File

@ -72,7 +72,7 @@ impl fmt::Display for PodState {
/// 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(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)] #[cfg(test)]

View File

@ -17,10 +17,11 @@
//! Diff between two accounts. //! Diff between two accounts.
use util::*; 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). /// Diff type for specifying a change (or not).
pub enum Diff<T> where T: Eq { pub enum Diff<T> where T: Eq + BinaryConvertable {
/// Both sides are the same. /// Both sides are the same.
Same, Same,
/// Left (pre, source) side doesn't include value, right side (post, destination) does. /// Left (pre, source) side doesn't include value, right side (post, destination) does.
@ -31,7 +32,7 @@ pub enum Diff<T> where T: Eq {
Died(T), Died(T),
} }
impl<T> Diff<T> where T: Eq { impl<T> Diff<T> where T: Eq + BinaryConvertable {
/// Construct new object with given `pre` and `post`. /// 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) } } 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)] #[derive(Debug, PartialEq, Eq, Clone)]
/// Change in existance type. /// Change in existance type.
// TODO: include other types of change. // TODO: include other types of change.
pub enum Existance { pub enum Existance {
/// Item came into existance. /// Item came into existance.

View File

@ -22,18 +22,21 @@ use account_diff::*;
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
/// Expression for the delta between two system states. Encoded the /// Expression for the delta between two system states. Encoded the
/// delta of every altered account. /// delta of every altered account.
pub struct StateDiff (pub BTreeMap<Address, AccountDiff>); pub struct StateDiff {
/// Raw diff key-value
pub raw: BTreeMap<Address, AccountDiff>
}
impl StateDiff { impl StateDiff {
/// Get the actual data. /// Get the actual data.
pub fn get(&self) -> &BTreeMap<Address, AccountDiff> { pub fn get(&self) -> &BTreeMap<Address, AccountDiff> {
&self.0 &self.raw
} }
} }
impl fmt::Display for StateDiff { impl fmt::Display for StateDiff {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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)); try!(write!(f, "{} {}: {}", acc.existance(), add, acc));
} }
Ok(()) Ok(())
@ -44,6 +47,6 @@ impl Deref for StateDiff {
type Target = BTreeMap<Address, AccountDiff>; type Target = BTreeMap<Address, AccountDiff>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.raw
} }
} }