Update some types and interfaces to match the spec (#24)

* Update primitives.

* Fix validator interface.
This commit is contained in:
Tomasz Drwięga
2017-12-04 16:51:43 +01:00
committed by Robert Habermeier
parent d17dcc1893
commit 55e74cd426
9 changed files with 199 additions and 153 deletions
+35 -49
View File
@@ -17,49 +17,31 @@
//! Validator primitives.
use bytes;
use parachain;
/// Parachain incoming messages.
/// Parachain outgoing message.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct IngressPosts(#[serde(with="bytes")] pub Vec<u8>);
pub struct EgressPost(#[serde(with="bytes")] pub Vec<u8>);
/// Parachain incoming messages delta.
/// Balance upload.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct IngressPostsDelta(#[serde(with="bytes")] pub Vec<u8>);
pub struct BalanceUpload(#[serde(with="bytes")] pub Vec<u8>);
/// Parachain outgoing messages.
/// Balance download.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct EgressPosts(#[serde(with="bytes")] pub Vec<u8>);
pub struct BalanceDownload(#[serde(with="bytes")] pub Vec<u8>);
/// Validity result of particular proof and ingress queue.
/// The result of parachain validation.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag="type", content="data")]
#[serde(rename_all = "camelCase")]
#[serde(deny_unknown_fields)]
pub enum ProofValidity {
/// The proof is invalid.
Invalid,
/// The proof is processed and new egress queue is created.
/// Also includes current ingress queue delta.
Valid(IngressPostsDelta, EgressPosts),
}
impl ProofValidity {
/// The proof is valid.
pub fn is_valid(&self) -> bool {
match *self {
ProofValidity::Invalid => false,
ProofValidity::Valid(..) => true,
}
}
}
impl From<Option<(IngressPostsDelta, EgressPosts)>> for ProofValidity {
fn from(posts: Option<(IngressPostsDelta, EgressPosts)>) -> Self {
match posts {
Some((delta, posts)) => ProofValidity::Valid(delta, posts),
None => ProofValidity::Invalid,
}
}
pub struct ValidationResult {
/// New head data that should be included in the relay chain state.
pub head_data: parachain::HeadData,
/// Outgoing messages (a vec for each parachain).
pub egress_queues: Vec<Vec<EgressPost>>,
/// Balance uploads
pub balance_uploads: Vec<BalanceUpload>,
}
// TODO [ToDr] This shouldn't be here!
@@ -73,10 +55,13 @@ pub trait Validator {
/// In case of success produces egress posts.
fn validate(
&self,
messages: &IngressPosts,
proof: &::parachain::Proof,
code: &[u8],
) -> Result<ProofValidity, Self::Error>;
// TODO [ToDr] actually consolidate
consolidated_ingress: &[(u64, Vec<parachain::Message>)],
balance_downloads: &[BalanceDownload],
block_data: &parachain::BlockData,
previous_head_data: &parachain::HeadData,
) -> Result<ValidationResult, Self::Error>;
}
#[cfg(test)]
@@ -85,19 +70,20 @@ mod tests {
use polkadot_serializer as ser;
#[test]
fn test_proof_validity_serialization() {
assert_eq!(
ser::to_string_pretty(&ProofValidity::Invalid),
r#"{
"type": "invalid"
}"#);
assert_eq!(
ser::to_string_pretty(&ProofValidity::Valid(IngressPostsDelta(vec![1]), EgressPosts(vec![1, 2, 3]))),
r#"{
"type": "valid",
"data": [
"0x01",
"0x010203"
fn test_validation_result() {
assert_eq!(ser::to_string_pretty(&ValidationResult {
head_data: parachain::HeadData(vec![1]),
egress_queues: vec![vec![EgressPost(vec![1])]],
balance_uploads: vec![BalanceUpload(vec![2])],
}), r#"{
"headData": "0x01",
"egressQueues": [
[
"0x01"
]
],
"balanceUploads": [
"0x02"
]
}"#);
}