Fix Determination of state roots.

This commit is contained in:
Gav Wood 2016-01-14 02:09:43 +01:00
parent 035b29c481
commit 59e2df3e40
2 changed files with 12 additions and 7 deletions

View File

@ -459,7 +459,7 @@ impl Account {
/// Get (and cache) the contents of the trie's storage at `key`. /// Get (and cache) the contents of the trie's storage at `key`.
pub fn storage_at(&self, db: &HashDB, key: &H256) -> H256 { pub fn storage_at(&self, db: &HashDB, key: &H256) -> H256 {
self.storage_overlay.borrow_mut().entry(key.clone()).or_insert_with(||{ self.storage_overlay.borrow_mut().entry(key.clone()).or_insert_with(||{
H256::from_slice(TrieDB::new(db, &self.storage_root).get(key.bytes()).unwrap_or(&[0u8;32][..])) H256::from_slice(SecTrieDB::new(db, &self.storage_root).get(key.bytes()).unwrap_or(&[0u8;32][..]))
}).clone() }).clone()
} }
@ -539,13 +539,13 @@ impl Account {
/// Commit the `storage_overlay` to the backing DB and update `storage_root`. /// Commit the `storage_overlay` to the backing DB and update `storage_root`.
pub fn commit_storage(&mut self, db: &mut HashDB) { pub fn commit_storage(&mut self, db: &mut HashDB) {
let mut t = TrieDBMut::new(db, &mut self.storage_root); let mut t = SecTrieDBMut::new(db, &mut self.storage_root);
for (k, v) in self.storage_overlay.borrow().iter() { for (k, v) in self.storage_overlay.borrow().iter() {
// cast key and value to trait type, // cast key and value to trait type,
// so we can call overloaded `to_bytes` method // so we can call overloaded `to_bytes` method
t.insert(k, v); t.insert(k, &encode(&U256::from(v.as_slice())));
} }
self.storage_overlay.borrow_mut().clear(); // self.storage_overlay.borrow_mut().clear();
} }
/// Commit any unsaved code. `code_hash` will always return the hash of the `code_cache` after this. /// Commit any unsaved code. `code_hash` will always return the hash of the `code_cache` after this.

View File

@ -50,14 +50,19 @@ fn do_json_test(json_data: &[u8]) -> Vec<String> {
//println!("Transaction: {:?}", t); //println!("Transaction: {:?}", t);
//println!("Env: {:?}", env); //println!("Env: {:?}", env);
{
let mut s = State::new_temp();
s.populate_from(post.clone());
s.commit();
assert_eq!(&post_state_root, s.root());
}
let mut s = State::new_temp(); let mut s = State::new_temp();
s.populate_from(pre); s.populate_from(pre);
let e = Executive::new(&mut s, &env, engine.deref()).transact(&t).unwrap(); s.apply(&env, engine.deref(), &t).unwrap();
println!("executed: {:?}", e);
let our_post = s.to_pod_map(); let our_post = s.to_pod_map();
s.commit();
if fail_unless(s.root() == &post_state_root) { if fail_unless(s.root() == &post_state_root) {
println!("FAILED {}. Diff:\n{}", name, pod_map_diff(&post, &our_post)); println!("FAILED {}. Diff:\n{}", name, pod_map_diff(&post, &our_post));
} }