From 19184e8529b7e8401738d39f2c5b54fcab51cb81 Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Thu, 19 Sep 2019 13:12:07 +0200 Subject: [PATCH 1/2] [sync]: rust 2018 (#11067) * [sync]: rust 2018 * fix(grumble): explicit use for RlpResponseResult * fix(grumble): types -> common_types * fix: bad rebase * fix: wildcard import * fix(grumble): rename crate hash to `keccak_hash` --- ethcore/sync/Cargo.toml | 9 +- ethcore/sync/src/api.rs | 66 ++++++++------ ethcore/sync/src/block_sync.rs | 53 ++++++----- ethcore/sync/src/blocks.rs | 32 +++---- ethcore/sync/src/chain/handler.rs | 88 ++++++++----------- ethcore/sync/src/chain/mod.rs | 76 +++++++++------- ethcore/sync/src/chain/propagator.rs | 34 ++++--- ethcore/sync/src/chain/requester.rs | 13 ++- ethcore/sync/src/chain/supplier.rs | 41 +++++---- ethcore/sync/src/chain/sync_packet.rs | 10 ++- ethcore/sync/src/lib.rs | 45 +--------- ethcore/sync/src/light_sync/mod.rs | 34 ++++--- ethcore/sync/src/light_sync/response.rs | 6 +- ethcore/sync/src/light_sync/sync_round.rs | 6 +- ethcore/sync/src/light_sync/tests/mod.rs | 4 +- ethcore/sync/src/light_sync/tests/test_net.rs | 53 ++++++----- ethcore/sync/src/snapshot_sync.rs | 32 +++---- ethcore/sync/src/sync_io.rs | 12 +-- ethcore/sync/src/tests/chain.rs | 36 ++++---- ethcore/sync/src/tests/consensus.rs | 17 ++-- ethcore/sync/src/tests/helpers.rs | 61 +++++++------ ethcore/sync/src/tests/private.rs | 24 +++-- ethcore/sync/src/tests/snapshot.rs | 28 +++--- ethcore/sync/src/transactions_stats.rs | 8 +- 24 files changed, 404 insertions(+), 384 deletions(-) diff --git a/ethcore/sync/Cargo.toml b/ethcore/sync/Cargo.toml index ff2d36ef892..22136b9e219 100644 --- a/ethcore/sync/Cargo.toml +++ b/ethcore/sync/Cargo.toml @@ -4,26 +4,27 @@ name = "ethcore-sync" version = "1.12.0" license = "GPL-3.0" authors = ["Parity Technologies "] +edition = "2018" [lib] [dependencies] +bytes = { package = "parity-bytes", version = "0.1" } client-traits = { path = "../client-traits" } common-types = { path = "../types" } +devp2p = { package = "ethcore-network-devp2p", path = "../../util/network-devp2p" } enum_primitive = "0.1.1" ethcore-io = { path = "../../util/io" } -ethcore-light = { path = "../light" } -ethcore-network = { path = "../../util/network" } -ethcore-network-devp2p = { path = "../../util/network-devp2p" } ethcore-private-tx = { path = "../private-tx" } ethereum-types = "0.6.0" ethkey = { path = "../../accounts/ethkey" } fastmap = { path = "../../util/fastmap" } futures = "0.1" keccak-hash = "0.2.0" +light = { package = "ethcore-light", path = "../light" } log = "0.4" macros = { path = "../../util/macros" } -parity-bytes = "0.1" +network = { package = "ethcore-network", path = "../../util/network" } parity-runtime = { path = "../../util/runtime" } parity-util-mem = "0.2.0" parking_lot = "0.8" diff --git a/ethcore/sync/src/api.rs b/ethcore/sync/src/api.rs index 75f9634ad69..d8d20bad3aa 100644 --- a/ethcore/sync/src/api.rs +++ b/ethcore/sync/src/api.rs @@ -19,46 +19,54 @@ use std::collections::{HashMap, BTreeMap}; use std::io; use std::ops::RangeInclusive; use std::time::Duration; +use std::net::{SocketAddr, AddrParseError}; +use std::str::FromStr; +use std::sync::atomic::{AtomicBool, Ordering}; + +use crate::sync_io::NetSyncIo; +use crate::light_sync::{self, SyncInfo}; +use crate::private_tx::PrivateTxHandler; +use crate::chain::{ + sync_packet::SyncPacket::{PrivateTransactionPacket, SignedPrivateTransactionPacket}, + ChainSyncApi, SyncState, SyncStatus as EthSyncStatus, ETH_PROTOCOL_VERSION_62, + ETH_PROTOCOL_VERSION_63, PAR_PROTOCOL_VERSION_1, PAR_PROTOCOL_VERSION_2, + PAR_PROTOCOL_VERSION_3, PAR_PROTOCOL_VERSION_4, +}; + use bytes::Bytes; +use client_traits::{BlockChainClient, ChainNotify}; use devp2p::NetworkService; -use network::{NetworkProtocolHandler, NetworkContext, PeerId, ProtocolId, - NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, Error, - ConnectionFilter}; -use network::client_version::ClientVersion; +use ethcore_io::TimerToken; +use ethcore_private_tx::PrivateStateDB; use ethereum_types::{H256, H512, U256}; +use ethkey::Secret; use futures::sync::mpsc as futures_mpsc; use futures::Stream; -use io::{TimerToken}; -use ethkey::Secret; -use client_traits::{BlockChainClient, ChainNotify}; -use snapshot::SnapshotService; -use ethcore_private_tx::PrivateStateDB; -use types::BlockNumber; -use sync_io::NetSyncIo; -use chain::{ChainSyncApi, SyncStatus as EthSyncStatus}; -use std::net::{SocketAddr, AddrParseError}; -use std::str::FromStr; -use parking_lot::{RwLock, Mutex}; -use chain::{ETH_PROTOCOL_VERSION_63, ETH_PROTOCOL_VERSION_62, - PAR_PROTOCOL_VERSION_1, PAR_PROTOCOL_VERSION_2, PAR_PROTOCOL_VERSION_3, PAR_PROTOCOL_VERSION_4, SyncState}; -use chain::sync_packet::SyncPacket::{PrivateTransactionPacket, SignedPrivateTransactionPacket}; use light::client::AsLightClient; use light::Provider; use light::net::{ self as light_net, LightProtocol, Params as LightParams, Capabilities, Handler as LightHandler, EventContext, SampleStore, }; +use log::{trace, warn}; +use macros::hash_map; +use network::{ + client_version::ClientVersion, + NetworkProtocolHandler, NetworkContext, PeerId, ProtocolId, + NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, Error, + ConnectionFilter, IpFilter +}; +use snapshot::SnapshotService; +use parking_lot::{RwLock, Mutex}; use parity_runtime::Executor; -use std::sync::atomic::{AtomicBool, Ordering}; -use network::IpFilter; -use private_tx::PrivateTxHandler; -use types::{ +use trace_time::trace_time; +use common_types::{ + BlockNumber, chain_notify::{NewBlocks, ChainMessageType}, pruning_info::PruningInfo, transaction::UnverifiedTransaction, }; -use super::light_sync::SyncInfo; /// Parity sync protocol pub const WARP_SYNC_PROTOCOL_ID: ProtocolId = *b"par"; @@ -646,14 +654,14 @@ impl ChainNotify for EthSync { struct TxRelay(Arc); impl LightHandler for TxRelay { - fn on_transactions(&self, ctx: &dyn EventContext, relay: &[::types::transaction::UnverifiedTransaction]) { + fn on_transactions(&self, ctx: &dyn EventContext, relay: &[UnverifiedTransaction]) { trace!(target: "pip", "Relaying {} transactions from peer {}", relay.len(), ctx.peer()); - self.0.queue_transactions(relay.iter().map(|tx| ::rlp::encode(tx)).collect(), ctx.peer()) + self.0.queue_transactions(relay.iter().map(|tx| rlp::encode(tx)).collect(), ctx.peer()) } } /// Trait for managing network -pub trait ManageNetwork : Send + Sync { +pub trait ManageNetwork: Send + Sync { /// Set to allow unreserved peers to connect fn accept_unreserved_peers(&self); /// Set to deny unreserved peers to connect @@ -945,15 +953,15 @@ impl LightSync { } -impl ::std::ops::Deref for LightSync { - type Target = dyn (::light_sync::SyncInfo); +impl std::ops::Deref for LightSync { + type Target = dyn (light_sync::SyncInfo); fn deref(&self) -> &Self::Target { &*self.sync } } impl LightNetworkDispatcher for LightSync { - fn with_context(&self, f: F) -> Option where F: FnOnce(&dyn (::light::net::BasicContext)) -> T { + fn with_context(&self, f: F) -> Option where F: FnOnce(&dyn (light::net::BasicContext)) -> T { self.network.with_context_eval( self.subprotocol_name, move |ctx| self.proto.with_context(&ctx, f), diff --git a/ethcore/sync/src/block_sync.rs b/ethcore/sync/src/block_sync.rs index a505e63cd7c..126799a3dd0 100644 --- a/ethcore/sync/src/block_sync.rs +++ b/ethcore/sync/src/block_sync.rs @@ -20,20 +20,24 @@ use std::collections::{HashSet, VecDeque}; use std::cmp; -use parity_util_mem::MallocSizeOf; + +use crate::{ + blocks::{BlockCollection, SyncBody, SyncHeader}, + chain::BlockSet, + sync_io::SyncIo +}; + use ethereum_types::H256; -use rlp::{self, Rlp}; -use types::{ +use log::{debug, trace}; +use network::{client_version::ClientCapabilities, PeerId}; +use rlp::Rlp; +use parity_util_mem::MallocSizeOf; +use common_types::{ BlockNumber, block_status::BlockStatus, ids::BlockId, errors::{EthcoreError, BlockError, ImportError}, }; -use sync_io::SyncIo; -use blocks::{BlockCollection, SyncBody, SyncHeader}; -use chain::BlockSet; -use network::PeerId; -use network::client_version::ClientCapabilities; const MAX_HEADERS_TO_REQUEST: usize = 128; const MAX_BODIES_TO_REQUEST_LARGE: usize = 128; @@ -635,18 +639,23 @@ fn all_expected(values: &[A], expected_values: &[B], is_expected: F) -> #[cfg(test)] mod tests { - use super::*; + use super::{ + BlockSet, BlockDownloader, BlockDownloaderImportError, DownloadAction, SyncIo, H256, + MAX_HEADERS_TO_REQUEST, MAX_USELESS_HEADERS_PER_ROUND, SUBCHAIN_SIZE, State, Rlp, VecDeque + }; + + use crate::tests::{helpers::TestIo, snapshot::TestSnapshotService}; + use ethcore::test_helpers::TestBlockChainClient; - use spec; - use ethkey::{Generator, Random}; - use hash::keccak; + use ethkey::{Random, Generator}; + use keccak_hash::keccak; use parking_lot::RwLock; use rlp::{encode_list, RlpStream}; - use tests::helpers::TestIo; - use tests::snapshot::TestSnapshotService; - use types::transaction::{Transaction, SignedTransaction}; use triehash_ethereum::ordered_trie_root; - use types::header::Header as BlockHeader; + use common_types::{ + transaction::{Transaction, SignedTransaction}, + header::Header as BlockHeader, + }; fn dummy_header(number: u64, parent_hash: H256) -> BlockHeader { let mut header = BlockHeader::new(); @@ -680,7 +689,7 @@ mod tests { #[test] fn import_headers_in_chain_head_state() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let spec = spec::new_test(); let genesis_hash = spec.genesis_header().hash(); @@ -752,7 +761,7 @@ mod tests { #[test] fn import_headers_in_blocks_state() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let mut chain = TestBlockChainClient::new(); let snapshot_service = TestSnapshotService::new(); @@ -802,7 +811,7 @@ mod tests { #[test] fn import_bodies() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let mut chain = TestBlockChainClient::new(); let snapshot_service = TestSnapshotService::new(); @@ -870,7 +879,7 @@ mod tests { #[test] fn import_receipts() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let mut chain = TestBlockChainClient::new(); let snapshot_service = TestSnapshotService::new(); @@ -929,7 +938,7 @@ mod tests { #[test] fn reset_after_multiple_sets_of_useless_headers() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let spec = spec::new_test(); let genesis_hash = spec.genesis_header().hash(); @@ -969,7 +978,7 @@ mod tests { #[test] fn dont_reset_after_multiple_sets_of_useless_headers_for_chain_head() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let spec = spec::new_test(); let genesis_hash = spec.genesis_header().hash(); diff --git a/ethcore/sync/src/blocks.rs b/ethcore/sync/src/blocks.rs index 2db1fd1f3f1..97e4835802f 100644 --- a/ethcore/sync/src/blocks.rs +++ b/ethcore/sync/src/blocks.rs @@ -15,14 +15,15 @@ // along with Parity Ethereum. If not, see . use std::collections::{HashSet, HashMap, hash_map}; -use hash::{keccak, KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP}; -use parity_util_mem::MallocSizeOf; -use ethereum_types::H256; -use triehash_ethereum::ordered_trie_root; + use bytes::Bytes; +use ethereum_types::H256; +use keccak_hash::{keccak, KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP}; +use log::{trace, warn}; +use parity_util_mem::MallocSizeOf; use rlp::{Rlp, RlpStream, DecoderError}; -use network; -use types::{ +use triehash_ethereum::ordered_trie_root; +use common_types::{ transaction::UnverifiedTransaction, header::Header as BlockHeader, verification::Unverified, @@ -40,7 +41,7 @@ pub struct SyncHeader { impl SyncHeader { pub fn from_rlp(bytes: Bytes) -> Result { let result = SyncHeader { - header: ::rlp::decode(&bytes)?, + header: rlp::decode(&bytes)?, bytes, }; @@ -151,18 +152,7 @@ pub struct BlockCollection { impl BlockCollection { /// Create a new instance. pub fn new(download_receipts: bool) -> BlockCollection { - BlockCollection { - need_receipts: download_receipts, - blocks: HashMap::new(), - header_ids: HashMap::new(), - receipt_ids: HashMap::new(), - heads: Vec::new(), - parents: HashMap::new(), - head: None, - downloading_headers: HashSet::new(), - downloading_bodies: HashSet::new(), - downloading_receipts: HashSet::new(), - } + Self { need_receipts: download_receipts, ..Default::default() } } /// Clear everything. @@ -545,12 +535,12 @@ mod test { use super::{BlockCollection, SyncHeader}; use client_traits::BlockChainClient; use ethcore::test_helpers::{TestBlockChainClient, EachBlockWith}; - use types::{ + use common_types::{ ids::BlockId, BlockNumber, verification::Unverified, }; - use rlp::*; + use rlp::Rlp; fn is_empty(bc: &BlockCollection) -> bool { bc.heads.is_empty() && diff --git a/ethcore/sync/src/chain/handler.rs b/ethcore/sync/src/chain/handler.rs index 3775189e4dd..08ed6503511 100644 --- a/ethcore/sync/src/chain/handler.rs +++ b/ethcore/sync/src/chain/handler.rs @@ -14,22 +14,38 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -use api::WARP_SYNC_PROTOCOL_ID; -use block_sync::{BlockDownloaderImportError as DownloaderImportError, DownloadAction}; +use std::time::Instant; +use std::{mem, cmp}; + +use crate::{ + snapshot_sync::ChunkType, + sync_io::SyncIo, + api::WARP_SYNC_PROTOCOL_ID, + block_sync::{BlockDownloaderImportError as DownloaderImportError, DownloadAction}, + chain::{ + sync_packet::{ + PacketInfo, + SyncPacket::{ + self, BlockBodiesPacket, BlockHeadersPacket, NewBlockHashesPacket, NewBlockPacket, + PrivateStatePacket, PrivateTransactionPacket, ReceiptsPacket, SignedPrivateTransactionPacket, + SnapshotDataPacket, SnapshotManifestPacket, StatusPacket, + } + }, + BlockSet, ChainSync, ForkConfirmation, PacketDecodeError, PeerAsking, PeerInfo, SyncRequester, + SyncState, ETH_PROTOCOL_VERSION_62, ETH_PROTOCOL_VERSION_63, MAX_NEW_BLOCK_AGE, MAX_NEW_HASHES, + PAR_PROTOCOL_VERSION_1, PAR_PROTOCOL_VERSION_3, PAR_PROTOCOL_VERSION_4, + } +}; + use bytes::Bytes; use enum_primitive::FromPrimitive; use ethereum_types::{H256, U256}; -use hash::keccak; +use keccak_hash::keccak; use network::PeerId; use network::client_version::ClientVersion; +use log::{debug, trace, error}; use rlp::Rlp; -use crate::{ - snapshot_sync::ChunkType, - sync_io::SyncIo, -}; -use std::time::Instant; -use std::{mem, cmp}; -use types::{ +use common_types::{ BlockNumber, block_status::BlockStatus, ids::BlockId, @@ -38,38 +54,6 @@ use types::{ snapshot::{ManifestData, RestorationStatus}, }; -use super::sync_packet::{PacketInfo, SyncPacket}; -use super::sync_packet::SyncPacket::{ - StatusPacket, - NewBlockHashesPacket, - BlockHeadersPacket, - BlockBodiesPacket, - NewBlockPacket, - ReceiptsPacket, - SnapshotManifestPacket, - SnapshotDataPacket, - PrivateTransactionPacket, - SignedPrivateTransactionPacket, - PrivateStatePacket, -}; - -use super::{ - BlockSet, - ChainSync, - ForkConfirmation, - PacketDecodeError, - PeerAsking, - PeerInfo, - SyncRequester, - SyncState, - ETH_PROTOCOL_VERSION_62, - ETH_PROTOCOL_VERSION_63, - MAX_NEW_BLOCK_AGE, - MAX_NEW_HASHES, - PAR_PROTOCOL_VERSION_1, - PAR_PROTOCOL_VERSION_3, - PAR_PROTOCOL_VERSION_4, -}; /// The Chain Sync Handler: handles responses from peers pub struct SyncHandler; @@ -800,21 +784,19 @@ impl SyncHandler { #[cfg(test)] mod tests { + use std::collections::VecDeque; + + use super::{ + super::tests::{dummy_sync_with_peer, get_dummy_block, get_dummy_blocks, get_dummy_hashes}, + SyncHandler + }; + + use crate::tests::{helpers::TestIo, snapshot::TestSnapshotService}; + use client_traits::ChainInfo; use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient}; use parking_lot::RwLock; use rlp::Rlp; - use std::collections::VecDeque; - use tests::helpers::TestIo; - use tests::snapshot::TestSnapshotService; - - use super::*; - use super::super::tests::{ - dummy_sync_with_peer, - get_dummy_block, - get_dummy_blocks, - get_dummy_hashes, - }; #[test] fn handles_peer_new_hashes() { diff --git a/ethcore/sync/src/chain/mod.rs b/ethcore/sync/src/chain/mod.rs index cd2255d4927..780bda34d66 100644 --- a/ethcore/sync/src/chain/mod.rs +++ b/ethcore/sync/src/chain/mod.rs @@ -88,38 +88,41 @@ //! All other messages are ignored. mod handler; -pub mod sync_packet; mod propagator; mod requester; mod supplier; +pub mod sync_packet; + use std::sync::{Arc, mpsc}; use std::collections::{HashSet, HashMap, BTreeMap}; use std::cmp; use std::time::{Duration, Instant}; -use hash::keccak; -use parity_util_mem::MallocSizeOfExt; -use futures::sync::mpsc as futures_mpsc; -use api::Notification; -use ethereum_types::{H256, U256}; -use fastmap::{H256FastMap, H256FastSet}; -use parking_lot::{Mutex, RwLock, RwLockWriteGuard}; -use bytes::Bytes; -use rlp::{RlpStream, DecoderError}; -use network::{self, PeerId, PacketId}; -use network::client_version::ClientVersion; -use client_traits::BlockChainClient; + use crate::{ + EthProtocolInfo as PeerInfoDigest, PriorityTask, SyncConfig, WarpSync, WARP_SYNC_PROTOCOL_ID, + api::{Notification, PRIORITY_TIMER_INTERVAL}, + block_sync::{BlockDownloader, DownloadAction}, sync_io::SyncIo, snapshot_sync::Snapshot, + transactions_stats::{TransactionsStats, Stats as TransactionStats}, + private_tx::PrivateTxHandler, }; -use super::{WarpSync, SyncConfig}; -use block_sync::{BlockDownloader, DownloadAction}; + +use bytes::Bytes; +use client_traits::BlockChainClient; +use ethereum_types::{H256, U256}; +use fastmap::{H256FastMap, H256FastSet}; +use futures::sync::mpsc as futures_mpsc; +use keccak_hash::keccak; +use log::{error, trace, debug}; +use network::client_version::ClientVersion; +use network::{self, PeerId, PacketId}; +use parity_util_mem::{MallocSizeOfExt, malloc_size_of_is_0}; +use parking_lot::{Mutex, RwLock, RwLockWriteGuard}; use rand::{Rng, seq::SliceRandom}; -use api::{EthProtocolInfo as PeerInfoDigest, WARP_SYNC_PROTOCOL_ID, PriorityTask}; -use private_tx::PrivateTxHandler; -use transactions_stats::{TransactionsStats, Stats as TransactionStats}; -use types::{ +use rlp::{RlpStream, DecoderError}; +use common_types::{ BlockNumber, ids::BlockId, transaction::UnverifiedTransaction, @@ -434,7 +437,7 @@ impl ChainSyncApi { } /// Returns transactions propagation statistics - pub fn transactions_stats(&self) -> BTreeMap { + pub fn transactions_stats(&self) -> BTreeMap { self.sync.read().transactions_stats() .iter() .map(|(hash, stats)| (*hash, stats.into())) @@ -463,7 +466,7 @@ impl ChainSyncApi { } // deadline to get the task from the queue - let deadline = Instant::now() + ::api::PRIORITY_TIMER_INTERVAL; + let deadline = Instant::now() + PRIORITY_TIMER_INTERVAL; let mut work = || { let task = { let tasks = self.priority_tasks.try_lock_until(deadline)?; @@ -1405,22 +1408,27 @@ impl ChainSync { #[cfg(test)] pub mod tests { - use std::collections::{VecDeque}; - use ethkey; - use network::PeerId; - use tests::helpers::TestIo; - use tests::snapshot::TestSnapshotService; - use ethereum_types::{H256, U256, Address}; - use parking_lot::RwLock; + use std::{collections::VecDeque, time::Instant}; + + use super::{ + BlockId, BlockQueueInfo, ChainSync, ClientVersion, PeerInfo, PeerAsking, + SyncHandler, SyncState, SyncStatus, SyncPropagator, UnverifiedTransaction + }; + + use crate::{ + api::SyncConfig, + tests::{helpers::TestIo, snapshot::TestSnapshotService}, + }; + use bytes::Bytes; - use rlp::{Rlp, RlpStream}; - use super::*; - use ::SyncConfig; - use super::{PeerInfo, PeerAsking}; - use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient}; use client_traits::{BlockInfo, BlockChainClient, ChainInfo}; + use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient}; use ethcore::miner::{MinerService, PendingOrdering}; - use types::header::Header; + use ethereum_types::{H256, U256, Address}; + use network::PeerId; + use parking_lot::RwLock; + use rlp::{Rlp, RlpStream}; + use common_types::header::Header; pub fn get_dummy_block(order: u32, parent_hash: H256) -> Bytes { let mut header = Header::new(); diff --git a/ethcore/sync/src/chain/propagator.rs b/ethcore/sync/src/chain/propagator.rs index 07856e1f0df..9ca9c91ce7e 100644 --- a/ethcore/sync/src/chain/propagator.rs +++ b/ethcore/sync/src/chain/propagator.rs @@ -17,19 +17,18 @@ use std::cmp; use std::collections::HashSet; +use crate::{sync_io::SyncIo, chain::sync_packet::SyncPacket}; + use bytes::Bytes; use ethereum_types::H256; use fastmap::H256FastSet; +use log::{debug, error, trace}; use network::client_version::ClientCapabilities; use network::PeerId; use rand::RngCore; use rlp::{Encodable, RlpStream}; -use sync_io::SyncIo; -use types::transaction::SignedTransaction; -use types::BlockNumber; -use types::blockchain_info::BlockChainInfo; +use common_types::{blockchain_info::BlockChainInfo, transaction::SignedTransaction, BlockNumber}; -use super::sync_packet::SyncPacket; use super::sync_packet::SyncPacket::{ NewBlockHashesPacket, TransactionsPacket, @@ -335,17 +334,26 @@ impl SyncPropagator { #[cfg(test)] mod tests { - use client_traits::{BlockInfo, ChainInfo}; + use std::{collections::VecDeque, time::Instant}; + + use crate::{ + api::SyncConfig, + chain::{ChainSync, ForkConfirmation, PeerAsking, PeerInfo}, + tests::{helpers::TestIo, snapshot::TestSnapshotService}, + }; + + use super::{ + super::tests::{dummy_sync_with_peer, insert_dummy_peer}, + SyncPropagator, + }; + + use client_traits::{BlockChainClient, BlockInfo, ChainInfo}; use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient}; + use ethereum_types::{H256, U256}; + use network::client_version::ClientVersion; use parking_lot::RwLock; use rlp::Rlp; - use std::collections::VecDeque; - use tests::{ - helpers::TestIo, - snapshot::TestSnapshotService, - }; - - use super::{*, super::{*, tests::*}}; + use common_types::{ids::BlockId, transaction::UnverifiedTransaction}; #[test] fn sends_new_hashes_to_lagging_peer() { diff --git a/ethcore/sync/src/chain/requester.rs b/ethcore/sync/src/chain/requester.rs index e79de22929d..c090f9ca9f7 100644 --- a/ethcore/sync/src/chain/requester.rs +++ b/ethcore/sync/src/chain/requester.rs @@ -14,14 +14,19 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -use block_sync::BlockRequest; +use std::time::Instant; + +use crate::{ + block_sync::BlockRequest, + sync_io::SyncIo +}; + use bytes::Bytes; use ethereum_types::H256; +use log::{debug, trace, warn}; use network::{PeerId}; use rlp::RlpStream; -use std::time::Instant; -use sync_io::SyncIo; -use types::BlockNumber; +use common_types::BlockNumber; use super::sync_packet::SyncPacket; use super::sync_packet::SyncPacket::{ diff --git a/ethcore/sync/src/chain/supplier.rs b/ethcore/sync/src/chain/supplier.rs index f9ea49dd009..cd322106247 100644 --- a/ethcore/sync/src/chain/supplier.rs +++ b/ethcore/sync/src/chain/supplier.rs @@ -14,17 +14,18 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . +use std::cmp; + +use crate::sync_io::SyncIo; + use bytes::Bytes; use enum_primitive::FromPrimitive; use ethereum_types::H256; +use log::{debug, trace}; use network::{self, PeerId}; use parking_lot::RwLock; use rlp::{Rlp, RlpStream}; -use std::cmp; -use types::BlockNumber; -use types::ids::BlockId; - -use sync_io::SyncIo; +use common_types::{ids::BlockId, BlockNumber}; use super::sync_packet::{PacketInfo, SyncPacket}; use super::sync_packet::SyncPacket::{ @@ -394,18 +395,27 @@ impl SyncSupplier { #[cfg(test)] mod test { - use std::collections::VecDeque; - use tests::helpers::TestIo; - use tests::snapshot::TestSnapshotService; - use ethereum_types::H256; - use parking_lot::RwLock; + use std::{collections::VecDeque, str::FromStr}; + + use crate::{ + blocks::SyncHeader, + chain::RlpResponseResult, + tests::{helpers::TestIo, snapshot::TestSnapshotService} + }; + + use super::{ + SyncPacket::{GetReceiptsPacket, GetNodeDataPacket}, + BlockNumber, BlockId, SyncSupplier, PacketInfo + }; + + use super::super::tests::dummy_sync_with_peer; + use bytes::Bytes; - use rlp::{Rlp, RlpStream}; - use super::{*, super::tests::*}; - use blocks::SyncHeader; use client_traits::BlockChainClient; use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient}; - use std::str::FromStr; + use ethereum_types::H256; + use parking_lot::RwLock; + use rlp::{Rlp, RlpStream}; #[test] fn return_block_headers() { @@ -426,7 +436,8 @@ mod test { rlp.append(&if reverse {1u32} else {0u32}); rlp.out() } - fn to_header_vec(rlp: ::chain::RlpResponseResult) -> Vec { + + fn to_header_vec(rlp: RlpResponseResult) -> Vec { Rlp::new(&rlp.unwrap().unwrap().1.out()).iter().map(|r| SyncHeader::from_rlp(r.as_raw().to_vec()).unwrap()).collect() } diff --git a/ethcore/sync/src/chain/sync_packet.rs b/ethcore/sync/src/chain/sync_packet.rs index c31ce78760a..56a4de41b56 100644 --- a/ethcore/sync/src/chain/sync_packet.rs +++ b/ethcore/sync/src/chain/sync_packet.rs @@ -22,7 +22,10 @@ //! to convert to/from the packet id values transmitted over the //! wire. -use api::{ETH_PROTOCOL, WARP_SYNC_PROTOCOL_ID}; +use crate::api::{ETH_PROTOCOL, WARP_SYNC_PROTOCOL_ID}; +use self::SyncPacket::*; + +use enum_primitive::{enum_from_primitive, enum_from_primitive_impl, enum_from_primitive_impl_ty}; use network::{PacketId, ProtocolId}; enum_from_primitive! { @@ -60,12 +63,14 @@ enum_from_primitive! { } } -use self::SyncPacket::*; /// Provide both subprotocol and packet id information within the /// same object. pub trait PacketInfo { + /// Get packet id fn id(&self) -> PacketId; + + /// Get protocol id fn protocol(&self) -> ProtocolId; } @@ -113,7 +118,6 @@ impl PacketInfo for SyncPacket { #[cfg(test)] mod tests { use super::*; - use enum_primitive::FromPrimitive; #[test] diff --git a/ethcore/sync/src/lib.rs b/ethcore/sync/src/lib.rs index 61e82ad0379..9fb0cf076dd 100644 --- a/ethcore/sync/src/lib.rs +++ b/ethcore/sync/src/lib.rs @@ -21,47 +21,10 @@ //! https://github.com/ethereum/wiki/wiki/Ethereum-Wire-Protocol //! -extern crate client_traits; -extern crate common_types as types; -extern crate ethcore_io as io; -extern crate ethcore_light as light; -extern crate ethcore_network as network; -extern crate ethcore_network_devp2p as devp2p; -extern crate ethcore_private_tx; -extern crate ethereum_types; -extern crate ethkey; -extern crate fastmap; -extern crate futures; -extern crate keccak_hash as hash; -extern crate parity_bytes as bytes; -extern crate parity_runtime; -extern crate parking_lot; -extern crate rand; -extern crate rlp; -extern crate snapshot; -extern crate triehash_ethereum; - -#[cfg(test)] extern crate engine; -#[cfg(test)] extern crate env_logger; -#[cfg(test)] extern crate ethcore; -#[cfg(test)] extern crate kvdb_memorydb; -#[cfg(test)] extern crate machine; -#[cfg(test)] extern crate rand_xorshift; -#[cfg(test)] extern crate rustc_hex; -#[cfg(test)] extern crate spec; - -#[macro_use] -extern crate enum_primitive; -#[macro_use] -extern crate macros; -#[macro_use] -extern crate log; -extern crate parity_util_mem; -#[macro_use] -extern crate parity_util_mem as malloc_size_of; -#[macro_use] -extern crate trace_time; +// needed to make the procedural macro `MallocSizeOf` to work +#[macro_use] extern crate parity_util_mem as malloc_size_of; +mod api; mod chain; mod blocks; mod block_sync; @@ -75,8 +38,6 @@ pub mod light_sync; #[cfg(test)] mod tests; -mod api; - pub use api::*; pub use chain::{SyncStatus, SyncState}; pub use devp2p::validate_node_url; diff --git a/ethcore/sync/src/light_sync/mod.rs b/ethcore/sync/src/light_sync/mod.rs index 20cfdd9f1da..2de8003d697 100644 --- a/ethcore/sync/src/light_sync/mod.rs +++ b/ethcore/sync/src/light_sync/mod.rs @@ -38,15 +38,20 @@ use std::ops::Deref; use std::sync::Arc; use std::time::{Instant, Duration}; -use types::encoded; +use crate::{ + api::Notification, + chain::SyncState as ChainSyncState, +}; + +use common_types::encoded; use light::client::{AsLightClient, LightChainClient}; use light::net::{ PeerStatus, Announcement, Handler, BasicContext, EventContext, Capabilities, ReqId, Status, Error as NetError, }; -use chain::SyncState as ChainSyncState; use light::request::{self, CompleteHeadersRequest as HeadersRequest}; +use log::{debug, trace}; use network::PeerId; use ethereum_types::{H256, U256}; use parking_lot::{Mutex, RwLock}; @@ -54,7 +59,6 @@ use rand::{rngs::OsRng, seq::SliceRandom}; use futures::sync::mpsc; use self::sync_round::{AbortReason, SyncRound, ResponseContext}; -use api::Notification; mod response; mod sync_round; @@ -78,13 +82,13 @@ struct ChainInfo { } impl PartialOrd for ChainInfo { - fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> { + fn partial_cmp(&self, other: &Self) -> Option { self.head_td.partial_cmp(&other.head_td) } } impl Ord for ChainInfo { - fn cmp(&self, other: &Self) -> ::std::cmp::Ordering { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.head_td.cmp(&other.head_td) } } @@ -102,14 +106,20 @@ impl Peer { } } -// search for a common ancestor with the best chain. +/// Search for a common ancestor with the best chain. #[derive(Debug)] enum AncestorSearch { - Queued(u64), // queued to search for blocks starting from here. - Awaiting(ReqId, u64, HeadersRequest), // awaiting response for this request. - Prehistoric, // prehistoric block found. TODO: start to roll back CHTs. - FoundCommon(u64, H256), // common block found. - Genesis, // common ancestor is the genesis. + /// Queued to search for blocks starting from here. + Queued(u64), // + /// Awaiting response for this request. + Awaiting(ReqId, u64, HeadersRequest), + /// Pre-historic block found. + // TODO: start to roll back CHTs. + Prehistoric, + /// Common block found. + FoundCommon(u64, H256), + /// Common ancestor is the genesis. + Genesis, } impl AncestorSearch { @@ -493,7 +503,7 @@ impl LightSync { // handles request dispatch, block import, state machine transitions, and timeouts. fn maintain_sync(&self, ctx: &dyn BasicContext) { - use types::errors::{EthcoreError, ImportError}; + use common_types::errors::{EthcoreError, ImportError}; const DRAIN_AMOUNT: usize = 128; diff --git a/ethcore/sync/src/light_sync/response.rs b/ethcore/sync/src/light_sync/response.rs index 96d2a8822e9..cf0c2fcac4c 100644 --- a/ethcore/sync/src/light_sync/response.rs +++ b/ethcore/sync/src/light_sync/response.rs @@ -16,7 +16,7 @@ //! Helpers for decoding and verifying responses for headers. -use types::{encoded, header::Header}; +use common_types::{encoded, header::Header}; use ethereum_types::H256; use light::request::{HashOrNumber, CompleteHeadersRequest as HeadersRequest}; use rlp::DecoderError; @@ -153,8 +153,8 @@ impl Constraint for Max { #[cfg(test)] mod tests { - use types::encoded; - use types::header::Header; + use common_types::encoded; + use common_types::header::Header; use light::request::CompleteHeadersRequest as HeadersRequest; use super::*; diff --git a/ethcore/sync/src/light_sync/sync_round.rs b/ethcore/sync/src/light_sync/sync_round.rs index 7c2a2bc0169..be458c331dd 100644 --- a/ethcore/sync/src/light_sync/sync_round.rs +++ b/ethcore/sync/src/light_sync/sync_round.rs @@ -20,11 +20,11 @@ use std::cmp::Ordering; use std::collections::{BinaryHeap, HashMap, HashSet, VecDeque}; use std::fmt; -use types::encoded; -use types::header::Header; +use common_types::{encoded, header::Header}; use light::net::ReqId; use light::request::CompleteHeadersRequest as HeadersRequest; +use log::trace; use network::PeerId; use ethereum_types::H256; @@ -37,7 +37,7 @@ const SCAFFOLD_ATTEMPTS: usize = 3; /// Context for a headers response. pub trait ResponseContext { /// Get the peer who sent this response. - fn responder(&self) -> PeerId; + fn responder(&self) -> PeerId; /// Get the request ID this response corresponds to. fn req_id(&self) -> &ReqId; /// Get the (unverified) response data. diff --git a/ethcore/sync/src/light_sync/tests/mod.rs b/ethcore/sync/src/light_sync/tests/mod.rs index 406f6ccb9e6..a8c1f0fc8de 100644 --- a/ethcore/sync/src/light_sync/tests/mod.rs +++ b/ethcore/sync/src/light_sync/tests/mod.rs @@ -14,11 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -use tests::helpers::TestNet; +use crate::tests::helpers::TestNet; use ethcore::test_helpers::EachBlockWith; use client_traits::BlockInfo; -use types::ids::BlockId; +use common_types::ids::BlockId; mod test_net; diff --git a/ethcore/sync/src/light_sync/tests/test_net.rs b/ethcore/sync/src/light_sync/tests/test_net.rs index 6eabda2bd9c..aa48fa0d017 100644 --- a/ethcore/sync/src/light_sync/tests/test_net.rs +++ b/ethcore/sync/src/light_sync/tests/test_net.rs @@ -18,26 +18,27 @@ use std::collections::{HashSet, VecDeque}; use std::sync::Arc; +use std::time::Duration; -use light_sync::*; -use tests::helpers::{TestNet, Peer as PeerLike, TestPacket}; +use crate::{ + light_sync::LightSync, + tests::helpers::{TestNet, Peer as PeerLike, TestPacket} +}; use ethcore::test_helpers::TestBlockChainClient; -use spec; -use io::IoChannel; -use kvdb_memorydb; -use light::client::fetch::{self, Unavailable}; -use light::net::{LightProtocol, IoContext, Capabilities, Params as LightParams}; -use light::provider::LightProvider; +use ethcore_io::IoChannel; +use light::{ + cache::Cache, + client::fetch::{self, Unavailable}, + net::{LightProtocol, IoContext, Capabilities, Params as LightParams}, + provider::LightProvider +}; use network::{NodeId, PeerId}; -use parking_lot::RwLock; - -use std::time::Duration; -use light::cache::Cache; +use parking_lot::{Mutex, RwLock}; const NETWORK_ID: u64 = 0xcafebabe; -pub type LightClient = ::light::client::Client; +pub type LightClient = light::client::Client; struct TestIoContext<'a> { queue: &'a RwLock>, @@ -49,7 +50,7 @@ impl<'a> IoContext for TestIoContext<'a> { fn send(&self, peer: PeerId, packet_id: u8, packet_body: Vec) { self.queue.write().push_back(TestPacket { data: packet_body, - packet_id: packet_id, + packet_id, recipient: peer, }) } @@ -64,11 +65,21 @@ impl<'a> IoContext for TestIoContext<'a> { self.to_disconnect.write().insert(peer); } - fn disable_peer(&self, peer: PeerId) { self.disconnect_peer(peer) } - fn protocol_version(&self, _peer: PeerId) -> Option { Some(::light::net::MAX_PROTOCOL_VERSION) } + fn disable_peer(&self, peer: PeerId) { + self.disconnect_peer(peer) + } - fn persistent_peer_id(&self, _peer: PeerId) -> Option { unimplemented!() } - fn is_reserved_peer(&self, _peer: PeerId) -> bool { false } + fn protocol_version(&self, _peer: PeerId) -> Option { + Some(light::net::MAX_PROTOCOL_VERSION) + } + + fn persistent_peer_id(&self, _peer: PeerId) -> Option { + unimplemented!() + } + + fn is_reserved_peer(&self, _peer: PeerId) -> bool { + false + } } // peer-specific data. @@ -219,7 +230,7 @@ impl TestNet { pub fn light(n_light: usize, n_full: usize) -> Self { let mut peers = Vec::with_capacity(n_light + n_full); for _ in 0..n_light { - let mut config = ::light::client::Config::default(); + let mut config = light::client::Config::default(); // skip full verification because the blocks are bad. config.verify_full = false; @@ -242,8 +253,8 @@ impl TestNet { peers.push(Arc::new(Peer::new_full(Arc::new(TestBlockChainClient::new())))) } - TestNet { - peers: peers, + Self { + peers, started: false, disconnect_events: Vec::new(), } diff --git a/ethcore/sync/src/snapshot_sync.rs b/ethcore/sync/src/snapshot_sync.rs index 6aa00738031..84b7b3e71a3 100644 --- a/ethcore/sync/src/snapshot_sync.rs +++ b/ethcore/sync/src/snapshot_sync.rs @@ -14,21 +14,22 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -use snapshot::SnapshotService; -use ethereum_types::H256; -use hash::keccak; -use types::snapshot::ManifestData; - use std::collections::HashSet; use std::iter::FromIterator; +use ethereum_types::H256; +use keccak_hash::keccak; +use log::trace; +use snapshot::SnapshotService; +use common_types::snapshot::ManifestData; + #[derive(PartialEq, Eq, Debug)] pub enum ChunkType { State(H256), Block(H256), } -#[derive(MallocSizeOf)] +#[derive(Default, MallocSizeOf)] pub struct Snapshot { pending_state_chunks: Vec, pending_block_chunks: Vec, @@ -41,16 +42,8 @@ pub struct Snapshot { impl Snapshot { /// Create a new instance. - pub fn new() -> Snapshot { - Snapshot { - pending_state_chunks: Vec::new(), - pending_block_chunks: Vec::new(), - downloading_chunks: HashSet::new(), - completed_chunks: HashSet::new(), - snapshot_hash: None, - bad_hashes: HashSet::new(), - initialized: false, - } + pub fn new() -> Self { + Default::default() } /// Sync the Snapshot completed chunks with the Snapshot Service @@ -176,10 +169,11 @@ impl Snapshot { #[cfg(test)] mod test { - use hash::keccak; + use super::{ChunkType, H256, Snapshot}; + use bytes::Bytes; - use super::*; - use types::snapshot::ManifestData; + use keccak_hash::keccak; + use common_types::snapshot::ManifestData; fn is_empty(snapshot: &Snapshot) -> bool { snapshot.pending_block_chunks.is_empty() && diff --git a/ethcore/sync/src/sync_io.rs b/ethcore/sync/src/sync_io.rs index 0f92220af6e..50fc74afcf7 100644 --- a/ethcore/sync/src/sync_io.rs +++ b/ethcore/sync/src/sync_io.rs @@ -16,15 +16,17 @@ use std::sync::Arc; use std::collections::HashMap; -use chain::sync_packet::{PacketInfo, SyncPacket}; -use network::{NetworkContext, PeerId, PacketId, Error, SessionInfo, ProtocolId}; -use network::client_version::ClientVersion; + +use crate::chain::sync_packet::{PacketInfo, SyncPacket}; + use bytes::Bytes; use client_traits::BlockChainClient; use ethcore_private_tx::PrivateStateDB; -use types::BlockNumber; -use snapshot::SnapshotService; +use network::client_version::ClientVersion; +use network::{NetworkContext, PeerId, PacketId, Error, SessionInfo, ProtocolId}; use parking_lot::RwLock; +use snapshot::SnapshotService; +use common_types::BlockNumber; /// IO interface for the syncing handler. /// Provides peer connection management and an interface to the blockchain client. diff --git a/ethcore/sync/src/tests/chain.rs b/ethcore/sync/src/tests/chain.rs index 8a47f005404..d1c175e7c2f 100644 --- a/ethcore/sync/src/tests/chain.rs +++ b/ethcore/sync/src/tests/chain.rs @@ -15,18 +15,20 @@ // along with Parity Ethereum. If not, see . use std::sync::Arc; -use types::ids::BlockId; -use client_traits::{BlockChainClient, ChainInfo}; + +use crate::{ + api::{SyncConfig, WarpSync}, + chain::SyncState, + tests::helpers::TestNet, +}; + +use client_traits::{BlockChainClient, BlockInfo, ChainInfo}; +use common_types::ids::BlockId; use ethcore::test_helpers::{TestBlockChainClient, EachBlockWith}; -use client_traits::BlockInfo; -use chain::SyncState; -use super::helpers::*; -use {SyncConfig, WarpSync}; -use spec; #[test] fn two_peers() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let mut net = TestNet::new(3); net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle); net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle); @@ -37,7 +39,7 @@ fn two_peers() { #[test] fn long_chain() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let mut net = TestNet::new(2); net.peer(1).chain.add_blocks(50000, EachBlockWith::Nothing); net.sync(); @@ -47,7 +49,7 @@ fn long_chain() { #[test] fn status_after_sync() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let mut net = TestNet::new(3); net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle); net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle); @@ -67,7 +69,7 @@ fn takes_few_steps() { #[test] fn empty_blocks() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let mut net = TestNet::new(3); for n in 0..200 { let with = if n % 2 == 0 { EachBlockWith::Nothing } else { EachBlockWith::Uncle }; @@ -81,7 +83,7 @@ fn empty_blocks() { #[test] fn forked() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let mut net = TestNet::new(3); net.peer(0).chain.add_blocks(30, EachBlockWith::Uncle); net.peer(1).chain.add_blocks(30, EachBlockWith::Uncle); @@ -102,7 +104,7 @@ fn forked() { #[test] fn forked_with_misbehaving_peer() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let mut net = TestNet::new(3); let mut alt_spec = spec::new_test(); @@ -126,7 +128,7 @@ fn forked_with_misbehaving_peer() { #[test] fn net_hard_fork() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let ref_client = TestBlockChainClient::new(); ref_client.add_blocks(50, EachBlockWith::Uncle); { @@ -145,7 +147,7 @@ fn net_hard_fork() { #[test] fn restart() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let mut net = TestNet::new(3); net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle); net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle); @@ -229,7 +231,7 @@ fn propagate_blocks() { #[test] fn restart_on_malformed_block() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let mut net = TestNet::new(2); net.peer(1).chain.add_blocks(5, EachBlockWith::Nothing); net.peer(1).chain.add_block(EachBlockWith::Nothing, |mut header| { @@ -255,7 +257,7 @@ fn reject_on_broken_chain() { #[test] fn disconnect_on_unrelated_chain() { - ::env_logger::try_init().ok(); + env_logger::try_init().ok(); let mut net = TestNet::new(2); net.peer(0).chain.set_history(Some(20)); net.peer(1).chain.set_history(Some(20)); diff --git a/ethcore/sync/src/tests/consensus.rs b/ethcore/sync/src/tests/consensus.rs index 1223678046b..6c444a27889 100644 --- a/ethcore/sync/src/tests/consensus.rs +++ b/ethcore/sync/src/tests/consensus.rs @@ -15,21 +15,24 @@ // along with Parity Ethereum. If not, see . use std::sync::Arc; -use hash::keccak; -use ethereum_types::{U256, Address}; -use io::{IoHandler, IoChannel}; + +use crate::{ + api::SyncConfig, + tests::helpers::{TestIoHandler, TestNet}, +}; + use client_traits::ChainInfo; use engine::signer; -use spec; use ethcore::client::Client; use ethcore::miner::{self, MinerService}; +use ethcore_io::{IoHandler, IoChannel}; +use ethereum_types::{U256, Address}; use ethkey::{KeyPair, Secret}; -use types::{ +use keccak_hash::keccak; +use common_types::{ io_message::ClientIoMessage, transaction::{Action, PendingTransaction, Transaction} }; -use super::helpers::*; -use SyncConfig; fn new_tx(secret: &Secret, nonce: U256, chain_id: u64) -> PendingTransaction { let signed = Transaction { diff --git a/ethcore/sync/src/tests/helpers.rs b/ethcore/sync/src/tests/helpers.rs index 515a777c543..2e91e2490c2 100644 --- a/ethcore/sync/src/tests/helpers.rs +++ b/ethcore/sync/src/tests/helpers.rs @@ -16,36 +16,42 @@ use std::collections::{VecDeque, HashSet, HashMap}; use std::sync::Arc; -use ethereum_types::H256; -use parking_lot::{RwLock, Mutex}; -use bytes::Bytes; -use network::{self, PeerId, ProtocolId, PacketId, SessionInfo}; -use network::client_version::ClientVersion; -use tests::snapshot::*; -use types::{ + +use crate::{ + api::{SyncConfig, WARP_SYNC_PROTOCOL_ID}, + chain::{ + sync_packet::{ + PacketInfo, + SyncPacket::{self, PrivateTransactionPacket, SignedPrivateTransactionPacket} + }, + ChainSync, SyncSupplier, ETH_PROTOCOL_VERSION_63, PAR_PROTOCOL_VERSION_4 + }, + private_tx::SimplePrivateTxHandler, + sync_io::SyncIo, + tests::snapshot::TestSnapshotService, +}; + +use client_traits::{BlockChainClient, ChainNotify}; +use common_types::{ chain_notify::{NewBlocks, ChainMessageType}, io_message::ClientIoMessage, + BlockNumber, }; -use client_traits::{BlockChainClient, ChainNotify}; use ethcore::{ client::{Client as EthcoreClient, ClientConfig}, - test_helpers::TestBlockChainClient + test_helpers::{self, TestBlockChainClient}, }; -use snapshot::SnapshotService; -use spec::{self, Spec}; -use ethcore_private_tx::PrivateStateDB; use ethcore::miner::Miner; -use ethcore::test_helpers; -use sync_io::SyncIo; -use io::{IoChannel, IoContext, IoHandler}; -use api::WARP_SYNC_PROTOCOL_ID; -use chain::{ChainSync, SyncSupplier, ETH_PROTOCOL_VERSION_63, PAR_PROTOCOL_VERSION_4}; -use chain::sync_packet::{PacketInfo, SyncPacket}; -use chain::sync_packet::SyncPacket::{PrivateTransactionPacket, SignedPrivateTransactionPacket}; - -use SyncConfig; -use private_tx::SimplePrivateTxHandler; -use types::BlockNumber; +use ethcore_io::{IoChannel, IoContext, IoHandler}; +use ethcore_private_tx::PrivateStateDB; +use ethereum_types::H256; +use bytes::Bytes; +use network::{self, PeerId, ProtocolId, PacketId, SessionInfo}; +use network::client_version::ClientVersion; +use log::trace; +use snapshot::SnapshotService; +use spec::Spec; +use parking_lot::{RwLock, Mutex}; pub trait FlushingBlockChainClient: BlockChainClient { fn flush(&self) {} @@ -123,7 +129,7 @@ impl<'p, C> SyncIo for TestIo<'p, C> where C: FlushingBlockChainClient, C: 'p { fn send(&mut self,peer_id: PeerId, packet_id: SyncPacket, data: Vec) -> Result<(), network::Error> { self.packets.push(TestPacket { - data: data, + data, packet_id: packet_id.id(), recipient: peer_id, }); @@ -135,11 +141,10 @@ impl<'p, C> SyncIo for TestIo<'p, C> where C: FlushingBlockChainClient, C: 'p { } fn peer_version(&self, peer_id: PeerId) -> ClientVersion { - let client_id = self.peers_info.get(&peer_id) + self.peers_info.get(&peer_id) .cloned() - .unwrap_or_else(|| peer_id.to_string()); - - ClientVersion::from(client_id) + .unwrap_or_else(|| peer_id.to_string()) + .into() } fn snapshot_service(&self) -> &dyn SnapshotService { diff --git a/ethcore/sync/src/tests/private.rs b/ethcore/sync/src/tests/private.rs index be9df431eb0..2a71973326e 100644 --- a/ethcore/sync/src/tests/private.rs +++ b/ethcore/sync/src/tests/private.rs @@ -15,32 +15,38 @@ // along with Parity Ethereum. If not, see . use std::sync::Arc; -use hash::keccak; -use io::{IoHandler, IoChannel}; -use types::transaction::{Transaction, Action}; -use types::{ + +use crate::{ + api::SyncConfig, + tests::helpers::{TestIoHandler, TestNet} +}; + +use client_traits::BlockChainClient; +use common_types::{ ids::BlockId, io_message::ClientIoMessage, + transaction::{Transaction, Action}, }; -use client_traits::BlockChainClient; use engine::signer; use ethcore::{ client::Client, miner::{self, MinerService}, test_helpers::{CreateContractAddress, push_block_with_transactions, new_db}, }; -use ethcore_private_tx::{Provider, ProviderConfig, NoopEncryptor, Importer, SignedPrivateTransaction, StoringKeyProvider}; +use ethcore_io::{IoHandler, IoChannel}; +use ethcore_private_tx::{ + Provider, ProviderConfig, NoopEncryptor, Importer, SignedPrivateTransaction, StoringKeyProvider +}; use ethkey::KeyPair; +use keccak_hash::keccak; use machine::executive::contract_address; -use tests::helpers::{TestNet, TestIoHandler}; use rustc_hex::FromHex; use rlp::Rlp; use spec::Spec; -use SyncConfig; fn seal_spec() -> Spec { let spec_data = include_str!("../res/private_spec.json"); - Spec::load(&::std::env::temp_dir(), spec_data.as_bytes()).unwrap() + Spec::load(&std::env::temp_dir(), spec_data.as_bytes()).unwrap() } #[test] diff --git a/ethcore/sync/src/tests/snapshot.rs b/ethcore/sync/src/tests/snapshot.rs index deeb8dcf60f..484349154ee 100644 --- a/ethcore/sync/src/tests/snapshot.rs +++ b/ethcore/sync/src/tests/snapshot.rs @@ -16,37 +16,35 @@ use std::collections::HashMap; use std::sync::Arc; -use hash::keccak; + +use crate::{ + api::{SyncConfig, WarpSync}, + tests::helpers::TestNet +}; + +use bytes::Bytes; +use ethcore::test_helpers::EachBlockWith; use ethereum_types::H256; +use keccak_hash::keccak; use parking_lot::Mutex; -use bytes::Bytes; use snapshot::SnapshotService; -use ethcore::test_helpers::EachBlockWith; -use types::{ +use common_types::{ BlockNumber, snapshot::{ManifestData, RestorationStatus}, }; -use super::helpers::*; -use {SyncConfig, WarpSync}; +#[derive(Default)] pub struct TestSnapshotService { manifest: Option, chunks: HashMap, - restoration_manifest: Mutex>, state_restoration_chunks: Mutex>, block_restoration_chunks: Mutex>, } 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()), - } + pub fn new() -> Self { + Default::default() } pub fn new_with_snapshot(num_chunks: usize, block_hash: H256, block_number: BlockNumber) -> TestSnapshotService { diff --git a/ethcore/sync/src/transactions_stats.rs b/ethcore/sync/src/transactions_stats.rs index 853cf232d36..0c5ebfee912 100644 --- a/ethcore/sync/src/transactions_stats.rs +++ b/ethcore/sync/src/transactions_stats.rs @@ -14,12 +14,14 @@ // You should have received a copy of the GNU General Public License // along with Parity Ethereum. If not, see . -use api::TransactionStats; use std::hash::BuildHasher; use std::collections::{HashSet, HashMap}; + +use crate::api::TransactionStats; + use ethereum_types::{H256, H512}; use fastmap::H256FastMap; -use types::BlockNumber; +use common_types::BlockNumber; type NodeId = H512; @@ -89,9 +91,9 @@ impl TransactionsStats { #[cfg(test)] mod tests { - use std::collections::{HashMap, HashSet}; use super::{Stats, TransactionsStats, NodeId, H256}; + use macros::hash_map; #[test] fn should_keep_track_of_propagations() { From 2627288311e952ff3b6b9675dfeda24f75b8249e Mon Sep 17 00:00:00 2001 From: Talha Cross <47772477+soc1c@users.noreply.github.com> Date: Fri, 20 Sep 2019 09:53:35 +0200 Subject: [PATCH 2/2] =?UTF-8?q?ethcore/res:=20activate=20Istanbul=20on=20R?= =?UTF-8?q?opsten,=20G=C3=B6rli,=20Rinkeby,=20Kovan=20(#11068)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ethcore/res: activate Istanbul on Ropsten block 6485846 * ethcore/res: activate Istanbul on Goerli block 1561651 * ethcore/res: use hex values for Istanbul specs * ethcore/res: fix trailing comma * ethcore/res: be pedantic about EIP-1283 in Petersburg and Istanbul test specs * ethcore/res: activate Istanbul on Rinkeby block 5435345 * ethcore/res: activate Istanbul on Kovan block 14111141 * ethcore/res: fix kovan istanbul number to 0xd751a5 --- ethcore/res/ethereum/goerli.json | 22 ++++++++++++++++++---- ethcore/res/ethereum/istanbul_test.json | 1 + ethcore/res/ethereum/kovan.json | 22 +++++++++++++++++++--- ethcore/res/ethereum/rinkeby.json | 22 ++++++++++++++++++---- ethcore/res/ethereum/ropsten.json | 24 +++++++++++++++++++----- ethcore/res/ethereum/st_peters_test.json | 1 + 6 files changed, 76 insertions(+), 16 deletions(-) diff --git a/ethcore/res/ethereum/goerli.json b/ethcore/res/ethereum/goerli.json index 2a0fdc0cabd..e7aaa4c485a 100644 --- a/ethcore/res/ethereum/goerli.json +++ b/ethcore/res/ethereum/goerli.json @@ -26,6 +26,11 @@ "eip1052Transition": "0x0", "eip1283Transition": "0x0", "eip1283DisableTransition": "0x0", + "eip1283ReenableTransition": "0x17d433", + "eip1344Transition": "0x17d433", + "eip1706Transition": "0x17d433", + "eip1884Transition": "0x17d433", + "eip2028Transition": "0x17d433", "gasLimitBoundDivisor": "0x400", "maxCodeSize": "0x6000", "maxCodeSizeTransition": "0x0", @@ -126,7 +131,7 @@ "builtin": { "name": "alt_bn128_add", "activate_at": "0x00", - "eip1108_transition": "0x7fffffffffffff", + "eip1108_transition": "0x17d433", "pricing": { "alt_bn128_const_operations": { "price": 500, @@ -140,7 +145,7 @@ "builtin": { "name": "alt_bn128_mul", "activate_at": "0x00", - "eip1108_transition": "0x7fffffffffffff", + "eip1108_transition": "0x17d433", "pricing": { "alt_bn128_const_operations": { "price": 40000, @@ -154,7 +159,7 @@ "builtin": { "name": "alt_bn128_pairing", "activate_at": "0x00", - "eip1108_transition": "0x7fffffffffffff", + "eip1108_transition": "0x17d433", "pricing": { "alt_bn128_pairing": { "base": 100000, @@ -166,7 +171,16 @@ } }, "0x0000000000000000000000000000000000000009": { - "balance": "0x1" + "balance": "0x1", + "builtin": { + "name": "blake2_f", + "activate_at": "0x17d433", + "pricing": { + "blake2_f": { + "gas_per_round": 1 + } + } + } }, "0x000000000000000000000000000000000000000a": { "balance": "0x1" diff --git a/ethcore/res/ethereum/istanbul_test.json b/ethcore/res/ethereum/istanbul_test.json index 1f93c8319dd..1276dc4c53c 100644 --- a/ethcore/res/ethereum/istanbul_test.json +++ b/ethcore/res/ethereum/istanbul_test.json @@ -37,6 +37,7 @@ "eip1014Transition": "0x0", "eip1052Transition": "0x0", "eip1283Transition": "0x0", + "eip1283DisableTransition": "0x0", "eip1283ReenableTransition": "0x0", "eip1344Transition": "0x0", "eip1706Transition": "0x0", diff --git a/ethcore/res/ethereum/kovan.json b/ethcore/res/ethereum/kovan.json index 44a1f3c6dc5..cba31899bd4 100644 --- a/ethcore/res/ethereum/kovan.json +++ b/ethcore/res/ethereum/kovan.json @@ -64,6 +64,11 @@ "eip1052Transition": "0x8c6180", "eip1283Transition": "0x8c6180", "eip1283DisableTransition": "0x9c7b61", + "eip1283ReenableTransition": "0xd751a5", + "eip1344Transition": "0xd751a5", + "eip1706Transition": "0xd751a5", + "eip1884Transition": "0xd751a5", + "eip2028Transition": "0xd751a5", "kip4Transition": "0x8c6180", "kip6Transition": "0x8c6180" }, @@ -6722,7 +6727,7 @@ "builtin": { "name": "alt_bn128_add", "activate_at": "0x4d50f8", - "eip1108_transition": "0x7fffffffffffff", + "eip1108_transition": "0xd751a5", "pricing": { "alt_bn128_const_operations": { "price": 500, @@ -6735,7 +6740,7 @@ "builtin": { "name": "alt_bn128_mul", "activate_at": "0x4d50f8", - "eip1108_transition": "0x7fffffffffffff", + "eip1108_transition": "0xd751a5", "pricing": { "alt_bn128_const_operations": { "price": 40000, @@ -6748,7 +6753,7 @@ "builtin": { "name": "alt_bn128_pairing", "activate_at": "0x4d50f8", - "eip1108_transition": "0x7fffffffffffff", + "eip1108_transition": "0xd751a5", "pricing": { "alt_bn128_pairing": { "base": 100000, @@ -6759,6 +6764,17 @@ } } }, + "0x0000000000000000000000000000000000000009": { + "builtin": { + "name": "blake2_f", + "activate_at": "0xd751a5", + "pricing": { + "blake2_f": { + "gas_per_round": 1 + } + } + } + }, "0x00521965e7bd230323c423d96c657db5b79d099f": { "balance": "1606938044258990275541962092341162602522202993782792835301376" } diff --git a/ethcore/res/ethereum/rinkeby.json b/ethcore/res/ethereum/rinkeby.json index b6c7abe20d7..3945b88118d 100644 --- a/ethcore/res/ethereum/rinkeby.json +++ b/ethcore/res/ethereum/rinkeby.json @@ -26,6 +26,11 @@ "eip1052Transition": "0x37db77", "eip1283Transition": "0x37db77", "eip1283DisableTransition": "0x41efd2", + "eip1283ReenableTransition": "0x52efd1", + "eip1344Transition": "0x52efd1", + "eip1706Transition": "0x52efd1", + "eip1884Transition": "0x52efd1", + "eip2028Transition": "0x52efd1", "gasLimitBoundDivisor": "0x400", "maxCodeSize": "0x6000", "maxCodeSizeTransition": "0x0", @@ -120,7 +125,7 @@ "builtin": { "name": "alt_bn128_add", "activate_at": "0xfcc25", - "eip1108_transition": "0x7fffffffffffff", + "eip1108_transition": "0x52efd1", "pricing": { "alt_bn128_const_operations": { "price": 500, @@ -133,7 +138,7 @@ "builtin": { "name": "alt_bn128_mul", "activate_at": "0xfcc25", - "eip1108_transition": "0x7fffffffffffff", + "eip1108_transition": "0x52efd1", "pricing": { "alt_bn128_const_operations": { "price": 40000, @@ -146,7 +151,7 @@ "builtin": { "name": "alt_bn128_pairing", "activate_at": "0xfcc25", - "eip1108_transition": "0x7fffffffffffff", + "eip1108_transition": "0x52efd1", "pricing": { "alt_bn128_pairing": { "base": 100000, @@ -158,7 +163,16 @@ } }, "0x0000000000000000000000000000000000000009": { - "balance": "0x1" + "balance": "0x1", + "builtin": { + "name": "blake2_f", + "activate_at": "0x52efd1", + "pricing": { + "blake2_f": { + "gas_per_round": 1 + } + } + } }, "0x000000000000000000000000000000000000000a": { "balance": "0x1" diff --git a/ethcore/res/ethereum/ropsten.json b/ethcore/res/ethereum/ropsten.json index 2d23a0a2974..aefca8682cc 100644 --- a/ethcore/res/ethereum/ropsten.json +++ b/ethcore/res/ethereum/ropsten.json @@ -45,7 +45,12 @@ "eip1014Transition": "0x408b70", "eip1052Transition": "0x408b70", "eip1283Transition": "0x408b70", - "eip1283DisableTransition": "0x4b5e82" + "eip1283DisableTransition": "0x4b5e82", + "eip1283ReenableTransition": "0x62f756", + "eip1344Transition": "0x62f756", + "eip1706Transition": "0x62f756", + "eip1884Transition": "0x62f756", + "eip2028Transition": "0x62f756" }, "genesis": { "seal": { @@ -2735,7 +2740,7 @@ "builtin": { "name": "alt_bn128_add", "activate_at": "0x19f0a0", - "eip1108_transition": "0x7fffffffffffff", + "eip1108_transition": "0x62f756", "pricing": { "alt_bn128_const_operations": { "price": 500, @@ -2750,7 +2755,7 @@ "builtin": { "name": "alt_bn128_mul", "activate_at": "0x19f0a0", - "eip1108_transition": "0x7fffffffffffff", + "eip1108_transition": "0x62f756", "pricing": { "alt_bn128_const_operations": { "price": 40000, @@ -2765,7 +2770,7 @@ "builtin": { "name": "alt_bn128_pairing", "activate_at": "0x19f0a0", - "eip1108_transition": "0x7fffffffffffff", + "eip1108_transition": "0x62f756", "pricing": { "alt_bn128_pairing": { "base": 100000, @@ -2777,7 +2782,16 @@ } }, "0x0000000000000000000000000000000000000009": { - "balance": "0x1" + "balance": "0x1", + "builtin": { + "name": "blake2_f", + "activate_at": "0x62f756", + "pricing": { + "blake2_f": { + "gas_per_round": 1 + } + } + } }, "0x000000000000000000000000000000000000000a": { "balance": "0x0" diff --git a/ethcore/res/ethereum/st_peters_test.json b/ethcore/res/ethereum/st_peters_test.json index 9ae2f748949..768e6057fd2 100644 --- a/ethcore/res/ethereum/st_peters_test.json +++ b/ethcore/res/ethereum/st_peters_test.json @@ -36,6 +36,7 @@ "eip145Transition": "0x0", "eip1014Transition": "0x0", "eip1052Transition": "0x0", + "eip1283Transition": "0x0", "eip1283DisableTransition": "0x0" }, "genesis": {