cosmos-sdk/store/tools/ics23/smt/create_test.go

83 lines
2.1 KiB
Go
Raw Normal View History

feat: Add ics23 proof tools: `ics23-{iavl,tendermint,smt}` (#10802) 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)
2022-02-22 03:39:08 -08:00
package smtproofs
import (
"crypto/sha256"
"testing"
ics23 "github.com/confio/ics23/go"
tmproofs "github.com/cosmos/cosmos-sdk/store/internal/proofs"
"github.com/cosmos/cosmos-sdk/store/tools/ics23/smt/helpers"
)
var numKeys = 50
var cases = map[string]struct {
size int
loc tmproofs.Where
}{
"tiny left": {size: 10, loc: tmproofs.Left},
"tiny middle": {size: 10, loc: tmproofs.Middle},
"tiny right": {size: 10, loc: tmproofs.Right},
"small left": {size: 100, loc: tmproofs.Left},
"small middle": {size: 100, loc: tmproofs.Middle},
"small right": {size: 100, loc: tmproofs.Right},
"big left": {size: 5431, loc: tmproofs.Left},
"big middle": {size: 5431, loc: tmproofs.Middle},
"big right": {size: 5431, loc: tmproofs.Right},
}
func TestCreateMembership(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
tree, preim, err := helpers.BuildTree(tc.size)
if err != nil {
t.Fatalf("Creating tree: %+v", err)
}
for i := 0; i < numKeys; i++ {
key := preim.GetKey(tc.loc)
val, err := tree.Get(key)
if err != nil {
t.Fatalf("Getting key: %+v", err)
}
proof, err := CreateMembershipProof(tree, key)
if err != nil {
t.Fatalf("Creating proof: %+v", err)
}
root := tree.Root()
path := sha256.Sum256(key)
valid := ics23.VerifyMembership(ics23.SmtSpec, root, proof, path[:], val)
if !valid {
t.Fatalf("Membership proof invalid")
}
}
})
}
}
func TestCreateNonMembership(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
tree, preim, err := helpers.BuildTree(tc.size)
if err != nil {
t.Fatalf("Creating tree: %+v", err)
}
for i := 0; i < numKeys; i++ {
key := preim.GetNonKey(tc.loc)
proof, err := CreateNonMembershipProof(tree, key, preim)
if err != nil {
t.Fatalf("Creating proof: %+v", err)
}
root := tree.Root()
path := sha256.Sum256(key)
valid := ics23.VerifyNonMembership(ics23.SmtSpec, root, proof, path[:])
if !valid {
t.Fatalf("Non-membership proof invalid")
}
}
})
}
}