add a database migration for new accountdb

This commit is contained in:
Robert Habermeier
2016-07-04 12:47:03 +02:00
parent d42ea6b69e
commit 601ebcf3cc
5 changed files with 71 additions and 19 deletions

View File

@@ -2,4 +2,4 @@
mod v6;
pub use self::v6::ToV6;
pub use self::v6::ToV6;

View File

@@ -1,3 +1,4 @@
//! Database migrations.
pub mod extras;
pub mod state;

View File

@@ -0,0 +1,5 @@
//! State database migrations.
mod v7;
pub use self::v7::ToV7;

View File

@@ -0,0 +1,31 @@
use util::hash::{FixedHash, H256};
use util::migration::Migration;
use util::sha3::Hashable;
/// This migration migrates the state db to use an accountdb which ensures uniqueness
/// using an address' hash as opposed to the address itself.
pub struct ToV7;
impl Migration for ToV7 {
fn version(&self) -> u32 {
7
}
fn simple_migrate(&self, mut key: Vec<u8>, value: Vec<u8>) -> Option<(Vec<u8>, Vec<u8>)> {
let val_hash = value.sha3();
assert!(key.len() == 32); // all keys in the state db are hashes.
let key_h = H256::from_slice(&key[..]);
if key_h != val_hash {
// this is a key which has been xor'd with an address.
// recover the address
let address = key_h ^ val_hash;
let address_hash = address.sha3();
let new_key = address_hash ^ val_hash;
key.copy_from_slice(&new_key[..]);
}
// nothing to do here
Some((key, value))
}
}