Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Made block message compatible with poc-1 #226

Merged
merged 1 commit into from
Jun 22, 2018
Merged
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
2 changes: 1 addition & 1 deletion substrate/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 1 addition & 1 deletion substrate/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
38 changes: 36 additions & 2 deletions substrate/network/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<u8>);
// 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<Extrinsic> {
/// Poc-1. Extrinsics as bytes.
V1(Vec<V1Extrinsic>),
/// Poc-2 or later. A structured type.
Extrinsics(Vec<Extrinsic>),
}

impl<Extrinsic> Body<Extrinsic> where Extrinsic: Slicable {
/// Extracts extrinsic from the body.
pub fn to_extrinsics(self) -> Vec<Extrinsic> {
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<Header, Hash, Extrinsic> {
Expand All @@ -183,7 +217,7 @@ pub mod generic {
/// Block header if requested.
pub header: Option<Header>,
/// Block body if requested.
pub body: Option<Vec<Extrinsic>>,
pub body: Option<Body<Extrinsic>>,
/// Block receipt if requested.
pub receipt: Option<Vec<u8>>,
/// Block message queue if requested.
Expand Down
2 changes: 1 addition & 1 deletion substrate/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl<B: BlockT> Protocol<B> 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 },
Expand Down
2 changes: 1 addition & 1 deletion substrate/network/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl<B: BlockT> ChainSync<B> where
is_best,
header,
justification,
block.body
block.body.map(|b| b.to_extrinsics()),
);
match result {
Ok(ImportResult::AlreadyInChain) => {
Expand Down