cosmos-sdk/store/v2alpha1/multi/proof_test.go
likhita-809 4081bc3d00
chore: Rename store/v2 -> store/v2alpha1 (#11392)
## Description

Closes: #11378 



---

### 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...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] 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)
- [ ] 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`
- [ ] 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-03-16 18:30:27 +00:00

126 lines
3.7 KiB
Go

package multi
import (
"crypto/sha256"
"testing"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/db/memdb"
"github.com/cosmos/cosmos-sdk/store/v2alpha1/smt"
)
// We hash keys produce SMT paths, so reflect that here
func keyPath(prefix, key string) string {
hashed := sha256.Sum256([]byte(key))
return prefix + string(hashed[:])
}
func TestVerifySMTStoreProof(t *testing.T) {
// Create main tree for testing.
txn := memdb.NewDB().ReadWriter()
store := smt.NewStore(txn)
store.Set([]byte("MYKEY"), []byte("MYVALUE"))
root := store.Root()
res, err := proveKey(store, []byte("MYKEY"))
require.NoError(t, err)
// Verify good proof.
prt := DefaultProofRuntime()
err = prt.VerifyValue(res, root, keyPath("/", "MYKEY"), []byte("MYVALUE"))
require.NoError(t, err)
// Fail to verify bad proofs.
err = prt.VerifyValue(res, root, keyPath("/", "MYKEY_NOT"), []byte("MYVALUE"))
require.Error(t, err)
err = prt.VerifyValue(res, root, keyPath("/", "MYKEY/MYKEY"), []byte("MYVALUE"))
require.Error(t, err)
err = prt.VerifyValue(res, root, keyPath("", "MYKEY"), []byte("MYVALUE"))
require.Error(t, err)
err = prt.VerifyValue(res, root, keyPath("/", "MYKEY"), []byte("MYVALUE_NOT"))
require.Error(t, err)
err = prt.VerifyValue(res, root, keyPath("/", "MYKEY"), []byte(nil))
require.Error(t, err)
}
func TestVerifyMultiStoreQueryProof(t *testing.T) {
db := memdb.NewDB()
store, err := NewStore(db, simpleStoreConfig(t))
require.NoError(t, err)
substore := store.GetKVStore(skey_1)
substore.Set([]byte("MYKEY"), []byte("MYVALUE"))
cid := store.Commit()
res := store.Query(abci.RequestQuery{
Path: "/store1/key", // required path to get key/value+proof
Data: []byte("MYKEY"),
Prove: true,
})
require.NotNil(t, res.ProofOps)
// Verify good proofs.
prt := DefaultProofRuntime()
err = prt.VerifyValue(res.ProofOps, cid.Hash, keyPath("/store1/", "MYKEY"), []byte("MYVALUE"))
require.NoError(t, err)
err = prt.VerifyValue(res.ProofOps, cid.Hash, keyPath("/store1/", "MYKEY"), []byte("MYVALUE"))
require.NoError(t, err)
// Fail to verify bad proofs.
err = prt.VerifyValue(res.ProofOps, cid.Hash, keyPath("/store1/", "MYKEY_NOT"), []byte("MYVALUE"))
require.Error(t, err)
err = prt.VerifyValue(res.ProofOps, cid.Hash, keyPath("/store1/MYKEY/", "MYKEY"), []byte("MYVALUE"))
require.Error(t, err)
err = prt.VerifyValue(res.ProofOps, cid.Hash, keyPath("store1/", "MYKEY"), []byte("MYVALUE"))
require.Error(t, err)
err = prt.VerifyValue(res.ProofOps, cid.Hash, keyPath("/", "MYKEY"), []byte("MYVALUE"))
require.Error(t, err)
err = prt.VerifyValue(res.ProofOps, cid.Hash, keyPath("/store1/", "MYKEY"), []byte("MYVALUE_NOT"))
require.Error(t, err)
err = prt.VerifyValue(res.ProofOps, cid.Hash, keyPath("/store1/", "MYKEY"), []byte(nil))
require.Error(t, err)
}
func TestVerifyMultiStoreQueryProofAbsence(t *testing.T) {
db := memdb.NewDB()
store, err := NewStore(db, simpleStoreConfig(t))
require.NoError(t, err)
substore := store.GetKVStore(skey_1)
substore.Set([]byte("MYKEY"), []byte("MYVALUE"))
cid := store.Commit()
res := store.Query(abci.RequestQuery{
Path: "/store1/key", // required path to get key/value+proof
Data: []byte("MYABSENTKEY"),
Prove: true,
})
require.NotNil(t, res.ProofOps)
// Verify good proof.
prt := DefaultProofRuntime()
err = prt.VerifyAbsence(res.ProofOps, cid.Hash, keyPath("/store1/", "MYABSENTKEY"))
require.NoError(t, err)
// Fail to verify bad proofs.
prt = DefaultProofRuntime()
err = prt.VerifyAbsence(res.ProofOps, cid.Hash, keyPath("/", "MYABSENTKEY"))
require.Error(t, err)
prt = DefaultProofRuntime()
err = prt.VerifyValue(res.ProofOps, cid.Hash, keyPath("/store1/", "MYABSENTKEY"), []byte(""))
require.Error(t, err)
}