Skip to content

Commit

Permalink
Use concrete basic types for time, hash, bytes, validator id
Browse files Browse the repository at this point in the history
  • Loading branch information
liamsi committed Oct 29, 2019
1 parent a28f145 commit bad27b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
21 changes: 12 additions & 9 deletions lite/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::time::{Duration, Instant};
/// TrustedState stores the latest state trusted by a lite client,
/// including the last header and the validator set to use to verify
/// the next header.
Expand All @@ -10,20 +11,22 @@ where
pub validators: V, // height H
}

/// Need to do something better here :)
pub type Height = u64;
pub type Hash = u64; // TODO
pub type Time = u64; // TODO
pub type Bytes = u64; // TODO
pub type ValID = u64; // TODO
/// Size of the underlying Hash in bytes
pub const HASH_LENGTH: usize = 32;
#[derive(Eq, PartialEq)]
pub struct Hash([u8; HASH_LENGTH]);
/// Size of a validator ID in bytes
pub const VAL_ID_LENGTH: usize = 20;
pub struct ValID([u8; VAL_ID_LENGTH]);

/// Header contains meta data about the block -
/// the height, the time, the hash of the validator set
/// that should sign this header, and the hash of the validator
/// set that should sign the next header.
pub trait Header {
fn height(&self) -> Height;
fn bft_time(&self) -> Time;
fn bft_time(&self) -> Instant;
fn validators_hash(&self) -> Hash;
fn next_validators_hash(&self) -> Hash;

Expand Down Expand Up @@ -60,7 +63,7 @@ pub trait ValidatorSetLookup: ValidatorSet {
/// to its public key material to verify signatures.
pub trait Validator {
fn power(&self) -> u64;
fn verify_signature(&self, sign_bytes: Bytes, signature: Bytes) -> bool;
fn verify_signature(&self, sign_bytes: &[u8], signature: &[u8]) -> bool;
}

/// Commit is proof a Header is valid.
Expand Down Expand Up @@ -93,8 +96,8 @@ pub trait Commit {
/// is only necessary to avoid slashing in the multi chain context.
pub trait Vote {
fn validator_id(&self) -> ValID;
fn sign_bytes(&self) -> Bytes;
fn signature(&self) -> Bytes;
fn sign_bytes(&self) -> &[u8];
fn signature(&self) -> &[u8];
}

pub enum Error {
Expand Down
3 changes: 2 additions & 1 deletion lite/src/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::types::*;
use std::time::{Duration, Instant};

/// Returns an error if the header has expired according to the given
/// trusting_period and current time. If so, the verifier must be reset subjectively.
/// NOTE: this doesn't belong here. It should be called by something that handles whether to trust
/// a verifieds commit. Verified here is really just about the header/commit/validators. Time is an
/// external concern :)
fn expired<H>(last_header: &H, trusting_period: Time, now: Time) -> Result<(), Error>
fn expired<H>(last_header: &H, trusting_period: Duration, now: Instant) -> Result<(), Error>
where
H: Header,
{
Expand Down

0 comments on commit bad27b0

Please sign in to comment.