openethereum/crates/ethcore/sync/src/tests/snapshot.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

228 lines
7.1 KiB
Rust
Raw Normal View History

2020-09-22 14:53:52 +02:00
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.
2020-09-22 14:53:52 +02:00
// OpenEthereum 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.
2020-09-22 14:53:52 +02:00
// OpenEthereum 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
2020-09-22 14:53:52 +02:00
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
2017-07-29 17:12:07 +02:00
use super::helpers::*;
use bytes::Bytes;
2017-07-29 17:12:07 +02:00
use ethcore::{
client::EachBlockWith,
snapshot::{CreationStatus, ManifestData, RestorationStatus, SnapshotService},
2017-08-31 11:35:41 +02:00
};
use ethereum_types::H256;
2017-08-31 11:35:41 +02:00
use hash::keccak;
use parking_lot::Mutex;
use std::{collections::HashMap, sync::Arc};
use types::BlockNumber;
use SyncConfig;
use WarpSync;
pub struct TestSnapshotService {
manifest: Option<ManifestData>,
chunks: HashMap<H256, Bytes>,
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(),
restoration_manifest: Mutex::new(None),
state_restoration_chunks: Mutex::new(HashMap::new()),
block_restoration_chunks: Mutex::new(HashMap::new()),
2020-08-05 06:08:03 +02:00
}
}
2020-08-05 06:08:03 +02:00
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 {
2017-03-24 14:29:03 +01:00
version: 2,
2017-08-31 11:35:41 +02:00
state_hashes: state_chunks.iter().map(|data| keccak(data)).collect(),
block_hashes: block_chunks.iter().map(|data| keccak(data)).collect(),
state_root: H256::new(),
block_number: block_number,
block_hash: block_hash,
};
2017-08-31 11:35:41 +02:00
let mut chunks: HashMap<H256, Bytes> = state_chunks
.into_iter()
.map(|data| (keccak(&data), data))
.collect();
chunks.extend(block_chunks.into_iter().map(|data| (keccak(&data), data)));
TestSnapshotService {
manifest: Some(manifest),
chunks: chunks,
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()
}
2020-08-05 06:08:03 +02:00
fn manifest_block(&self) -> Option<(u64, H256)> {
self.manifest
.as_ref()
.map(|manifest| (manifest.block_number, manifest.block_hash))
}
fn supported_versions(&self) -> Option<(u64, u64)> {
Some((1, 2))
}
2020-08-05 06:08:03 +02:00
fn completed_chunks(&self) -> Option<Vec<H256>> {
Some(vec![])
}
2020-08-05 06:08:03 +02:00
fn chunk(&self, hash: H256) -> Option<Bytes> {
self.chunks.get(&hash).cloned()
}
2020-08-05 06:08:03 +02:00
fn creation_status(&self) -> CreationStatus {
CreationStatus::Inactive
}
fn restoration_status(&self) -> RestorationStatus {
match *self.restoration_manifest.lock() {
Some(ref manifest)
if self.state_restoration_chunks.lock().len() == manifest.state_hashes.len()
&& self.block_restoration_chunks.lock().len()
== manifest.block_hashes.len() =>
2020-08-05 06:08:03 +02:00
{
RestorationStatus::Inactive
2020-08-05 06:08:03 +02:00
}
Some(ref manifest) => RestorationStatus::Ongoing {
block_number: 0,
state_chunks: manifest.state_hashes.len() as u32,
block_chunks: manifest.block_hashes.len() as u32,
state_chunks_done: self.state_restoration_chunks.lock().len() as u32,
block_chunks_done: self.block_restoration_chunks.lock().len() as u32,
},
None => RestorationStatus::Inactive,
2020-08-05 06:08:03 +02:00
}
}
2020-08-05 06:08:03 +02:00
fn begin_restore(&self, manifest: ManifestData) {
let mut restoration_manifest = self.restoration_manifest.lock();
2020-08-05 06:08:03 +02:00
if let Some(ref c_manifest) = *restoration_manifest {
if c_manifest.state_root == manifest.state_root {
return;
2020-08-05 06:08:03 +02:00
}
}
2020-08-05 06:08:03 +02:00
*restoration_manifest = Some(manifest);
self.state_restoration_chunks.lock().clear();
self.block_restoration_chunks.lock().clear();
}
2020-08-05 06:08:03 +02:00
fn abort_restore(&self) {
*self.restoration_manifest.lock() = None;
self.state_restoration_chunks.lock().clear();
self.block_restoration_chunks.lock().clear();
}
2020-08-05 06:08:03 +02:00
Beta 2.5.3 (#10776) * ethcore/res: activate atlantis classic hf on block 8772000 (#10766) * fix docker tags for publishing (#10741) * fix: aura don't add `SystemTime::now()` (#10720) This commit does the following: - Prevent overflow in `verify_timestamp()` by not adding `now` to found faulty timestamp - Use explicit `CheckedSystemTime::checked_add` to prevent potential consensus issues because SystemTime is platform depedent - remove `#[cfg(not(time_checked_add))]` conditional compilation * Update version * Treat empty account the same as non-exist accounts in EIP-1052 (#10775) * DevP2p: Get node IP address and udp port from Socket, if not included in PING packet (#10705) * get node IP address and udp port from Socket, if not included in PING packet * prevent bootnodes from being added to host nodes * code corrections * code corrections * code corrections * code corrections * docs * code corrections * code corrections * Apply suggestions from code review Co-Authored-By: David <dvdplm@gmail.com> * Add a way to signal shutdown to snapshotting threads (#10744) * Add a way to signal shutdown to snapshotting threads * Pass Progress to fat_rlps() so we can abort from there too. * Checking for abort in a single spot * Remove nightly-only weak/strong counts * fix warning * Fix tests * Add dummy impl to abort snapshots * Add another dummy impl for TestSnapshotService * Remove debugging code * Return error instead of the odd Ok(()) Switch to AtomicU64 * revert .as_bytes() change * fix build * fix build maybe
2019-06-25 15:38:29 +02:00
fn abort_snapshot(&self) {}
2020-08-05 06:08:03 +02:00
fn restore_state_chunk(&self, hash: H256, chunk: Bytes) {
if self
.restoration_manifest
.lock()
.as_ref()
.map_or(false, |m| m.state_hashes.iter().any(|h| h == &hash))
{
self.state_restoration_chunks.lock().insert(hash, chunk);
2020-08-05 06:08:03 +02:00
}
}
2020-08-05 06:08:03 +02:00
fn restore_block_chunk(&self, hash: H256, chunk: Bytes) {
if self
.restoration_manifest
.lock()
.as_ref()
.map_or(false, |m| m.block_hashes.iter().any(|h| h == &hash))
{
self.block_restoration_chunks.lock().insert(hash, chunk);
}
2020-08-05 06:08:03 +02:00
}
fn shutdown(&self) {
self.abort_restore();
}
}
#[test]
fn snapshot_sync() {
::env_logger::try_init().ok();
let mut config = SyncConfig::default();
config.warp_sync = WarpSync::Enabled;
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();
2016-12-06 19:23:15 +01:00
net.peer(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()
);
}