From 45eea29aa39783f7546faff157ebcc5d61150102 Mon Sep 17 00:00:00 2001 From: Ismail Khoffi Date: Wed, 25 Sep 2019 10:39:12 +0200 Subject: [PATCH] make the lite client a module instead of a separate crate: This is just the simplest way to move forward implementing the traits of the lite package. There are alternatives: We do not want a create a circular dependency between lite and tendermint (which does not even compile). To avoid this we could: 1) make lite a module of tendermint, or, 2) replicate a lot of the types of the tendermint crate in the lite package (e.g. Time, Ids etc), or 3) have a dependency from tendermint to the lite package only (but then the trait impls do need to live in the lite crate instead with the types in the tendermint crate directly). --- Cargo.toml | 1 - lite/Cargo.toml | 7 ------ lite/src/lib.rs | 2 -- tendermint/Cargo.toml | 2 +- tendermint/src/block/header.rs | 25 +++++++++++++++++-- tendermint/src/lib.rs | 1 + tendermint/src/lite.rs | 4 +++ {lite/src => tendermint/src/lite}/types.rs | 21 +++++++--------- {lite/src => tendermint/src/lite}/verifier.rs | 24 +++++++++++------- 9 files changed, 53 insertions(+), 34 deletions(-) delete mode 100644 lite/Cargo.toml delete mode 100644 lite/src/lib.rs create mode 100644 tendermint/src/lite.rs rename {lite/src => tendermint/src/lite}/types.rs (87%) rename {lite/src => tendermint/src/lite}/verifier.rs (86%) diff --git a/Cargo.toml b/Cargo.toml index 77f2fb737..40857dd30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,4 @@ members = [ "tendermint", - "lite", ] diff --git a/lite/Cargo.toml b/lite/Cargo.toml deleted file mode 100644 index 8a66b287b..000000000 --- a/lite/Cargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "lite" -version = "0.1.0" -authors = ["Ethan Buchman "] -edition = "2018" - -[dependencies] diff --git a/lite/src/lib.rs b/lite/src/lib.rs deleted file mode 100644 index 8398f513e..000000000 --- a/lite/src/lib.rs +++ /dev/null @@ -1,2 +0,0 @@ -mod types; -mod verifier; diff --git a/tendermint/Cargo.toml b/tendermint/Cargo.toml index a7921321d..c7930d0da 100644 --- a/tendermint/Cargo.toml +++ b/tendermint/Cargo.toml @@ -25,7 +25,7 @@ authors = [ ] [badges] -circle-ci = { repository = "tendermint/kms" } +circle-ci = { repository = "interchainio/tendermint-rs" } [dependencies] byteorder = { version = "1.2" } diff --git a/tendermint/src/block/header.rs b/tendermint/src/block/header.rs index c609bd05a..d2480337c 100644 --- a/tendermint/src/block/header.rs +++ b/tendermint/src/block/header.rs @@ -1,6 +1,5 @@ //! Block headers - -use crate::{account, block, chain, Hash, Time}; +use crate::{account, block, chain, Hash, Time, lite}; use { crate::serializers, serde::{Deserialize, Serialize}, @@ -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. /// diff --git a/tendermint/src/lib.rs b/tendermint/src/lib.rs index 08c765150..f25a94f3e 100644 --- a/tendermint/src/lib.rs +++ b/tendermint/src/lib.rs @@ -40,6 +40,7 @@ pub mod merkle; mod moniker; pub mod net; pub mod node; +#[allow(dead_code, missing_docs)] pub mod lite; pub mod private_key; pub mod public_key; pub mod rpc; diff --git a/tendermint/src/lite.rs b/tendermint/src/lite.rs new file mode 100644 index 000000000..283bd2e99 --- /dev/null +++ b/tendermint/src/lite.rs @@ -0,0 +1,4 @@ +pub use self::types::*; +pub mod types; +pub mod verifier; +pub use self::verifier::*; diff --git a/lite/src/types.rs b/tendermint/src/lite/types.rs similarity index 87% rename from lite/src/types.rs rename to tendermint/src/lite/types.rs index 4a37257c6..e80d4e668 100644 --- a/lite/src/types.rs +++ b/tendermint/src/lite/types.rs @@ -1,4 +1,9 @@ -use std::time::{Duration, Instant}; +#[allow(clippy::all)] +use crate::Time; +use crate::block::Height; +use crate::Hash; +use crate::account::Id; + /// 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. @@ -11,14 +16,6 @@ where pub validators: V, // height H } -pub type Height = u64; -/// 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 @@ -26,7 +23,7 @@ pub struct ValID([u8; VAL_ID_LENGTH]); /// set that should sign the next header. pub trait Header { fn height(&self) -> Height; - fn bft_time(&self) -> Instant; + fn bft_time(&self) -> Time; fn validators_hash(&self) -> Hash; fn next_validators_hash(&self) -> Hash; @@ -54,7 +51,7 @@ 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; + fn validator(&self, val_id: Id) -> Option; } /// Validator has a voting power and can verify @@ -90,7 +87,7 @@ pub trait Commit { /// so each vote is for a slightly different message. /// Note the Vote must also know which validator it is from. pub trait Vote { - fn validator_id(&self) -> ValID; + fn validator_id(&self) -> Id; fn sign_bytes(&self) -> &[u8]; fn signature(&self) -> &[u8]; } diff --git a/lite/src/verifier.rs b/tendermint/src/lite/verifier.rs similarity index 86% rename from lite/src/verifier.rs rename to tendermint/src/lite/verifier.rs index 0e3794d1d..1834912fe 100644 --- a/lite/src/verifier.rs +++ b/tendermint/src/lite/verifier.rs @@ -1,18 +1,24 @@ -use crate::types::*; -use std::time::{Duration, Instant}; +#[allow(clippy::all)] +use crate::lite::{Header, Error, Validator, ValidatorSet, Vote, Commit, ValidatorSetLookup}; +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(last_header: &H, trusting_period: Duration, now: Instant) -> Result<(), Error> +fn expired(_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 + // - uncomment above logic but also deal with overflows etc (proper err handling) Ok(()) } @@ -82,7 +88,7 @@ where } // ensure that +1/3 of last trusted validators signed correctly - if let Err(e) = verify_commit_trusting(&validators, &commit) { + if let Err(e) = verify_commit_trusting(&last_validators, &commit) { return Err(e); } @@ -107,7 +113,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. let vals_iter = vals_vec.into_iter();