From 02439df55097a9c2fc24b9342574dbbf614c74de Mon Sep 17 00:00:00 2001 From: hu55a1n1 Date: Sat, 30 Dec 2023 15:10:11 -0800 Subject: [PATCH] Borrow/ToOwned impl for Verifier::Value --- utils/cw-proof/src/verifier/cw.rs | 4 ++-- 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, 24 insertions(+), 17 deletions(-) diff --git a/utils/cw-proof/src/verifier/cw.rs b/utils/cw-proof/src/verifier/cw.rs index 98aa677..734a3a8 100644 --- a/utils/cw-proof/src/verifier/cw.rs +++ b/utils/cw-proof/src/verifier/cw.rs @@ -6,7 +6,7 @@ use crate::error::ProofError; use crate::verifier::{ics23::Ics23MembershipVerifier, multi::MultiVerifier, Verifier}; #[derive(Clone, Debug)] -pub struct CwVerifier(MultiVerifier, Vec>, 2>); +pub struct CwVerifier(MultiVerifier>, 2>); impl CwVerifier { pub fn verify( @@ -14,7 +14,7 @@ impl CwVerifier { proofs: &[CommitmentProof; 2], root: &Vec, keys: &[Vec; 2], - value: &Vec, + value: &[u8], ) -> Result<(), ProofError> { if root.is_empty() { return Err(ProofError::EmptyMerkleRoot); diff --git a/utils/cw-proof/src/verifier/ics23.rs b/utils/cw-proof/src/verifier/ics23.rs index 0ee7dbe..eefb891 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<(K, V)>, + _phantom: PhantomData, } -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 = V; + type Value = Vec; + type ValueRef = [u8]; type Error = ProofError; fn verify( &self, commitment_proof: &Self::Proof, key: &Self::Key, - value: &Self::Value, + value: &Self::ValueRef, ) -> Result { if value.as_ref().is_empty() { return Err(ProofError::EmptyVerifiedValue); @@ -57,7 +57,7 @@ where &self.spec, &root, key.as_ref(), - value.as_ref(), + value, ) { return Err(ProofError::VerificationFailure); } diff --git a/utils/cw-proof/src/verifier/mod.rs b/utils/cw-proof/src/verifier/mod.rs index 1ce6bc6..7f9024e 100644 --- a/utils/cw-proof/src/verifier/mod.rs +++ b/utils/cw-proof/src/verifier/mod.rs @@ -1,3 +1,6 @@ +use alloc::borrow::ToOwned; +use core::borrow::Borrow; + pub mod cw; pub mod ics23; pub mod multi; @@ -6,21 +9,22 @@ trait Verifier { type Proof; type Root: Eq; type Key; - type Value; + type Value: Borrow; + type ValueRef: ?Sized + ToOwned; type Error; fn verify( &self, proof: &Self::Proof, key: &Self::Key, - value: &Self::Value, + value: &Self::ValueRef, ) -> Result; fn verify_against_root( &self, proof: &Self::Proof, key: &Self::Key, - value: &Self::Value, + value: &Self::ValueRef, 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 da586c0..eb8987a 100644 --- a/utils/cw-proof/src/verifier/multi.rs +++ b/utils/cw-proof/src/verifier/multi.rs @@ -1,3 +1,6 @@ +use alloc::borrow::ToOwned; +use core::borrow::Borrow; + use crate::verifier::Verifier; #[derive(Clone, Debug)] @@ -15,28 +18,28 @@ impl MultiVerifier { impl Verifier for MultiVerifier where V: Verifier, - V::Value: Clone, V::Root: Into + 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::Value, + value: &Self::ValueRef, ) -> Result { let mut root = None; - let mut value = value.clone(); + let mut value: V::Value = value.to_owned(); 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)?; + let sub_root = verifier.verify(proof, key, value.borrow())?; value = sub_root.clone().into(); root = Some(sub_root);