2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-03-11 10:17:20 +01:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2016-03-11 10:17:20 +01: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.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2016-03-11 10:17:20 +01: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
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-03-11 10:17:20 +01:00
|
|
|
|
2016-03-18 19:16:46 +01:00
|
|
|
//! Test implementation of SyncProvider.
|
|
|
|
|
2021-03-12 10:12:42 +01:00
|
|
|
use ethereum_types::{H256, H512};
|
2019-02-07 15:27:09 +01:00
|
|
|
use network::client_version::ClientVersion;
|
2017-09-02 20:09:13 +02:00
|
|
|
use parking_lot::RwLock;
|
2021-03-03 22:44:35 +01:00
|
|
|
use stats::{PrometheusMetrics, PrometheusRegistry};
|
2016-11-16 13:37:21 +01:00
|
|
|
use std::collections::BTreeMap;
|
2018-04-10 12:13:49 +02:00
|
|
|
use sync::{EthProtocolInfo, PeerInfo, SyncProvider, SyncState, SyncStatus, TransactionStats};
|
2016-03-11 10:17:20 +01:00
|
|
|
|
2016-03-18 19:16:46 +01:00
|
|
|
/// TestSyncProvider config.
|
2016-03-11 10:17:20 +01:00
|
|
|
pub struct Config {
|
2016-03-18 19:16:46 +01:00
|
|
|
/// Protocol version.
|
2016-12-05 15:54:31 +01:00
|
|
|
pub network_id: u64,
|
2016-03-18 19:16:46 +01:00
|
|
|
/// Number of peers.
|
2016-03-11 10:17:20 +01:00
|
|
|
pub num_peers: usize,
|
|
|
|
}
|
|
|
|
|
2016-03-18 19:16:46 +01:00
|
|
|
/// Test sync provider.
|
2016-03-11 10:17:20 +01:00
|
|
|
pub struct TestSyncProvider {
|
2016-03-18 19:16:46 +01:00
|
|
|
/// Sync status.
|
2016-03-16 10:37:08 +01:00
|
|
|
pub status: RwLock<SyncStatus>,
|
2016-03-11 10:17:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestSyncProvider {
|
2016-03-18 22:57:26 +01:00
|
|
|
/// Creates new sync provider.
|
2016-03-11 10:17:20 +01:00
|
|
|
pub fn new(config: Config) -> Self {
|
|
|
|
TestSyncProvider {
|
2016-03-16 10:37:08 +01:00
|
|
|
status: RwLock::new(SyncStatus {
|
2016-05-16 14:41:41 +02:00
|
|
|
state: SyncState::Idle,
|
2016-03-26 00:22:09 +01:00
|
|
|
network_id: config.network_id,
|
2020-09-21 14:48:14 +02:00
|
|
|
protocol_version: 64,
|
2016-03-11 10:17:20 +01:00
|
|
|
start_block_number: 0,
|
|
|
|
last_imported_block_number: None,
|
|
|
|
highest_block_number: None,
|
|
|
|
blocks_total: 0,
|
|
|
|
blocks_received: 0,
|
|
|
|
num_peers: config.num_peers,
|
|
|
|
num_active_peers: 0,
|
2016-09-06 15:31:13 +02:00
|
|
|
num_snapshot_chunks: 0,
|
|
|
|
snapshot_chunks_done: 0,
|
2016-10-18 18:16:00 +02:00
|
|
|
last_imported_old_block_number: None,
|
2020-09-14 16:08:57 +02:00
|
|
|
item_sizes: BTreeMap::new(),
|
2016-03-17 11:23:30 +01:00
|
|
|
}),
|
2016-03-11 10:17:20 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-19 18:35:39 +02:00
|
|
|
|
|
|
|
/// Simulate importing blocks.
|
|
|
|
pub fn increase_imported_block_number(&self, count: u64) {
|
|
|
|
let mut status = self.status.write();
|
|
|
|
let current_number = status.last_imported_block_number.unwrap_or(0);
|
|
|
|
status.last_imported_block_number = Some(current_number + count);
|
|
|
|
}
|
2016-03-11 10:17:20 +01:00
|
|
|
}
|
|
|
|
|
2020-09-14 16:08:57 +02:00
|
|
|
impl PrometheusMetrics for TestSyncProvider {
|
2021-03-03 22:44:35 +01:00
|
|
|
fn prometheus_metrics(&self, _: &mut PrometheusRegistry) {}
|
2020-09-14 16:08:57 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 10:17:20 +01:00
|
|
|
impl SyncProvider for TestSyncProvider {
|
|
|
|
fn status(&self) -> SyncStatus {
|
2016-07-13 19:59:59 +02:00
|
|
|
self.status.read().clone()
|
2016-03-11 10:17:20 +01:00
|
|
|
}
|
2016-10-12 20:18:59 +02:00
|
|
|
|
|
|
|
fn peers(&self) -> Vec<PeerInfo> {
|
|
|
|
vec![
|
|
|
|
PeerInfo {
|
|
|
|
id: Some("node1".to_owned()),
|
2019-02-07 15:27:09 +01:00
|
|
|
client_version: ClientVersion::from("Parity-Ethereum/1/v2.4.0/linux/rustc"),
|
2020-09-21 14:48:14 +02:00
|
|
|
capabilities: vec!["eth/63".to_owned(), "eth/64".to_owned()],
|
2017-10-17 14:50:53 +02:00
|
|
|
remote_address: "127.0.0.1:7777".to_owned(),
|
2016-10-12 20:18:59 +02:00
|
|
|
local_address: "127.0.0.1:8888".to_owned(),
|
2017-01-20 12:41:49 +01:00
|
|
|
eth_info: Some(EthProtocolInfo {
|
2020-09-21 14:48:14 +02:00
|
|
|
version: 63,
|
2017-01-20 12:41:49 +01:00
|
|
|
difficulty: Some(40.into()),
|
2021-03-12 10:12:42 +01:00
|
|
|
head: H256::from_low_u64_be(50),
|
2017-01-20 12:41:49 +01:00
|
|
|
}),
|
2016-10-12 20:18:59 +02:00
|
|
|
},
|
|
|
|
PeerInfo {
|
|
|
|
id: None,
|
2020-09-22 14:53:52 +02:00
|
|
|
client_version: ClientVersion::from("Open-Ethereum/2/v2.4.0/linux/rustc"),
|
2020-09-21 14:48:14 +02:00
|
|
|
capabilities: vec!["eth/64".to_owned(), "eth/65".to_owned()],
|
2017-10-17 14:50:53 +02:00
|
|
|
remote_address: "Handshake".to_owned(),
|
2016-10-12 20:18:59 +02:00
|
|
|
local_address: "127.0.0.1:3333".to_owned(),
|
2017-01-20 12:41:49 +01:00
|
|
|
eth_info: Some(EthProtocolInfo {
|
2020-09-21 14:48:14 +02:00
|
|
|
version: 65,
|
2017-01-20 12:41:49 +01:00
|
|
|
difficulty: None,
|
2021-03-12 10:12:42 +01:00
|
|
|
head: H256::from_low_u64_be(60),
|
2017-01-20 12:41:49 +01:00
|
|
|
}),
|
2016-10-12 20:18:59 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2016-11-02 19:43:21 +01:00
|
|
|
|
|
|
|
fn enode(&self) -> Option<String> {
|
|
|
|
None
|
|
|
|
}
|
2016-11-16 13:37:21 +01:00
|
|
|
|
|
|
|
fn transactions_stats(&self) -> BTreeMap<H256, TransactionStats> {
|
|
|
|
map![
|
2021-03-12 10:12:42 +01:00
|
|
|
H256::from_low_u64_be(1) => TransactionStats {
|
2016-11-16 13:37:21 +01:00
|
|
|
first_seen: 10,
|
|
|
|
propagated_to: map![
|
2021-03-12 10:12:42 +01:00
|
|
|
H512::from_low_u64_be(128) => 16
|
2016-12-10 14:56:41 +01:00
|
|
|
],
|
2016-11-16 13:37:21 +01:00
|
|
|
},
|
2021-03-12 10:12:42 +01:00
|
|
|
H256::from_low_u64_be(5) => TransactionStats {
|
2016-11-16 13:37:21 +01:00
|
|
|
first_seen: 16,
|
|
|
|
propagated_to: map![
|
2021-03-12 10:12:42 +01:00
|
|
|
H512::from_low_u64_be(16) => 1
|
2016-12-10 14:56:41 +01:00
|
|
|
],
|
2016-11-16 13:37:21 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2016-03-11 10:17:20 +01:00
|
|
|
}
|