mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 17:31:05 +00:00
PoC-1 backwards compatibility (#281)
* Poc-1 justification compatibility * Poc-1 version compatibility * Fixed comment placement
This commit is contained in:
@@ -124,6 +124,8 @@ impl<D: NativeExecutionDispatch + Sync + Send> CodeExecutor for NativeExecutor<D
|
|||||||
let wasm_module = WasmModule::from_buffer(code)
|
let wasm_module = WasmModule::from_buffer(code)
|
||||||
.expect("all modules compiled with rustc are valid wasm code; qed");
|
.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()
|
if WasmExecutor.call_in_wasm_module(ext, &wasm_module, "version", &[]).ok()
|
||||||
.and_then(|version| RuntimeVersion::decode(&mut version.as_slice()))
|
.and_then(|version| RuntimeVersion::decode(&mut version.as_slice()))
|
||||||
.map_or(false, |v| D::VERSION.can_call_with(&v))
|
.map_or(false, |v| D::VERSION.can_call_with(&v))
|
||||||
|
|||||||
@@ -172,6 +172,7 @@ pub mod generic {
|
|||||||
use codec::Slicable;
|
use codec::Slicable;
|
||||||
use runtime_primitives::bft::Justification;
|
use runtime_primitives::bft::Justification;
|
||||||
use ed25519;
|
use ed25519;
|
||||||
|
use primitives::Signature;
|
||||||
|
|
||||||
use super::{Role, BlockAttribute, RemoteCallResponse, RequestId, Transactions, Direction};
|
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<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.
|
/// Block data sent in the response.
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
|
||||||
pub struct BlockData<Header, Hash, Extrinsic> {
|
pub struct BlockData<Header, Hash, Extrinsic> {
|
||||||
@@ -221,7 +260,7 @@ pub mod generic {
|
|||||||
/// Block message queue if requested.
|
/// Block message queue if requested.
|
||||||
pub message_queue: Option<Vec<u8>>,
|
pub message_queue: Option<Vec<u8>>,
|
||||||
/// Justification if requested.
|
/// Justification if requested.
|
||||||
pub justification: Option<Justification<Hash>>,
|
pub justification: Option<BlockJustification<Hash>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Identifies starting point of a block sequence.
|
/// Identifies starting point of a block sequence.
|
||||||
|
|||||||
@@ -142,7 +142,8 @@ impl<B: BlockT> Protocol<B> where
|
|||||||
let message: Message<B> = match serde_json::from_slice(data) {
|
let message: Message<B> = match serde_json::from_slice(data) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(e) => {
|
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);
|
io.disable_peer(peer_id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -159,13 +160,13 @@ impl<B: BlockT> Protocol<B> where
|
|||||||
match mem::replace(&mut peer.block_request, None) {
|
match mem::replace(&mut peer.block_request, None) {
|
||||||
Some(r) => r,
|
Some(r) => r,
|
||||||
None => {
|
None => {
|
||||||
debug!("Unexpected response packet from {}", peer_id);
|
debug!(target: "sync", "Unexpected response packet from {}", peer_id);
|
||||||
io.disable_peer(peer_id);
|
io.disable_peer(peer_id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
debug!("Unexpected packet from {}", peer_id);
|
debug!(target: "sync", "Unexpected packet from {}", peer_id);
|
||||||
io.disable_peer(peer_id);
|
io.disable_peer(peer_id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -259,13 +260,14 @@ impl<B: BlockT> Protocol<B> where
|
|||||||
}
|
}
|
||||||
let number = header.number().clone();
|
let number = header.number().clone();
|
||||||
let hash = header.hash();
|
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 {
|
let block_data = message::generic::BlockData {
|
||||||
hash: hash,
|
hash: hash,
|
||||||
header: if get_header { Some(header) } else { None },
|
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)),
|
body: (if get_body { self.chain.body(&BlockId::Hash(hash)).unwrap_or(None) } else { None }).map(|body| message::Body::Extrinsics(body)),
|
||||||
receipt: None,
|
receipt: None,
|
||||||
message_queue: 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);
|
blocks.push(block_data);
|
||||||
match request.direction {
|
match request.direction {
|
||||||
|
|||||||
@@ -258,7 +258,7 @@ impl<B: BlockT> ChainSync<B> where
|
|||||||
let result = protocol.chain().import(
|
let result = protocol.chain().import(
|
||||||
is_best,
|
is_best,
|
||||||
header,
|
header,
|
||||||
justification,
|
justification.to_justification(),
|
||||||
block.body.map(|b| b.to_extrinsics()),
|
block.body.map(|b| b.to_extrinsics()),
|
||||||
);
|
);
|
||||||
match result {
|
match result {
|
||||||
|
|||||||
Reference in New Issue
Block a user