      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339

    // Copyright 2015-2017 Parity Technologies (UK) Ltd.
    // This file is part of Parity.

    // Parity is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.

    // Parity is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.

    // You should have received a copy of the GNU General Public License
    // along with Parity.  If not, see <http://www.gnu.org/licenses/>.

    use std::fs;
    use std::fs::File;
    use std::io::{Read, Write, Error as IoError, ErrorKind};
    use std::path::{Path, PathBuf};
    use std::fmt::{Display, Formatter, Error as FmtError};
    use std::sync::Arc;
    use util::journaldb::Algorithm;
    use util::migration::{Manager as MigrationManager, Config as MigrationConfig, Error as MigrationError, Migration};
    use util::kvdb::{CompactionProfile, Database, DatabaseConfig};
    use ethcore::migrations;
    use ethcore::db;
    use ethcore::migrations::Extract;

    /// 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 = 10;
    /// First version of the consolidated database.
    const CONSOLIDATION_VERSION: u32 = 9;
    /// Defines how many items are migrated to the new version of database at once.
    const BATCH_SIZE: usize = 1024;
    /// Version file name.
    const VERSION_FILE_NAME: &'static str = "db_version";

    /// Migration related erorrs.
    #[derive(Debug)]
    pub enum Error {
    	/// Returned when current version cannot be read or guessed.
    	UnknownDatabaseVersion,
    	/// Migration does not support existing pruning algorithm.
    	UnsupportedPruningMethod,
    	/// Existing DB is newer than the known one.
    	FutureDBVersion,
    	/// Migration is not possible.
    	MigrationImpossible,
    	/// Migration unexpectadly failed.
    	MigrationFailed,
    	/// Internal migration error.
    	Internal(MigrationError),
    	/// Migration was completed succesfully,
    	/// but there was a problem with io.
    	Io(IoError),
    }

    impl Display for Error {
    	fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
    		let out = match *self {
    			Error::UnknownDatabaseVersion => "Current database version cannot be read".into(),
    			Error::UnsupportedPruningMethod => "Unsupported pruning method for database migration. Delete DB and resync.".into(),
    			Error::FutureDBVersion => "Database was created with newer client version. Upgrade your client or delete DB and resync.".into(),
    			Error::MigrationImpossible => format!("Database migration to version {} is not possible.", CURRENT_VERSION),
    			Error::MigrationFailed => "Database migration unexpectedly failed".into(),
    			Error::Internal(ref err) => format!("{}", err),
    			Error::Io(ref err) => format!("Unexpected io error on DB migration: {}.", err),
    		};

    		write!(f, "{}", out)
    	}
    }

    impl From<IoError> for Error {
    	fn from(err: IoError) -> Self {
    		Error::Io(err)
    	}
    }

    impl From<MigrationError> for Error {
    	fn from(err: MigrationError) -> Self {
    		match err {
    			MigrationError::Io(e) => Error::Io(e),
    			_ => Error::Internal(err),
    		}
    	}
    }

    /// Returns the version file path.
    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: &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),
    		Ok(mut file) => {
    			let mut s = String::new();
    			file.read_to_string(&mut s).map_err(|_| Error::UnknownDatabaseVersion)?;
    			u32::from_str_radix(&s, 10).map_err(|_| Error::UnknownDatabaseVersion)
    		},
    	}
    }

    /// Writes current database version to the file.
    /// Creates a new file if the version file does not exist yet.
    fn update_version(path: &Path) -> Result<(), Error> {
    	fs::create_dir_all(path)?;
    	let mut file = File::create(version_file_path(path))?;
    	file.write_all(format!("{}", CURRENT_VERSION).as_bytes())?;
    	Ok(())
    }

    /// Consolidated database path
    fn consolidated_database_path(path: &Path) -> PathBuf {
    	let mut state_path = path.to_owned();
    	state_path.push("db");
    	state_path
    }

    /// Database backup
    fn backup_database_path(path: &Path) -> PathBuf {
    	let mut backup_path = path.to_owned();
    	backup_path.pop();
    	backup_path.push("temp_backup");
    	backup_path
    }

    /// Default migration settings.
    pub fn default_migration_settings(compaction_profile: &CompactionProfile) -> MigrationConfig {
    	MigrationConfig {
    		batch_size: BATCH_SIZE,
    		compaction_profile: *compaction_profile,
    	}
    }

    /// Migrations on the consolidated database.
    fn consolidated_database_migrations(compaction_profile: &CompactionProfile) -> Result<MigrationManager, Error> {
    	let mut manager = MigrationManager::new(default_migration_settings(compaction_profile));
    	manager.add_migration(migrations::ToV10::new()).map_err(|_| Error::MigrationImpossible)?;
    	Ok(manager)
    }

    /// Consolidates legacy databases into single one.
    fn consolidate_database(
    	old_db_path: PathBuf,
    	new_db_path: PathBuf,
    	column: Option<u32>,
    	extract: Extract,
    	compaction_profile: &CompactionProfile) -> Result<(), Error> {
    	fn db_error(e: String) -> Error {
    		warn!("Cannot open Database for consolidation: {:?}", e);
    		Error::MigrationFailed
    	}

    	let mut migration = migrations::ToV9::new(column, extract);
    	let config = default_migration_settings(compaction_profile);
    	let mut db_config = DatabaseConfig {
    		max_open_files: 64,
    		cache_sizes: Default::default(),
    		compaction: config.compaction_profile,
    		columns: None,
    		wal: true,
    	};

    	let old_path_str = old_db_path.to_str().ok_or(Error::MigrationImpossible)?;
    	let new_path_str = new_db_path.to_str().ok_or(Error::MigrationImpossible)?;

    	let cur_db = Arc::new(Database::open(&db_config, old_path_str).map_err(db_error)?);
    	// open new DB with proper number of columns
    	db_config.columns = migration.columns();
    	let mut new_db = Database::open(&db_config, new_path_str).map_err(db_error)?;

    	// Migrate to new database (default column only)
    	migration.migrate(cur_db, &config, &mut new_db, None)?;

    	Ok(())
    }

    /// Migrates database at given position with given migration rules.
    fn migrate_database(version: u32, db_path: PathBuf, mut migrations: MigrationManager) -> Result<(), Error> {
    	// check if migration is needed
    	if !migrations.is_needed(version) {
    		return Ok(())
    	}

    	let backup_path = backup_database_path(&db_path);
    	// remove the backup dir if it exists
    	let _ = fs::remove_dir_all(&backup_path);

    	// migrate old database to the new one
    	let temp_path = migrations.execute(&db_path, version)?;

    	// create backup
    	fs::rename(&db_path, &backup_path)?;

    	// replace the old database with the new one
    	if let Err(err) = fs::rename(&temp_path, &db_path) {
    		// if something went wrong, bring back backup
    		fs::rename(&backup_path, &db_path)?;
    		return Err(err.into());
    	}

    	// remove backup
    	fs::remove_dir_all(&backup_path)?;

    	Ok(())
    }

    fn exists(path: &Path) -> bool {
    	fs::metadata(path).is_ok()
    }

    /// Migrates the database.
    pub fn migrate(path: &Path, pruning: Algorithm, compaction_profile: CompactionProfile) -> Result<(), Error> {
    	// read version file.
    	let version = current_version(path)?;

    	// migrate the databases.
    	// main db directory may already exists, so let's check if we have blocks dir
    	if version > CURRENT_VERSION {
    		return Err(Error::FutureDBVersion);
    	}

    	// We are in the latest version, yay!
    	if version == CURRENT_VERSION {
    		return Ok(())
    	}

    	// Perform pre-consolidation migrations
    	if version < CONSOLIDATION_VERSION && exists(&legacy::blocks_database_path(path)) {
    		println!("Migrating database from version {} to {}", version, CONSOLIDATION_VERSION);

    		migrate_database(version, legacy::extras_database_path(path), legacy::extras_database_migrations(&compaction_profile)?)?;
    		migrate_database(version, legacy::state_database_path(path), legacy::state_database_migrations(pruning, &compaction_profile)?)?;
    		migrate_database(version, legacy::blocks_database_path(path), legacy::blocks_database_migrations(&compaction_profile)?)?;

    		let db_path = consolidated_database_path(path);
    		// Remove the database dir (it shouldn't exist anyway, but it might when migration was interrupted)
    		let _ = fs::remove_dir_all(db_path.clone());
    		consolidate_database(legacy::blocks_database_path(path), db_path.clone(), db::COL_HEADERS, Extract::Header, &compaction_profile)?;
    		consolidate_database(legacy::blocks_database_path(path), db_path.clone(), db::COL_BODIES, Extract::Body, &compaction_profile)?;
    		consolidate_database(legacy::extras_database_path(path), db_path.clone(), db::COL_EXTRA, Extract::All, &compaction_profile)?;
    		consolidate_database(legacy::state_database_path(path), db_path.clone(), db::COL_STATE, Extract::All, &compaction_profile)?;
    		consolidate_database(legacy::trace_database_path(path), db_path.clone(), db::COL_TRACE, Extract::All, &compaction_profile)?;
    		let _ = fs::remove_dir_all(legacy::blocks_database_path(path));
    		let _ = fs::remove_dir_all(legacy::extras_database_path(path));
    		let _ = fs::remove_dir_all(legacy::state_database_path(path));
    		let _ = fs::remove_dir_all(legacy::trace_database_path(path));
    		println!("Migration finished");
    	}

    	// update version so we can apply post-consolidation migrations.
    	let version = ::std::cmp::max(CONSOLIDATION_VERSION, version);

    	// Further migrations
    	if version >= CONSOLIDATION_VERSION && version < CURRENT_VERSION && exists(&consolidated_database_path(path)) {
    		println!("Migrating database from version {} to {}", ::std::cmp::max(CONSOLIDATION_VERSION, version), CURRENT_VERSION);
    		migrate_database(version, consolidated_database_path(path), consolidated_database_migrations(&compaction_profile)?)?;
    		println!("Migration finished");
    	}

    	// update version file.
    	update_version(path)
    }

    /// Old migrations utilities
    mod legacy {
    	use super::*;
    	use std::path::{Path, PathBuf};
    	use util::journaldb::Algorithm;
    	use util::migration::{Manager as MigrationManager};
    	use util::kvdb::CompactionProfile;
    	use ethcore::migrations;

    	/// Blocks database path.
    	pub fn blocks_database_path(path: &Path) -> PathBuf {
    		let mut blocks_path = path.to_owned();
    		blocks_path.push("blocks");
    		blocks_path
    	}

    	/// Extras database path.
    	pub fn extras_database_path(path: &Path) -> PathBuf {
    		let mut extras_path = path.to_owned();
    		extras_path.push("extras");
    		extras_path
    	}

    	/// State database path.
    	pub fn state_database_path(path: &Path) -> PathBuf {
    		let mut state_path = path.to_owned();
    		state_path.push("state");
    		state_path
    	}

    	/// Trace database path.
    	pub fn trace_database_path(path: &Path) -> PathBuf {
    		let mut blocks_path = path.to_owned();
    		blocks_path.push("tracedb");
    		blocks_path
    	}

    	/// Migrations on the blocks database.
    	pub fn blocks_database_migrations(compaction_profile: &CompactionProfile) -> Result<MigrationManager, Error> {
    		let mut manager = MigrationManager::new(default_migration_settings(compaction_profile));
    		manager.add_migration(migrations::blocks::V8::default()).map_err(|_| Error::MigrationImpossible)?;
    		Ok(manager)
    	}

    	/// Migrations on the extras database.
    	pub fn extras_database_migrations(compaction_profile: &CompactionProfile) -> Result<MigrationManager, Error> {
    		let mut manager = MigrationManager::new(default_migration_settings(compaction_profile));
    		manager.add_migration(migrations::extras::ToV6).map_err(|_| Error::MigrationImpossible)?;
    		Ok(manager)
    	}

    	/// Migrations on the state database.
    	pub fn state_database_migrations(pruning: Algorithm, compaction_profile: &CompactionProfile) -> Result<MigrationManager, Error> {
    		let mut manager = MigrationManager::new(default_migration_settings(compaction_profile));
    		let res = match pruning {
    			Algorithm::Archive => manager.add_migration(migrations::state::ArchiveV7::default()),
    			Algorithm::OverlayRecent => manager.add_migration(migrations::state::OverlayRecentV7::default()),
    			_ => return Err(Error::UnsupportedPruningMethod),
    		};

    		res.map_err(|_| Error::MigrationImpossible)?;
    		Ok(manager)
    	}
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)