2020-01-17 14:27:28 +01:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
2019-01-07 11:33:07 +01:00
|
|
|
// This file is part of Parity Ethereum.
|
2018-06-20 15:13:07 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2018-06-20 15:13:07 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2018-06-20 15:13:07 +02:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2018-06-20 15:13:07 +02:00
|
|
|
|
|
|
|
//! Blooms migration from rocksdb to blooms-db
|
|
|
|
|
|
|
|
use std::path::Path;
|
|
|
|
use ethereum_types::Bloom;
|
2019-07-18 12:27:08 +02:00
|
|
|
use types::errors::EthcoreError as Error;
|
2018-06-20 15:13:07 +02:00
|
|
|
use rlp;
|
|
|
|
use super::kvdb_rocksdb::DatabaseConfig;
|
|
|
|
use super::open_database;
|
|
|
|
|
2018-11-30 05:08:20 +01:00
|
|
|
const LOG_BLOOMS_ELEMENTS_PER_INDEX: u64 = 16;
|
|
|
|
|
2018-06-20 15:13:07 +02:00
|
|
|
pub fn migrate_blooms<P: AsRef<Path>>(path: P, config: &DatabaseConfig) -> Result<(), Error> {
|
|
|
|
// init
|
|
|
|
let db = open_database(&path.as_ref().to_string_lossy(), config)?;
|
|
|
|
|
|
|
|
// possible optimization:
|
|
|
|
// pre-allocate space on disk for faster migration
|
|
|
|
|
|
|
|
// iterate over header blooms and insert them in blooms-db
|
|
|
|
// Some(3) -> COL_EXTRA
|
|
|
|
// 3u8 -> ExtrasIndex::BlocksBlooms
|
|
|
|
// 0u8 -> level 0
|
|
|
|
let blooms_iterator = db.key_value()
|
2019-12-20 12:27:38 +01:00
|
|
|
.iter_from_prefix(3, &[3u8, 0u8])
|
2018-06-20 15:13:07 +02:00
|
|
|
.filter(|(key, _)| key.len() == 6)
|
|
|
|
.take_while(|(key, _)| {
|
|
|
|
key[0] == 3u8 && key[1] == 0u8
|
|
|
|
})
|
|
|
|
.map(|(key, group)| {
|
2018-11-30 05:08:20 +01:00
|
|
|
let index =
|
2018-06-20 15:13:07 +02:00
|
|
|
(key[2] as u64) << 24 |
|
|
|
|
(key[3] as u64) << 16 |
|
|
|
|
(key[4] as u64) << 8 |
|
|
|
|
(key[5] as u64);
|
2018-11-30 05:08:20 +01:00
|
|
|
let number = index * LOG_BLOOMS_ELEMENTS_PER_INDEX;
|
2018-06-20 15:13:07 +02:00
|
|
|
|
|
|
|
let blooms = rlp::decode_list::<Bloom>(&group);
|
|
|
|
(number, blooms)
|
|
|
|
});
|
|
|
|
|
|
|
|
for (number, blooms) in blooms_iterator {
|
|
|
|
db.blooms().insert_blooms(number, blooms.iter())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over trace blooms and insert them in blooms-db
|
|
|
|
// Some(4) -> COL_TRACE
|
|
|
|
// 1u8 -> TraceDBIndex::BloomGroups
|
|
|
|
// 0u8 -> level 0
|
|
|
|
let trace_blooms_iterator = db.key_value()
|
2019-12-20 12:27:38 +01:00
|
|
|
.iter_from_prefix(4, &[1u8, 0u8])
|
2018-06-20 15:13:07 +02:00
|
|
|
.filter(|(key, _)| key.len() == 6)
|
|
|
|
.take_while(|(key, _)| {
|
|
|
|
key[0] == 1u8 && key[1] == 0u8
|
|
|
|
})
|
|
|
|
.map(|(key, group)| {
|
2018-11-30 05:08:20 +01:00
|
|
|
let index =
|
2018-06-20 15:13:07 +02:00
|
|
|
(key[2] as u64) |
|
|
|
|
(key[3] as u64) << 8 |
|
|
|
|
(key[4] as u64) << 16 |
|
|
|
|
(key[5] as u64) << 24;
|
2018-11-30 05:08:20 +01:00
|
|
|
let number = index * LOG_BLOOMS_ELEMENTS_PER_INDEX;
|
2018-06-20 15:13:07 +02:00
|
|
|
|
|
|
|
let blooms = rlp::decode_list::<Bloom>(&group);
|
|
|
|
(number, blooms)
|
|
|
|
});
|
|
|
|
|
|
|
|
for (number, blooms) in trace_blooms_iterator {
|
|
|
|
db.trace_blooms().insert_blooms(number, blooms.iter())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|