Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit d629b1b

Browse files
committed
Support diferent packet counts in different protocol versions.
1 parent e30839e commit d629b1b

File tree

8 files changed

+54
-48
lines changed

8 files changed

+54
-48
lines changed

ethcore/light/src/net/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,17 @@ const RECALCULATE_COSTS_INTERVAL: Duration = Duration::from_secs(60 * 60);
7575
// minimum interval between updates.
7676
const UPDATE_INTERVAL: Duration = Duration::from_millis(5000);
7777

78+
/// Packet count for PIP.
79+
const PACKET_COUNT_V1: u8 = 9;
80+
7881
/// Supported protocol versions.
79-
pub const PROTOCOL_VERSIONS: &'static [u8] = &[1];
82+
pub const PROTOCOL_VERSIONS: &'static [(u8, u8)] = &[
83+
(1, PACKET_COUNT_V1),
84+
];
8085

8186
/// Max protocol version.
8287
pub const MAX_PROTOCOL_VERSION: u8 = 1;
8388

84-
/// Packet count for PIP.
85-
pub const PACKET_COUNT: u8 = 9;
8689

8790
// packet ID definitions.
8891
mod packet {
@@ -688,7 +691,7 @@ impl LightProtocol {
688691
Err(e) => { punish(*peer, io, e); return }
689692
};
690693

691-
if PROTOCOL_VERSIONS.iter().find(|x| **x == proto_version).is_none() {
694+
if PROTOCOL_VERSIONS.iter().find(|x| x.0 == proto_version).is_none() {
692695
punish(*peer, io, Error::UnsupportedProtocolVersion(proto_version));
693696
return;
694697
}

ethcore/sync/src/api.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use chain::{ChainSync, SyncStatus as EthSyncStatus};
3333
use std::net::{SocketAddr, AddrParseError};
3434
use std::str::FromStr;
3535
use parking_lot::RwLock;
36-
use chain::{ETH_PACKET_COUNT, SNAPSHOT_SYNC_PACKET_COUNT, ETH_PROTOCOL_VERSION_63, ETH_PROTOCOL_VERSION_62,
36+
use chain::{ETH_PROTOCOL_VERSION_63, ETH_PROTOCOL_VERSION_62,
3737
PAR_PROTOCOL_VERSION_1, PAR_PROTOCOL_VERSION_2, PAR_PROTOCOL_VERSION_3};
3838
use light::client::AsLightClient;
3939
use light::Provider;
@@ -202,18 +202,15 @@ pub struct AttachedProtocol {
202202
pub handler: Arc<NetworkProtocolHandler + Send + Sync>,
203203
/// 3-character ID for the protocol.
204204
pub protocol_id: ProtocolId,
205-
/// Packet count.
206-
pub packet_count: u8,
207-
/// Supported versions.
208-
pub versions: &'static [u8],
205+
/// Supported versions and their packet counts.
206+
pub versions: &'static [(u8, u8)],
209207
}
210208

211209
impl AttachedProtocol {
212210
fn register(&self, network: &NetworkService) {
213211
let res = network.register_protocol(
214212
self.handler.clone(),
215213
self.protocol_id,
216-
self.packet_count,
217214
self.versions
218215
);
219216

@@ -456,15 +453,15 @@ impl ChainNotify for EthSync {
456453
Err(err) => warn!("Error starting network: {}", err),
457454
_ => {},
458455
}
459-
self.network.register_protocol(self.eth_handler.clone(), self.subprotocol_name, ETH_PACKET_COUNT, &[ETH_PROTOCOL_VERSION_62, ETH_PROTOCOL_VERSION_63])
456+
self.network.register_protocol(self.eth_handler.clone(), self.subprotocol_name, &[ETH_PROTOCOL_VERSION_62, ETH_PROTOCOL_VERSION_63])
460457
.unwrap_or_else(|e| warn!("Error registering ethereum protocol: {:?}", e));
461458
// register the warp sync subprotocol
462-
self.network.register_protocol(self.eth_handler.clone(), WARP_SYNC_PROTOCOL_ID, SNAPSHOT_SYNC_PACKET_COUNT, &[PAR_PROTOCOL_VERSION_1, PAR_PROTOCOL_VERSION_2, PAR_PROTOCOL_VERSION_3])
459+
self.network.register_protocol(self.eth_handler.clone(), WARP_SYNC_PROTOCOL_ID, &[PAR_PROTOCOL_VERSION_1, PAR_PROTOCOL_VERSION_2, PAR_PROTOCOL_VERSION_3])
463460
.unwrap_or_else(|e| warn!("Error registering snapshot sync protocol: {:?}", e));
464461

465462
// register the light protocol.
466463
if let Some(light_proto) = self.light_proto.as_ref().map(|x| x.clone()) {
467-
self.network.register_protocol(light_proto, self.light_subprotocol_name, ::light::net::PACKET_COUNT, ::light::net::PROTOCOL_VERSIONS)
464+
self.network.register_protocol(light_proto, self.light_subprotocol_name, ::light::net::PROTOCOL_VERSIONS)
468465
.unwrap_or_else(|e| warn!("Error registering light client protocol: {:?}", e));
469466
}
470467

@@ -824,7 +821,7 @@ impl ManageNetwork for LightSync {
824821

825822
let light_proto = self.proto.clone();
826823

827-
self.network.register_protocol(light_proto, self.subprotocol_name, ::light::net::PACKET_COUNT, ::light::net::PROTOCOL_VERSIONS)
824+
self.network.register_protocol(light_proto, self.subprotocol_name, ::light::net::PROTOCOL_VERSIONS)
828825
.unwrap_or_else(|e| warn!("Error registering light client protocol: {:?}", e));
829826

830827
for proto in &self.attached_protos { proto.register(&self.network) }

ethcore/sync/src/chain.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ known_heap_size!(0, PeerInfo);
120120
type PacketDecodeError = DecoderError;
121121

122122
/// 63 version of Ethereum protocol.
123-
pub const ETH_PROTOCOL_VERSION_63: u8 = 63;
123+
pub const ETH_PROTOCOL_VERSION_63: (u8, u8) = (63, 0x11);
124124
/// 62 version of Ethereum protocol.
125-
pub const ETH_PROTOCOL_VERSION_62: u8 = 62;
126-
/// 1 version of Parity protocol.
127-
pub const PAR_PROTOCOL_VERSION_1: u8 = 1;
125+
pub const ETH_PROTOCOL_VERSION_62: (u8, u8) = (62, 0x11);
126+
/// 1 version of Parity protocol and the packet count.
127+
pub const PAR_PROTOCOL_VERSION_1: (u8, u8) = (1, 0x15);
128128
/// 2 version of Parity protocol (consensus messages added).
129-
pub const PAR_PROTOCOL_VERSION_2: u8 = 2;
129+
pub const PAR_PROTOCOL_VERSION_2: (u8, u8) = (2, 0x16);
130130
/// 3 version of Parity protocol (private transactions messages added).
131-
pub const PAR_PROTOCOL_VERSION_3: u8 = 3;
131+
pub const PAR_PROTOCOL_VERSION_3: (u8, u8) = (3, 0x18);
132132

133133
const MAX_BODIES_TO_SEND: usize = 256;
134134
const MAX_HEADERS_TO_SEND: usize = 512;
@@ -162,8 +162,6 @@ const NODE_DATA_PACKET: u8 = 0x0e;
162162
const GET_RECEIPTS_PACKET: u8 = 0x0f;
163163
const RECEIPTS_PACKET: u8 = 0x10;
164164

165-
pub const ETH_PACKET_COUNT: u8 = 0x11;
166-
167165
const GET_SNAPSHOT_MANIFEST_PACKET: u8 = 0x11;
168166
const SNAPSHOT_MANIFEST_PACKET: u8 = 0x12;
169167
const GET_SNAPSHOT_DATA_PACKET: u8 = 0x13;
@@ -172,8 +170,6 @@ const CONSENSUS_DATA_PACKET: u8 = 0x15;
172170
const PRIVATE_TRANSACTION_PACKET: u8 = 0x16;
173171
const SIGNED_PRIVATE_TRANSACTION_PACKET: u8 = 0x17;
174172

175-
pub const SNAPSHOT_SYNC_PACKET_COUNT: u8 = 0x18;
176-
177173
const MAX_SNAPSHOT_CHUNKS_DOWNLOAD_AHEAD: usize = 3;
178174

179175
const WAIT_PEERS_TIMEOUT: Duration = Duration::from_secs(5);
@@ -442,7 +438,7 @@ impl ChainSync {
442438
let last_imported_number = self.new_blocks.last_imported_block_number();
443439
SyncStatus {
444440
state: self.state.clone(),
445-
protocol_version: ETH_PROTOCOL_VERSION_63,
441+
protocol_version: ETH_PROTOCOL_VERSION_63.0,
446442
network_id: self.network_id,
447443
start_block_number: self.starting_block,
448444
last_imported_block_number: Some(last_imported_number),
@@ -676,8 +672,10 @@ impl ChainSync {
676672
trace!(target: "sync", "Peer {} network id mismatch (ours: {}, theirs: {})", peer_id, self.network_id, peer.network_id);
677673
return Ok(());
678674
}
679-
if (warp_protocol && peer.protocol_version != PAR_PROTOCOL_VERSION_1 && peer.protocol_version != PAR_PROTOCOL_VERSION_2 && peer.protocol_version != PAR_PROTOCOL_VERSION_3)
680-
|| (!warp_protocol && peer.protocol_version != ETH_PROTOCOL_VERSION_63 && peer.protocol_version != ETH_PROTOCOL_VERSION_62) {
675+
if false
676+
|| (warp_protocol && (peer.protocol_version < PAR_PROTOCOL_VERSION_1.0 || peer.protocol_version > PAR_PROTOCOL_VERSION_3.0))
677+
|| (!warp_protocol && (peer.protocol_version < ETH_PROTOCOL_VERSION_62.0 || peer.protocol_version > ETH_PROTOCOL_VERSION_63.0))
678+
{
681679
io.disable_peer(peer_id);
682680
trace!(target: "sync", "Peer {} unsupported eth protocol ({})", peer_id, peer.protocol_version);
683681
return Ok(());
@@ -1479,7 +1477,7 @@ impl ChainSync {
14791477
}
14801478
peer.asking = asking;
14811479
peer.ask_time = Instant::now();
1482-
let result = if packet_id >= ETH_PACKET_COUNT {
1480+
let result = if packet_id >= ETH_PROTOCOL_VERSION_63.0 {
14831481
sync.send_protocol(WARP_SYNC_PROTOCOL_ID, peer_id, packet_id, packet)
14841482
} else {
14851483
sync.send(peer_id, packet_id, packet)
@@ -1527,7 +1525,7 @@ impl ChainSync {
15271525
fn send_status(&mut self, io: &mut SyncIo, peer: PeerId) -> Result<(), network::Error> {
15281526
let warp_protocol_version = io.protocol_version(&WARP_SYNC_PROTOCOL_ID, peer);
15291527
let warp_protocol = warp_protocol_version != 0;
1530-
let protocol = if warp_protocol { warp_protocol_version } else { ETH_PROTOCOL_VERSION_63 };
1528+
let protocol = if warp_protocol { warp_protocol_version } else { ETH_PROTOCOL_VERSION_63.0 };
15311529
trace!(target: "sync", "Sending status to {}, protocol version {}", peer, protocol);
15321530
let mut packet = RlpStream::new_list(if warp_protocol { 7 } else { 5 });
15331531
let chain = io.chain().chain_info();
@@ -1959,11 +1957,11 @@ impl ChainSync {
19591957
}
19601958

19611959
fn get_consensus_peers(&self) -> Vec<PeerId> {
1962-
self.peers.iter().filter_map(|(id, p)| if p.protocol_version >= PAR_PROTOCOL_VERSION_2 { Some(*id) } else { None }).collect()
1960+
self.peers.iter().filter_map(|(id, p)| if p.protocol_version >= PAR_PROTOCOL_VERSION_2.0 { Some(*id) } else { None }).collect()
19631961
}
19641962

19651963
fn get_private_transaction_peers(&self) -> Vec<PeerId> {
1966-
self.peers.iter().filter_map(|(id, p)| if p.protocol_version >= PAR_PROTOCOL_VERSION_3 { Some(*id) } else { None }).collect()
1964+
self.peers.iter().filter_map(|(id, p)| if p.protocol_version >= PAR_PROTOCOL_VERSION_3.0 { Some(*id) } else { None }).collect()
19671965
}
19681966

19691967
/// propagates latest block to a set of peers

parity/whisper.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,13 @@ pub fn setup(target_pool_size: usize, protos: &mut Vec<AttachedProtocol>)
8989

9090
protos.push(AttachedProtocol {
9191
handler: net.clone() as Arc<_>,
92-
packet_count: whisper_net::PACKET_COUNT,
9392
versions: whisper_net::SUPPORTED_VERSIONS,
9493
protocol_id: whisper_net::PROTOCOL_ID,
9594
});
9695

9796
// parity-only extensions to whisper.
9897
protos.push(AttachedProtocol {
9998
handler: Arc::new(whisper_net::ParityExtensions),
100-
packet_count: whisper_net::PACKET_COUNT,
10199
versions: whisper_net::SUPPORTED_VERSIONS,
102100
protocol_id: whisper_net::PARITY_PROTOCOL_ID,
103101
});

util/network-devp2p/src/host.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ const NODE_TABLE_TIMEOUT: Duration = Duration::from_secs(300);
7979
#[derive(Debug, PartialEq, Eq)]
8080
/// Protocol info
8181
pub struct CapabilityInfo {
82+
/// Protocol ID
8283
pub protocol: ProtocolId,
84+
/// Protocol version
8385
pub version: u8,
8486
/// Total number of packet IDs this protocol support.
8587
pub packet_count: u8,
@@ -982,7 +984,6 @@ impl IoHandler<NetworkIoMessage> for Host {
982984
ref handler,
983985
ref protocol,
984986
ref versions,
985-
ref packet_count,
986987
} => {
987988
let h = handler.clone();
988989
let reserved = self.reserved_nodes.read();
@@ -992,8 +993,12 @@ impl IoHandler<NetworkIoMessage> for Host {
992993
);
993994
self.handlers.write().insert(*protocol, h);
994995
let mut info = self.info.write();
995-
for v in versions {
996-
info.capabilities.push(CapabilityInfo { protocol: *protocol, version: *v, packet_count: *packet_count });
996+
for &(version, packet_count) in versions {
997+
info.capabilities.push(CapabilityInfo {
998+
protocol: *protocol,
999+
version,
1000+
packet_count,
1001+
});
9971002
}
9981003
},
9991004
NetworkIoMessage::AddTimer {

util/network-devp2p/src/service.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,17 @@ impl NetworkService {
6767
}
6868

6969
/// Regiter a new protocol handler with the event loop.
70-
pub fn register_protocol(&self, handler: Arc<NetworkProtocolHandler + Send + Sync>, protocol: ProtocolId, packet_count: u8, versions: &[u8]) -> Result<(), Error> {
70+
pub fn register_protocol(
71+
&self,
72+
handler: Arc<NetworkProtocolHandler + Send + Sync>,
73+
protocol: ProtocolId,
74+
// version id + packet count
75+
versions: &[(u8, u8)]
76+
) -> Result<(), Error> {
7177
self.io_service.send_message(NetworkIoMessage::AddHandler {
72-
handler: handler,
73-
protocol: protocol,
78+
handler,
79+
protocol,
7480
versions: versions.to_vec(),
75-
packet_count: packet_count,
7681
})?;
7782
Ok(())
7883
}

util/network/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ pub enum NetworkIoMessage {
6464
handler: Arc<NetworkProtocolHandler + Sync>,
6565
/// Protocol Id.
6666
protocol: ProtocolId,
67-
/// Supported protocol versions.
68-
versions: Vec<u8>,
69-
/// Number of packet IDs reserved by the protocol.
70-
packet_count: u8,
67+
/// Supported protocol versions and number of packet IDs reserved by the protocol (packet count).
68+
versions: Vec<(u8, u8)>,
7169
},
7270
/// Register a new protocol timer
7371
AddTimer {

whisper/src/net/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,17 @@ const RALLY_TIMEOUT: Duration = Duration::from_millis(2500);
4141
/// Current protocol version.
4242
pub const PROTOCOL_VERSION: usize = 6;
4343

44+
/// Number of packets. A bunch are reserved.
45+
const PACKET_COUNT: u8 = 128;
46+
4447
/// Supported protocol versions.
45-
pub const SUPPORTED_VERSIONS: &'static [u8] = &[PROTOCOL_VERSION as u8];
48+
pub const SUPPORTED_VERSIONS: &'static [(u8, u8)] = &[
49+
(PROTOCOL_VERSION as u8, PACKET_COUNT)
50+
];
4651

4752
// maximum tolerated delay between messages packets.
4853
const MAX_TOLERATED_DELAY: Duration = Duration::from_millis(5000);
4954

50-
/// Number of packets. A bunch are reserved.
51-
pub const PACKET_COUNT: u8 = 128;
52-
5355
/// Whisper protocol ID
5456
pub const PROTOCOL_ID: ::network::ProtocolId = *b"shh";
5557

0 commit comments

Comments
 (0)