Expanse compatibility (#2369)

* Add support for Expanse.

* Fix build.

* Refactor to be able to alter the eth subprotocol name

* Fix JSON.

* Support exp hardfork.

* Fix exp json again.

* Fixed test

* Fix tests.
This commit is contained in:
Gav Wood
2016-09-28 14:21:59 +02:00
committed by GitHub
parent fb92a98451
commit 15a14a5f49
15 changed files with 190 additions and 45 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};
@@ -29,9 +30,6 @@ use ipc::{BinaryConvertable, BinaryConvertError, IpcConfig};
use std::str::FromStr;
use parking_lot::RwLock;
/// Ethereum sync protocol
pub const ETH_PROTOCOL: &'static str = "eth";
/// Sync configuration
#[derive(Debug, Clone, Copy)]
pub struct SyncConfig {
@@ -39,6 +37,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)>,
}
@@ -48,6 +48,7 @@ impl Default for SyncConfig {
SyncConfig {
max_download_ahead_blocks: 20000,
network_id: U256::from(1),
subprotocol_name: *b"eth",
fork_block: None,
}
}
@@ -68,6 +69,8 @@ pub struct EthSync {
network: NetworkService,
/// Protocol handler
handler: Arc<SyncProtocolHandler>,
/// The main subprotocol name
subprotocol_name: [u8; 3],
}
impl EthSync {
@@ -78,6 +81,7 @@ impl EthSync {
let sync = Arc::new(EthSync{
network: service,
handler: Arc::new(SyncProtocolHandler { sync: RwLock::new(chain_sync), chain: chain, snapshot_service: snapshot_service }),
subprotocol_name: config.subprotocol_name,
});
Ok(sync)
@@ -135,7 +139,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.handler.chain, &*self.handler.snapshot_service);
self.handler.sync.write().chain_new_blocks(
&mut sync_io,
@@ -149,7 +153,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.handler.clone(), ETH_PROTOCOL, &[62u8, 63u8, 64u8])
self.network.register_protocol(self.handler.clone(), self.subprotocol_name, &[62u8, 63u8, 64u8])
.unwrap_or_else(|e| warn!("Error registering ethereum protocol: {:?}", e));
}
@@ -204,7 +208,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.handler.chain, &*self.handler.snapshot_service);
self.handler.sync.write().abort(&mut sync_io);
});

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)
}
}