2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-08-05 17:00:46 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-08-05 17:00:46 +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,
|
2016-08-05 17:00:46 +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/>.
|
2016-08-05 17:00:46 +02:00
|
|
|
|
2017-05-17 12:41:33 +02:00
|
|
|
//! PoW block chunker and rebuilder tests.
|
2016-08-05 17:00:46 +02:00
|
|
|
|
2018-04-19 11:52:54 +02:00
|
|
|
use error::{Error, ErrorKind};
|
2018-01-19 17:32:53 +01:00
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
|
use tempdir::TempDir;
|
2016-08-05 17:00:46 +02:00
|
|
|
|
2018-02-16 10:11:29 +01:00
|
|
|
use blockchain::{
|
|
|
|
generator::{BlockBuilder, BlockGenerator},
|
2018-05-16 08:58:01 +02:00
|
|
|
BlockChain, ExtrasInsert,
|
|
|
|
};
|
2017-04-19 20:31:53 +02:00
|
|
|
use snapshot::{
|
|
|
|
chunk_secondary,
|
2016-08-05 17:00:46 +02:00
|
|
|
io::{PackedReader, PackedWriter, SnapshotReader, SnapshotWriter},
|
|
|
|
Error as SnapshotError, Progress, SnapshotComponents,
|
|
|
|
};
|
|
|
|
|
2018-06-20 15:13:07 +02:00
|
|
|
use kvdb::DBTransaction;
|
2017-09-02 20:09:13 +02:00
|
|
|
use parking_lot::Mutex;
|
2017-10-10 20:01:27 +02:00
|
|
|
use snappy;
|
2018-06-20 15:13:07 +02:00
|
|
|
use test_helpers;
|
2016-08-05 17:00:46 +02:00
|
|
|
|
2017-08-22 11:24:56 +02:00
|
|
|
const SNAPSHOT_MODE: ::snapshot::PowSnapshot = ::snapshot::PowSnapshot {
|
|
|
|
blocks: 30000,
|
|
|
|
max_restore_blocks: 30000,
|
|
|
|
};
|
2017-04-24 13:14:50 +02:00
|
|
|
|
2016-08-05 17:00:46 +02:00
|
|
|
fn chunk_and_restore(amount: u64) {
|
2018-02-16 10:11:29 +01:00
|
|
|
let genesis = BlockBuilder::genesis();
|
|
|
|
let rest = genesis.add_blocks(amount as usize);
|
|
|
|
let generator = BlockGenerator::new(vec![rest]);
|
|
|
|
let genesis = genesis.last();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-09-26 14:19:08 +02:00
|
|
|
let engine = ::spec::Spec::new_test().engine;
|
2018-01-19 17:32:53 +01:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let snapshot_path = tempdir.path().join("SNAP");
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-06-20 15:13:07 +02:00
|
|
|
let old_db = test_helpers::new_db();
|
2018-07-30 11:45:10 +02:00
|
|
|
let bc = BlockChain::new(Default::default(), genesis.encoded().raw(), old_db.clone());
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-08-05 17:00:46 +02:00
|
|
|
// build the blockchain.
|
2017-04-19 20:31:53 +02:00
|
|
|
let mut batch = DBTransaction::new();
|
2018-02-16 10:11:29 +01:00
|
|
|
for block in generator {
|
2018-07-30 11:45:10 +02:00
|
|
|
bc.insert_block(
|
|
|
|
&mut batch,
|
|
|
|
block.encoded(),
|
|
|
|
vec![],
|
|
|
|
ExtrasInsert {
|
2018-05-16 08:58:01 +02:00
|
|
|
fork_choice: ::engines::ForkChoice::New,
|
|
|
|
is_finalized: false,
|
|
|
|
},
|
|
|
|
);
|
2016-08-05 17:00:46 +02:00
|
|
|
bc.commit();
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-06-20 15:13:07 +02:00
|
|
|
old_db.key_value().write(batch).unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-08-05 17:00:46 +02:00
|
|
|
let best_hash = bc.best_block_hash();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-08-05 17:00:46 +02:00
|
|
|
// snapshot it.
|
|
|
|
let writer = Mutex::new(PackedWriter::new(&snapshot_path).unwrap());
|
2017-04-19 20:31:53 +02:00
|
|
|
let block_hashes = chunk_secondary(
|
2017-04-24 13:14:50 +02:00
|
|
|
Box::new(SNAPSHOT_MODE),
|
2017-04-19 20:31:53 +02:00
|
|
|
&bc,
|
|
|
|
best_hash,
|
|
|
|
&writer,
|
|
|
|
&Progress::default(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-28 16:10:30 +02:00
|
|
|
let manifest = ::snapshot::ManifestData {
|
2017-03-24 14:02:04 +01:00
|
|
|
version: 2,
|
2016-08-05 17:00:46 +02:00
|
|
|
state_hashes: Vec::new(),
|
|
|
|
block_hashes: block_hashes,
|
2017-08-30 19:18:28 +02:00
|
|
|
state_root: ::hash::KECCAK_NULL_RLP,
|
2016-08-05 17:00:46 +02:00
|
|
|
block_number: amount,
|
|
|
|
block_hash: best_hash,
|
2016-10-28 16:10:30 +02:00
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-28 16:10:30 +02:00
|
|
|
writer.into_inner().finish(manifest.clone()).unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-08-05 17:00:46 +02:00
|
|
|
// restore it.
|
2018-06-20 15:13:07 +02:00
|
|
|
let new_db = test_helpers::new_db();
|
2018-07-30 11:45:10 +02:00
|
|
|
let new_chain = BlockChain::new(Default::default(), genesis.encoded().raw(), new_db.clone());
|
2017-04-24 13:14:50 +02:00
|
|
|
let mut rebuilder = SNAPSHOT_MODE
|
|
|
|
.rebuilder(new_chain, new_db.clone(), &manifest)
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-08-05 17:00:46 +02:00
|
|
|
let reader = PackedReader::new(&snapshot_path).unwrap().unwrap();
|
2016-11-13 13:52:53 +01:00
|
|
|
let flag = AtomicBool::new(true);
|
2016-08-05 17:00:46 +02:00
|
|
|
for chunk_hash in &reader.manifest().block_hashes {
|
|
|
|
let compressed = reader.chunk(*chunk_hash).unwrap();
|
|
|
|
let chunk = snappy::decompress(&compressed).unwrap();
|
2016-12-05 16:20:32 +01:00
|
|
|
rebuilder.feed(&chunk, engine.as_ref(), &flag).unwrap();
|
2016-08-05 17:00:46 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-06-28 13:17:36 +02:00
|
|
|
rebuilder.finalize(engine.as_ref()).unwrap();
|
2017-04-19 20:31:53 +02:00
|
|
|
drop(rebuilder);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-08-05 17:00:46 +02:00
|
|
|
// and test it.
|
2018-07-30 11:45:10 +02:00
|
|
|
let new_chain = BlockChain::new(Default::default(), genesis.encoded().raw(), new_db);
|
2016-08-05 17:00:46 +02:00
|
|
|
assert_eq!(new_chain.best_block_hash(), best_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-02-16 10:11:29 +01:00
|
|
|
fn chunk_and_restore_500() {
|
|
|
|
chunk_and_restore(500)
|
|
|
|
}
|
2016-08-05 17:00:46 +02:00
|
|
|
|
|
|
|
#[test]
|
2018-02-16 10:11:29 +01:00
|
|
|
fn chunk_and_restore_4k() {
|
|
|
|
chunk_and_restore(4000)
|
|
|
|
}
|
2016-11-13 13:52:53 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn checks_flag() {
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::H256;
|
2017-03-20 19:14:29 +01:00
|
|
|
use rlp::RlpStream;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-13 13:52:53 +01:00
|
|
|
let mut stream = RlpStream::new_list(5);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-13 13:52:53 +01:00
|
|
|
stream
|
|
|
|
.append(&100u64)
|
|
|
|
.append(&H256::default())
|
|
|
|
.append(&(!0u64));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-13 13:52:53 +01:00
|
|
|
stream.append_empty_data().append_empty_data();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-02-16 10:11:29 +01:00
|
|
|
let genesis = BlockBuilder::genesis();
|
2016-11-13 13:52:53 +01:00
|
|
|
let chunk = stream.out();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-06-20 15:13:07 +02:00
|
|
|
let db = test_helpers::new_db();
|
2017-09-26 14:19:08 +02:00
|
|
|
let engine = ::spec::Spec::new_test().engine;
|
2018-07-30 11:45:10 +02:00
|
|
|
let chain = BlockChain::new(
|
|
|
|
Default::default(),
|
|
|
|
genesis.last().encoded().raw(),
|
|
|
|
db.clone(),
|
|
|
|
);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-13 13:52:53 +01:00
|
|
|
let manifest = ::snapshot::ManifestData {
|
2017-03-24 14:02:04 +01:00
|
|
|
version: 2,
|
2016-11-13 13:52:53 +01:00
|
|
|
state_hashes: Vec::new(),
|
|
|
|
block_hashes: Vec::new(),
|
2017-08-30 19:18:28 +02:00
|
|
|
state_root: ::hash::KECCAK_NULL_RLP,
|
2016-11-13 13:52:53 +01:00
|
|
|
block_number: 102,
|
|
|
|
block_hash: H256::default(),
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-04-24 13:14:50 +02:00
|
|
|
let mut rebuilder = SNAPSHOT_MODE
|
|
|
|
.rebuilder(chain, db.clone(), &manifest)
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-05 16:20:32 +01:00
|
|
|
match rebuilder.feed(&chunk, engine.as_ref(), &AtomicBool::new(false)) {
|
2018-04-19 11:52:54 +02:00
|
|
|
Err(Error(ErrorKind::Snapshot(SnapshotError::RestorationAborted), _)) => {}
|
2016-11-13 13:52:53 +01:00
|
|
|
_ => panic!("Wrong result on abort flag set"),
|
|
|
|
}
|
2016-12-05 16:20:32 +01:00
|
|
|
}
|