Skip to content

Commit

Permalink
restore proof_ics23.go
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed Mar 28, 2022
1 parent f660a25 commit f56de3a
Showing 1 changed file with 6 additions and 82 deletions.
88 changes: 6 additions & 82 deletions proof_ics23.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package iavl

import (
"bytes"
"encoding/binary"
"fmt"

Expand Down Expand Up @@ -29,32 +28,7 @@ func (t *ImmutableTree) GetMembershipProof(key []byte) (*ics23.CommitmentProof,
GetNonMembershipProof will produce a CommitmentProof that the given key doesn't exist in the iavl tree.
If the key exists in the tree, this will return an error.
*/
func (t *ImmutableTree) GetNonMembershipProof(key []byte) (proof *ics23.CommitmentProof, err error) {
var nonexist *ics23.NonExistenceProof
// TODO: to investigate more and potentially enable fast storage
// introduced in: https://github.com/osmosis-labs/iavl/pull/12
// if t.IsFastCacheEnabled() {
// nonexist, err = t.getNonMembershipProofFast(key)
// } else {
// nonexist, err = t.getNonMembershipProof(key)
// }
nonexist, err = t.getNonMembershipProof(key)

if err != nil {
return nil, err
}

proof = &ics23.CommitmentProof{
Proof: &ics23.CommitmentProof_Nonexist{
Nonexist: nonexist,
},
}
return proof, nil
}

// getNonMembershipProof using regular strategy
// invariant: fast storage is enabled
func (t *ImmutableTree) getNonMembershipProof(key []byte) (*ics23.NonExistenceProof, error) {
func (t *ImmutableTree) GetNonMembershipProof(key []byte) (*ics23.CommitmentProof, error) {
// idx is one node right of what we want....
idx, val := t.GetWithIndex(key)
if val != nil {
Expand Down Expand Up @@ -83,62 +57,12 @@ func (t *ImmutableTree) getNonMembershipProof(key []byte) (*ics23.NonExistencePr
}
}

return nonexist, nil
}

// getNonMembershipProofFast using fast storage
// invariant: fast storage is enabled
// TODO: need further investigation if this is going to be more optimal
// So far, benchmarks have been showing an improvement. However, it makes the proof asymptotically slower O(log(n)) -> O(n)
// Need to test on an extremely large tree. It might be the case that the fast proofs are still faster than regular due to less
// disk accesses even though they are asymptotically slower.
// nolint: unused
func (t *ImmutableTree) getNonMembershipProofFast(key []byte) (*ics23.NonExistenceProof, error) {
index := 0
var prevKey []byte = nil
var nextKey []byte = nil

done := false
itr := t.Iterator(nil, nil, true)
defer itr.Close()
for ; !done && itr.Valid(); itr.Next() {
switch bytes.Compare(itr.Key(), key) {
case -1:
index++
prevKey = itr.Key()
case 1:
nextKey = itr.Key()
done = true
default:
done = true
}
}

// If next was not set, that means we found the key during iterations above
if done && nextKey == nil {
return nil, fmt.Errorf("cannot create NonExistanceProof when Key in State")
}

var err error
nonexist := &ics23.NonExistenceProof{
Key: key,
}

if prevKey != nil {
nonexist.Left, err = createExistenceProof(t, prevKey)
if err != nil {
return nil, err
}
}

if nextKey != nil {
nonexist.Right, err = createExistenceProof(t, nextKey)
if err != nil {
return nil, err
}
proof := &ics23.CommitmentProof{
Proof: &ics23.CommitmentProof_Nonexist{
Nonexist: nonexist,
},
}

return nonexist, nil
return proof, nil
}

func createExistenceProof(tree *ImmutableTree, key []byte) (*ics23.ExistenceProof, error) {
Expand Down

0 comments on commit f56de3a

Please sign in to comment.