Merge remote-tracking branch 'parity/master' into bft

Conflicts:
	ethcore/src/client/client.rs
	sync/src/api.rs
This commit is contained in:
keorn
2016-09-30 12:44:52 +01:00
99 changed files with 2515 additions and 897 deletions

View File

@@ -15,6 +15,7 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::sync::Arc;
use std::str;
use network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId,
NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, NetworkError};
use util::{U256, H256};
@@ -31,9 +32,9 @@ use std::str::FromStr;
use parking_lot::RwLock;
/// Ethereum sync protocol
pub const ETH_PROTOCOL: &'static str = "eth";
pub const ETH_PROTOCOL: [u8; 3] = *b"eth";
/// Infinity protocol
pub const INF_PROTOCOL: &'static str = "inf";
pub const INF_PROTOCOL: [u8; 3] = *b"inf";
/// Sync configuration
#[derive(Debug, Clone, Copy)]
@@ -42,6 +43,8 @@ pub struct SyncConfig {
pub max_download_ahead_blocks: usize,
/// Network ID
pub network_id: U256,
/// Main "eth" subprotocol name.
pub subprotocol_name: [u8; 3],
/// Fork block to check
pub fork_block: Option<(BlockNumber, H256)>,
}
@@ -51,6 +54,7 @@ impl Default for SyncConfig {
SyncConfig {
max_download_ahead_blocks: 20000,
network_id: U256::from(1),
subprotocol_name: ETH_PROTOCOL,
fork_block: None,
}
}
@@ -73,6 +77,8 @@ pub struct EthSync {
eth_handler: Arc<SyncProtocolHandler>,
/// Infinity Protocol handler
inf_handler: Arc<InfProtocolHandler>,
/// The main subprotocol name
subprotocol_name: [u8; 3],
}
impl EthSync {
@@ -85,6 +91,7 @@ impl EthSync {
network: service,
eth_handler: Arc::new(SyncProtocolHandler { sync: RwLock::new(chain_sync), chain: chain.clone(), snapshot_service: snapshot_service.clone() }),
inf_handler: Arc::new(InfProtocolHandler { sync: RwLock::new(inf_sync), chain: chain, snapshot_service: snapshot_service }),
subprotocol_name: config.subprotocol_name,
});
Ok(sync)
@@ -171,7 +178,7 @@ impl ChainNotify for EthSync {
sealed: Vec<H256>,
_duration: u64)
{
self.network.with_context(ETH_PROTOCOL, |context| {
self.network.with_context(self.subprotocol_name, |context| {
let mut sync_io = NetSyncIo::new(context, &*self.eth_handler.chain, &*self.eth_handler.snapshot_service);
self.eth_handler.sync.write().chain_new_blocks(
&mut sync_io,
@@ -185,7 +192,7 @@ impl ChainNotify for EthSync {
fn start(&self) {
self.network.start().unwrap_or_else(|e| warn!("Error starting network: {:?}", e));
self.network.register_protocol(self.eth_handler.clone(), ETH_PROTOCOL, &[62u8, 63u8, 64u8])
self.network.register_protocol(self.eth_handler.clone(), self.subprotocol_name, &[62u8, 63u8, 64u8])
.unwrap_or_else(|e| warn!("Error registering ethereum protocol: {:?}", e));
self.network.register_protocol(self.inf_handler.clone(), INF_PROTOCOL, &[1u8])
.unwrap_or_else(|e| warn!("Error registering infinity protocol: {:?}", e));
@@ -249,7 +256,7 @@ impl ManageNetwork for EthSync {
}
fn stop_network(&self) {
self.network.with_context(ETH_PROTOCOL, |context| {
self.network.with_context(self.subprotocol_name, |context| {
let mut sync_io = NetSyncIo::new(context, &*self.eth_handler.chain, &*self.eth_handler.snapshot_service);
self.eth_handler.sync.write().abort(&mut sync_io);
});

View File

@@ -19,7 +19,7 @@ use rlp::*;
use network::NetworkError;
use ethcore::header::{ Header as BlockHeader};
known_heap_size!(0, HeaderId, SyncBlock);
known_heap_size!(0, HeaderId);
/// Block data with optional body.
struct SyncBlock {
@@ -27,6 +27,12 @@ struct SyncBlock {
body: Option<Bytes>,
}
impl HeapSizeOf for SyncBlock {
fn heap_size_of_children(&self) -> usize {
self.header.heap_size_of_children() + self.body.heap_size_of_children()
}
}
/// Used to identify header by transactions and uncles hashes
#[derive(Eq, PartialEq, Hash)]
struct HeaderId {
@@ -219,10 +225,14 @@ impl BlockCollection {
self.blocks.contains_key(hash)
}
/// Return heap size.
/// Return used heap size.
pub fn heap_size(&self) -> usize {
//TODO: other collections
self.blocks.heap_size_of_children()
self.heads.heap_size_of_children()
+ self.blocks.heap_size_of_children()
+ self.parents.heap_size_of_children()
+ self.header_ids.heap_size_of_children()
+ self.downloading_headers.heap_size_of_children()
+ self.downloading_bodies.heap_size_of_children()
}
/// Check if given block hash is marked as being downloaded.

View File

@@ -17,7 +17,6 @@
use network::{NetworkContext, PeerId, PacketId, NetworkError};
use ethcore::client::BlockChainClient;
use ethcore::snapshot::SnapshotService;
use api::ETH_PROTOCOL;
/// IO interface for the syning handler.
/// Provides peer connection management and an interface to the blockchain client.
@@ -101,7 +100,7 @@ impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> {
}
fn eth_protocol_version(&self, peer_id: PeerId) -> u8 {
self.network.protocol_version(peer_id, ETH_PROTOCOL).unwrap_or(0)
self.network.protocol_version(peer_id, self.network.subprotocol_name()).unwrap_or(0)
}
}