mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-17 22:55:40 +00:00
validator -> authority
This commit is contained in:
@@ -16,14 +16,14 @@
|
||||
|
||||
//! The statement table.
|
||||
//!
|
||||
//! This stores messages other validators issue about candidates.
|
||||
//! This stores messages other authorities issue about candidates.
|
||||
//!
|
||||
//! These messages are used to create a proposal submitted to a BFT consensus process.
|
||||
//!
|
||||
//! Proposals are formed of sets of candidates which have the requisite number of
|
||||
//! validity and availability votes.
|
||||
//!
|
||||
//! Each parachain is associated with two sets of validators: those which can
|
||||
//! Each parachain is associated with two sets of authorities: those which can
|
||||
//! propose and attest to validity of candidates, and those who can only attest
|
||||
//! to availability.
|
||||
|
||||
@@ -36,8 +36,8 @@ use super::StatementBatch;
|
||||
|
||||
/// Context for the statement table.
|
||||
pub trait Context {
|
||||
/// A validator ID
|
||||
type ValidatorId: Debug + Hash + Eq + Clone;
|
||||
/// A authority ID
|
||||
type AuthorityId: Debug + Hash + Eq + Clone;
|
||||
/// The digest (hash or other unique attribute) of a candidate.
|
||||
type Digest: Debug + Hash + Eq + Clone;
|
||||
/// The group ID type
|
||||
@@ -53,16 +53,16 @@ pub trait Context {
|
||||
/// get the group of a candidate.
|
||||
fn candidate_group(candidate: &Self::Candidate) -> Self::GroupId;
|
||||
|
||||
/// Whether a validator is a member of a group.
|
||||
/// Whether a authority is a member of a group.
|
||||
/// Members are meant to submit candidates and vote on validity.
|
||||
fn is_member_of(&self, validator: &Self::ValidatorId, group: &Self::GroupId) -> bool;
|
||||
fn is_member_of(&self, authority: &Self::AuthorityId, group: &Self::GroupId) -> bool;
|
||||
|
||||
/// Whether a validator is an availability guarantor of a group.
|
||||
/// Whether a authority is an availability guarantor of a group.
|
||||
/// Guarantors are meant to vote on availability for candidates submitted
|
||||
/// in a group.
|
||||
fn is_availability_guarantor_of(
|
||||
&self,
|
||||
validator: &Self::ValidatorId,
|
||||
authority: &Self::AuthorityId,
|
||||
group: &Self::GroupId,
|
||||
) -> bool;
|
||||
|
||||
@@ -73,18 +73,18 @@ pub trait Context {
|
||||
/// Statements circulated among peers.
|
||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||
pub enum Statement<C, D> {
|
||||
/// Broadcast by a validator to indicate that this is his candidate for
|
||||
/// Broadcast by a authority to indicate that this is his candidate for
|
||||
/// inclusion.
|
||||
///
|
||||
/// Broadcasting two different candidate messages per round is not allowed.
|
||||
Candidate(C),
|
||||
/// Broadcast by a validator to attest that the candidate with given digest
|
||||
/// Broadcast by a authority to attest that the candidate with given digest
|
||||
/// is valid.
|
||||
Valid(D),
|
||||
/// Broadcast by a validator to attest that the auxiliary data for a candidate
|
||||
/// Broadcast by a authority to attest that the auxiliary data for a candidate
|
||||
/// with given digest is available.
|
||||
Available(D),
|
||||
/// Broadcast by a validator to attest that the candidate with given digest
|
||||
/// Broadcast by a authority to attest that the candidate with given digest
|
||||
/// is invalid.
|
||||
Invalid(D),
|
||||
}
|
||||
@@ -100,23 +100,23 @@ pub struct SignedStatement<C, D, V, S> {
|
||||
pub sender: V,
|
||||
}
|
||||
|
||||
// A unique trace for a class of valid statements issued by a validator.
|
||||
// A unique trace for a class of valid statements issued by a authority.
|
||||
//
|
||||
// We keep track of which statements we have received or sent to other validators
|
||||
// We keep track of which statements we have received or sent to other authorities
|
||||
// in order to prevent relaying the same data multiple times.
|
||||
//
|
||||
// The signature of the statement is replaced by the validator because the validator
|
||||
// The signature of the statement is replaced by the authority because the authority
|
||||
// is unique while signatures are not (at least under common schemes like
|
||||
// Schnorr or ECDSA).
|
||||
#[derive(Hash, PartialEq, Eq, Clone)]
|
||||
enum StatementTrace<V, D> {
|
||||
/// The candidate proposed by the validator.
|
||||
/// The candidate proposed by the authority.
|
||||
Candidate(V),
|
||||
/// A validity statement from that validator about the given digest.
|
||||
/// A validity statement from that authority about the given digest.
|
||||
Valid(V, D),
|
||||
/// An invalidity statement from that validator about the given digest.
|
||||
/// An invalidity statement from that authority about the given digest.
|
||||
Invalid(V, D),
|
||||
/// An availability statement from that validator about the given digest.
|
||||
/// An availability statement from that authority about the given digest.
|
||||
Available(V, D),
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ pub trait ResolveMisbehavior {
|
||||
}
|
||||
|
||||
impl<C: Context + ?Sized> ResolveMisbehavior for C {
|
||||
type Misbehavior = Misbehavior<C::Candidate, C::Digest, C::ValidatorId, C::Signature>;
|
||||
type Misbehavior = Misbehavior<C::Candidate, C::Digest, C::AuthorityId, C::Signature>;
|
||||
}
|
||||
|
||||
// kinds of votes for validity
|
||||
@@ -203,9 +203,9 @@ pub struct Summary<D, G> {
|
||||
pub struct CandidateData<C: Context> {
|
||||
group_id: C::GroupId,
|
||||
candidate: C::Candidate,
|
||||
validity_votes: HashMap<C::ValidatorId, ValidityVote<C::Signature>>,
|
||||
availability_votes: HashMap<C::ValidatorId, C::Signature>,
|
||||
indicated_bad_by: Vec<C::ValidatorId>,
|
||||
validity_votes: HashMap<C::AuthorityId, ValidityVote<C::Signature>>,
|
||||
availability_votes: HashMap<C::AuthorityId, C::Signature>,
|
||||
indicated_bad_by: Vec<C::AuthorityId>,
|
||||
}
|
||||
|
||||
impl<C: Context> CandidateData<C> {
|
||||
@@ -216,7 +216,7 @@ impl<C: Context> CandidateData<C> {
|
||||
|
||||
// Candidate data can be included in a proposal
|
||||
// if it has enough validity and availability votes
|
||||
// and no validators have called it bad.
|
||||
// and no authorities have called it bad.
|
||||
fn can_be_included(&self, validity_threshold: usize, availability_threshold: usize) -> bool {
|
||||
self.indicated_bad_by.is_empty()
|
||||
&& self.validity_votes.len() >= validity_threshold
|
||||
@@ -234,15 +234,15 @@ impl<C: Context> CandidateData<C> {
|
||||
}
|
||||
}
|
||||
|
||||
// validator metadata
|
||||
struct ValidatorData<C: Context> {
|
||||
// authority metadata
|
||||
struct AuthorityData<C: Context> {
|
||||
proposal: Option<(C::Digest, C::Signature)>,
|
||||
known_statements: HashSet<StatementTrace<C::ValidatorId, C::Digest>>,
|
||||
known_statements: HashSet<StatementTrace<C::AuthorityId, C::Digest>>,
|
||||
}
|
||||
|
||||
impl<C: Context> Default for ValidatorData<C> {
|
||||
impl<C: Context> Default for AuthorityData<C> {
|
||||
fn default() -> Self {
|
||||
ValidatorData {
|
||||
AuthorityData {
|
||||
proposal: None,
|
||||
known_statements: HashSet::default(),
|
||||
}
|
||||
@@ -251,15 +251,15 @@ impl<C: Context> Default for ValidatorData<C> {
|
||||
|
||||
/// Stores votes
|
||||
pub struct Table<C: Context> {
|
||||
validator_data: HashMap<C::ValidatorId, ValidatorData<C>>,
|
||||
detected_misbehavior: HashMap<C::ValidatorId, <C as ResolveMisbehavior>::Misbehavior>,
|
||||
authority_data: HashMap<C::AuthorityId, AuthorityData<C>>,
|
||||
detected_misbehavior: HashMap<C::AuthorityId, <C as ResolveMisbehavior>::Misbehavior>,
|
||||
candidate_votes: HashMap<C::Digest, CandidateData<C>>,
|
||||
}
|
||||
|
||||
impl<C: Context> Default for Table<C> {
|
||||
fn default() -> Self {
|
||||
Table {
|
||||
validator_data: HashMap::new(),
|
||||
authority_data: HashMap::new(),
|
||||
detected_misbehavior: HashMap::new(),
|
||||
candidate_votes: HashMap::new(),
|
||||
}
|
||||
@@ -307,15 +307,15 @@ impl<C: Context> Table<C> {
|
||||
}
|
||||
|
||||
/// Import a signed statement. Signatures should be checked for validity, and the
|
||||
/// sender should be checked to actually be a validator.
|
||||
/// sender should be checked to actually be a authority.
|
||||
///
|
||||
/// This can note the origin of the statement to indicate that he has
|
||||
/// seen it already.
|
||||
pub fn import_statement(
|
||||
&mut self,
|
||||
context: &C,
|
||||
statement: SignedStatement<C::Candidate, C::Digest, C::ValidatorId, C::Signature>,
|
||||
from: Option<C::ValidatorId>
|
||||
statement: SignedStatement<C::Candidate, C::Digest, C::AuthorityId, C::Signature>,
|
||||
from: Option<C::AuthorityId>
|
||||
) -> Option<Summary<C::Digest, C::GroupId>> {
|
||||
let SignedStatement { statement, signature, sender: signer } = statement;
|
||||
|
||||
@@ -375,7 +375,7 @@ impl<C: Context> Table<C> {
|
||||
|
||||
/// Access all witnessed misbehavior.
|
||||
pub fn get_misbehavior(&self)
|
||||
-> &HashMap<C::ValidatorId, <C as ResolveMisbehavior>::Misbehavior>
|
||||
-> &HashMap<C::AuthorityId, <C as ResolveMisbehavior>::Misbehavior>
|
||||
{
|
||||
&self.detected_misbehavior
|
||||
}
|
||||
@@ -383,8 +383,8 @@ impl<C: Context> Table<C> {
|
||||
/// Fill a statement batch and note messages seen by the targets.
|
||||
pub fn fill_batch<B>(&mut self, batch: &mut B)
|
||||
where B: StatementBatch<
|
||||
C::ValidatorId,
|
||||
SignedStatement<C::Candidate, C::Digest, C::ValidatorId, C::Signature>,
|
||||
C::AuthorityId,
|
||||
SignedStatement<C::Candidate, C::Digest, C::AuthorityId, C::Signature>,
|
||||
>
|
||||
{
|
||||
// naively iterate all statements so far, taking any that
|
||||
@@ -394,24 +394,24 @@ impl<C: Context> Table<C> {
|
||||
// entries out of a hashmap mutably -- we just move them out and
|
||||
// replace them when we're done.
|
||||
struct SwappedTargetData<'a, C: 'a + Context> {
|
||||
validator_data: &'a mut HashMap<C::ValidatorId, ValidatorData<C>>,
|
||||
target_data: Vec<(C::ValidatorId, ValidatorData<C>)>,
|
||||
authority_data: &'a mut HashMap<C::AuthorityId, AuthorityData<C>>,
|
||||
target_data: Vec<(C::AuthorityId, AuthorityData<C>)>,
|
||||
}
|
||||
|
||||
impl<'a, C: 'a + Context> Drop for SwappedTargetData<'a, C> {
|
||||
fn drop(&mut self) {
|
||||
for (id, data) in self.target_data.drain(..) {
|
||||
self.validator_data.insert(id, data);
|
||||
self.authority_data.insert(id, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pre-fetch authority data for all the targets.
|
||||
let mut target_data = {
|
||||
let validator_data = &mut self.validator_data;
|
||||
let authority_data = &mut self.authority_data;
|
||||
let mut target_data = Vec::with_capacity(batch.targets().len());
|
||||
for target in batch.targets() {
|
||||
let active_data = match validator_data.get_mut(target) {
|
||||
let active_data = match authority_data.get_mut(target) {
|
||||
None => Default::default(),
|
||||
Some(x) => ::std::mem::replace(x, Default::default()),
|
||||
};
|
||||
@@ -420,7 +420,7 @@ impl<C: Context> Table<C> {
|
||||
}
|
||||
|
||||
SwappedTargetData {
|
||||
validator_data,
|
||||
authority_data,
|
||||
target_data
|
||||
}
|
||||
};
|
||||
@@ -503,8 +503,8 @@ impl<C: Context> Table<C> {
|
||||
|
||||
}
|
||||
|
||||
fn note_trace_seen(&mut self, trace: StatementTrace<C::ValidatorId, C::Digest>, known_by: C::ValidatorId) {
|
||||
self.validator_data.entry(known_by).or_insert_with(|| ValidatorData {
|
||||
fn note_trace_seen(&mut self, trace: StatementTrace<C::AuthorityId, C::Digest>, known_by: C::AuthorityId) {
|
||||
self.authority_data.entry(known_by).or_insert_with(|| AuthorityData {
|
||||
proposal: None,
|
||||
known_statements: HashSet::default(),
|
||||
}).known_statements.insert(trace);
|
||||
@@ -513,7 +513,7 @@ impl<C: Context> Table<C> {
|
||||
fn import_candidate(
|
||||
&mut self,
|
||||
context: &C,
|
||||
from: C::ValidatorId,
|
||||
from: C::AuthorityId,
|
||||
candidate: C::Candidate,
|
||||
signature: C::Signature,
|
||||
) -> (Option<<C as ResolveMisbehavior>::Misbehavior>, Option<Summary<C::Digest, C::GroupId>>) {
|
||||
@@ -531,10 +531,10 @@ impl<C: Context> Table<C> {
|
||||
);
|
||||
}
|
||||
|
||||
// check that validator hasn't already specified another candidate.
|
||||
// check that authority hasn't already specified another candidate.
|
||||
let digest = C::candidate_digest(&candidate);
|
||||
|
||||
let new_proposal = match self.validator_data.entry(from.clone()) {
|
||||
let new_proposal = match self.authority_data.entry(from.clone()) {
|
||||
Entry::Occupied(mut occ) => {
|
||||
// if digest is different, fetch candidate and
|
||||
// note misbehavior.
|
||||
@@ -543,7 +543,7 @@ impl<C: Context> Table<C> {
|
||||
if let Some((ref old_digest, ref old_sig)) = existing.proposal {
|
||||
if old_digest != &digest {
|
||||
const EXISTENCE_PROOF: &str =
|
||||
"when proposal first received from validator, candidate \
|
||||
"when proposal first received from authority, candidate \
|
||||
votes entry is created. proposal here is `Some`, therefore \
|
||||
candidate votes entry exists; qed";
|
||||
|
||||
@@ -568,7 +568,7 @@ impl<C: Context> Table<C> {
|
||||
}
|
||||
}
|
||||
Entry::Vacant(vacant) => {
|
||||
vacant.insert(ValidatorData {
|
||||
vacant.insert(AuthorityData {
|
||||
proposal: Some((digest.clone(), signature.clone())),
|
||||
known_statements: HashSet::new(),
|
||||
});
|
||||
@@ -599,7 +599,7 @@ impl<C: Context> Table<C> {
|
||||
fn validity_vote(
|
||||
&mut self,
|
||||
context: &C,
|
||||
from: C::ValidatorId,
|
||||
from: C::AuthorityId,
|
||||
digest: C::Digest,
|
||||
vote: ValidityVote<C::Signature>,
|
||||
) -> (Option<<C as ResolveMisbehavior>::Misbehavior>, Option<Summary<C::Digest, C::GroupId>>) {
|
||||
@@ -608,7 +608,7 @@ impl<C: Context> Table<C> {
|
||||
Some(votes) => votes,
|
||||
};
|
||||
|
||||
// check that this validator actually can vote in this group.
|
||||
// check that this authority actually can vote in this group.
|
||||
if !context.is_member_of(&from, &votes.group_id) {
|
||||
let (sig, valid) = match vote {
|
||||
ValidityVote::Valid(s) => (s, true),
|
||||
@@ -678,7 +678,7 @@ impl<C: Context> Table<C> {
|
||||
fn availability_vote(
|
||||
&mut self,
|
||||
context: &C,
|
||||
from: C::ValidatorId,
|
||||
from: C::AuthorityId,
|
||||
digest: C::Digest,
|
||||
signature: C::Signature,
|
||||
) -> (Option<<C as ResolveMisbehavior>::Misbehavior>, Option<Summary<C::Digest, C::GroupId>>) {
|
||||
@@ -687,7 +687,7 @@ impl<C: Context> Table<C> {
|
||||
Some(votes) => votes,
|
||||
};
|
||||
|
||||
// check that this validator actually can vote in this group.
|
||||
// check that this authority actually can vote in this group.
|
||||
if !context.is_availability_guarantor_of(&from, &votes.group_id) {
|
||||
return (
|
||||
Some(Misbehavior::UnauthorizedStatement(UnauthorizedStatement {
|
||||
@@ -714,14 +714,14 @@ mod tests {
|
||||
|
||||
fn create<C: Context>() -> Table<C> {
|
||||
Table {
|
||||
validator_data: HashMap::default(),
|
||||
authority_data: HashMap::default(),
|
||||
detected_misbehavior: HashMap::default(),
|
||||
candidate_votes: HashMap::default(),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
|
||||
struct ValidatorId(usize);
|
||||
struct AuthorityId(usize);
|
||||
|
||||
#[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
|
||||
struct GroupId(usize);
|
||||
@@ -739,11 +739,11 @@ mod tests {
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct TestContext {
|
||||
// v -> (validity, availability)
|
||||
validators: HashMap<ValidatorId, (GroupId, GroupId)>
|
||||
authorities: HashMap<AuthorityId, (GroupId, GroupId)>
|
||||
}
|
||||
|
||||
impl Context for TestContext {
|
||||
type ValidatorId = ValidatorId;
|
||||
type AuthorityId = AuthorityId;
|
||||
type Digest = Digest;
|
||||
type Candidate = Candidate;
|
||||
type GroupId = GroupId;
|
||||
@@ -759,18 +759,18 @@ mod tests {
|
||||
|
||||
fn is_member_of(
|
||||
&self,
|
||||
validator: &ValidatorId,
|
||||
authority: &AuthorityId,
|
||||
group: &GroupId
|
||||
) -> bool {
|
||||
self.validators.get(validator).map(|v| &v.0 == group).unwrap_or(false)
|
||||
self.authorities.get(authority).map(|v| &v.0 == group).unwrap_or(false)
|
||||
}
|
||||
|
||||
fn is_availability_guarantor_of(
|
||||
&self,
|
||||
validator: &ValidatorId,
|
||||
authority: &AuthorityId,
|
||||
group: &GroupId
|
||||
) -> bool {
|
||||
self.validators.get(validator).map(|v| &v.1 == group).unwrap_or(false)
|
||||
self.authorities.get(authority).map(|v| &v.1 == group).unwrap_or(false)
|
||||
}
|
||||
|
||||
fn requisite_votes(&self, _id: &GroupId) -> (usize, usize) {
|
||||
@@ -781,9 +781,9 @@ mod tests {
|
||||
#[test]
|
||||
fn submitting_two_candidates_is_misbehavior() {
|
||||
let context = TestContext {
|
||||
validators: {
|
||||
authorities: {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(ValidatorId(1), (GroupId(2), GroupId(455)));
|
||||
map.insert(AuthorityId(1), (GroupId(2), GroupId(455)));
|
||||
map
|
||||
}
|
||||
};
|
||||
@@ -792,21 +792,21 @@ mod tests {
|
||||
let statement_a = SignedStatement {
|
||||
statement: Statement::Candidate(Candidate(2, 100)),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
};
|
||||
|
||||
let statement_b = SignedStatement {
|
||||
statement: Statement::Candidate(Candidate(2, 999)),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
};
|
||||
|
||||
table.import_statement(&context, statement_a, None);
|
||||
assert!(!table.detected_misbehavior.contains_key(&ValidatorId(1)));
|
||||
assert!(!table.detected_misbehavior.contains_key(&AuthorityId(1)));
|
||||
|
||||
table.import_statement(&context, statement_b, None);
|
||||
assert_eq!(
|
||||
table.detected_misbehavior.get(&ValidatorId(1)).unwrap(),
|
||||
table.detected_misbehavior.get(&AuthorityId(1)).unwrap(),
|
||||
&Misbehavior::MultipleCandidates(MultipleCandidates {
|
||||
first: (Candidate(2, 100), Signature(1)),
|
||||
second: (Candidate(2, 999), Signature(1)),
|
||||
@@ -817,9 +817,9 @@ mod tests {
|
||||
#[test]
|
||||
fn submitting_candidate_from_wrong_group_is_misbehavior() {
|
||||
let context = TestContext {
|
||||
validators: {
|
||||
authorities: {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(ValidatorId(1), (GroupId(3), GroupId(455)));
|
||||
map.insert(AuthorityId(1), (GroupId(3), GroupId(455)));
|
||||
map
|
||||
}
|
||||
};
|
||||
@@ -828,18 +828,18 @@ mod tests {
|
||||
let statement = SignedStatement {
|
||||
statement: Statement::Candidate(Candidate(2, 100)),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
};
|
||||
|
||||
table.import_statement(&context, statement, None);
|
||||
|
||||
assert_eq!(
|
||||
table.detected_misbehavior.get(&ValidatorId(1)).unwrap(),
|
||||
table.detected_misbehavior.get(&AuthorityId(1)).unwrap(),
|
||||
&Misbehavior::UnauthorizedStatement(UnauthorizedStatement {
|
||||
statement: SignedStatement {
|
||||
statement: Statement::Candidate(Candidate(2, 100)),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
},
|
||||
})
|
||||
);
|
||||
@@ -848,10 +848,10 @@ mod tests {
|
||||
#[test]
|
||||
fn unauthorized_votes() {
|
||||
let context = TestContext {
|
||||
validators: {
|
||||
authorities: {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(ValidatorId(1), (GroupId(2), GroupId(455)));
|
||||
map.insert(ValidatorId(2), (GroupId(3), GroupId(222)));
|
||||
map.insert(AuthorityId(1), (GroupId(2), GroupId(455)));
|
||||
map.insert(AuthorityId(2), (GroupId(3), GroupId(222)));
|
||||
map
|
||||
}
|
||||
};
|
||||
@@ -861,56 +861,56 @@ mod tests {
|
||||
let candidate_a = SignedStatement {
|
||||
statement: Statement::Candidate(Candidate(2, 100)),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
};
|
||||
let candidate_a_digest = Digest(100);
|
||||
|
||||
let candidate_b = SignedStatement {
|
||||
statement: Statement::Candidate(Candidate(3, 987)),
|
||||
signature: Signature(2),
|
||||
sender: ValidatorId(2),
|
||||
sender: AuthorityId(2),
|
||||
};
|
||||
let candidate_b_digest = Digest(987);
|
||||
|
||||
table.import_statement(&context, candidate_a, None);
|
||||
table.import_statement(&context, candidate_b, None);
|
||||
assert!(!table.detected_misbehavior.contains_key(&ValidatorId(1)));
|
||||
assert!(!table.detected_misbehavior.contains_key(&ValidatorId(2)));
|
||||
assert!(!table.detected_misbehavior.contains_key(&AuthorityId(1)));
|
||||
assert!(!table.detected_misbehavior.contains_key(&AuthorityId(2)));
|
||||
|
||||
// validator 1 votes for availability on 2's candidate.
|
||||
// authority 1 votes for availability on 2's candidate.
|
||||
let bad_availability_vote = SignedStatement {
|
||||
statement: Statement::Available(candidate_b_digest.clone()),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
};
|
||||
table.import_statement(&context, bad_availability_vote, None);
|
||||
|
||||
assert_eq!(
|
||||
table.detected_misbehavior.get(&ValidatorId(1)).unwrap(),
|
||||
table.detected_misbehavior.get(&AuthorityId(1)).unwrap(),
|
||||
&Misbehavior::UnauthorizedStatement(UnauthorizedStatement {
|
||||
statement: SignedStatement {
|
||||
statement: Statement::Available(candidate_b_digest),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
// validator 2 votes for validity on 1's candidate.
|
||||
// authority 2 votes for validity on 1's candidate.
|
||||
let bad_validity_vote = SignedStatement {
|
||||
statement: Statement::Valid(candidate_a_digest.clone()),
|
||||
signature: Signature(2),
|
||||
sender: ValidatorId(2),
|
||||
sender: AuthorityId(2),
|
||||
};
|
||||
table.import_statement(&context, bad_validity_vote, None);
|
||||
|
||||
assert_eq!(
|
||||
table.detected_misbehavior.get(&ValidatorId(2)).unwrap(),
|
||||
table.detected_misbehavior.get(&AuthorityId(2)).unwrap(),
|
||||
&Misbehavior::UnauthorizedStatement(UnauthorizedStatement {
|
||||
statement: SignedStatement {
|
||||
statement: Statement::Valid(candidate_a_digest),
|
||||
signature: Signature(2),
|
||||
sender: ValidatorId(2),
|
||||
sender: AuthorityId(2),
|
||||
},
|
||||
})
|
||||
);
|
||||
@@ -919,10 +919,10 @@ mod tests {
|
||||
#[test]
|
||||
fn validity_double_vote_is_misbehavior() {
|
||||
let context = TestContext {
|
||||
validators: {
|
||||
authorities: {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(ValidatorId(1), (GroupId(2), GroupId(455)));
|
||||
map.insert(ValidatorId(2), (GroupId(2), GroupId(246)));
|
||||
map.insert(AuthorityId(1), (GroupId(2), GroupId(455)));
|
||||
map.insert(AuthorityId(2), (GroupId(2), GroupId(246)));
|
||||
map
|
||||
}
|
||||
};
|
||||
@@ -931,32 +931,32 @@ mod tests {
|
||||
let statement = SignedStatement {
|
||||
statement: Statement::Candidate(Candidate(2, 100)),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
};
|
||||
let candidate_digest = Digest(100);
|
||||
|
||||
table.import_statement(&context, statement, None);
|
||||
assert!(!table.detected_misbehavior.contains_key(&ValidatorId(1)));
|
||||
assert!(!table.detected_misbehavior.contains_key(&AuthorityId(1)));
|
||||
|
||||
let valid_statement = SignedStatement {
|
||||
statement: Statement::Valid(candidate_digest.clone()),
|
||||
signature: Signature(2),
|
||||
sender: ValidatorId(2),
|
||||
sender: AuthorityId(2),
|
||||
};
|
||||
|
||||
let invalid_statement = SignedStatement {
|
||||
statement: Statement::Invalid(candidate_digest.clone()),
|
||||
signature: Signature(2),
|
||||
sender: ValidatorId(2),
|
||||
sender: AuthorityId(2),
|
||||
};
|
||||
|
||||
table.import_statement(&context, valid_statement, None);
|
||||
assert!(!table.detected_misbehavior.contains_key(&ValidatorId(2)));
|
||||
assert!(!table.detected_misbehavior.contains_key(&AuthorityId(2)));
|
||||
|
||||
table.import_statement(&context, invalid_statement, None);
|
||||
|
||||
assert_eq!(
|
||||
table.detected_misbehavior.get(&ValidatorId(2)).unwrap(),
|
||||
table.detected_misbehavior.get(&AuthorityId(2)).unwrap(),
|
||||
&Misbehavior::ValidityDoubleVote(ValidityDoubleVote::ValidityAndInvalidity(
|
||||
candidate_digest,
|
||||
Signature(2),
|
||||
@@ -968,9 +968,9 @@ mod tests {
|
||||
#[test]
|
||||
fn issue_and_vote_is_misbehavior() {
|
||||
let context = TestContext {
|
||||
validators: {
|
||||
authorities: {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(ValidatorId(1), (GroupId(2), GroupId(455)));
|
||||
map.insert(AuthorityId(1), (GroupId(2), GroupId(455)));
|
||||
map
|
||||
}
|
||||
};
|
||||
@@ -979,22 +979,22 @@ mod tests {
|
||||
let statement = SignedStatement {
|
||||
statement: Statement::Candidate(Candidate(2, 100)),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
};
|
||||
let candidate_digest = Digest(100);
|
||||
|
||||
table.import_statement(&context, statement, None);
|
||||
assert!(!table.detected_misbehavior.contains_key(&ValidatorId(1)));
|
||||
assert!(!table.detected_misbehavior.contains_key(&AuthorityId(1)));
|
||||
|
||||
let extra_vote = SignedStatement {
|
||||
statement: Statement::Valid(candidate_digest.clone()),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
};
|
||||
|
||||
table.import_statement(&context, extra_vote, None);
|
||||
assert_eq!(
|
||||
table.detected_misbehavior.get(&ValidatorId(1)).unwrap(),
|
||||
table.detected_misbehavior.get(&AuthorityId(1)).unwrap(),
|
||||
&Misbehavior::ValidityDoubleVote(ValidityDoubleVote::IssuedAndValidity(
|
||||
(Candidate(2, 100), Signature(1)),
|
||||
(Digest(100), Signature(1)),
|
||||
@@ -1018,18 +1018,18 @@ mod tests {
|
||||
assert!(!candidate.can_be_included(validity_threshold, availability_threshold));
|
||||
|
||||
for i in 0..validity_threshold {
|
||||
candidate.validity_votes.insert(ValidatorId(i + 100), ValidityVote::Valid(Signature(i + 100)));
|
||||
candidate.validity_votes.insert(AuthorityId(i + 100), ValidityVote::Valid(Signature(i + 100)));
|
||||
}
|
||||
|
||||
assert!(!candidate.can_be_included(validity_threshold, availability_threshold));
|
||||
|
||||
for i in 0..availability_threshold {
|
||||
candidate.availability_votes.insert(ValidatorId(i + 255), Signature(i + 255));
|
||||
candidate.availability_votes.insert(AuthorityId(i + 255), Signature(i + 255));
|
||||
}
|
||||
|
||||
assert!(candidate.can_be_included(validity_threshold, availability_threshold));
|
||||
|
||||
candidate.indicated_bad_by.push(ValidatorId(1024));
|
||||
candidate.indicated_bad_by.push(AuthorityId(1024));
|
||||
|
||||
assert!(!candidate.can_be_included(validity_threshold, availability_threshold));
|
||||
}
|
||||
@@ -1037,9 +1037,9 @@ mod tests {
|
||||
#[test]
|
||||
fn candidate_import_gives_summary() {
|
||||
let context = TestContext {
|
||||
validators: {
|
||||
authorities: {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(ValidatorId(1), (GroupId(2), GroupId(455)));
|
||||
map.insert(AuthorityId(1), (GroupId(2), GroupId(455)));
|
||||
map
|
||||
}
|
||||
};
|
||||
@@ -1048,7 +1048,7 @@ mod tests {
|
||||
let statement = SignedStatement {
|
||||
statement: Statement::Candidate(Candidate(2, 100)),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
};
|
||||
|
||||
let summary = table.import_statement(&context, statement, None)
|
||||
@@ -1063,10 +1063,10 @@ mod tests {
|
||||
#[test]
|
||||
fn candidate_vote_gives_summary() {
|
||||
let context = TestContext {
|
||||
validators: {
|
||||
authorities: {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(ValidatorId(1), (GroupId(2), GroupId(455)));
|
||||
map.insert(ValidatorId(2), (GroupId(2), GroupId(455)));
|
||||
map.insert(AuthorityId(1), (GroupId(2), GroupId(455)));
|
||||
map.insert(AuthorityId(2), (GroupId(2), GroupId(455)));
|
||||
map
|
||||
}
|
||||
};
|
||||
@@ -1075,23 +1075,23 @@ mod tests {
|
||||
let statement = SignedStatement {
|
||||
statement: Statement::Candidate(Candidate(2, 100)),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
};
|
||||
let candidate_digest = Digest(100);
|
||||
|
||||
table.import_statement(&context, statement, None);
|
||||
assert!(!table.detected_misbehavior.contains_key(&ValidatorId(1)));
|
||||
assert!(!table.detected_misbehavior.contains_key(&AuthorityId(1)));
|
||||
|
||||
let vote = SignedStatement {
|
||||
statement: Statement::Valid(candidate_digest.clone()),
|
||||
signature: Signature(2),
|
||||
sender: ValidatorId(2),
|
||||
sender: AuthorityId(2),
|
||||
};
|
||||
|
||||
let summary = table.import_statement(&context, vote, None)
|
||||
.expect("candidate vote to give summary");
|
||||
|
||||
assert!(!table.detected_misbehavior.contains_key(&ValidatorId(2)));
|
||||
assert!(!table.detected_misbehavior.contains_key(&AuthorityId(2)));
|
||||
|
||||
assert_eq!(summary.candidate, Digest(100));
|
||||
assert_eq!(summary.group_id, GroupId(2));
|
||||
@@ -1102,10 +1102,10 @@ mod tests {
|
||||
#[test]
|
||||
fn availability_vote_gives_summary() {
|
||||
let context = TestContext {
|
||||
validators: {
|
||||
authorities: {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(ValidatorId(1), (GroupId(2), GroupId(455)));
|
||||
map.insert(ValidatorId(2), (GroupId(5), GroupId(2)));
|
||||
map.insert(AuthorityId(1), (GroupId(2), GroupId(455)));
|
||||
map.insert(AuthorityId(2), (GroupId(5), GroupId(2)));
|
||||
map
|
||||
}
|
||||
};
|
||||
@@ -1114,23 +1114,23 @@ mod tests {
|
||||
let statement = SignedStatement {
|
||||
statement: Statement::Candidate(Candidate(2, 100)),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
};
|
||||
let candidate_digest = Digest(100);
|
||||
|
||||
table.import_statement(&context, statement, None);
|
||||
assert!(!table.detected_misbehavior.contains_key(&ValidatorId(1)));
|
||||
assert!(!table.detected_misbehavior.contains_key(&AuthorityId(1)));
|
||||
|
||||
let vote = SignedStatement {
|
||||
statement: Statement::Available(candidate_digest.clone()),
|
||||
signature: Signature(2),
|
||||
sender: ValidatorId(2),
|
||||
sender: AuthorityId(2),
|
||||
};
|
||||
|
||||
let summary = table.import_statement(&context, vote, None)
|
||||
.expect("candidate vote to give summary");
|
||||
|
||||
assert!(!table.detected_misbehavior.contains_key(&ValidatorId(2)));
|
||||
assert!(!table.detected_misbehavior.contains_key(&AuthorityId(2)));
|
||||
|
||||
assert_eq!(summary.candidate, Digest(100));
|
||||
assert_eq!(summary.group_id, GroupId(2));
|
||||
@@ -1141,10 +1141,10 @@ mod tests {
|
||||
#[test]
|
||||
fn filling_batch_sets_known_flag() {
|
||||
let context = TestContext {
|
||||
validators: {
|
||||
authorities: {
|
||||
let mut map = HashMap::new();
|
||||
for i in 1..10 {
|
||||
map.insert(ValidatorId(i), (GroupId(2), GroupId(400 + i)));
|
||||
map.insert(AuthorityId(i), (GroupId(2), GroupId(400 + i)));
|
||||
}
|
||||
map
|
||||
}
|
||||
@@ -1154,7 +1154,7 @@ mod tests {
|
||||
let statement = SignedStatement {
|
||||
statement: Statement::Candidate(Candidate(2, 100)),
|
||||
signature: Signature(1),
|
||||
sender: ValidatorId(1),
|
||||
sender: AuthorityId(1),
|
||||
};
|
||||
|
||||
table.import_statement(&context, statement, None);
|
||||
@@ -1163,7 +1163,7 @@ mod tests {
|
||||
let statement = SignedStatement {
|
||||
statement: Statement::Valid(Digest(100)),
|
||||
signature: Signature(i),
|
||||
sender: ValidatorId(i),
|
||||
sender: AuthorityId(i),
|
||||
};
|
||||
|
||||
table.import_statement(&context, statement, None);
|
||||
@@ -1171,7 +1171,7 @@ mod tests {
|
||||
|
||||
let mut batch = VecBatch {
|
||||
max_len: 5,
|
||||
targets: (1..10).map(ValidatorId).collect(),
|
||||
targets: (1..10).map(AuthorityId).collect(),
|
||||
items: Vec::new(),
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user