-
Notifications
You must be signed in to change notification settings - Fork 256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port over ics23-iavl #276
Merged
Merged
Port over ics23-iavl #276
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d6906e9
port logic from ics23-iavl into iavl repo
AdityaSripal 17ba814
appease linter
AdityaSripal 0eeb898
remove TM dependency again
AdityaSripal 6803c00
rename package
AdityaSripal 754300f
move ics files to top-level package
AdityaSripal 7222578
fix merge
AdityaSripal fb2dfec
make linter ignore test rand use
AdityaSripal e97e786
fix api
AdityaSripal 0d15db8
address @erikgrinaker review
AdityaSripal 42890cb
fix merge and add changelog
AdityaSripal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package iavl | ||
|
||
import ( | ||
"fmt" | ||
|
||
ics23 "github.com/confio/ics23/go" | ||
) | ||
|
||
/* | ||
GetMembershipProof will produce a CommitmentProof that the given key (and queries value) exists in the iavl tree. | ||
If the key doesn't exist in the tree, this will return an error. | ||
*/ | ||
func (tree *ImmutableTree) GetMembershipProof(key []byte) (*ics23.CommitmentProof, error) { | ||
exist, err := createExistenceProof(tree, key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
proof := &ics23.CommitmentProof{ | ||
Proof: &ics23.CommitmentProof_Exist{ | ||
Exist: exist, | ||
}, | ||
} | ||
return proof, nil | ||
} | ||
|
||
/* | ||
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 (tree *ImmutableTree) GetNonMembershipProof(key []byte) (*ics23.CommitmentProof, error) { | ||
// idx is one node right of what we want.... | ||
idx, val := tree.Get(key) | ||
if val != nil { | ||
return nil, fmt.Errorf("cannot create NonExistanceProof when Key in State") | ||
} | ||
|
||
var err error | ||
nonexist := &ics23.NonExistenceProof{ | ||
Key: key, | ||
} | ||
|
||
if idx >= 1 { | ||
leftkey, _ := tree.GetByIndex(idx - 1) | ||
nonexist.Left, err = createExistenceProof(tree, leftkey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
// this will be nil if nothing right of the queried key | ||
rightkey, _ := tree.GetByIndex(idx) | ||
if rightkey != nil { | ||
nonexist.Right, err = createExistenceProof(tree, rightkey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
proof := &ics23.CommitmentProof{ | ||
Proof: &ics23.CommitmentProof_Nonexist{ | ||
Nonexist: nonexist, | ||
}, | ||
} | ||
return proof, nil | ||
} | ||
|
||
func createExistenceProof(tree *ImmutableTree, key []byte) (*ics23.ExistenceProof, error) { | ||
value, proof, err := tree.GetWithProof(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if value == nil { | ||
return nil, fmt.Errorf("cannot create ExistanceProof when Key not in State") | ||
} | ||
return convertExistenceProof(proof, key, value) | ||
} | ||
|
||
// convertExistenceProof will convert the given proof into a valid | ||
// existence proof, if that's what it is. | ||
// | ||
// This is the simplest case of the range proof and we will focus on | ||
// demoing compatibility here | ||
func convertExistenceProof(p *RangeProof, key, value []byte) (*ics23.ExistenceProof, error) { | ||
if len(p.Leaves) != 1 { | ||
return nil, fmt.Errorf("existence proof requires RangeProof to have exactly one leaf") | ||
} | ||
return &ics23.ExistenceProof{ | ||
Key: key, | ||
Value: value, | ||
Leaf: convertLeafOp(p.Leaves[0].Version), | ||
Path: convertInnerOps(p.LeftPath), | ||
}, nil | ||
} | ||
|
||
func convertLeafOp(version int64) *ics23.LeafOp { | ||
// this is adapted from iavl/proof.go:proofLeafNode.Hash() | ||
prefix := aminoVarInt(0) | ||
prefix = append(prefix, aminoVarInt(1)...) | ||
prefix = append(prefix, aminoVarInt(version)...) | ||
|
||
return &ics23.LeafOp{ | ||
Hash: ics23.HashOp_SHA256, | ||
PrehashValue: ics23.HashOp_SHA256, | ||
Length: ics23.LengthOp_VAR_PROTO, | ||
Prefix: prefix, | ||
} | ||
} | ||
|
||
// we cannot get the proofInnerNode type, so we need to do the whole path in one function | ||
func convertInnerOps(path PathToLeaf) []*ics23.InnerOp { | ||
steps := make([]*ics23.InnerOp, 0, len(path)) | ||
|
||
// lengthByte is the length prefix prepended to each of the sha256 sub-hashes | ||
var lengthByte byte = 0x20 | ||
|
||
// we need to go in reverse order, iavl starts from root to leaf, | ||
// we want to go up from the leaf to the root | ||
for i := len(path) - 1; i >= 0; i-- { | ||
// this is adapted from iavl/proof.go:proofInnerNode.Hash() | ||
prefix := aminoVarInt(int64(path[i].Height)) | ||
prefix = append(prefix, aminoVarInt(path[i].Size)...) | ||
prefix = append(prefix, aminoVarInt(path[i].Version)...) | ||
|
||
var suffix []byte | ||
if len(path[i].Left) > 0 { | ||
// length prefixed left side | ||
prefix = append(prefix, lengthByte) | ||
prefix = append(prefix, path[i].Left...) | ||
// prepend the length prefix for child | ||
prefix = append(prefix, lengthByte) | ||
} else { | ||
// prepend the length prefix for child | ||
prefix = append(prefix, lengthByte) | ||
// length-prefixed right side | ||
suffix = []byte{lengthByte} | ||
suffix = append(suffix, path[i].Right...) | ||
} | ||
|
||
op := &ics23.InnerOp{ | ||
Hash: ics23.HashOp_SHA256, | ||
Prefix: prefix, | ||
Suffix: suffix, | ||
} | ||
steps = append(steps, op) | ||
} | ||
return steps | ||
} | ||
|
||
func aminoVarInt(orig int64) []byte { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really necessary? As far as I know, Amino uses standard Go variants, and we have utility functions for generating these in |
||
// amino-specific byte swizzling | ||
i := uint64(orig) << 1 | ||
if orig < 0 { | ||
i = ^i | ||
} | ||
|
||
// avoid multiple allocs for normal case | ||
res := make([]byte, 0, 8) | ||
|
||
// standard protobuf encoding | ||
for i >= 1<<7 { | ||
res = append(res, uint8(i&0x7f|0x80)) | ||
i >>= 7 | ||
} | ||
res = append(res, uint8(i)) | ||
return res | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll have to use
t *ImmutableTree
to appease the linter.