From bad27b092118fd0350e0a2fe7420f84c38b937ae Mon Sep 17 00:00:00 2001 From: Ismail Khoffi Date: Tue, 29 Oct 2019 20:06:25 +0100 Subject: [PATCH] Use concrete basic types for time, hash, bytes, validator id --- lite/src/types.rs | 21 ++++++++++++--------- lite/src/verifier.rs | 3 ++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lite/src/types.rs b/lite/src/types.rs index f1e5ab43f..6c0aea846 100644 --- a/lite/src/types.rs +++ b/lite/src/types.rs @@ -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. @@ -10,12 +11,14 @@ 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 @@ -23,7 +26,7 @@ pub type ValID = u64; // TODO /// 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; @@ -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. @@ -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 { diff --git a/lite/src/verifier.rs b/lite/src/verifier.rs index 91d0ebe6a..45104b9d2 100644 --- a/lite/src/verifier.rs +++ b/lite/src/verifier.rs @@ -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(last_header: &H, trusting_period: Time, now: Time) -> Result<(), Error> +fn expired(last_header: &H, trusting_period: Duration, now: Instant) -> Result<(), Error> where H: Header, {