2016-09-06 15:31:13 +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/>.
|
|
|
|
|
|
|
|
use util::*;
|
|
|
|
use ethcore::snapshot::{SnapshotService, ManifestData, RestorationStatus};
|
|
|
|
use ethcore::header::BlockNumber;
|
|
|
|
use ethcore::client::{EachBlockWith};
|
|
|
|
use super::helpers::*;
|
2016-10-29 13:07:06 +02:00
|
|
|
use SyncConfig;
|
2016-09-06 15:31:13 +02:00
|
|
|
|
|
|
|
pub struct TestSnapshotService {
|
|
|
|
manifest: Option<ManifestData>,
|
|
|
|
chunks: HashMap<H256, Bytes>,
|
2016-10-28 16:10:30 +02:00
|
|
|
canon_hashes: Mutex<HashMap<u64, H256>>,
|
2016-09-06 15:31:13 +02:00
|
|
|
|
|
|
|
restoration_manifest: Mutex<Option<ManifestData>>,
|
|
|
|
state_restoration_chunks: Mutex<HashMap<H256, Bytes>>,
|
|
|
|
block_restoration_chunks: Mutex<HashMap<H256, Bytes>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestSnapshotService {
|
|
|
|
pub fn new() -> TestSnapshotService {
|
|
|
|
TestSnapshotService {
|
|
|
|
manifest: None,
|
|
|
|
chunks: HashMap::new(),
|
2016-10-28 16:10:30 +02:00
|
|
|
canon_hashes: Mutex::new(HashMap::new()),
|
2016-09-06 15:31:13 +02:00
|
|
|
restoration_manifest: Mutex::new(None),
|
|
|
|
state_restoration_chunks: Mutex::new(HashMap::new()),
|
|
|
|
block_restoration_chunks: Mutex::new(HashMap::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_snapshot(num_chunks: usize, block_hash: H256, block_number: BlockNumber) -> TestSnapshotService {
|
|
|
|
let num_state_chunks = num_chunks / 2;
|
|
|
|
let num_block_chunks = num_chunks - num_state_chunks;
|
|
|
|
let state_chunks: Vec<Bytes> = (0..num_state_chunks).map(|_| H256::random().to_vec()).collect();
|
|
|
|
let block_chunks: Vec<Bytes> = (0..num_block_chunks).map(|_| H256::random().to_vec()).collect();
|
|
|
|
let manifest = ManifestData {
|
|
|
|
state_hashes: state_chunks.iter().map(|data| data.sha3()).collect(),
|
|
|
|
block_hashes: block_chunks.iter().map(|data| data.sha3()).collect(),
|
|
|
|
state_root: H256::new(),
|
|
|
|
block_number: block_number,
|
|
|
|
block_hash: block_hash,
|
|
|
|
};
|
|
|
|
let mut chunks: HashMap<H256, Bytes> = state_chunks.into_iter().map(|data| (data.sha3(), data)).collect();
|
|
|
|
chunks.extend(block_chunks.into_iter().map(|data| (data.sha3(), data)));
|
|
|
|
TestSnapshotService {
|
|
|
|
manifest: Some(manifest),
|
|
|
|
chunks: chunks,
|
2016-10-28 16:10:30 +02:00
|
|
|
canon_hashes: Mutex::new(HashMap::new()),
|
2016-09-06 15:31:13 +02:00
|
|
|
restoration_manifest: Mutex::new(None),
|
|
|
|
state_restoration_chunks: Mutex::new(HashMap::new()),
|
|
|
|
block_restoration_chunks: Mutex::new(HashMap::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SnapshotService for TestSnapshotService {
|
|
|
|
fn manifest(&self) -> Option<ManifestData> {
|
|
|
|
self.manifest.as_ref().cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn chunk(&self, hash: H256) -> Option<Bytes> {
|
|
|
|
self.chunks.get(&hash).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn status(&self) -> RestorationStatus {
|
2016-09-16 23:03:26 +02:00
|
|
|
match *self.restoration_manifest.lock() {
|
|
|
|
Some(ref manifest) if self.state_restoration_chunks.lock().len() == manifest.state_hashes.len() &&
|
2016-09-06 15:31:13 +02:00
|
|
|
self.block_restoration_chunks.lock().len() == manifest.block_hashes.len() => RestorationStatus::Inactive,
|
2016-10-18 18:16:00 +02:00
|
|
|
Some(ref manifest) => RestorationStatus::Ongoing {
|
|
|
|
state_chunks: manifest.state_hashes.len() as u32,
|
|
|
|
block_chunks: manifest.block_hashes.len() as u32,
|
2016-09-06 15:31:13 +02:00
|
|
|
state_chunks_done: self.state_restoration_chunks.lock().len() as u32,
|
|
|
|
block_chunks_done: self.block_restoration_chunks.lock().len() as u32,
|
|
|
|
},
|
2016-09-16 23:03:26 +02:00
|
|
|
None => RestorationStatus::Inactive,
|
2016-09-06 15:31:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn begin_restore(&self, manifest: ManifestData) {
|
|
|
|
*self.restoration_manifest.lock() = Some(manifest);
|
|
|
|
self.state_restoration_chunks.lock().clear();
|
|
|
|
self.block_restoration_chunks.lock().clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn abort_restore(&self) {
|
|
|
|
*self.restoration_manifest.lock() = None;
|
|
|
|
self.state_restoration_chunks.lock().clear();
|
|
|
|
self.block_restoration_chunks.lock().clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_state_chunk(&self, hash: H256, chunk: Bytes) {
|
2016-09-16 23:03:26 +02:00
|
|
|
if self.restoration_manifest.lock().as_ref().map_or(false, |m| m.state_hashes.iter().any(|h| h == &hash)) {
|
2016-09-06 15:31:13 +02:00
|
|
|
self.state_restoration_chunks.lock().insert(hash, chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_block_chunk(&self, hash: H256, chunk: Bytes) {
|
2016-09-16 23:03:26 +02:00
|
|
|
if self.restoration_manifest.lock().as_ref().map_or(false, |m| m.block_hashes.iter().any(|h| h == &hash)) {
|
2016-09-06 15:31:13 +02:00
|
|
|
self.block_restoration_chunks.lock().insert(hash, chunk);
|
|
|
|
}
|
|
|
|
}
|
2016-10-28 16:10:30 +02:00
|
|
|
|
|
|
|
fn provide_canon_hashes(&self, hashes: &[(u64, H256)]) {
|
|
|
|
self.canon_hashes.lock().extend(hashes.iter().cloned());
|
|
|
|
}
|
2016-09-06 15:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn snapshot_sync() {
|
|
|
|
::env_logger::init().ok();
|
2016-10-29 13:07:06 +02:00
|
|
|
let mut config = SyncConfig::default();
|
|
|
|
config.warp_sync = true;
|
|
|
|
let mut net = TestNet::new_with_config(5, config);
|
|
|
|
let snapshot_service = Arc::new(TestSnapshotService::new_with_snapshot(16, H256::new(), 500000));
|
|
|
|
for i in 0..4 {
|
|
|
|
net.peer_mut(i).snapshot_service = snapshot_service.clone();
|
|
|
|
net.peer_mut(i).chain.add_blocks(1, EachBlockWith::Nothing);
|
|
|
|
}
|
|
|
|
net.sync_steps(50);
|
|
|
|
assert_eq!(net.peer(4).snapshot_service.state_restoration_chunks.lock().len(), net.peer(0).snapshot_service.manifest.as_ref().unwrap().state_hashes.len());
|
|
|
|
assert_eq!(net.peer(4).snapshot_service.block_restoration_chunks.lock().len(), net.peer(0).snapshot_service.manifest.as_ref().unwrap().block_hashes.len());
|
2016-09-06 15:31:13 +02:00
|
|
|
}
|
|
|
|
|