Skip to content

Commit

Permalink
distinguish implementations based on feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
blasrodri committed Nov 15, 2022
1 parent 3cce56f commit 0cafbb9
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
2 changes: 1 addition & 1 deletion light-client-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["flex-error/std", "flex-error/eyre_tracer"]

rust-crypto = []
[dependencies]
tendermint = { version = "0.26.0", path = "../tendermint", default-features = false }

Expand Down
34 changes: 33 additions & 1 deletion light-client-verifier/src/operations/commit_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use core::marker::PhantomData;

use tendermint::block::CommitSig;
#[cfg(feature = "rust-crypto")]
use tendermint::crypto::DefaultHostFunctionsManager;

use crate::{
errors::VerificationError,
Expand Down Expand Up @@ -79,7 +81,7 @@ impl<C> CommitValidator<C> {
/// The batteries-included validator, for when you don't mind the dependencies on
/// the full rust-crypto stack.
#[cfg(feature = "rust-crypto")]
pub type ProdCommitValidator = CommitValidator<RustCryptoProvider>;
pub type ProdCommitValidator = CommitValidator<DefaultHostFunctionsManager>;

#[cfg(not(feature = "rust-crypto"))]
/// Production-ready implementation of a commit validator
Expand All @@ -88,11 +90,21 @@ pub struct ProdCommitValidator<C> {
inner: CommitValidator<C>,
}

#[cfg(not(feature = "rust-crypto"))]
impl<C> AsRef<CommitValidator<C>> for ProdCommitValidator<C> {
fn as_ref(&self) -> &CommitValidator<C> {
&self.inner
}
}

#[cfg(feature = "rust-crypto")]
impl AsRef<CommitValidator<DefaultHostFunctionsManager>> for ProdCommitValidator {
fn as_ref(&self) -> &CommitValidator<DefaultHostFunctionsManager> {
self
}
}

#[cfg(not(feature = "rust-crypto"))]
impl<C> ProdCommitValidator<C> {
/// Create a new commit validator using the given [`Hasher`]
/// to compute the hash of headers and validator sets.
Expand All @@ -106,8 +118,28 @@ impl<C> ProdCommitValidator<C> {
}
}

#[cfg(feature = "rust-crypto")]
impl ProdCommitValidator {
/// Create a new commit validator using the given [`Hasher`]
/// to compute the hash of headers and validator sets.
pub fn new(hasher: ProdHasher) -> Self {
CommitValidator {
hasher,
_c: PhantomData::default(),
}
}
}

#[cfg(not(feature = "rust-crypto"))]
impl<C> Default for ProdCommitValidator<C> {
fn default() -> Self {
Self::new(ProdHasher::default())
}
}

#[cfg(feature = "rust-crypto")]
impl Default for ProdCommitValidator {
fn default() -> Self {
Self::new(ProdHasher::default())
}
}
15 changes: 15 additions & 0 deletions light-client-verifier/src/predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub trait VerificationPredicates: Send + Sync {
}
}

#[cfg(not(feature = "rust-crypto"))]
/// Validate the commit using the given commit validator.
fn valid_commit(
&self,
Expand All @@ -99,6 +100,20 @@ pub trait VerificationPredicates: Send + Sync {
Ok(())
}

#[cfg(feature = "rust-crypto")]
/// Validate the commit using the given commit validator.
fn valid_commit(
&self,
signed_header: &SignedHeader,
validators: &ValidatorSet,
commit_validator: &CommitValidator<DefaultHostFunctionsManager>,
) -> Result<(), VerificationError> {
commit_validator.validate(signed_header, validators)?;
commit_validator.validate_full(signed_header, validators)?;

Ok(())
}

/// Check that the trusted header is within the trusting period, adjusting for clock drift.
fn is_within_trust_period(
&self,
Expand Down
60 changes: 60 additions & 0 deletions light-client-verifier/src/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Provides an interface and default implementation of the `Verifier` component
#[cfg(feature = "rust-crypto")]
use core::marker::PhantomData;

use preds::{ProdPredicates, VerificationPredicates};
use serde::{Deserialize, Serialize};
use tendermint::crypto::{CryptoProvider, DefaultHostFunctionsManager};
Expand Down Expand Up @@ -69,6 +72,7 @@ macro_rules! verdict {
};
}

#[cfg(not(feature = "rust-crypto"))]
/// Predicate verifier encapsulating components necessary to facilitate
/// verification.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -78,7 +82,19 @@ pub struct PredicateVerifier<P, C, H, CP> {
commit_validator: ProdCommitValidator<CP>,
hasher: H,
}
#[cfg(feature = "rust-crypto")]
/// Predicate verifier encapsulating components necessary to facilitate
/// verification.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PredicateVerifier<P, C, H, CP> {
predicates: P,
voting_power_calculator: C,
commit_validator: ProdCommitValidator,
hasher: H,
_p: PhantomData<CP>,
}

#[cfg(not(feature = "rust-crypto"))]
impl<P, C, H, CP> Default for PredicateVerifier<P, C, H, CP>
where
P: Default,
Expand All @@ -96,6 +112,25 @@ where
}
}

#[cfg(feature = "rust-crypto")]
impl<P, C, H, CP> Default for PredicateVerifier<P, C, H, CP>
where
P: Default,
C: Default,
H: Default,
{
fn default() -> Self {
Self {
predicates: P::default(),
voting_power_calculator: C::default(),
commit_validator: ProdCommitValidator::default(),
hasher: H::default(),
_p: PhantomData::default(),
}
}
}

#[cfg(not(feature = "rust-crypto"))]
impl<P, C, H, CP> PredicateVerifier<P, C, H, CP>
where
P: VerificationPredicates,
Expand All @@ -119,6 +154,31 @@ where
}
}

#[cfg(feature = "rust-crypto")]
impl<P, C, H, CP> PredicateVerifier<P, C, H, CP>
where
P: VerificationPredicates,
C: VotingPowerCalculator,
H: Hasher,
CP: CryptoProvider,
{
/// Constructor.
pub fn new(
predicates: P,
voting_power_calculator: C,
commit_validator: ProdCommitValidator,
hasher: H,
) -> Self {
Self {
predicates,
voting_power_calculator,
hasher,
commit_validator,
_p: PhantomData::default(),
}
}
}

impl<P, C, H, CP> Verifier for PredicateVerifier<P, C, H, CP>
where
P: VerificationPredicates + VerificationPredicates<CryptoProvider = CP>,
Expand Down
1 change: 1 addition & 0 deletions tendermint/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub trait CryptoProvider {
/// A default implementation of the HostFunctionManager that uses the [`CryptoProvider`] trait
use core::marker::PhantomData;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DefaultHostFunctionsManager;
use k256::ecdsa::{SigningKey, VerifyingKey};

Expand Down

0 comments on commit 0cafbb9

Please sign in to comment.