Skip to content

Commit

Permalink
fix: apply default restrictions to max depth (#352)
Browse files Browse the repository at this point in the history
* fix: apply fix, uncomment test

* test: add test case

* fix: tests

* Update testdata/TestCheckAgainstSpecData.json

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>

* chore: regen proto with comment

* chore: add changelog

* Update CHANGELOG.md

* rm: rust proto_descriptor bin

* add back rust bin

---------

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
  • Loading branch information
colin-axner and crodriguezvega authored Aug 27, 2024
1 parent 6dbe7e3 commit 1363533
Show file tree
Hide file tree
Showing 10 changed files with 692 additions and 121 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# Unreleased

- fix(go): interpret max_depth in proof specs as 128 if left to 0 [#352](https://github.com/cosmos/ics23/pull/352).

# 0.12.0

## Rust
Expand Down
2 changes: 1 addition & 1 deletion go/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func FuzzExistenceProofCheckAgainstSpec(f *testing.F) {

seedDataMap := CheckAgainstSpecTestData(f)
for _, seed := range seedDataMap {
if seed.IsErr {
if seed.Err != "" {
// Erroneous data, skip it.
continue
}
Expand Down
8 changes: 7 additions & 1 deletion go/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,13 @@ func (p *ExistenceProof) CheckAgainstSpec(spec *ProofSpec) error {
if spec.MinDepth > 0 && len(p.Path) < int(spec.MinDepth) {
return fmt.Errorf("innerOps depth too short: %d", len(p.Path))
}
if spec.MaxDepth > 0 && len(p.Path) > int(spec.MaxDepth) {

maxDepth := spec.MaxDepth
if maxDepth == 0 {
maxDepth = 128
}

if len(p.Path) > int(maxDepth) {
return fmt.Errorf("innerOps depth too long: %d", len(p.Path))
}

Expand Down
3 changes: 2 additions & 1 deletion go/proof_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func CheckLeafTestData(tb testing.TB) map[string]CheckLeafTestStruct {
type CheckAgainstSpecTestStruct struct {
Proof *ExistenceProof
Spec *ProofSpec
IsErr bool
Err string
}

func CheckAgainstSpecTestData(tb testing.TB) map[string]CheckAgainstSpecTestStruct {
Expand All @@ -70,6 +70,7 @@ func CheckAgainstSpecTestData(tb testing.TB) map[string]CheckAgainstSpecTestStru
if err != nil {
tb.Fatal(err)
}

return cases
}

Expand Down
7 changes: 3 additions & 4 deletions go/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,15 @@ func TestCheckLeaf(t *testing.T) {
}

func TestCheckAgainstSpec(t *testing.T) {
t.Skip()
cases := CheckAgainstSpecTestData(t)

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
err := tc.Proof.CheckAgainstSpec(tc.Spec)
if tc.IsErr && err == nil {
t.Fatal("Expected error, but got nil")
} else if !tc.IsErr && err != nil {
if tc.Err == "" && err != nil {
t.Fatalf("Unexpected error: %v", err)
} else if tc.Err != "" && tc.Err != err.Error() {
t.Fatalf("Expected error: %s, got %s", tc.Err, err.Error())
}
})
}
Expand Down
1 change: 1 addition & 0 deletions go/proofs.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proto/cosmos/ics23/v1/proofs.proto
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ message ProofSpec {
LeafOp leaf_spec = 1;
InnerSpec inner_spec = 2;
// max_depth (if > 0) is the maximum number of InnerOps allowed (mainly for fixed-depth tries)
// the max_depth is interpreted as 128 if set to 0
int32 max_depth = 3;
// min_depth (if > 0) is the minimum number of InnerOps allowed (mainly for fixed-depth tries)
int32 min_depth = 4;
Expand Down
2 changes: 2 additions & 0 deletions rust/src/cosmos.ics23.v1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// This file is @generated by prost-build.
/// *
/// ExistenceProof takes a key and a value and a set of steps to perform on it.
/// The result of peforming all these steps will provide a "root hash", which can
Expand Down Expand Up @@ -146,6 +147,7 @@ pub struct ProofSpec {
#[prost(message, optional, tag = "2")]
pub inner_spec: ::core::option::Option<InnerSpec>,
/// max_depth (if > 0) is the maximum number of InnerOps allowed (mainly for fixed-depth tries)
/// the max_depth is interpreted as 128 if set to 0
#[prost(int32, tag = "3")]
pub max_depth: i32,
/// min_depth (if > 0) is the minimum number of InnerOps allowed (mainly for fixed-depth tries)
Expand Down
Binary file modified rust/src/proto_descriptor.bin
Binary file not shown.
Loading

0 comments on commit 1363533

Please sign in to comment.