Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use concrete basic types for time, hash, bytes, validator id #51

Merged
merged 5 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

members = [
"tendermint",
"lite",
]
7 changes: 0 additions & 7 deletions lite/Cargo.toml

This file was deleted.

2 changes: 0 additions & 2 deletions lite/src/lib.rs

This file was deleted.

2 changes: 1 addition & 1 deletion tendermint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ authors = [
]

[badges]
circle-ci = { repository = "tendermint/kms" }
circle-ci = { repository = "interchainio/tendermint-rs" }

[dependencies]
byteorder = { version = "1.2" }
Expand Down
25 changes: 23 additions & 2 deletions tendermint/src/block/header.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Block headers

use crate::{account, block, chain, Hash, Time};
use crate::{account, block, chain, lite, Hash, Time};
use {
crate::serializers,
serde::{Deserialize, Serialize},
Expand Down Expand Up @@ -70,6 +69,28 @@ pub struct Header {
pub proposer_address: account::Id,
}

impl lite::Header for Header {
fn height(&self) -> block::Height {
self.height
}

fn bft_time(&self) -> Time {
self.time
}

fn validators_hash(&self) -> Hash {
self.validators_hash
}

fn next_validators_hash(&self) -> Hash {
unimplemented!()
}

fn hash(&self) -> Hash {
unimplemented!()
}
}

/// `Version` contains the protocol version for the blockchain and the
/// application.
///
Expand Down
2 changes: 2 additions & 0 deletions tendermint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub mod consensus;
pub mod evidence;
pub mod genesis;
pub mod hash;
#[allow(dead_code, missing_docs)]
pub mod lite;
pub mod merkle;
mod moniker;
pub mod net;
Expand Down
4 changes: 4 additions & 0 deletions tendermint/src/lite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub use self::types::*;
pub mod types;
pub mod verifier;
pub use self::verifier::*;
23 changes: 11 additions & 12 deletions lite/src/types.rs → tendermint/src/lite/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use crate::account::Id;
use crate::block::Height;
use crate::Hash;
#[allow(clippy::all)]
use crate::Time;

/// 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,13 +16,6 @@ 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

/// 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
Expand Down Expand Up @@ -52,15 +51,15 @@ pub trait ValidatorSet {
/// ValidatorSetLookup allows validator to be fetched via their ID
/// (ie. their address).
pub trait ValidatorSetLookup: ValidatorSet {
fn validator(&self, val_id: ValID) -> Option<Self::Validator>;
fn validator(&self, val_id: Id) -> Option<Self::Validator>;
}

/// Validator has a voting power and can verify
/// its own signatures. Note it must have implicit access
/// 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 @@ -92,9 +91,9 @@ pub trait Commit {
/// within an enum at the VoteSet level indicating which block it is for, and the chain id
/// 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 validator_id(&self) -> Id;
fn sign_bytes(&self) -> &[u8];
fn signature(&self) -> &[u8];
}

pub enum Error {
Expand Down
20 changes: 13 additions & 7 deletions lite/src/verifier.rs → tendermint/src/lite/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use crate::types::*;
#[allow(clippy::all)]
use crate::lite::{Commit, Error, Header, Validator, ValidatorSet, ValidatorSetLookup, Vote};
use crate::Time;
use std::time::Duration;

/// 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
/// a verified 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: Time) -> Result<(), Error>
where
H: Header,
{
if last_header.bft_time() + trusting_period < now {
return Err(Error::Expired);
if let Ok(passed) = now.duration_since(last_header.bft_time()) {
if passed > trusting_period {
return Err(Error::Expired);
}
}
// TODO move this out of the verifier and deal with overflows etc (proper err handling)
Ok(())
}

Expand Down Expand Up @@ -106,7 +112,7 @@ where
return Err(Error::InvalidCommitLength);
}

// The vals and commit have a 1-to-1 correspondance.
// The vals and commit have a 1-to-1 correspondence.
// This means we don't need the validator IDs or to do any lookup,
// we can just zip the iterators.
for (val, vote_opt) in vals_iter.zip(commit_iter) {
Expand Down Expand Up @@ -176,7 +182,7 @@ where
// check the signers account for +1/3 of the voting power
// TODO: incorporate "trust_level" in here to possibly increase
// beyond 1/3.
if signed_power * 3 <= total_power * 1 {
if signed_power * 3 <= total_power {
return Err(Error::InsufficientVotingPower);
}

Expand Down