From 45ac233cd1bc3ff374fdd33bd9c304b5386fd875 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Tue, 12 Dec 2017 18:27:24 +0100 Subject: [PATCH] test context for table --- substrate/candidate-agreement/src/table.rs | 72 ++++++++++++++++++++-- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/substrate/candidate-agreement/src/table.rs b/substrate/candidate-agreement/src/table.rs index ae284f8bf2..1a97d32b8c 100644 --- a/substrate/candidate-agreement/src/table.rs +++ b/substrate/candidate-agreement/src/table.rs @@ -94,12 +94,6 @@ pub trait Context { &self, statement: &SignedStatement, ) -> Option; - - // sign a statement with the local key. - fn sign_statement( - &self, - statement: Statement, - ) -> SignedStatement; } /// Misbehavior: voting both ways on candidate validity. @@ -330,3 +324,69 @@ impl Table { None } } + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::HashMap; + + #[derive(Copy, Clone, Hash, PartialEq, Eq)] + struct ValidatorId(usize); + + #[derive(Copy, Clone, Hash, PartialEq, Eq)] + struct GroupId(usize); + + // group, body + #[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] + struct Candidate(usize, usize); + + #[derive(Copy, Clone, Hash)] + struct Signature(usize); + + #[derive(Copy, Clone, Hash, PartialEq, Eq)] + struct Digest(usize); + + struct TestContext { + // v -> (validity, availability) + validators: HashMap + } + + impl Context for TestContext { + type ValidatorId = ValidatorId; + type Digest = Digest; + type Candidate = Candidate; + type GroupId = GroupId; + type Signature = Signature; + + fn candidate_digest(&self, candidate: &Candidate) -> Digest { + Digest(candidate.1) + } + + fn candidate_group(&self, candidate: &Candidate) -> GroupId { + GroupId(candidate.0) + } + + fn is_member_of( + &self, + validator: &ValidatorId, + group: &GroupId + ) -> bool { + self.validators.get(validator).map(|v| &v.0 == group).unwrap_or(false) + } + + fn is_availability_guarantor_of( + &self, + validator: &ValidatorId, + group: &GroupId + ) -> bool { + self.validators.get(validator).map(|v| &v.1 == group).unwrap_or(false) + } + + fn statement_signer( + &self, + statement: &SignedStatement, + ) -> Option { + Some(ValidatorId(statement.signature.0)) + } + } +}