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

removed redundant fmt::Display implementations #10806

Merged
merged 3 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ethcore/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2678,7 +2678,7 @@ mod tests {
state.kill_account(&a);

let diff = state.diff_from(original).unwrap();
let diff_map = diff.get();
let diff_map = diff.raw;
assert_eq!(diff_map.len(), 1);
assert!(diff_map.get(&a).is_some());
assert_eq!(diff_map.get(&a),
Expand Down Expand Up @@ -2709,7 +2709,7 @@ mod tests {
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_map = diff.get();
let diff_map = diff.raw;
assert_eq!(diff_map.len(), 1);
assert!(diff_map.get(&a).is_some());
assert_eq!(diff_map.get(&a),
Expand Down
96 changes: 9 additions & 87 deletions ethcore/types/src/account_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

//! Diff between two accounts.

use std::cmp::*;
use std::fmt;
use std::collections::BTreeMap;
use std::convert::TryFrom;
use ethereum_types::{BigEndianHash as _, H256, U256};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BigEndianHash is used here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code has been removed as well

use bytes::Bytes;
use ethereum_types::{H256, U256};

#[derive(Debug, PartialEq, Eq, Clone)]
/// Diff type for specifying a change (or not).
Expand All @@ -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.
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)]
Expand All @@ -62,83 +58,9 @@ pub struct AccountDiff {
/// Change in balance, allowed to be `Diff::Same`.
pub balance: Diff<U256>,
/// 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`.
pub code: Diff<Bytes>, // Allowed to be Same
pub code: Diff<Bytes>,
/// Change in storage, values are not allowed to be `Diff::Same`.
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(())
}
}
28 changes: 1 addition & 27 deletions ethcore/types/src/state_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

//! State diff module.

use std::fmt;
use std::ops::*;
use std::collections::BTreeMap;
use account_diff::AccountDiff;
use ethereum_types::Address;
use account_diff::*;

/// Expression for the delta between two system states. Encoded the
/// delta of every altered account.
Expand All @@ -29,27 +27,3 @@ 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.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
}
}
3 changes: 0 additions & 3 deletions ethcore/types/src/verification_queue_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ impl VerificationQueueInfo {
/// 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 }

/// 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
pub fn is_full(&self) -> bool {
self.unverified_queue_size + self.verified_queue_size + self.verifying_queue_size > self.max_queue_size ||
Expand Down