Skip to content
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 10 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/tendermint/iavl
go 1.13

require (
github.com/confio/ics23/go v0.0.0-20200610201322-18c7bd6b2dd3
github.com/gogo/protobuf v1.3.1
github.com/golang/protobuf v1.4.2 // indirect
github.com/google/gofuzz v1.1.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/confio/ics23/go v0.0.0-20200610201322-18c7bd6b2dd3 h1:h/HM69qj1llEyq9N8+2sm6HfWsE7P9SFyl4CM2hyK98=
github.com/confio/ics23/go v0.0.0-20200610201322-18c7bd6b2dd3/go.mod h1:W1I3XC8d9N8OTu/ct5VJ84ylcOunZwMXsWkd27nvVts=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -19,6 +21,7 @@ github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870 h1:E2s37DuLxFhQD
github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -54,6 +57,7 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jmhodges/levigo v1.0.0 h1:q5EC36kV79HWeTBWsod3mG11EgStG3qArTKcvlksN1U=
github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+nNuzMJQ=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0 h1:AV2c/EiW3KqPNT9ZKl07ehoAGi4C5/01Cfbblndcapg=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand All @@ -67,6 +71,7 @@ github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -89,6 +94,7 @@ github.com/tendermint/tm-db v0.5.1/go.mod h1:g92zWjHpCYlEvQXvy9M168Su8V1IBEeawpX
go.etcd.io/bbolt v1.3.3 h1:MUGmc65QhB3pIlaQ5bB4LwqSj6GIonVJXpZiaKNyaKk=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -122,6 +128,7 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
167 changes: 167 additions & 0 deletions ics23.go/proof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package iavl
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"

ics23 "github.com/confio/ics23/go"
"github.com/tendermint/iavl"
)

/*
CreateMembershipProof 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 CreateMembershipProof(tree *iavl.ImmutableTree, 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
}

/*
CreateNonMembershipProof 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 CreateNonMembershipProof(tree *iavl.ImmutableTree, 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 *iavl.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 *iavl.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 iavl.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 {
// 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
}
87 changes: 87 additions & 0 deletions ics23.go/proof_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package iavl

import (
"testing"

ics23 "github.com/confio/ics23/go"
"github.com/stretchr/testify/require"
)

func TestConvertExistence(t *testing.T) {
proof, err := GenerateResult(200, Middle)
require.NoError(t, err)

converted, err := convertExistenceProof(proof.Proof, proof.Key, proof.Value)
require.NoError(t, err)

calc, err := converted.Calculate()
require.NoError(t, err)

require.Equal(t, []byte(calc), proof.RootHash, "Calculated: %X\nExpected: %X", calc, proof.RootHash)
}

func TestCreateMembership(t *testing.T) {
cases := map[string]struct {
size int
loc Where
}{
"small left": {size: 100, loc: Left},
"small middle": {size: 100, loc: Middle},
"small right": {size: 100, loc: Right},
"big left": {size: 5431, loc: Left},
"big middle": {size: 5431, loc: Middle},
"big right": {size: 5431, loc: Right},
}

for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
tree, allkeys, err := BuildTree(tc.size)
require.NoError(t, err, "Creating tree: %+v", err)

key := GetKey(allkeys, tc.loc)
_, val := tree.Get(key)
proof, err := CreateMembershipProof(tree, key)
require.NoError(t, err, "Creating Proof: %+v", err)

root := tree.Hash()
valid := ics23.VerifyMembership(ics23.IavlSpec, root, proof, key, val)
if !valid {
require.NoError(t, err, "Membership Proof Invalid")
}
})
}
}

func TestCreateNonMembership(t *testing.T) {
cases := map[string]struct {
size int
loc Where
}{
"small left": {size: 100, loc: Left},
"small middle": {size: 100, loc: Middle},
"small right": {size: 100, loc: Right},
"big left": {size: 5431, loc: Left},
"big middle": {size: 5431, loc: Middle},
"big right": {size: 5431, loc: Right},
}

for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
tree, allkeys, err := BuildTree(tc.size)
require.NoError(t, err, "Creating tree: %+v", err)

key := GetNonKey(allkeys, tc.loc)

proof, err := CreateNonMembershipProof(tree, key)
require.NoError(t, err, "Creating Proof: %+v", err)

root := tree.Hash()
valid := ics23.VerifyNonMembership(ics23.IavlSpec, root, proof, key)
if !valid {
require.NoError(t, err, "Non Membership Proof Invalid")
}
})
}
}
Loading