Skip to content

Commit

Permalink
into_vec -> iter
Browse files Browse the repository at this point in the history
  • Loading branch information
ebuchman committed Oct 3, 2019
1 parent 4bb3f00 commit f521f04
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
7 changes: 4 additions & 3 deletions lite/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub trait Header {
/// Validator type which can be used for verifying signatures.
pub trait ValidatorSet {
type Validator: Validator;
type ValidatorIter: ExactSizeIterator<Item = Self::Validator>;

/// Hash of the validator set.
fn hash(&self) -> Hash;
Expand All @@ -45,8 +46,7 @@ pub trait ValidatorSet {
fn total_power(&self) -> u64;

/// For iterating over the underlying validators.
/// TODO: make this iter()
fn into_vec(&self) -> Vec<Self::Validator>;
fn iter(&self) -> Self::ValidatorIter;
}

/// ValidatorSetLookup allows validator to be fetched via their ID
Expand All @@ -68,6 +68,7 @@ pub trait Validator {
/// for verification.
pub trait Commit {
type Vote: Vote;
type VoteIter: ExactSizeIterator<Item = Option<Self::Vote>>;

/// Hash of the header this commit is for.
fn header_hash(&self) -> Hash;
Expand All @@ -77,7 +78,7 @@ pub trait Commit {
/// we ignore absent votes and votes for nil here.
/// NOTE: we may want to check signatures for nil votes,
/// and thus use an ternary enum here instead of the binary Option.
fn into_vec(&self) -> Vec<Option<Self::Vote>>;
fn iter(&self) -> Self::VoteIter;
}

/// Vote contains the data to verify a validator voted correctly in the commit.
Expand Down
11 changes: 4 additions & 7 deletions lite/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,16 @@ where
let total_power = vals.total_power();
let mut signed_power: u64 = 0;

let vals_vec = vals.into_vec();
let commit_vec = commit.into_vec();
let vals_iter = vals.iter();
let commit_iter = commit.iter();

if vals_vec.len() != commit_vec.len() {
if vals_iter.len() != commit_iter.len() {
return Err(Error::InvalidCommitLength);
}

// The vals and commit have a 1-to-1 correspondance.
// This means we don't need the validator IDs or to do any lookup,
// we can just zip the iterators.
let vals_iter = vals_vec.iter();
let commit_iter = commit_vec.iter();
for (val, vote_opt) in vals_iter.zip(commit_iter) {
// skip absent and nil votes
// NOTE: do we want to check the validity of votes
Expand Down Expand Up @@ -151,8 +149,7 @@ where

// NOTE we don't know the validators that committed this block,
// so we have to check for each vote if its validator is already known.
let commit_vec = commit.into_vec();
let commit_iter = commit_vec.iter();
let commit_iter = commit.iter();
for vote_opt in commit_iter {
// skip absent and nil votes
// NOTE: do we want to check the validity of votes
Expand Down

0 comments on commit f521f04

Please sign in to comment.