mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 22:41:02 +00:00
Fix warning and directory restructure.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Polkadot is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use serializer;
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
Serialization(serializer::Error);
|
||||
}
|
||||
errors {
|
||||
Timeout {
|
||||
description("Validation task has timed-out."),
|
||||
display("Validation timeout."),
|
||||
}
|
||||
InvalidCode(details: String) {
|
||||
description("The code is invalid."),
|
||||
display("invalid code: '{}'", details),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Polkadot is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Validator implementation.
|
||||
|
||||
#[warn(missing_docs)]
|
||||
|
||||
extern crate substrate_primitives as primitives;
|
||||
extern crate substrate_serializer as serializer;
|
||||
extern crate polkadot_primitives;
|
||||
extern crate serde;
|
||||
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
|
||||
mod error;
|
||||
mod parachains;
|
||||
mod validator;
|
||||
|
||||
pub use error::{Error, ErrorKind, Result};
|
||||
pub use validator::Validator;
|
||||
@@ -0,0 +1,68 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Polkadot is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use polkadot_primitives::validator;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use error::Result;
|
||||
|
||||
/// Parachain code implementation.
|
||||
pub trait ParachainCode: fmt::Debug {
|
||||
/// 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: 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.
|
||||
#[derive(Debug)]
|
||||
pub struct ParaChain1;
|
||||
|
||||
impl ParachainCode for ParaChain1 {
|
||||
type Message = ();
|
||||
type Download = ();
|
||||
type BlockData = ();
|
||||
type HeadData = ();
|
||||
type Result = validator::ValidationResult;
|
||||
|
||||
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!()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Polkadot is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use polkadot_primitives::{validator, parachain};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serializer;
|
||||
|
||||
use error::{ErrorKind, Result};
|
||||
use parachains::{ParachainCode, ParaChain1};
|
||||
|
||||
/// A dummy validator implementation.
|
||||
#[derive(Debug)]
|
||||
pub struct Validator {
|
||||
codes: Vec<Box<Code>>,
|
||||
}
|
||||
|
||||
impl Validator {
|
||||
/// Create a new validator.
|
||||
pub fn new() -> Self {
|
||||
Validator {
|
||||
codes: vec![
|
||||
Box::new(ParaChain1) as Box<Code>
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Validator {
|
||||
pub fn validate(
|
||||
&self,
|
||||
code: &[u8],
|
||||
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(consolidated_ingress, balance_downloads, block_data, previous_head_data),
|
||||
None => bail!(ErrorKind::InvalidCode(format!("Unknown parachain code."))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Simplified parachain code verification
|
||||
trait Code: fmt::Debug {
|
||||
/// 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, B, T, R> Code for T where
|
||||
M: DeserializeOwned,
|
||||
B: DeserializeOwned,
|
||||
R: Into<validator::ValidationResult>,
|
||||
T: ParachainCode<Message=M, BlockData=B, Result=R>,
|
||||
{
|
||||
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, downloads, block_data, head_data)?.into())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user