From 7a9f4cdec86b1d4d187a489be92fbc963cbf2efe Mon Sep 17 00:00:00 2001 From: arkpar Date: Sun, 17 Jun 2018 18:16:30 +0200 Subject: [PATCH] Made block message compatible with poc-1 --- substrate/network/Cargo.toml | 2 +- substrate/network/src/lib.rs | 2 +- substrate/network/src/message.rs | 38 +++++++++++++++++++++++++++++-- substrate/network/src/protocol.rs | 2 +- substrate/network/src/sync.rs | 2 +- 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/substrate/network/Cargo.toml b/substrate/network/Cargo.toml index e54b5137808a8..dd5011953a47f 100644 --- a/substrate/network/Cargo.toml +++ b/substrate/network/Cargo.toml @@ -29,9 +29,9 @@ substrate-serializer = { path = "../../substrate/serializer" } substrate-runtime-support = { path = "../../substrate/runtime-support" } substrate-runtime-primitives = { path = "../../substrate/runtime/primitives" } substrate-bft = { path = "../../substrate/bft" } +substrate-codec = { path = "../../substrate/codec" } [dev-dependencies] env_logger = "0.4" -substrate-codec = { path = "../../substrate/codec" } substrate-keyring = { path = "../../substrate/keyring" } substrate-test-client = { path = "../../substrate/test-client" } diff --git a/substrate/network/src/lib.rs b/substrate/network/src/lib.rs index 202a081510e8b..15779ea6522cd 100644 --- a/substrate/network/src/lib.rs +++ b/substrate/network/src/lib.rs @@ -32,6 +32,7 @@ extern crate substrate_client as client; extern crate substrate_runtime_support as runtime_support; extern crate substrate_runtime_primitives as runtime_primitives; extern crate substrate_bft; +extern crate substrate_codec as codec; extern crate serde; extern crate serde_json; extern crate futures; @@ -42,7 +43,6 @@ extern crate ed25519; #[macro_use] extern crate error_chain; #[cfg(test)] extern crate env_logger; -#[cfg(test)] extern crate substrate_codec as codec; #[cfg(test)] extern crate substrate_keyring as keyring; #[cfg(test)] extern crate substrate_test_client as test_client; diff --git a/substrate/network/src/message.rs b/substrate/network/src/message.rs index 1b1b3c1424de0..ba157535085f2 100644 --- a/substrate/network/src/message.rs +++ b/substrate/network/src/message.rs @@ -19,7 +19,7 @@ use runtime_primitives::traits::{Block as BlockT, Header as HeaderT}; use service::Role as RoleFlags; -pub use self::generic::{BlockAnnounce, RemoteCallRequest, ConsensusVote, SignedConsensusVote, FromBlock}; +pub use self::generic::{BlockAnnounce, RemoteCallRequest, ConsensusVote, SignedConsensusVote, FromBlock, Body}; pub type RequestId = u64; @@ -171,10 +171,44 @@ pub struct RemoteCallResponse { /// Generic types. pub mod generic { use primitives::AuthorityId; + use codec::Slicable; use runtime_primitives::bft::Justification; use ed25519; use super::{Role, BlockAttribute, RemoteCallResponse, RequestId, Transactions, Direction}; + + use primitives::bytes; + + /// Emulates Poc-1 extrinsic primitive. + #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] + pub struct V1Extrinsic(#[serde(with="bytes")] pub Vec); + // Alternative block format for poc-1 compatibility. + // TODO: remove this after poc-2 + #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] + #[serde(untagged)] + /// Serialized block body type. + pub enum Body { + /// Poc-1. Extrinsics as bytes. + V1(Vec), + /// Poc-2 or later. A structured type. + Extrinsics(Vec), + } + + impl Body where Extrinsic: Slicable { + /// Extracts extrinsic from the body. + pub fn to_extrinsics(self) -> Vec { + match self { + Body::Extrinsics(e) => e, + Body::V1(e) => { + e.into_iter().filter_map(|bytes| { + let bytes = bytes.0.encode(); + Slicable::decode(&mut bytes.as_slice()) + }).collect() + } + } + } + } + /// Block data sent in the response. #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] pub struct BlockData { @@ -183,7 +217,7 @@ pub mod generic { /// Block header if requested. pub header: Option
, /// Block body if requested. - pub body: Option>, + pub body: Option>, /// Block receipt if requested. pub receipt: Option>, /// Block message queue if requested. diff --git a/substrate/network/src/protocol.rs b/substrate/network/src/protocol.rs index d49b7be1c8139..4094fbf0b0e57 100644 --- a/substrate/network/src/protocol.rs +++ b/substrate/network/src/protocol.rs @@ -262,7 +262,7 @@ impl Protocol where let block_data = message::generic::BlockData { hash: hash, header: if get_header { Some(header) } else { None }, - body: if get_body { self.chain.body(&BlockId::Hash(hash)).unwrap_or(None) } else { None }, + body: (if get_body { self.chain.body(&BlockId::Hash(hash)).unwrap_or(None) } else { None }).map(|body| message::Body::Extrinsics(body)), receipt: None, message_queue: None, justification: if get_justification { self.chain.justification(&BlockId::Hash(hash)).unwrap_or(None) } else { None }, diff --git a/substrate/network/src/sync.rs b/substrate/network/src/sync.rs index ce7d6438db021..70315274ae12b 100644 --- a/substrate/network/src/sync.rs +++ b/substrate/network/src/sync.rs @@ -257,7 +257,7 @@ impl ChainSync where is_best, header, justification, - block.body + block.body.map(|b| b.to_extrinsics()), ); match result { Ok(ImportResult::AlreadyInChain) => {