Moves the separate repos for ICS23 proof tooling into `store/tools` I've set `github.com/confio/ics23/go` to version 0.7.0 in anticipation of https://github.com/confio/ics23/pull/61 being merged, as it's a dependency for SMT proofs, so this shouldn't be merged until that version exists. Closes: https://github.com/cosmos/cosmos-sdk/issues/10801 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
113 lines
3.1 KiB
Go
113 lines
3.1 KiB
Go
package proofs
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
ics23 "github.com/confio/ics23/go"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
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 {
|
|
t.Run(name, func(t *testing.T) {
|
|
data := BuildMap(tc.size)
|
|
allkeys := SortedKeys(data)
|
|
key := GetKey(allkeys, tc.loc)
|
|
val := data[key]
|
|
proof, err := CreateMembershipProof(data, []byte(key))
|
|
if err != nil {
|
|
t.Fatalf("Creating Proof: %+v", err)
|
|
}
|
|
if proof.GetExist() == nil {
|
|
t.Fatal("Unexpected proof format")
|
|
}
|
|
|
|
root := CalcRoot(data)
|
|
err = proof.GetExist().Verify(ics23.TendermintSpec, root, []byte(key), val)
|
|
if err != nil {
|
|
t.Fatalf("Verifying Proof: %+v", err)
|
|
}
|
|
|
|
valid := ics23.VerifyMembership(ics23.TendermintSpec, root, proof, []byte(key), val)
|
|
if !valid {
|
|
t.Fatalf("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 {
|
|
t.Run(name, func(t *testing.T) {
|
|
data := BuildMap(tc.size)
|
|
allkeys := SortedKeys(data)
|
|
key := GetNonKey(allkeys, tc.loc)
|
|
|
|
proof, err := CreateNonMembershipProof(data, []byte(key))
|
|
if err != nil {
|
|
t.Fatalf("Creating Proof: %+v", err)
|
|
}
|
|
if proof.GetNonexist() == nil {
|
|
t.Fatal("Unexpected proof format")
|
|
}
|
|
|
|
root := CalcRoot(data)
|
|
err = proof.GetNonexist().Verify(ics23.TendermintSpec, root, []byte(key))
|
|
if err != nil {
|
|
t.Fatalf("Verifying Proof: %+v", err)
|
|
}
|
|
|
|
valid := ics23.VerifyNonMembership(ics23.TendermintSpec, root, proof, []byte(key))
|
|
if !valid {
|
|
t.Fatalf("Non Membership Proof Invalid")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInvalidKey(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
f func(data map[string][]byte, key []byte) (*ics23.CommitmentProof, error)
|
|
data map[string][]byte
|
|
key []byte
|
|
err error
|
|
}{
|
|
{"CreateMembershipProof empty key", CreateMembershipProof, map[string][]byte{"": nil}, []byte(""), ErrEmptyKey},
|
|
{"CreateMembershipProof empty key in data", CreateMembershipProof, map[string][]byte{"": nil, " ": nil}, []byte(" "), ErrEmptyKeyInData},
|
|
{"CreateNonMembershipProof empty key", CreateNonMembershipProof, map[string][]byte{" ": nil}, []byte(""), ErrEmptyKey},
|
|
{"CreateNonMembershipProof empty key in data", CreateNonMembershipProof, map[string][]byte{"": nil}, []byte(" "), ErrEmptyKeyInData},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
_, err := tc.f(tc.data, tc.key)
|
|
assert.True(t, errors.Is(err, tc.err))
|
|
})
|
|
}
|
|
}
|