From 94c951e53b9286a7936b852760992c56eb7b99d5 Mon Sep 17 00:00:00 2001 From: hu55a1n1 Date: Sun, 31 Dec 2023 03:42:54 -0800 Subject: [PATCH] Simplify API --- utils/cw-proof/src/verifier/cw.rs | 15 ++++++++++----- utils/cw-proof/src/verifier/ics23.rs | 16 ++++++++-------- utils/cw-proof/src/verifier/mod.rs | 10 +++------- utils/cw-proof/src/verifier/multi.rs | 11 ++++------- 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/utils/cw-proof/src/verifier/cw.rs b/utils/cw-proof/src/verifier/cw.rs index 734a3a8..c8b3f79 100644 --- a/utils/cw-proof/src/verifier/cw.rs +++ b/utils/cw-proof/src/verifier/cw.rs @@ -1,3 +1,4 @@ +use alloc::borrow::Cow; use alloc::vec::Vec; use ics23::CommitmentProof; @@ -5,10 +6,13 @@ use ics23::CommitmentProof; use crate::error::ProofError; use crate::verifier::{ics23::Ics23MembershipVerifier, multi::MultiVerifier, Verifier}; -#[derive(Clone, Debug)] -pub struct CwVerifier(MultiVerifier>, 2>); +type Key = Vec; +type Value<'a> = Cow<'a, [u8]>; -impl CwVerifier { +#[derive(Clone, Debug)] +pub struct CwVerifier<'a>(MultiVerifier>, 2>); + +impl CwVerifier<'_> { pub fn verify( &self, proofs: &[CommitmentProof; 2], @@ -20,7 +24,8 @@ impl CwVerifier { return Err(ProofError::EmptyMerkleRoot); } - let verified = self.0.verify_against_root(proofs, keys, value, root)?; + let value = Cow::Borrowed(value); + let verified = self.0.verify_against_root(proofs, keys, &value, root)?; if !verified { return Err(ProofError::VerificationFailure); } @@ -29,7 +34,7 @@ impl CwVerifier { } } -impl Default for CwVerifier { +impl Default for CwVerifier<'_> { fn default() -> Self { let mv = MultiVerifier::new([ Ics23MembershipVerifier::new(ics23::iavl_spec()), diff --git a/utils/cw-proof/src/verifier/ics23.rs b/utils/cw-proof/src/verifier/ics23.rs index eefb891..0ee7dbe 100644 --- a/utils/cw-proof/src/verifier/ics23.rs +++ b/utils/cw-proof/src/verifier/ics23.rs @@ -10,12 +10,12 @@ use crate::error::ProofError; use crate::verifier::Verifier; #[derive(Clone, Debug)] -pub struct Ics23MembershipVerifier { +pub struct Ics23MembershipVerifier { spec: ProofSpec, - _phantom: PhantomData, + _phantom: PhantomData<(K, V)>, } -impl Ics23MembershipVerifier { +impl Ics23MembershipVerifier { pub fn new(spec: ProofSpec) -> Self { Self { spec, @@ -24,22 +24,22 @@ impl Ics23MembershipVerifier { } } -impl Verifier for Ics23MembershipVerifier +impl Verifier for Ics23MembershipVerifier where K: AsRef<[u8]>, + V: AsRef<[u8]>, { type Proof = CommitmentProof; type Root = Vec; type Key = K; - type Value = Vec; - type ValueRef = [u8]; + type Value = V; type Error = ProofError; fn verify( &self, commitment_proof: &Self::Proof, key: &Self::Key, - value: &Self::ValueRef, + value: &Self::Value, ) -> Result { if value.as_ref().is_empty() { return Err(ProofError::EmptyVerifiedValue); @@ -57,7 +57,7 @@ where &self.spec, &root, key.as_ref(), - value, + value.as_ref(), ) { return Err(ProofError::VerificationFailure); } diff --git a/utils/cw-proof/src/verifier/mod.rs b/utils/cw-proof/src/verifier/mod.rs index 7f9024e..1ce6bc6 100644 --- a/utils/cw-proof/src/verifier/mod.rs +++ b/utils/cw-proof/src/verifier/mod.rs @@ -1,6 +1,3 @@ -use alloc::borrow::ToOwned; -use core::borrow::Borrow; - pub mod cw; pub mod ics23; pub mod multi; @@ -9,22 +6,21 @@ trait Verifier { type Proof; type Root: Eq; type Key; - type Value: Borrow; - type ValueRef: ?Sized + ToOwned; + type Value; type Error; fn verify( &self, proof: &Self::Proof, key: &Self::Key, - value: &Self::ValueRef, + value: &Self::Value, ) -> Result; fn verify_against_root( &self, proof: &Self::Proof, key: &Self::Key, - value: &Self::ValueRef, + value: &Self::Value, root: &Self::Root, ) -> Result { let found_root = self.verify(proof, key, value)?; diff --git a/utils/cw-proof/src/verifier/multi.rs b/utils/cw-proof/src/verifier/multi.rs index eb8987a..30b65b5 100644 --- a/utils/cw-proof/src/verifier/multi.rs +++ b/utils/cw-proof/src/verifier/multi.rs @@ -1,6 +1,3 @@ -use alloc::borrow::ToOwned; -use core::borrow::Borrow; - use crate::verifier::Verifier; #[derive(Clone, Debug)] @@ -19,27 +16,27 @@ impl Verifier for MultiVerifier where V: Verifier, V::Root: Into + Clone, + V::Value: Clone, { type Proof = [V::Proof; N]; type Root = V::Root; type Key = [V::Key; N]; type Value = V::Value; - type ValueRef = V::ValueRef; type Error = V::Error; fn verify( &self, proofs: &Self::Proof, keys: &Self::Key, - value: &Self::ValueRef, + value: &Self::Value, ) -> Result { let mut root = None; - let mut value: V::Value = value.to_owned(); + let mut value: V::Value = value.clone(); for (idx, verifier) in self.verifiers.iter().enumerate() { let proof = &proofs[idx]; let key = &keys[N - idx - 1]; - let sub_root = verifier.verify(proof, key, value.borrow())?; + let sub_root = verifier.verify(proof, key, &value)?; value = sub_root.clone().into(); root = Some(sub_root);