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

Commit

Permalink
PoC-1 backwards compatibility (#281)
Browse files Browse the repository at this point in the history
* Poc-1 justification compatibility

* Poc-1 version compatibility

* Fixed comment placement
  • Loading branch information
arkpar authored Jul 5, 2018
1 parent 874d38e commit 014561b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions substrate/executor/src/native_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ impl<D: NativeExecutionDispatch + Sync + Send> CodeExecutor for NativeExecutor<D
let wasm_module = WasmModule::from_buffer(code)
.expect("all modules compiled with rustc are valid wasm code; qed");

// Missing version export is allowed in Poc-2 for Poc-1 compatibility.
// TODO: return an error on missing version.
if WasmExecutor.call_in_wasm_module(ext, &wasm_module, "version", &[]).ok()
.and_then(|version| RuntimeVersion::decode(&mut version.as_slice()))
.map_or(false, |v| D::VERSION.can_call_with(&v))
Expand Down
41 changes: 40 additions & 1 deletion substrate/network/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -207,6 +208,44 @@ pub mod generic {
}
}

/// Emulates Poc-1 justification format.
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct V1Justification<H> {
/// 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)>
}

// TODO: remove this after poc-2
/// Justification back compat
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum BlockJustification<H> {
/// Poc-1 format.
V1(V1Justification<H>),
/// Poc-2 format.
V2(Justification<H>),
}

impl<H> BlockJustification<H> {
/// Convert to PoC-2 justification format.
pub fn to_justification(self) -> Justification<H> {
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<Header, Hash, Extrinsic> {
Expand All @@ -221,7 +260,7 @@ pub mod generic {
/// Block message queue if requested.
pub message_queue: Option<Vec<u8>>,
/// Justification if requested.
pub justification: Option<Justification<Hash>>,
pub justification: Option<BlockJustification<Hash>>,
}

/// Identifies starting point of a block sequence.
Expand Down
10 changes: 6 additions & 4 deletions substrate/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ impl<B: BlockT> Protocol<B> where
let message: Message<B> = 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;
}
Expand All @@ -159,13 +160,13 @@ impl<B: BlockT> Protocol<B> 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;
}
Expand Down Expand Up @@ -259,13 +260,14 @@ impl<B: BlockT> Protocol<B> 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 {
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 @@ -258,7 +258,7 @@ impl<B: BlockT> ChainSync<B> where
let result = protocol.chain().import(
is_best,
header,
justification,
justification.to_justification(),
block.body.map(|b| b.to_extrinsics()),
);
match result {
Expand Down

0 comments on commit 014561b

Please sign in to comment.