From 6513705b47d8ec5abf5751a3d098f35f37a92272 Mon Sep 17 00:00:00 2001 From: arkpar Date: Thu, 5 Jul 2018 09:34:29 +0200 Subject: [PATCH 1/3] Poc-1 justification compatibility --- substrate/network/src/message.rs | 41 ++++++++++++++++++++++++++++++- substrate/network/src/protocol.rs | 10 +++++--- substrate/network/src/sync.rs | 2 +- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/substrate/network/src/message.rs b/substrate/network/src/message.rs index b2c44cdf96e8f..aad133c70d1d3 100644 --- a/substrate/network/src/message.rs +++ b/substrate/network/src/message.rs @@ -172,6 +172,7 @@ pub mod generic { use codec::Slicable; use runtime_primitives::bft::Justification; use ed25519; + use primitives::Signature; use super::{Role, BlockAttribute, RemoteCallResponse, RequestId, Transactions, Direction}; @@ -207,6 +208,44 @@ pub mod generic { } } + /// Emulates Poc-1 justification format. + #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] + pub struct V1Justification { + /// The round consensus was reached in. + pub round_number: u32, + /// The hash of the header justified. + pub hash: H, + /// The signatures and signers of the hash. + pub signatures: Vec<([u8; 32], Signature)> + } + + #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] + #[serde(untagged)] + // TODO: remove this after poc-2 + /// Justification back compat + pub enum BlockJustification { + /// Poc-1 format. + V1(V1Justification), + /// Poc-2 format. + V2(Justification), + } + + impl BlockJustification { + /// Convert to PoC-2 justification format. + pub fn to_justification(self) -> Justification { + match self { + BlockJustification::V2(j) => j, + BlockJustification::V1(j) => { + Justification { + round_number: j.round_number, + hash: j.hash, + signatures: j.signatures.into_iter().map(|(a, s)| (a.into(), s)).collect(), + } + } + } + } + } + /// Block data sent in the response. #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] pub struct BlockData { @@ -221,7 +260,7 @@ pub mod generic { /// Block message queue if requested. pub message_queue: Option>, /// Justification if requested. - pub justification: Option>, + pub justification: Option>, } /// Identifies starting point of a block sequence. diff --git a/substrate/network/src/protocol.rs b/substrate/network/src/protocol.rs index 6d23f1c7bd864..d46ff839c3f85 100644 --- a/substrate/network/src/protocol.rs +++ b/substrate/network/src/protocol.rs @@ -142,7 +142,8 @@ impl Protocol where let message: Message = match serde_json::from_slice(data) { Ok(m) => m, Err(e) => { - debug!("Invalid packet from {}: {}", peer_id, e); + debug!(target: "sync", "Invalid packet from {}: {}", peer_id, e); + trace!(target: "sync", "Invalid packet: {}", String::from_utf8_lossy(data)); io.disable_peer(peer_id); return; } @@ -159,13 +160,13 @@ impl Protocol where match mem::replace(&mut peer.block_request, None) { Some(r) => r, None => { - debug!("Unexpected response packet from {}", peer_id); + debug!(target: "sync", "Unexpected response packet from {}", peer_id); io.disable_peer(peer_id); return; } } } else { - debug!("Unexpected packet from {}", peer_id); + debug!(target: "sync", "Unexpected packet from {}", peer_id); io.disable_peer(peer_id); return; } @@ -259,13 +260,14 @@ impl Protocol where } let number = header.number().clone(); let hash = header.hash(); + let justification = if get_justification { self.chain.justification(&BlockId::Hash(hash)).unwrap_or(None) } else { None }; 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 }).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 }, + justification: justification.map(|j| message::generic::BlockJustification::V2(j)), }; blocks.push(block_data); match request.direction { diff --git a/substrate/network/src/sync.rs b/substrate/network/src/sync.rs index 8a296d4534f4f..e75fcd16f9d57 100644 --- a/substrate/network/src/sync.rs +++ b/substrate/network/src/sync.rs @@ -258,7 +258,7 @@ impl ChainSync where let result = protocol.chain().import( is_best, header, - justification, + justification.to_justification(), block.body.map(|b| b.to_extrinsics()), ); match result { From 5d0fbdd4285fddd992eb48a146efd4d82cb3e411 Mon Sep 17 00:00:00 2001 From: arkpar Date: Thu, 5 Jul 2018 09:39:57 +0200 Subject: [PATCH 2/3] Poc-1 version compatibility --- substrate/executor/src/native_executor.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/substrate/executor/src/native_executor.rs b/substrate/executor/src/native_executor.rs index 72ba2d33a727d..724c44299f940 100644 --- a/substrate/executor/src/native_executor.rs +++ b/substrate/executor/src/native_executor.rs @@ -124,6 +124,8 @@ impl CodeExecutor for NativeExecutor Date: Thu, 5 Jul 2018 19:39:42 +0200 Subject: [PATCH 3/3] Fixed comment placement --- substrate/network/src/message.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/substrate/network/src/message.rs b/substrate/network/src/message.rs index aad133c70d1d3..3b96b5dae383f 100644 --- a/substrate/network/src/message.rs +++ b/substrate/network/src/message.rs @@ -219,10 +219,10 @@ pub mod generic { pub signatures: Vec<([u8; 32], Signature)> } - #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] - #[serde(untagged)] // TODO: remove this after poc-2 /// Justification back compat + #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] + #[serde(untagged)] pub enum BlockJustification { /// Poc-1 format. V1(V1Justification),