state diff serialization
This commit is contained in:
parent
9aef8ba063
commit
3cca6c869e
@ -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)]
|
||||
|
@ -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<T> where T: Eq {
|
||||
pub enum Diff<T> 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<T> where T: Eq {
|
||||
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`.
|
||||
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.
|
||||
|
@ -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<Address, AccountDiff>);
|
||||
pub struct StateDiff {
|
||||
/// Raw diff key-value
|
||||
pub raw: BTreeMap<Address, AccountDiff>
|
||||
}
|
||||
|
||||
impl StateDiff {
|
||||
/// Get the actual data.
|
||||
pub fn get(&self) -> &BTreeMap<Address, AccountDiff> {
|
||||
&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<Address, AccountDiff>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
&self.raw
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user