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..fa7372b01 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, lite, Hash, Time}; 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..227a9893a 100644 --- a/tendermint/src/lib.rs +++ b/tendermint/src/lib.rs @@ -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; 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 88% rename from lite/src/types.rs rename to tendermint/src/lite/types.rs index f1e5ab43f..ab619c9fd 100644 --- a/lite/src/types.rs +++ b/tendermint/src/lite/types.rs @@ -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. @@ -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 @@ -52,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 @@ -60,7 +59,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. @@ -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 { diff --git a/lite/src/verifier.rs b/tendermint/src/lite/verifier.rs similarity index 88% rename from lite/src/verifier.rs rename to tendermint/src/lite/verifier.rs index 91d0ebe6a..f178a4101 100644 --- a/lite/src/verifier.rs +++ b/tendermint/src/lite/verifier.rs @@ -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(last_header: &H, trusting_period: Time, now: Time) -> 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 and deal with overflows etc (proper err handling) Ok(()) } @@ -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) { @@ -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); }