add a database migration for new accountdb
This commit is contained in:
parent
7df0aa2b60
commit
9b28e97ca8
@ -1,3 +1,4 @@
|
||||
//! Database migrations.
|
||||
|
||||
pub mod extras;
|
||||
pub mod state;
|
||||
|
5
ethcore/src/migrations/state/mod.rs
Normal file
5
ethcore/src/migrations/state/mod.rs
Normal file
@ -0,0 +1,5 @@
|
||||
//! State database migrations.
|
||||
|
||||
mod v7;
|
||||
|
||||
pub use self::v7::ToV7;
|
31
ethcore/src/migrations/state/v7.rs
Normal file
31
ethcore/src/migrations/state/v7.rs
Normal file
@ -0,0 +1,31 @@
|
||||
use util::hash::{FixedHash, H256};
|
||||
use util::migration::SimpleMigration;
|
||||
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 SimpleMigration 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))
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write, Error as IoError, ErrorKind};
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::fmt::{Display, Formatter, Error as FmtError};
|
||||
use util::migration::{Manager as MigrationManager, Config as MigrationConfig, Error as MigrationError};
|
||||
use ethcore::migrations;
|
||||
@ -25,7 +25,7 @@ use ethcore::migrations;
|
||||
/// Database is assumed to be at default version, when no version file is found.
|
||||
const DEFAULT_VERSION: u32 = 5;
|
||||
/// Current version of database models.
|
||||
const CURRENT_VERSION: u32 = 6;
|
||||
const CURRENT_VERSION: u32 = 7;
|
||||
/// Defines how many items are migrated to the new version of database at once.
|
||||
const BATCH_SIZE: usize = 1024;
|
||||
/// Version file name.
|
||||
@ -74,15 +74,15 @@ impl From<MigrationError> for Error {
|
||||
}
|
||||
|
||||
/// Returns the version file path.
|
||||
fn version_file_path(path: &PathBuf) -> PathBuf {
|
||||
let mut file_path = path.clone();
|
||||
fn version_file_path(path: &Path) -> PathBuf {
|
||||
let mut file_path = path.to_owned();
|
||||
file_path.push(VERSION_FILE_NAME);
|
||||
file_path
|
||||
}
|
||||
|
||||
/// Reads current database version from the file at given path.
|
||||
/// If the file does not exist returns `DEFAULT_VERSION`.
|
||||
fn current_version(path: &PathBuf) -> Result<u32, Error> {
|
||||
fn current_version(path: &Path) -> Result<u32, Error> {
|
||||
match File::open(version_file_path(path)) {
|
||||
Err(ref err) if err.kind() == ErrorKind::NotFound => Ok(DEFAULT_VERSION),
|
||||
Err(_) => Err(Error::UnknownDatabaseVersion),
|
||||
@ -96,7 +96,7 @@ fn current_version(path: &PathBuf) -> Result<u32, Error> {
|
||||
|
||||
/// Writes current database version to the file.
|
||||
/// Creates a new file if the version file does not exist yet.
|
||||
fn update_version(path: &PathBuf) -> Result<(), Error> {
|
||||
fn update_version(path: &Path) -> Result<(), Error> {
|
||||
try!(fs::create_dir_all(path));
|
||||
let mut file = try!(File::create(version_file_path(path)));
|
||||
try!(file.write_all(format!("{}", CURRENT_VERSION).as_bytes()));
|
||||
@ -104,22 +104,29 @@ fn update_version(path: &PathBuf) -> Result<(), Error> {
|
||||
}
|
||||
|
||||
/// Blocks database path.
|
||||
fn blocks_database_path(path: &PathBuf) -> PathBuf {
|
||||
let mut blocks_path = path.clone();
|
||||
fn blocks_database_path(path: &Path) -> PathBuf {
|
||||
let mut blocks_path = path.to_owned();
|
||||
blocks_path.push("blocks");
|
||||
blocks_path
|
||||
}
|
||||
|
||||
/// Extras database path.
|
||||
fn extras_database_path(path: &PathBuf) -> PathBuf {
|
||||
let mut extras_path = path.clone();
|
||||
fn extras_database_path(path: &Path) -> PathBuf {
|
||||
let mut extras_path = path.to_owned();
|
||||
extras_path.push("extras");
|
||||
extras_path
|
||||
}
|
||||
|
||||
/// State database path.
|
||||
fn state_database_path(path: &Path) -> PathBuf {
|
||||
let mut state_path = path.to_owned();
|
||||
state_path.push("state");
|
||||
state_path
|
||||
}
|
||||
|
||||
/// Database backup
|
||||
fn backup_database_path(path: &PathBuf) -> PathBuf {
|
||||
let mut backup_path = path.clone();
|
||||
fn backup_database_path(path: &Path) -> PathBuf {
|
||||
let mut backup_path = path.to_owned();
|
||||
backup_path.pop();
|
||||
backup_path.push("temp_backup");
|
||||
backup_path
|
||||
@ -132,19 +139,26 @@ fn default_migration_settings() -> MigrationConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/// Migrations on blocks database.
|
||||
/// Migrations on the blocks database.
|
||||
fn blocks_database_migrations() -> Result<MigrationManager, Error> {
|
||||
let manager = MigrationManager::new(default_migration_settings());
|
||||
Ok(manager)
|
||||
}
|
||||
|
||||
/// Migrations on extras database.
|
||||
/// Migrations on the extras database.
|
||||
fn extras_database_migrations() -> Result<MigrationManager, Error> {
|
||||
let mut manager = MigrationManager::new(default_migration_settings());
|
||||
try!(manager.add_migration(migrations::extras::ToV6).map_err(|_| Error::MigrationImpossible));
|
||||
Ok(manager)
|
||||
}
|
||||
|
||||
/// Migrations on the state database.
|
||||
fn state_database_migrations() -> Result<MigrationManager, Error> {
|
||||
let mut manager = MigrationManager::new(default_migration_settings());
|
||||
try!(manager.add_migration(migrations::state::ToV7).map_err(|_| Error::MigrationImpossible));
|
||||
Ok(manager)
|
||||
}
|
||||
|
||||
/// Migrates database at given position with given migration rules.
|
||||
fn migrate_database(version: u32, db_path: PathBuf, migrations: MigrationManager) -> Result<(), Error> {
|
||||
// check if migration is needed
|
||||
@ -175,12 +189,12 @@ fn migrate_database(version: u32, db_path: PathBuf, migrations: MigrationManager
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn exists(path: &PathBuf) -> bool {
|
||||
fn exists(path: &Path) -> bool {
|
||||
fs::metadata(path).is_ok()
|
||||
}
|
||||
|
||||
/// Migrates the database.
|
||||
pub fn migrate(path: &PathBuf) -> Result<(), Error> {
|
||||
pub fn migrate(path: &Path) -> Result<(), Error> {
|
||||
// read version file.
|
||||
let version = try!(current_version(path));
|
||||
|
||||
@ -190,6 +204,7 @@ pub fn migrate(path: &PathBuf) -> Result<(), Error> {
|
||||
println!("Migrating database from version {} to {}", version, CURRENT_VERSION);
|
||||
try!(migrate_database(version, blocks_database_path(path), try!(blocks_database_migrations())));
|
||||
try!(migrate_database(version, extras_database_path(path), try!(extras_database_migrations())));
|
||||
try!(migrate_database(version, state_database_path(path), try!(state_database_migrations())));
|
||||
println!("Migration finished");
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user