2016-06-10 12:19:50 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (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/>.
|
|
|
|
|
2016-06-13 20:00:00 +02:00
|
|
|
//! Snapshot creation helpers.
|
2016-06-10 12:19:50 +02:00
|
|
|
|
2016-06-11 19:28:18 +02:00
|
|
|
// Try to have chunks be around 16MB (before compression)
|
2016-06-10 12:19:50 +02:00
|
|
|
const PREFERRED_CHUNK_SIZE: usize = 16 * 1024 * 1024;
|
|
|
|
|
|
|
|
use std::collections::VecDeque;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
2016-06-10 13:10:12 +02:00
|
|
|
use std::path::Path;
|
2016-06-10 12:19:50 +02:00
|
|
|
|
2016-06-13 14:23:53 +02:00
|
|
|
use account_db::AccountDB;
|
2016-06-10 12:19:50 +02:00
|
|
|
use client::BlockChainClient;
|
2016-06-11 19:28:18 +02:00
|
|
|
use error::Error;
|
2016-06-10 12:19:50 +02:00
|
|
|
use ids::BlockID;
|
2016-06-10 13:10:12 +02:00
|
|
|
use views::BlockView;
|
2016-06-10 12:19:50 +02:00
|
|
|
|
2016-06-13 12:04:20 +02:00
|
|
|
use util::{Bytes, Hashable, HashDB, TrieDB};
|
2016-06-13 14:23:53 +02:00
|
|
|
use util::hash::{FixedHash, H256};
|
2016-06-13 12:04:20 +02:00
|
|
|
use util::numbers::U256;
|
|
|
|
use util::rlp::{DecoderError, Stream, Rlp, RlpStream, UntrustedRlp, View};
|
2016-06-10 12:19:50 +02:00
|
|
|
|
|
|
|
/// Used to build block chunks.
|
2016-06-13 16:29:26 +02:00
|
|
|
struct BlockChunker<'a> {
|
2016-06-10 12:19:50 +02:00
|
|
|
client: &'a BlockChainClient,
|
|
|
|
// block, receipt rlp pairs.
|
2016-06-13 16:21:23 +02:00
|
|
|
rlps: VecDeque<Bytes>,
|
2016-06-10 12:19:50 +02:00
|
|
|
genesis_hash: H256,
|
|
|
|
current_hash: H256,
|
2016-06-13 16:21:23 +02:00
|
|
|
hashes: Vec<H256>,
|
2016-06-10 12:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BlockChunker<'a> {
|
|
|
|
// Try to fill the buffers, moving backwards from current block hash.
|
|
|
|
// This will return true if it created a block chunk, false otherwise.
|
|
|
|
fn fill_buffers(&mut self) -> bool {
|
|
|
|
let mut loaded_size = 0;
|
2016-06-10 14:15:20 +02:00
|
|
|
let mut blocks_loaded = 0;
|
2016-06-10 12:19:50 +02:00
|
|
|
|
|
|
|
while loaded_size < PREFERRED_CHUNK_SIZE && self.current_hash != self.genesis_hash {
|
|
|
|
|
|
|
|
// skip compression for now
|
|
|
|
let block = self.client.block(BlockID::Hash(self.current_hash)).unwrap();
|
|
|
|
let receipts = self.client.block_receipts(&self.current_hash).unwrap();
|
|
|
|
|
2016-06-13 16:21:23 +02:00
|
|
|
let pair = {
|
|
|
|
let mut pair_stream = RlpStream::new_list(2);
|
|
|
|
pair_stream.append(&block).append(&receipts);
|
|
|
|
pair_stream.out()
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_loaded_size = loaded_size + pair.len();
|
2016-06-10 12:45:46 +02:00
|
|
|
|
2016-06-10 15:05:20 +02:00
|
|
|
// cut off the chunk if too large
|
|
|
|
if new_loaded_size > PREFERRED_CHUNK_SIZE {
|
|
|
|
break;
|
2016-06-10 12:19:50 +02:00
|
|
|
} else {
|
|
|
|
loaded_size = new_loaded_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.current_hash = BlockView::new(&block).header_view().parent_hash();
|
2016-06-10 14:15:20 +02:00
|
|
|
blocks_loaded += 1;
|
2016-06-10 12:19:50 +02:00
|
|
|
}
|
|
|
|
|
2016-06-10 14:15:20 +02:00
|
|
|
if blocks_loaded > 0 {
|
|
|
|
trace!(target: "pv64_snapshot", "prepared block chunk with {} blocks", blocks_loaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
loaded_size != 0
|
2016-06-10 12:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// write out the data in the buffers to a chunk on disk
|
2016-06-13 16:21:23 +02:00
|
|
|
fn write_chunk(&mut self, path: &Path) -> Result<(), Error> {
|
2016-06-10 15:05:20 +02:00
|
|
|
// Todo [rob]: compress raw data, put parent hash and block number into chunk.
|
2016-06-10 12:19:50 +02:00
|
|
|
let mut rlp_stream = RlpStream::new_list(self.rlps.len());
|
2016-06-13 16:21:23 +02:00
|
|
|
for pair in self.rlps.drain(..) {
|
|
|
|
rlp_stream.append(&pair);
|
2016-06-10 12:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let raw_data = rlp_stream.out();
|
2016-06-10 12:45:46 +02:00
|
|
|
let hash = raw_data.sha3();
|
|
|
|
|
2016-06-10 14:15:20 +02:00
|
|
|
trace!(target: "pv64_snapshot", "writing block chunk. hash: {}, size: {} bytes", hash.hex(), raw_data.len());
|
|
|
|
|
2016-06-10 12:45:46 +02:00
|
|
|
let mut file_path = path.to_owned();
|
2016-06-10 13:10:12 +02:00
|
|
|
file_path.push(hash.hex());
|
2016-06-10 12:19:50 +02:00
|
|
|
|
2016-06-13 16:21:23 +02:00
|
|
|
let mut file = try!(File::create(file_path));
|
|
|
|
try!(file.write_all(&raw_data));
|
2016-06-10 12:45:46 +02:00
|
|
|
|
2016-06-13 16:21:23 +02:00
|
|
|
self.hashes.push(hash);
|
|
|
|
Ok(())
|
2016-06-10 12:19:50 +02:00
|
|
|
}
|
2016-06-13 16:29:26 +02:00
|
|
|
}
|
2016-06-10 12:19:50 +02:00
|
|
|
|
2016-06-13 16:29:26 +02:00
|
|
|
/// Create and write out all block chunks to disk, returning a vector of all
|
|
|
|
/// the hashes of block chunks created.
|
|
|
|
///
|
|
|
|
/// The path parameter is the directory to store the block chunks in.
|
|
|
|
/// This function assumes the directory exists already.
|
|
|
|
pub fn chunk_blocks(client: &BlockChainClient, best_block_hash: H256, genesis_hash: H256, path: &Path) -> Result<Vec<H256>, Error> {
|
|
|
|
let mut chunker = BlockChunker {
|
|
|
|
client: client,
|
|
|
|
rlps: VecDeque::new(),
|
|
|
|
genesis_hash: genesis_hash,
|
|
|
|
current_hash: best_block_hash,
|
|
|
|
hashes: Vec::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
while chunker.fill_buffers() {
|
|
|
|
try!(chunker.write_chunk(path));
|
2016-06-10 12:19:50 +02:00
|
|
|
}
|
2016-06-13 16:29:26 +02:00
|
|
|
if chunker.rlps.len() != 0 {
|
|
|
|
try!(chunker.write_chunk(path));
|
|
|
|
}
|
|
|
|
Ok(chunker.hashes)
|
2016-06-10 17:19:55 +02:00
|
|
|
}
|
|
|
|
|
2016-06-11 19:28:18 +02:00
|
|
|
/// State trie chunker.
|
2016-06-13 16:29:26 +02:00
|
|
|
struct StateChunker<'a> {
|
2016-06-11 19:28:18 +02:00
|
|
|
hashes: Vec<H256>,
|
|
|
|
rlps: Vec<Bytes>,
|
|
|
|
cur_size: usize,
|
|
|
|
snapshot_path: &'a Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> StateChunker<'a> {
|
|
|
|
// Push a key, value pair to be encoded.
|
|
|
|
//
|
|
|
|
// If the buffer is greater than the desired chunk size,
|
|
|
|
// this will write out the data to disk.
|
2016-06-13 16:21:23 +02:00
|
|
|
fn push(&mut self, key: Bytes, value: Bytes) -> Result<(), Error> {
|
2016-06-13 12:04:20 +02:00
|
|
|
let pair = {
|
|
|
|
let mut stream = RlpStream::new_list(2);
|
|
|
|
stream.append(&key).append(&value);
|
|
|
|
stream.out()
|
|
|
|
};
|
2016-06-11 19:28:18 +02:00
|
|
|
|
|
|
|
if self.cur_size + pair.len() >= PREFERRED_CHUNK_SIZE {
|
2016-06-13 16:21:23 +02:00
|
|
|
try!(self.write_chunk());
|
2016-06-11 19:28:18 +02:00
|
|
|
}
|
|
|
|
|
2016-06-13 15:07:54 +02:00
|
|
|
self.cur_size += pair.len();
|
2016-06-11 19:28:18 +02:00
|
|
|
self.rlps.push(pair);
|
2016-06-13 16:21:23 +02:00
|
|
|
|
|
|
|
Ok(())
|
2016-06-11 19:28:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the buffer to disk, pushing the created chunk's hash to
|
|
|
|
// the list.
|
2016-06-13 16:21:23 +02:00
|
|
|
fn write_chunk(&mut self) -> Result<(), Error> {
|
2016-06-13 13:52:41 +02:00
|
|
|
trace!(target: "pv64_snapshot", "writing state chunk. uncompressed size: {}", self.cur_size);
|
|
|
|
|
2016-06-13 12:04:20 +02:00
|
|
|
let bytes = {
|
|
|
|
let mut stream = RlpStream::new();
|
|
|
|
stream.append(&&self.rlps[..]);
|
|
|
|
stream.out()
|
|
|
|
};
|
2016-06-11 19:28:18 +02:00
|
|
|
|
|
|
|
self.rlps.clear();
|
|
|
|
|
|
|
|
let hash = bytes.sha3();
|
|
|
|
|
|
|
|
let mut path = self.snapshot_path.to_owned();
|
|
|
|
path.push(hash.hex());
|
|
|
|
|
2016-06-13 16:21:23 +02:00
|
|
|
let mut file = try!(File::create(path));
|
|
|
|
try!(file.write_all(&bytes));
|
2016-06-11 19:28:18 +02:00
|
|
|
|
|
|
|
self.hashes.push(hash);
|
|
|
|
self.cur_size = 0;
|
2016-06-13 16:21:23 +02:00
|
|
|
|
|
|
|
Ok(())
|
2016-06-11 19:28:18 +02:00
|
|
|
}
|
2016-06-13 16:29:26 +02:00
|
|
|
}
|
2016-06-11 19:28:18 +02:00
|
|
|
|
2016-06-13 16:29:26 +02:00
|
|
|
/// Walk the given state database starting from the given root,
|
|
|
|
/// creating chunks and writing them out.
|
|
|
|
///
|
|
|
|
/// Returns a list of hashes of chunks created, or any error it may
|
|
|
|
/// have encountered.
|
|
|
|
pub fn chunk_state(db: &HashDB, root: &H256, path: &Path) -> Result<Vec<H256>, Error> {
|
|
|
|
let account_view = try!(TrieDB::new(db, &root));
|
2016-06-11 19:28:18 +02:00
|
|
|
|
2016-06-13 16:29:26 +02:00
|
|
|
let mut chunker = StateChunker {
|
|
|
|
hashes: Vec::new(),
|
|
|
|
rlps: Vec::new(),
|
|
|
|
cur_size: 0,
|
|
|
|
snapshot_path: path,
|
|
|
|
};
|
2016-06-13 13:52:41 +02:00
|
|
|
|
2016-06-13 16:29:26 +02:00
|
|
|
trace!(target: "pv64_snapshot", "beginning state chunking");
|
2016-06-13 12:04:20 +02:00
|
|
|
|
2016-06-13 16:29:26 +02:00
|
|
|
// account_key here is the address' hash.
|
|
|
|
for (account_key, account_data) in account_view.iter() {
|
|
|
|
let account = AccountReader::from_thin_rlp(account_data);
|
|
|
|
let account_key_hash = H256::from_slice(&account_key);
|
2016-06-13 14:23:53 +02:00
|
|
|
|
2016-06-13 16:29:26 +02:00
|
|
|
let account_db = AccountDB::from_hash(db, account_key_hash);
|
2016-06-13 12:04:20 +02:00
|
|
|
|
2016-06-13 16:29:26 +02:00
|
|
|
let fat_rlp = try!(account.to_fat_rlp(&account_db));
|
|
|
|
try!(chunker.push(account_key, fat_rlp));
|
|
|
|
}
|
2016-06-13 15:07:54 +02:00
|
|
|
|
2016-06-13 16:29:26 +02:00
|
|
|
if chunker.cur_size != 0 {
|
|
|
|
try!(chunker.write_chunk());
|
2016-06-13 12:04:20 +02:00
|
|
|
}
|
2016-06-13 16:29:26 +02:00
|
|
|
|
|
|
|
Ok(chunker.hashes)
|
2016-06-13 12:04:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// An alternate account structure, only used for reading the storage values
|
|
|
|
// out of the account as opposed to writing any.
|
|
|
|
struct AccountReader {
|
|
|
|
nonce: U256,
|
|
|
|
balance: U256,
|
|
|
|
storage_root: H256,
|
|
|
|
code_hash: H256,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AccountReader {
|
|
|
|
// deserialize the account from rlp.
|
2016-06-13 13:22:42 +02:00
|
|
|
fn from_thin_rlp(rlp: &[u8]) -> Self {
|
2016-06-13 12:04:20 +02:00
|
|
|
let r: Rlp = Rlp::new(rlp);
|
|
|
|
|
|
|
|
AccountReader {
|
|
|
|
nonce: r.val_at(0),
|
|
|
|
balance: r.val_at(1),
|
|
|
|
storage_root: r.val_at(2),
|
|
|
|
code_hash: r.val_at(3),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 13:22:42 +02:00
|
|
|
// walk the account's storage trie, returning an RLP item containing the
|
|
|
|
// account properties and the storage.
|
|
|
|
fn to_fat_rlp(&self, hash_db: &HashDB) -> Result<Bytes, Error> {
|
|
|
|
let db = try!(TrieDB::new(hash_db, &self.storage_root));
|
|
|
|
|
|
|
|
let mut pairs = Vec::new();
|
2016-06-11 19:28:18 +02:00
|
|
|
|
2016-06-13 12:04:20 +02:00
|
|
|
for (k, v) in db.iter() {
|
2016-06-13 13:22:42 +02:00
|
|
|
pairs.push((k, v));
|
2016-06-11 19:28:18 +02:00
|
|
|
}
|
|
|
|
|
2016-06-13 13:22:42 +02:00
|
|
|
let mut stream = RlpStream::new_list(pairs.len());
|
|
|
|
|
|
|
|
for (k, v) in pairs {
|
|
|
|
stream.begin_list(2).append(&k).append(&v);
|
|
|
|
}
|
|
|
|
|
|
|
|
let pairs_rlp = stream.out();
|
|
|
|
|
|
|
|
let mut account_stream = RlpStream::new_list(5);
|
|
|
|
account_stream.append(&self.nonce)
|
|
|
|
.append(&self.balance)
|
|
|
|
.append(&self.storage_root)
|
|
|
|
.append(&self.code_hash)
|
|
|
|
.append(&pairs_rlp);
|
|
|
|
|
|
|
|
Ok(account_stream.out())
|
2016-06-11 19:28:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:19:55 +02:00
|
|
|
/// Manifest data.
|
|
|
|
pub struct ManifestData {
|
|
|
|
/// List of state chunk hashes.
|
|
|
|
pub state_hashes: Vec<H256>,
|
|
|
|
/// List of block chunk hashes.
|
|
|
|
pub block_hashes: Vec<H256>,
|
|
|
|
/// The final, expected state root.
|
|
|
|
pub state_root: H256,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ManifestData {
|
|
|
|
/// Encode the manifest data to.
|
|
|
|
pub fn to_rlp(self) -> Bytes {
|
|
|
|
let mut stream = RlpStream::new_list(3);
|
|
|
|
stream.append(&self.state_hashes);
|
|
|
|
stream.append(&self.block_hashes);
|
|
|
|
stream.append(&self.state_root);
|
|
|
|
|
|
|
|
stream.out()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to restore manifest data from raw bytes interpreted as RLP.
|
|
|
|
pub fn from_rlp(raw: &[u8]) -> Result<Self, DecoderError> {
|
|
|
|
let decoder = UntrustedRlp::new(raw);
|
|
|
|
|
|
|
|
let state_hashes: Vec<H256> = try!(try!(decoder.at(0)).as_val());
|
|
|
|
let block_hashes: Vec<H256> = try!(try!(decoder.at(1)).as_val());
|
|
|
|
let state_root: H256 = try!(try!(decoder.at(2)).as_val());
|
|
|
|
|
|
|
|
Ok(ManifestData {
|
|
|
|
state_hashes: state_hashes,
|
|
|
|
block_hashes: block_hashes,
|
|
|
|
state_root: state_root,
|
|
|
|
})
|
|
|
|
}
|
2016-06-10 12:19:50 +02:00
|
|
|
}
|