-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Use im::HashMap for storage_changes #9465
Changes from 4 commits
e6ea681
fa155a6
ab2adfd
69a017b
987db0a
b9d3a5c
9a52788
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
|
||
use std::fmt; | ||
use std::sync::Arc; | ||
use std::collections::{HashMap, BTreeMap}; | ||
use std::collections::BTreeMap; | ||
use hash::{KECCAK_EMPTY, KECCAK_NULL_RLP, keccak}; | ||
use ethereum_types::{H256, U256, Address}; | ||
use error::Error; | ||
|
@@ -30,6 +30,7 @@ use trie::{Trie, Recorder}; | |
use ethtrie::{TrieFactory, TrieDB, SecTrieDB, Result as TrieResult}; | ||
use pod_account::*; | ||
use rlp::{RlpStream, encode}; | ||
use im::{HashMap as IMHashMap}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
use lru_cache::LruCache; | ||
use basic_account::BasicAccount; | ||
|
||
|
@@ -61,7 +62,7 @@ pub struct Account { | |
storage_cache: RefCell<LruCache<H256, H256>>, | ||
// Modified storage. Accumulates changes to storage made in `set_storage` | ||
// Takes precedence over `storage_cache`. | ||
storage_changes: HashMap<H256, H256>, | ||
storage_changes: IMHashMap<H256, H256>, | ||
// Code hash of the account. | ||
code_hash: H256, | ||
// Size of the account code. | ||
|
@@ -81,7 +82,7 @@ impl From<BasicAccount> for Account { | |
nonce: basic.nonce, | ||
storage_root: basic.storage_root, | ||
storage_cache: Self::empty_storage_cache(), | ||
storage_changes: HashMap::new(), | ||
storage_changes: IMHashMap::new(), | ||
code_hash: basic.code_hash, | ||
code_size: None, | ||
code_cache: Arc::new(vec![]), | ||
|
@@ -94,7 +95,7 @@ impl From<BasicAccount> for Account { | |
impl Account { | ||
#[cfg(test)] | ||
/// General constructor. | ||
pub fn new(balance: U256, nonce: U256, storage: HashMap<H256, H256>, code: Bytes) -> Account { | ||
pub fn new(balance: U256, nonce: U256, storage: IMHashMap<H256, H256>, code: Bytes) -> Account { | ||
Account { | ||
balance: balance, | ||
nonce: nonce, | ||
|
@@ -136,7 +137,7 @@ impl Account { | |
nonce: nonce, | ||
storage_root: KECCAK_NULL_RLP, | ||
storage_cache: Self::empty_storage_cache(), | ||
storage_changes: HashMap::new(), | ||
storage_changes: IMHashMap::new(), | ||
code_hash: KECCAK_EMPTY, | ||
code_cache: Arc::new(vec![]), | ||
code_size: Some(0), | ||
|
@@ -160,7 +161,7 @@ impl Account { | |
nonce: nonce, | ||
storage_root: KECCAK_NULL_RLP, | ||
storage_cache: Self::empty_storage_cache(), | ||
storage_changes: HashMap::new(), | ||
storage_changes: IMHashMap::new(), | ||
code_hash: KECCAK_EMPTY, | ||
code_cache: Arc::new(vec![]), | ||
code_size: None, | ||
|
@@ -184,7 +185,7 @@ impl Account { | |
} | ||
|
||
/// Reset this account's code and storage to given values. | ||
pub fn reset_code_and_storage(&mut self, code: Arc<Bytes>, storage: HashMap<H256, H256>) { | ||
pub fn reset_code_and_storage(&mut self, code: Arc<Bytes>, storage: IMHashMap<H256, H256>) { | ||
self.code_hash = keccak(&*code); | ||
self.code_cache = code; | ||
self.code_size = Some(self.code_cache.len()); | ||
|
@@ -359,7 +360,7 @@ impl Account { | |
pub fn storage_root(&self) -> Option<&H256> { if self.storage_is_clean() {Some(&self.storage_root)} else {None} } | ||
|
||
/// Return the storage overlay. | ||
pub fn storage_changes(&self) -> &HashMap<H256, H256> { &self.storage_changes } | ||
pub fn storage_changes(&self) -> &IMHashMap<H256, H256> { &self.storage_changes } | ||
|
||
/// Increment the nonce of the account by one. | ||
pub fn inc_nonce(&mut self) { | ||
|
@@ -381,7 +382,12 @@ impl Account { | |
/// Commit the `storage_changes` to the backing DB and update `storage_root`. | ||
pub fn commit_storage(&mut self, trie_factory: &TrieFactory, db: &mut HashDB<KeccakHasher>) -> TrieResult<()> { | ||
let mut t = trie_factory.from_existing(db, &mut self.storage_root)?; | ||
for (k, v) in self.storage_changes.drain() { | ||
let old_storage_changes = { | ||
let mut tmp = IMHashMap::new(); | ||
::std::mem::swap(&mut tmp, &mut self.storage_changes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not quite related to this specific PR, but I just wonder, what will happen if it'll fail to commit? Should we concern about this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then that error will be propagate out and cause the block import to fail. I think we handled that correctly in |
||
tmp | ||
}; | ||
for (k, v) in old_storage_changes { | ||
// cast key and value to trait type, | ||
// so we can call overloaded `to_bytes` method | ||
match v.is_zero() { | ||
|
@@ -428,7 +434,7 @@ impl Account { | |
nonce: self.nonce.clone(), | ||
storage_root: self.storage_root.clone(), | ||
storage_cache: Self::empty_storage_cache(), | ||
storage_changes: HashMap::new(), | ||
storage_changes: IMHashMap::new(), | ||
code_hash: self.code_hash.clone(), | ||
code_size: self.code_size.clone(), | ||
code_cache: self.code_cache.clone(), | ||
|
@@ -497,7 +503,7 @@ impl fmt::Debug for Account { | |
.field("balance", &self.balance) | ||
.field("nonce", &self.nonce) | ||
.field("code", &self.code()) | ||
.field("storage", &self.storage_changes.iter().collect::<BTreeMap<_, _>>()) | ||
.field("storage", &self.storage_changes.iter().cloned().collect::<BTreeMap<_, _>>()) | ||
.finish() | ||
} | ||
} | ||
|
@@ -613,7 +619,7 @@ mod tests { | |
|
||
#[test] | ||
fn rlpio() { | ||
let a = Account::new(69u8.into(), 0u8.into(), HashMap::new(), Bytes::new()); | ||
let a = Account::new(69u8.into(), 0u8.into(), IMHashMap::new(), Bytes::new()); | ||
let b = Account::from_rlp(&a.rlp()).unwrap(); | ||
assert_eq!(a.balance(), b.balance()); | ||
assert_eq!(a.nonce(), b.nonce()); | ||
|
@@ -623,7 +629,7 @@ mod tests { | |
|
||
#[test] | ||
fn new_account() { | ||
let a = Account::new(69u8.into(), 0u8.into(), HashMap::new(), Bytes::new()); | ||
let a = Account::new(69u8.into(), 0u8.into(), IMHashMap::new(), Bytes::new()); | ||
assert_eq!(a.rlp().to_hex(), "f8448045a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); | ||
assert_eq!(*a.balance(), 69u8.into()); | ||
assert_eq!(*a.nonce(), 0u8.into()); | ||
|
@@ -633,7 +639,7 @@ mod tests { | |
|
||
#[test] | ||
fn create_account() { | ||
let a = Account::new(69u8.into(), 0u8.into(), HashMap::new(), Bytes::new()); | ||
let a = Account::new(69u8.into(), 0u8.into(), IMHashMap::new(), Bytes::new()); | ||
assert_eq!(a.rlp().to_hex(), "f8448045a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe just
use im::HashMap as IMHashMap;