mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 20:27:58 +00:00
Update some types and interfaces to match the spec (#24)
* Update primitives. * Fix validator interface.
This commit is contained in:
committed by
Robert Habermeier
parent
d17dcc1893
commit
55e74cd426
@@ -19,16 +19,29 @@ use std::fmt;
|
||||
use primitives::validator;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use error::Result;
|
||||
|
||||
/// Parachain code implementation.
|
||||
pub trait ParachainCode: fmt::Debug {
|
||||
/// Deserialized messages type.
|
||||
type Messages: DeserializeOwned;
|
||||
/// Deserialized proof type.
|
||||
type Proof: DeserializeOwned;
|
||||
/// Deserialized message type.
|
||||
type Message: DeserializeOwned;
|
||||
/// Balance download.
|
||||
type Download: DeserializeOwned;
|
||||
/// Deserialized block data type.
|
||||
type BlockData: DeserializeOwned;
|
||||
/// Parachain head data.
|
||||
type HeadData: DeserializeOwned;
|
||||
/// Result
|
||||
type Result: Into<validator::ValidationResult>;
|
||||
|
||||
/// Given decoded messages and proof validate it and return egress posts.
|
||||
fn check(&self, messages: Self::Messages, proof: Self::Proof) ->
|
||||
Option<(validator::IngressPostsDelta, validator::EgressPosts)>;
|
||||
fn check(
|
||||
&self,
|
||||
messages: Vec<(u64, Vec<Self::Message>)>,
|
||||
downloads: Vec<Self::Download>,
|
||||
block_data: Self::BlockData,
|
||||
head_data: Self::HeadData,
|
||||
) -> Result<Self::Result>;
|
||||
}
|
||||
|
||||
/// Dummy implementation of the first parachain validation.
|
||||
@@ -36,11 +49,20 @@ pub trait ParachainCode: fmt::Debug {
|
||||
pub struct ParaChain1;
|
||||
|
||||
impl ParachainCode for ParaChain1 {
|
||||
type Messages = ();
|
||||
type Proof = ();
|
||||
type Message = ();
|
||||
type Download = ();
|
||||
type BlockData = ();
|
||||
type HeadData = ();
|
||||
type Result = validator::ValidationResult;
|
||||
|
||||
fn check(&self, _messages: Self::Messages, _proof: Self::Proof)
|
||||
-> Option<(validator::IngressPostsDelta, validator::EgressPosts)> {
|
||||
None
|
||||
fn check(
|
||||
&self,
|
||||
_messages: Vec<(u64, Vec<Self::Message>)>,
|
||||
_downloads: Vec<Self::Download>,
|
||||
_block_data: Self::BlockData,
|
||||
_head_data: Self::HeadData,
|
||||
) -> Result<Self::Result>
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,14 +45,16 @@ impl validator::Validator for Validator {
|
||||
|
||||
fn validate(
|
||||
&self,
|
||||
messages: &validator::IngressPosts,
|
||||
proof: ¶chain::Proof,
|
||||
code: &[u8],
|
||||
) -> Result<validator::ProofValidity> {
|
||||
consolidated_ingress: &[(u64, Vec<parachain::Message>)],
|
||||
balance_downloads: &[validator::BalanceDownload],
|
||||
block_data: ¶chain::BlockData,
|
||||
previous_head_data: ¶chain::HeadData,
|
||||
) -> Result<validator::ValidationResult> {
|
||||
ensure!(code.len() == 1, ErrorKind::InvalidCode(format!("The code should be a single byte.")));
|
||||
|
||||
match self.codes.get(code[0] as usize) {
|
||||
Some(code) => code.check(messages, proof),
|
||||
Some(code) => code.check(consolidated_ingress, balance_downloads, block_data, previous_head_data),
|
||||
None => bail!(ErrorKind::InvalidCode(format!("Unknown parachain code."))),
|
||||
}
|
||||
}
|
||||
@@ -60,20 +62,43 @@ impl validator::Validator for Validator {
|
||||
|
||||
/// Simplified parachain code verification
|
||||
trait Code: fmt::Debug {
|
||||
/// Given bytes of messages and proof determine if the proof is valid and return egress posts.
|
||||
fn check(&self, messages: &validator::IngressPosts, proof: ¶chain::Proof) -> Result<validator::ProofValidity>;
|
||||
/// Given parachain candidate block data returns it's validity
|
||||
/// and possible generated egress posts.
|
||||
fn check(
|
||||
&self,
|
||||
consolidated_ingress: &[(u64, Vec<parachain::Message>)],
|
||||
balance_downloads: &[validator::BalanceDownload],
|
||||
block_data: ¶chain::BlockData,
|
||||
previous_head_data: ¶chain::HeadData,
|
||||
) -> Result<validator::ValidationResult>;
|
||||
}
|
||||
|
||||
impl<M, P, T> Code for T where
|
||||
impl<M, B, T, R> Code for T where
|
||||
M: DeserializeOwned,
|
||||
P: DeserializeOwned,
|
||||
T: ParachainCode<Messages=M, Proof=P>,
|
||||
B: DeserializeOwned,
|
||||
R: Into<validator::ValidationResult>,
|
||||
T: ParachainCode<Message=M, BlockData=B, Result=R>,
|
||||
{
|
||||
fn check(&self, messages: &validator::IngressPosts, proof: ¶chain::Proof) -> Result<validator::ProofValidity> {
|
||||
let messages = serializer::from_slice(&messages.0)?;
|
||||
let proof = serializer::from_slice(&proof.raw().0)?;
|
||||
fn check(
|
||||
&self,
|
||||
consolidated_ingress: &[(u64, Vec<parachain::Message>)],
|
||||
balance_downloads: &[validator::BalanceDownload],
|
||||
block_data: ¶chain::BlockData,
|
||||
previous_head_data: ¶chain::HeadData,
|
||||
) -> Result<validator::ValidationResult> {
|
||||
let messages = consolidated_ingress.iter()
|
||||
.map(|&(ref block, ref vec)| Ok((*block, vec.iter()
|
||||
.map(|msg| serializer::from_slice(&msg.0).map_err(Into::into))
|
||||
.collect::<Result<Vec<_>>>()?
|
||||
)))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
let downloads = balance_downloads.iter()
|
||||
.map(|download| serializer::from_slice(&download.0).map_err(Into::into))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
let block_data = serializer::from_slice(&block_data.0)?;
|
||||
let head_data = serializer::from_slice(&previous_head_data.0)?;
|
||||
|
||||
Ok(self.check(messages, proof).into())
|
||||
Ok(self.check(messages, downloads, block_data, head_data)?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user