From 45970c298b3c440d49f3a6f40be64ed6ef2e877f Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Wed, 18 Sep 2024 10:56:38 +0200 Subject: [PATCH] fix(rust): Interpret `max_depth` in proof specs as 128 if left to 0 --- rust/src/lib.rs | 4 ++++ rust/src/verify.rs | 11 +++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 4799357b..aff96078 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -14,6 +14,10 @@ mod verify; mod ics23 { include!("cosmos.ics23.v1.rs"); + impl ProofSpec { + pub const DEFAULT_MAX_DEPTH: i32 = 128; + } + #[cfg(feature = "serde")] include!("cosmos.ics23.v1.serde.rs"); } diff --git a/rust/src/verify.rs b/rust/src/verify.rs index ade0b93a..c3b138f9 100644 --- a/rust/src/verify.rs +++ b/rust/src/verify.rs @@ -9,9 +9,9 @@ use anyhow::{bail, ensure}; use crate::api::{ensure_inner_prefix, ensure_leaf_prefix}; use crate::helpers::Result; use crate::host_functions::HostFunctionsProvider; -use crate::ics23; use crate::ops::do_hash; use crate::ops::{apply_inner, apply_leaf}; +use crate::{ics23, ProofSpec}; pub type CommitmentRoot = Vec; @@ -118,6 +118,13 @@ fn check_existence_spec(proof: &ics23::ExistenceProof, spec: &ics23::ProofSpec) if let (Some(leaf), Some(leaf_spec)) = (&proof.leaf, &spec.leaf_spec) { ensure_leaf_prefix(&leaf.prefix, spec)?; ensure_leaf(leaf, leaf_spec)?; + + let max_depth = if spec.max_depth == 0 { + ProofSpec::DEFAULT_MAX_DEPTH + } else { + spec.max_depth + }; + // ensure min/max depths if spec.min_depth != 0 { ensure!( @@ -126,7 +133,7 @@ fn check_existence_spec(proof: &ics23::ExistenceProof, spec: &ics23::ProofSpec) proof.path.len(), ); ensure!( - proof.path.len() <= spec.max_depth as usize, + proof.path.len() <= max_depth as usize, "Too many InnerOps: {}", proof.path.len(), );