From c65a5c8e9ce2134e0534e6c8ece1aca823eba323 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 8 Sep 2016 12:14:53 +0200 Subject: [PATCH] add restored_is_equivalent test --- Cargo.lock | 2 + ethcore/src/snapshot/tests/mod.rs | 1 + ethcore/src/snapshot/tests/service.rs | 96 +++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 ethcore/src/snapshot/tests/service.rs diff --git a/Cargo.lock b/Cargo.lock index 2ebf9b854..d9e5ced66 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -569,6 +569,7 @@ name = "ethkey" version = "0.2.0" dependencies = [ "bigint 0.1.0", + "docopt 0.6.80 (registry+https://github.com/rust-lang/crates.io-index)", "eth-secp256k1 0.5.4 (git+https://github.com/ethcore/rust-secp256k1)", "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", @@ -580,6 +581,7 @@ dependencies = [ name = "ethstore" version = "0.1.0" dependencies = [ + "docopt 0.6.80 (registry+https://github.com/rust-lang/crates.io-index)", "ethcrypto 0.1.0", "ethkey 0.2.0", "itertools 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/ethcore/src/snapshot/tests/mod.rs b/ethcore/src/snapshot/tests/mod.rs index 84096bead..d9c0abc73 100644 --- a/ethcore/src/snapshot/tests/mod.rs +++ b/ethcore/src/snapshot/tests/mod.rs @@ -18,6 +18,7 @@ mod blocks; mod state; +mod service; pub mod helpers; diff --git a/ethcore/src/snapshot/tests/service.rs b/ethcore/src/snapshot/tests/service.rs new file mode 100644 index 000000000..0660668cc --- /dev/null +++ b/ethcore/src/snapshot/tests/service.rs @@ -0,0 +1,96 @@ +// 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 . + +//! Tests for the snapshot service. + +use std::sync::Arc; + +use client::{BlockChainClient, Client}; +use ids::BlockID; +use snapshot::service::{Service, ServiceParams}; +use snapshot::SnapshotService; +use spec::Spec; +use tests::helpers::generate_dummy_client_with_spec_and_data; + +use devtools::RandomTempPath; +use io::IoChannel; +use util::kvdb::DatabaseConfig; + +#[test] +fn restored_is_equivalent() { + const NUM_BLOCKS: u32 = 400; + const TX_PER: usize = 5; + + let gas_prices = vec![1.into(), 2.into(), 3.into(), 999.into()]; + + let client = generate_dummy_client_with_spec_and_data(Spec::new_null, NUM_BLOCKS, TX_PER, &gas_prices); + + let path = RandomTempPath::create_dir(); + let mut path = path.as_path().clone(); + let mut client_db = path.clone(); + + client_db.push("client_db"); + path.push("snapshot"); + + let db_config = DatabaseConfig::with_columns(::db::NUM_COLUMNS); + + let spec = Spec::new_null(); + let client2 = Client::new( + Default::default(), + &spec, + &client_db, + Arc::new(::miner::Miner::with_spec(&spec)), + IoChannel::disconnected(), + &db_config, + ).unwrap(); + + let service_params = ServiceParams { + engine: spec.engine.clone(), + genesis_block: spec.genesis_block(), + db_config: db_config, + pruning: ::util::journaldb::Algorithm::Archive, + channel: IoChannel::disconnected(), + snapshot_root: path, + db_restore: client2.clone(), + }; + + let service = Service::new(service_params).unwrap(); + service.take_snapshot(&client, NUM_BLOCKS as u64).unwrap(); + + let manifest = service.manifest().unwrap(); + + service.init_restore(manifest.clone()).unwrap(); + assert!(service.init_restore(manifest.clone()).is_ok()); + + for hash in manifest.state_hashes { + let chunk = service.chunk(hash).unwrap(); + service.feed_state_chunk(hash, &chunk); + } + + for hash in manifest.block_hashes { + let chunk = service.chunk(hash).unwrap(); + service.feed_block_chunk(hash, &chunk); + } + + assert_eq!(service.status(), ::snapshot::RestorationStatus::Inactive); + + for x in 0..NUM_BLOCKS { + let block1 = client.block(BlockID::Number(x as u64)).unwrap(); + let block2 = client2.block(BlockID::Number(x as u64)).unwrap(); + + assert_eq!(block1, block2); + } +} \ No newline at end of file