Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ethcore/sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ authors = ["Parity Technologies <admin@parity.io>"]

[dependencies]
common-types = { path = "../types" }
enum_primitive = "0.1.1"
ethcore = { path = ".." }
ethcore-io = { path = "../../util/io" }
ethcore-light = { path = "../light" }
Expand All @@ -30,6 +29,7 @@ parity-bytes = "0.1"
parking_lot = "0.7"
rand = "0.4"
rlp = { version = "0.3.0", features = ["ethereum"] }
syncpacket = { path = "../../util/syncpacket" }
trace-time = "0.1"
triehash-ethereum = {version = "0.2", path = "../../util/triehash-ethereum" }

Expand Down
1 change: 0 additions & 1 deletion ethcore/sync/src/chain/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use api::WARP_SYNC_PROTOCOL_ID;
use block_sync::{BlockDownloaderImportError as DownloaderImportError, DownloadAction};
use bytes::Bytes;
use enum_primitive::FromPrimitive;
use ethcore::error::{Error as EthcoreError, ErrorKind as EthcoreErrorKind, ImportErrorKind, BlockError};
use ethcore::snapshot::{ManifestData, RestorationStatus};
use ethcore::verification::queue::kind::blocks::Unverified;
Expand Down
1 change: 0 additions & 1 deletion ethcore/sync/src/chain/supplier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.

use bytes::Bytes;
use enum_primitive::FromPrimitive;
use ethereum_types::H256;
use network::{self, PeerId};
use parking_lot::RwLock;
Expand Down
133 changes: 25 additions & 108 deletions ethcore/sync/src/chain/sync_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,120 +22,37 @@
//! to convert to/from the packet id values transmitted over the
//! wire.

use api::{ETH_PROTOCOL, WARP_SYNC_PROTOCOL_ID};
use network::{PacketId, ProtocolId};
use syncpacket::SyncPackets;
use crate::api::{ETH_PROTOCOL, WARP_SYNC_PROTOCOL_ID};

/// An enum that defines all known packet ids in the context of
/// synchronization and provides a mechanism to convert from
/// packet ids (of type PacketId or u8) directly read from the network
/// to enum variants. This implicitly provides a mechanism to
/// check whether a given packet id is known, and to prevent
/// packet id clashes when defining new ids.
enum_from_primitive! {
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SyncPacket {
StatusPacket = 0x00,
NewBlockHashesPacket = 0x01,
TransactionsPacket = 0x02,
GetBlockHeadersPacket = 0x03,
BlockHeadersPacket = 0x04,
GetBlockBodiesPacket = 0x05,
BlockBodiesPacket = 0x06,
NewBlockPacket = 0x07,

GetNodeDataPacket = 0x0d,
NodeDataPacket = 0x0e,
GetReceiptsPacket = 0x0f,
ReceiptsPacket = 0x10,

GetSnapshotManifestPacket = 0x11,
SnapshotManifestPacket = 0x12,
GetSnapshotDataPacket = 0x13,
SnapshotDataPacket = 0x14,
ConsensusDataPacket = 0x15,
PrivateTransactionPacket = 0x16,
SignedPrivateTransactionPacket = 0x17,
}
}

use self::SyncPacket::*;

/// Provide both subprotocol and packet id information within the
/// same object.
pub trait PacketInfo {
fn id(&self) -> PacketId;
fn protocol(&self) -> ProtocolId;
}

// The mechanism to match packet ids and protocol may be improved
// through some macro magic, but for now this works.
impl PacketInfo for SyncPacket {
fn protocol(&self) -> ProtocolId {
match self {
StatusPacket |
NewBlockHashesPacket |
TransactionsPacket |
GetBlockHeadersPacket |
BlockHeadersPacket |
GetBlockBodiesPacket |
BlockBodiesPacket |
NewBlockPacket |

GetNodeDataPacket|
NodeDataPacket |
GetReceiptsPacket |
ReceiptsPacket

=> ETH_PROTOCOL,

GetSnapshotManifestPacket|
SnapshotManifestPacket |
GetSnapshotDataPacket |
SnapshotDataPacket |
ConsensusDataPacket |
PrivateTransactionPacket |
SignedPrivateTransactionPacket

=> WARP_SYNC_PROTOCOL_ID,
}
}

fn id(&self) -> PacketId {
(*self) as PacketId
}
}


#[cfg(test)]
mod tests {
use super::*;

use enum_primitive::FromPrimitive;

#[test]
fn packet_ids_from_u8_when_from_primitive_zero_then_equals_status_packet() {
assert_eq!(SyncPacket::from_u8(0x00), Some(StatusPacket));
}

#[test]
fn packet_ids_from_u8_when_from_primitive_eleven_then_equals_get_snapshot_manifest_packet() {
assert_eq!(SyncPacket::from_u8(0x11), Some(GetSnapshotManifestPacket));
}

#[test]
fn packet_ids_from_u8_when_invalid_packet_id_then_none() {
assert!(SyncPacket::from_u8(0x99).is_none());
}

#[test]
fn when_status_packet_then_id_and_protocol_match() {
assert_eq!(StatusPacket.id(), StatusPacket as PacketId);
assert_eq!(StatusPacket.protocol(), ETH_PROTOCOL);
}

#[test]
fn when_consensus_data_packet_then_id_and_protocol_match() {
assert_eq!(ConsensusDataPacket.id(), ConsensusDataPacket as PacketId);
assert_eq!(ConsensusDataPacket.protocol(), WARP_SYNC_PROTOCOL_ID);
}
#[derive(SyncPackets, Clone, Copy)]
pub enum SyncPacket {
#[protocol(ETH_PROTOCOL)] StatusPacket = 0x00,
#[protocol(ETH_PROTOCOL)] NewBlockHashesPacket = 0x01,
#[protocol(ETH_PROTOCOL)] TransactionsPacket = 0x02,
#[protocol(ETH_PROTOCOL)] GetBlockHeadersPacket = 0x03,
#[protocol(ETH_PROTOCOL)] BlockHeadersPacket = 0x04,
#[protocol(ETH_PROTOCOL)] GetBlockBodiesPacket = 0x05,
#[protocol(ETH_PROTOCOL)] BlockBodiesPacket = 0x06,
#[protocol(ETH_PROTOCOL)] NewBlockPacket = 0x07,

#[protocol(ETH_PROTOCOL)] GetNodeDataPacket = 0x0d,
#[protocol(ETH_PROTOCOL)] NodeDataPacket = 0x0e,
#[protocol(ETH_PROTOCOL)] GetReceiptsPacket = 0x0f,
#[protocol(ETH_PROTOCOL)] ReceiptsPacket = 0x10,

#[protocol(WARP_SYNC_PROTOCOL_ID)] GetSnapshotManifestPacket = 0x11,
#[protocol(WARP_SYNC_PROTOCOL_ID)] SnapshotManifestPacket = 0x12,
#[protocol(WARP_SYNC_PROTOCOL_ID)] GetSnapshotDataPacket = 0x13,
#[protocol(WARP_SYNC_PROTOCOL_ID)] SnapshotDataPacket = 0x14,
#[protocol(WARP_SYNC_PROTOCOL_ID)] ConsensusDataPacket = 0x15,
#[protocol(WARP_SYNC_PROTOCOL_ID)] PrivateTransactionPacket = 0x16,
#[protocol(WARP_SYNC_PROTOCOL_ID)] SignedPrivateTransactionPacket = 0x17,
}
3 changes: 1 addition & 2 deletions ethcore/sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern crate parity_bytes as bytes;
extern crate parking_lot;
extern crate rand;
extern crate rlp;
extern crate syncpacket;
extern crate triehash_ethereum;

extern crate ethcore_light as light;
Expand All @@ -44,8 +45,6 @@ extern crate ethcore_light as light;
#[cfg(test)] extern crate kvdb_memorydb;
#[cfg(test)] extern crate rustc_hex;

#[macro_use]
extern crate enum_primitive;
#[macro_use]
extern crate macros;
#[macro_use]
Expand Down
17 changes: 17 additions & 0 deletions util/syncpacket/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
Comment thread
elferdo marked this conversation as resolved.
name = "syncpacket"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[lib]
name = "syncpacket"
proc-macro = true

[dependencies]
syn = "0.15"
quote = "0.6"
proc-macro2 = "0.4"

[dev-dependencies]
ethcore-network = {path = "../network"}
Loading