2019-02-01 17:03:09 -08:00
|
|
|
package rootmulti
|
2018-08-29 21:50:41 -07:00
|
|
|
|
|
|
|
import (
|
2018-08-31 15:22:37 -07:00
|
|
|
"testing"
|
|
|
|
|
2018-09-12 00:13:14 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-11-04 19:36:35 -08:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
dbm "github.com/tendermint/tendermint/libs/db"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
2019-02-01 17:03:09 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/store/iavl"
|
2019-02-05 10:39:22 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/store/types"
|
2018-08-29 21:50:41 -07:00
|
|
|
)
|
|
|
|
|
2018-11-04 19:36:35 -08:00
|
|
|
func TestVerifyIAVLStoreQueryProof(t *testing.T) {
|
|
|
|
// Create main tree for testing.
|
|
|
|
db := dbm.NewMemDB()
|
2019-07-15 09:45:45 -07:00
|
|
|
iStore, err := iavl.LoadStore(db, types.CommitID{}, types.PruneNothing, false)
|
2019-02-01 17:03:09 -08:00
|
|
|
store := iStore.(*iavl.Store)
|
2018-11-04 19:36:35 -08:00
|
|
|
require.Nil(t, err)
|
|
|
|
store.Set([]byte("MYKEY"), []byte("MYVALUE"))
|
|
|
|
cid := store.Commit()
|
|
|
|
|
|
|
|
// Get Proof
|
|
|
|
res := store.Query(abci.RequestQuery{
|
|
|
|
Path: "/key", // required path to get key/value+proof
|
|
|
|
Data: []byte("MYKEY"),
|
|
|
|
Prove: true,
|
2018-08-29 21:50:41 -07:00
|
|
|
})
|
2018-11-04 19:36:35 -08:00
|
|
|
require.NotNil(t, res.Proof)
|
2018-08-29 21:50:41 -07:00
|
|
|
|
2018-11-04 19:36:35 -08:00
|
|
|
// Verify proof.
|
|
|
|
prt := DefaultProofRuntime()
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY", []byte("MYVALUE"))
|
|
|
|
require.Nil(t, err)
|
2018-08-29 21:50:41 -07:00
|
|
|
|
2018-11-04 19:36:35 -08:00
|
|
|
// Verify (bad) proof.
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY_NOT", []byte("MYVALUE"))
|
|
|
|
require.NotNil(t, err)
|
2018-08-29 21:50:41 -07:00
|
|
|
|
2018-11-04 19:36:35 -08:00
|
|
|
// Verify (bad) proof.
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY/MYKEY", []byte("MYVALUE"))
|
|
|
|
require.NotNil(t, err)
|
2018-08-29 21:50:41 -07:00
|
|
|
|
2018-11-04 19:36:35 -08:00
|
|
|
// Verify (bad) proof.
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "MYKEY", []byte("MYVALUE"))
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
// Verify (bad) proof.
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY", []byte("MYVALUE_NOT"))
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
// Verify (bad) proof.
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY", []byte(nil))
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|
2018-08-29 21:50:41 -07:00
|
|
|
|
2018-11-04 19:36:35 -08:00
|
|
|
func TestVerifyMultiStoreQueryProof(t *testing.T) {
|
|
|
|
// Create main tree for testing.
|
|
|
|
db := dbm.NewMemDB()
|
2019-02-01 17:03:09 -08:00
|
|
|
store := NewStore(db)
|
|
|
|
iavlStoreKey := types.NewKVStoreKey("iavlStoreKey")
|
2018-11-04 19:36:35 -08:00
|
|
|
|
2019-02-01 17:03:09 -08:00
|
|
|
store.MountStoreWithDB(iavlStoreKey, types.StoreTypeIAVL, nil)
|
2018-11-04 19:36:35 -08:00
|
|
|
store.LoadVersion(0)
|
|
|
|
|
2019-02-01 17:03:09 -08:00
|
|
|
iavlStore := store.GetCommitStore(iavlStoreKey).(*iavl.Store)
|
2018-11-04 19:36:35 -08:00
|
|
|
iavlStore.Set([]byte("MYKEY"), []byte("MYVALUE"))
|
|
|
|
cid := store.Commit()
|
|
|
|
|
|
|
|
// Get Proof
|
|
|
|
res := store.Query(abci.RequestQuery{
|
|
|
|
Path: "/iavlStoreKey/key", // required path to get key/value+proof
|
|
|
|
Data: []byte("MYKEY"),
|
|
|
|
Prove: true,
|
2018-08-29 21:50:41 -07:00
|
|
|
})
|
2018-11-04 19:36:35 -08:00
|
|
|
require.NotNil(t, res.Proof)
|
2018-08-29 21:50:41 -07:00
|
|
|
|
2018-11-04 19:36:35 -08:00
|
|
|
// Verify proof.
|
|
|
|
prt := DefaultProofRuntime()
|
|
|
|
err := prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY", []byte("MYVALUE"))
|
2018-09-12 00:13:14 -07:00
|
|
|
require.Nil(t, err)
|
2018-08-29 21:50:41 -07:00
|
|
|
|
2018-11-04 19:36:35 -08:00
|
|
|
// Verify proof.
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY", []byte("MYVALUE"))
|
|
|
|
require.Nil(t, err)
|
2018-08-29 21:50:41 -07:00
|
|
|
|
2018-11-04 19:36:35 -08:00
|
|
|
// Verify (bad) proof.
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY_NOT", []byte("MYVALUE"))
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
// Verify (bad) proof.
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY/MYKEY", []byte("MYVALUE"))
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
// Verify (bad) proof.
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "iavlStoreKey/MYKEY", []byte("MYVALUE"))
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
// Verify (bad) proof.
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/MYKEY", []byte("MYVALUE"))
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
// Verify (bad) proof.
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY", []byte("MYVALUE_NOT"))
|
|
|
|
require.NotNil(t, err)
|
2018-08-29 21:50:41 -07:00
|
|
|
|
2018-11-04 19:36:35 -08:00
|
|
|
// Verify (bad) proof.
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY", []byte(nil))
|
|
|
|
require.NotNil(t, err)
|
2018-08-30 00:52:17 -07:00
|
|
|
}
|
2018-11-07 00:03:02 -08:00
|
|
|
|
|
|
|
func TestVerifyMultiStoreQueryProofEmptyStore(t *testing.T) {
|
|
|
|
// Create main tree for testing.
|
|
|
|
db := dbm.NewMemDB()
|
2019-02-01 17:03:09 -08:00
|
|
|
store := NewStore(db)
|
|
|
|
iavlStoreKey := types.NewKVStoreKey("iavlStoreKey")
|
2018-11-07 00:03:02 -08:00
|
|
|
|
2019-02-01 17:03:09 -08:00
|
|
|
store.MountStoreWithDB(iavlStoreKey, types.StoreTypeIAVL, nil)
|
2018-11-07 00:03:02 -08:00
|
|
|
store.LoadVersion(0)
|
|
|
|
cid := store.Commit() // Commit with empty iavl store.
|
|
|
|
|
|
|
|
// Get Proof
|
|
|
|
res := store.Query(abci.RequestQuery{
|
|
|
|
Path: "/iavlStoreKey/key", // required path to get key/value+proof
|
|
|
|
Data: []byte("MYKEY"),
|
|
|
|
Prove: true,
|
|
|
|
})
|
|
|
|
require.NotNil(t, res.Proof)
|
|
|
|
|
|
|
|
// Verify proof.
|
|
|
|
prt := DefaultProofRuntime()
|
|
|
|
err := prt.VerifyAbsence(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY")
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Verify (bad) proof.
|
|
|
|
prt = DefaultProofRuntime()
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY", []byte("MYVALUE"))
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifyMultiStoreQueryProofAbsence(t *testing.T) {
|
|
|
|
// Create main tree for testing.
|
|
|
|
db := dbm.NewMemDB()
|
2019-02-01 17:03:09 -08:00
|
|
|
store := NewStore(db)
|
|
|
|
iavlStoreKey := types.NewKVStoreKey("iavlStoreKey")
|
2018-11-07 00:03:02 -08:00
|
|
|
|
2019-02-01 17:03:09 -08:00
|
|
|
store.MountStoreWithDB(iavlStoreKey, types.StoreTypeIAVL, nil)
|
2018-11-07 00:03:02 -08:00
|
|
|
store.LoadVersion(0)
|
|
|
|
|
2019-02-01 17:03:09 -08:00
|
|
|
iavlStore := store.GetCommitStore(iavlStoreKey).(*iavl.Store)
|
2018-11-07 00:03:02 -08:00
|
|
|
iavlStore.Set([]byte("MYKEY"), []byte("MYVALUE"))
|
|
|
|
cid := store.Commit() // Commit with empty iavl store.
|
|
|
|
|
|
|
|
// Get Proof
|
|
|
|
res := store.Query(abci.RequestQuery{
|
|
|
|
Path: "/iavlStoreKey/key", // required path to get key/value+proof
|
|
|
|
Data: []byte("MYABSENTKEY"),
|
|
|
|
Prove: true,
|
|
|
|
})
|
|
|
|
require.NotNil(t, res.Proof)
|
|
|
|
|
|
|
|
// Verify proof.
|
|
|
|
prt := DefaultProofRuntime()
|
|
|
|
err := prt.VerifyAbsence(res.Proof, cid.Hash, "/iavlStoreKey/MYABSENTKEY")
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Verify (bad) proof.
|
|
|
|
prt = DefaultProofRuntime()
|
|
|
|
err = prt.VerifyAbsence(res.Proof, cid.Hash, "/MYABSENTKEY")
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
// Verify (bad) proof.
|
|
|
|
prt = DefaultProofRuntime()
|
|
|
|
err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYABSENTKEY", []byte(""))
|
|
|
|
require.NotNil(t, err)
|
|
|
|
}
|