2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-10-03 12:02:43 +02:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
//! Bloom upgrade
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
use db::{COL_EXTRA, COL_HEADERS, COL_STATE};
|
|
|
|
use state_db::{ACCOUNT_BLOOM_SPACE, DEFAULT_ACCOUNT_PRESET, StateDB};
|
2017-09-06 20:47:45 +02:00
|
|
|
use trie::TrieDB;
|
2016-10-03 12:02:43 +02:00
|
|
|
use views::HeaderView;
|
|
|
|
use bloom_journal::Bloom;
|
2017-10-10 20:01:27 +02:00
|
|
|
use migration::{Error, Migration, Progress, Batch, Config};
|
2016-10-03 12:02:43 +02:00
|
|
|
use util::journaldb;
|
2017-09-04 16:36:49 +02:00
|
|
|
use bigint::hash::H256;
|
2017-09-06 20:47:45 +02:00
|
|
|
use trie::Trie;
|
2017-10-10 20:01:27 +02:00
|
|
|
use kvdb::{Database, DBTransaction};
|
2016-10-03 12:02:43 +02:00
|
|
|
|
|
|
|
/// Account bloom upgrade routine. If bloom already present, does nothing.
|
|
|
|
/// If database empty (no best block), does nothing.
|
|
|
|
/// Can be called on upgraded database with no issues (will do nothing).
|
|
|
|
pub fn generate_bloom(source: Arc<Database>, dest: &mut Database) -> Result<(), Error> {
|
|
|
|
trace!(target: "migration", "Account bloom upgrade started");
|
2016-12-27 12:53:56 +01:00
|
|
|
let best_block_hash = match source.get(COL_EXTRA, b"best")? {
|
2016-10-03 12:02:43 +02:00
|
|
|
// no migration needed
|
|
|
|
None => {
|
|
|
|
trace!(target: "migration", "No best block hash, skipping");
|
|
|
|
return Ok(());
|
|
|
|
},
|
|
|
|
Some(hash) => hash,
|
|
|
|
};
|
2016-12-27 12:53:56 +01:00
|
|
|
let best_block_header = match source.get(COL_HEADERS, &best_block_hash)? {
|
2016-10-03 12:02:43 +02:00
|
|
|
// no best block, nothing to do
|
|
|
|
None => {
|
|
|
|
trace!(target: "migration", "No best block header, skipping");
|
|
|
|
return Ok(())
|
|
|
|
},
|
|
|
|
Some(x) => x,
|
|
|
|
};
|
|
|
|
let state_root = HeaderView::new(&best_block_header).state_root();
|
|
|
|
|
|
|
|
trace!("Adding accounts bloom (one-time upgrade)");
|
|
|
|
let bloom_journal = {
|
|
|
|
let mut bloom = Bloom::new(ACCOUNT_BLOOM_SPACE, DEFAULT_ACCOUNT_PRESET);
|
|
|
|
// no difference what algorithm is passed, since there will be no writes
|
|
|
|
let state_db = journaldb::new(
|
|
|
|
source.clone(),
|
|
|
|
journaldb::Algorithm::OverlayRecent,
|
|
|
|
COL_STATE);
|
2016-12-27 12:53:56 +01:00
|
|
|
let account_trie = TrieDB::new(state_db.as_hashdb(), &state_root).map_err(|e| Error::Custom(format!("Cannot open trie: {:?}", e)))?;
|
|
|
|
for item in account_trie.iter().map_err(|_| Error::MigrationImpossible)? {
|
|
|
|
let (ref account_key, _) = item.map_err(|_| Error::MigrationImpossible)?;
|
2016-10-27 08:28:12 +02:00
|
|
|
let account_key_hash = H256::from_slice(account_key);
|
2016-10-03 12:02:43 +02:00
|
|
|
bloom.set(&*account_key_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
bloom.drain_journal()
|
|
|
|
};
|
|
|
|
|
|
|
|
trace!(target: "migration", "Generated {} bloom updates", bloom_journal.entries.len());
|
|
|
|
|
2017-02-20 17:21:55 +01:00
|
|
|
let mut batch = DBTransaction::new();
|
2016-12-27 12:53:56 +01:00
|
|
|
StateDB::commit_bloom(&mut batch, bloom_journal).map_err(|_| Error::Custom("Failed to commit bloom".to_owned()))?;
|
|
|
|
dest.write(batch)?;
|
2016-10-03 12:02:43 +02:00
|
|
|
|
|
|
|
trace!(target: "migration", "Finished bloom update");
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Account bloom migration.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ToV10 {
|
|
|
|
progress: Progress,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToV10 {
|
|
|
|
/// New v10 migration
|
|
|
|
pub fn new() -> ToV10 { ToV10 { progress: Progress::default() } }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Migration for ToV10 {
|
|
|
|
fn version(&self) -> u32 {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pre_columns(&self) -> Option<u32> { Some(5) }
|
|
|
|
|
|
|
|
fn columns(&self) -> Option<u32> { Some(6) }
|
|
|
|
|
|
|
|
fn migrate(&mut self, source: Arc<Database>, config: &Config, dest: &mut Database, col: Option<u32>) -> Result<(), Error> {
|
|
|
|
let mut batch = Batch::new(config, col);
|
2017-04-19 14:58:19 +02:00
|
|
|
for (key, value) in source.iter(col).into_iter().flat_map(|inner| inner) {
|
2016-10-03 12:02:43 +02:00
|
|
|
self.progress.tick();
|
2017-06-28 14:16:53 +02:00
|
|
|
batch.insert(key.into_vec(), value.into_vec(), dest)?;
|
2016-10-03 12:02:43 +02:00
|
|
|
}
|
2016-12-27 12:53:56 +01:00
|
|
|
batch.commit(dest)?;
|
2016-10-03 12:02:43 +02:00
|
|
|
|
|
|
|
if col == COL_STATE {
|
2016-12-27 12:53:56 +01:00
|
|
|
generate_bloom(source, dest)?;
|
2016-10-03 12:02:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|