removed redundant fmt::Display implementations (#10806)

* removed redundant fmt::Display implementations for Account and State Diff

* bring back is_full() method

* fix failing test
This commit is contained in:
Marek Kotewicz 2019-06-28 20:58:27 +08:00 committed by Andronik Ordian
parent decc9eaa85
commit e53bf9a95e
4 changed files with 12 additions and 119 deletions

View File

@ -2678,7 +2678,7 @@ mod tests {
state.kill_account(&a); state.kill_account(&a);
let diff = state.diff_from(original).unwrap(); let diff = state.diff_from(original).unwrap();
let diff_map = diff.get(); let diff_map = diff.raw;
assert_eq!(diff_map.len(), 1); assert_eq!(diff_map.len(), 1);
assert!(diff_map.get(&a).is_some()); assert!(diff_map.get(&a).is_some());
assert_eq!(diff_map.get(&a), assert_eq!(diff_map.get(&a),
@ -2709,7 +2709,7 @@ mod tests {
state.set_storage(&a, BigEndianHash::from_uint(&U256::from(1u64)), BigEndianHash::from_uint(&U256::from(100u64))).unwrap(); state.set_storage(&a, BigEndianHash::from_uint(&U256::from(1u64)), BigEndianHash::from_uint(&U256::from(100u64))).unwrap();
let diff = state.diff_from(original).unwrap(); let diff = state.diff_from(original).unwrap();
let diff_map = diff.get(); let diff_map = diff.raw;
assert_eq!(diff_map.len(), 1); assert_eq!(diff_map.len(), 1);
assert!(diff_map.get(&a).is_some()); assert!(diff_map.get(&a).is_some());
assert_eq!(diff_map.get(&a), assert_eq!(diff_map.get(&a),

View File

@ -16,12 +16,9 @@
//! Diff between two accounts. //! Diff between two accounts.
use std::cmp::*;
use std::fmt;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::convert::TryFrom;
use ethereum_types::{BigEndianHash as _, H256, U256};
use bytes::Bytes; use bytes::Bytes;
use ethereum_types::{H256, U256};
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
/// Diff type for specifying a change (or not). /// Diff type for specifying a change (or not).
@ -46,14 +43,13 @@ impl<T> Diff<T> {
} }
} }
/// Get the before value, if there is one.
pub fn pre(&self) -> Option<&T> { match *self { Diff::Died(ref x) | Diff::Changed(ref x, _) => Some(x), _ => None } }
/// Get the after value, if there is one.
pub fn post(&self) -> Option<&T> { match *self { Diff::Born(ref x) | Diff::Changed(_, ref x) => Some(x), _ => None } }
/// Determine whether there was a change or not. /// Determine whether there was a change or not.
pub fn is_same(&self) -> bool { match *self { Diff::Same => true, _ => false }} pub fn is_same(&self) -> bool {
match *self {
Diff::Same => true,
_ => false
}
}
} }
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
@ -62,83 +58,9 @@ pub struct AccountDiff {
/// Change in balance, allowed to be `Diff::Same`. /// Change in balance, allowed to be `Diff::Same`.
pub balance: Diff<U256>, pub balance: Diff<U256>,
/// Change in nonce, allowed to be `Diff::Same`. /// Change in nonce, allowed to be `Diff::Same`.
pub nonce: Diff<U256>, // Allowed to be Same pub nonce: Diff<U256>,
/// Change in code, allowed to be `Diff::Same`. /// Change in code, allowed to be `Diff::Same`.
pub code: Diff<Bytes>, // Allowed to be Same pub code: Diff<Bytes>,
/// Change in storage, values are not allowed to be `Diff::Same`. /// Change in storage, values are not allowed to be `Diff::Same`.
pub storage: BTreeMap<H256, Diff<H256>>, pub storage: BTreeMap<H256, Diff<H256>>,
} }
#[derive(Debug, PartialEq, Eq, Clone)]
/// Change in existance type.
// TODO: include other types of change.
pub enum Existance {
/// Item came into existance.
Born,
/// Item stayed in existance.
Alive,
/// Item went out of existance.
Died,
}
impl fmt::Display for Existance {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Existance::Born => write!(f, "+++")?,
Existance::Alive => write!(f, "***")?,
Existance::Died => write!(f, "XXX")?,
}
Ok(())
}
}
impl AccountDiff {
/// Get `Existance` projection.
pub fn existance(&self) -> Existance {
match self.balance {
Diff::Born(_) => Existance::Born,
Diff::Died(_) => Existance::Died,
_ => Existance::Alive,
}
}
}
// TODO: refactor into something nicer.
fn interpreted_hash(u: &H256) -> String {
let uint = u.into_uint();
if let Ok(n) = u64::try_from(uint) {
format!("{} = {:#x}", n, n)
} else {
format!("#{}", u)
}
}
impl fmt::Display for AccountDiff {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use bytes::ToPretty;
match self.nonce {
Diff::Born(ref x) => write!(f, " non {}", x)?,
Diff::Changed(ref pre, ref post) => write!(f, "#{} ({} {} {})", post, pre, if pre > post {"-"} else {"+"}, *max(pre, post) - * min(pre, post))?,
_ => {},
}
match self.balance {
Diff::Born(ref x) => write!(f, " bal {}", x)?,
Diff::Changed(ref pre, ref post) => write!(f, "${} ({} {} {})", post, pre, if pre > post {"-"} else {"+"}, *max(pre, post) - *min(pre, post))?,
_ => {},
}
if let Diff::Born(ref x) = self.code {
write!(f, " code {}", x.pretty())?;
}
write!(f, "\n")?;
for (k, dv) in &self.storage {
match *dv {
Diff::Born(ref v) => write!(f, " + {} => {}\n", interpreted_hash(k), interpreted_hash(v))?,
Diff::Changed(ref pre, ref post) => write!(f, " * {} => {} (was {})\n", interpreted_hash(k), interpreted_hash(post), interpreted_hash(pre))?,
Diff::Died(_) => write!(f, " X {}\n", interpreted_hash(k))?,
_ => {},
}
}
Ok(())
}
}

View File

@ -16,11 +16,9 @@
//! State diff module. //! State diff module.
use std::fmt;
use std::ops::*;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use account_diff::AccountDiff;
use ethereum_types::Address; use ethereum_types::Address;
use account_diff::*;
/// 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.
@ -29,27 +27,3 @@ pub struct StateDiff {
/// Raw diff key-value /// Raw diff key-value
pub raw: BTreeMap<Address, AccountDiff> pub raw: BTreeMap<Address, AccountDiff>
} }
impl StateDiff {
/// Get the actual data.
pub fn get(&self) -> &BTreeMap<Address, AccountDiff> {
&self.raw
}
}
impl fmt::Display for StateDiff {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (add, acc) in &self.raw {
write!(f, "{} {}: {}", acc.existance(), add, acc)?;
}
Ok(())
}
}
impl Deref for StateDiff {
type Target = BTreeMap<Address, AccountDiff>;
fn deref(&self) -> &Self::Target {
&self.raw
}
}

View File

@ -37,9 +37,6 @@ impl VerificationQueueInfo {
/// The total size of the queues. /// The total size of the queues.
pub fn total_queue_size(&self) -> usize { self.unverified_queue_size + self.verified_queue_size + self.verifying_queue_size } pub fn total_queue_size(&self) -> usize { self.unverified_queue_size + self.verified_queue_size + self.verifying_queue_size }
/// The size of the unverified and verifying queues.
pub fn incomplete_queue_size(&self) -> usize { self.unverified_queue_size + self.verifying_queue_size }
/// Indicates that queue is full /// Indicates that queue is full
pub fn is_full(&self) -> bool { pub fn is_full(&self) -> bool {
self.unverified_queue_size + self.verified_queue_size + self.verifying_queue_size > self.max_queue_size || self.unverified_queue_size + self.verified_queue_size + self.verifying_queue_size > self.max_queue_size ||