chore: bring back store v1 to main (#20263)
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package rootmulti
|
||||
|
||||
import (
|
||||
"cosmossdk.io/store/dbadapter"
|
||||
pruningtypes "cosmossdk.io/store/pruning/types"
|
||||
"cosmossdk.io/store/types"
|
||||
)
|
||||
|
||||
var commithash = []byte("FAKE_HASH")
|
||||
|
||||
var (
|
||||
_ types.KVStore = (*commitDBStoreAdapter)(nil)
|
||||
_ types.Committer = (*commitDBStoreAdapter)(nil)
|
||||
)
|
||||
|
||||
//----------------------------------------
|
||||
// commitDBStoreWrapper should only be used for simulation/debugging,
|
||||
// as it doesn't compute any commit hash, and it cannot load older state.
|
||||
|
||||
// Wrapper type for dbm.Db with implementation of KVStore
|
||||
type commitDBStoreAdapter struct {
|
||||
dbadapter.Store
|
||||
}
|
||||
|
||||
func (cdsa commitDBStoreAdapter) Commit() types.CommitID {
|
||||
return types.CommitID{
|
||||
Version: -1,
|
||||
Hash: commithash,
|
||||
}
|
||||
}
|
||||
|
||||
func (cdsa commitDBStoreAdapter) LastCommitID() types.CommitID {
|
||||
return types.CommitID{
|
||||
Version: -1,
|
||||
Hash: commithash,
|
||||
}
|
||||
}
|
||||
|
||||
func (cdsa commitDBStoreAdapter) WorkingHash() []byte {
|
||||
return commithash
|
||||
}
|
||||
|
||||
func (cdsa commitDBStoreAdapter) SetPruning(_ pruningtypes.PruningOptions) {}
|
||||
|
||||
// GetPruning is a no-op as pruning options cannot be directly set on this store.
|
||||
// They must be set on the root commit multi-store.
|
||||
func (cdsa commitDBStoreAdapter) GetPruning() pruningtypes.PruningOptions {
|
||||
return pruningtypes.NewPruningOptions(pruningtypes.PruningUndefined)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package rootmulti
|
||||
|
||||
import (
|
||||
"github.com/cometbft/cometbft/crypto/merkle"
|
||||
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
)
|
||||
|
||||
// RequireProof returns whether proof is required for the subpath.
|
||||
func RequireProof(subpath string) bool {
|
||||
// XXX: create a better convention.
|
||||
// Currently, only when query subpath is "/key", will proof be included in
|
||||
// response. If there are some changes about proof building in iavlstore.go,
|
||||
// we must change code here to keep consistency with iavlStore#Query.
|
||||
return subpath == "/key"
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// XXX: This should be managed by the rootMultiStore which may want to register
|
||||
// more proof ops?
|
||||
func DefaultProofRuntime() (prt *merkle.ProofRuntime) {
|
||||
prt = merkle.NewProofRuntime()
|
||||
prt.RegisterOpDecoder(storetypes.ProofOpIAVLCommitment, storetypes.CommitmentOpDecoder)
|
||||
prt.RegisterOpDecoder(storetypes.ProofOpSimpleMerkleCommitment, storetypes.CommitmentOpDecoder)
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package rootmulti
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
dbm "github.com/cosmos/cosmos-db"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/log"
|
||||
"cosmossdk.io/store/iavl"
|
||||
"cosmossdk.io/store/metrics"
|
||||
"cosmossdk.io/store/types"
|
||||
)
|
||||
|
||||
func TestVerifyIAVLStoreQueryProof(t *testing.T) {
|
||||
// Create main tree for testing.
|
||||
db := dbm.NewMemDB()
|
||||
iStore, err := iavl.LoadStore(db, log.NewNopLogger(), types.NewKVStoreKey("test"), types.CommitID{}, iavl.DefaultIAVLCacheSize, false, metrics.NewNoOpMetrics())
|
||||
store := iStore.(*iavl.Store)
|
||||
require.Nil(t, err)
|
||||
store.Set([]byte("MYKEY"), []byte("MYVALUE"))
|
||||
cid := store.Commit()
|
||||
|
||||
// Get Proof
|
||||
res, err := store.Query(&types.RequestQuery{
|
||||
Path: "/key", // required path to get key/value+proof
|
||||
Data: []byte("MYKEY"),
|
||||
Prove: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, res.ProofOps)
|
||||
|
||||
// Verify proof.
|
||||
prt := DefaultProofRuntime()
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/MYKEY", []byte("MYVALUE"))
|
||||
require.Nil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/MYKEY_NOT", []byte("MYVALUE"))
|
||||
require.NotNil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/MYKEY/MYKEY", []byte("MYVALUE"))
|
||||
require.NotNil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "MYKEY", []byte("MYVALUE"))
|
||||
require.NotNil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/MYKEY", []byte("MYVALUE_NOT"))
|
||||
require.NotNil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/MYKEY", []byte(nil))
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestVerifyMultiStoreQueryProof(t *testing.T) {
|
||||
// Create main tree for testing.
|
||||
db := dbm.NewMemDB()
|
||||
store := NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
|
||||
iavlStoreKey := types.NewKVStoreKey("iavlStoreKey")
|
||||
|
||||
store.MountStoreWithDB(iavlStoreKey, types.StoreTypeIAVL, nil)
|
||||
require.NoError(t, store.LoadVersion(0))
|
||||
|
||||
iavlStore := store.GetCommitStore(iavlStoreKey).(*iavl.Store)
|
||||
iavlStore.Set([]byte("MYKEY"), []byte("MYVALUE"))
|
||||
cid := store.Commit()
|
||||
|
||||
// Get Proof
|
||||
res, err := store.Query(&types.RequestQuery{
|
||||
Path: "/iavlStoreKey/key", // required path to get key/value+proof
|
||||
Data: []byte("MYKEY"),
|
||||
Prove: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, res.ProofOps)
|
||||
|
||||
// Verify proof.
|
||||
prt := DefaultProofRuntime()
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/iavlStoreKey/MYKEY", []byte("MYVALUE"))
|
||||
require.Nil(t, err)
|
||||
|
||||
// Verify proof.
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/iavlStoreKey/MYKEY", []byte("MYVALUE"))
|
||||
require.Nil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/iavlStoreKey/MYKEY_NOT", []byte("MYVALUE"))
|
||||
require.NotNil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/iavlStoreKey/MYKEY/MYKEY", []byte("MYVALUE"))
|
||||
require.NotNil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "iavlStoreKey/MYKEY", []byte("MYVALUE"))
|
||||
require.NotNil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/MYKEY", []byte("MYVALUE"))
|
||||
require.NotNil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/iavlStoreKey/MYKEY", []byte("MYVALUE_NOT"))
|
||||
require.NotNil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/iavlStoreKey/MYKEY", []byte(nil))
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestVerifyMultiStoreQueryProofAbsence(t *testing.T) {
|
||||
// Create main tree for testing.
|
||||
db := dbm.NewMemDB()
|
||||
store := NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
|
||||
iavlStoreKey := types.NewKVStoreKey("iavlStoreKey")
|
||||
|
||||
store.MountStoreWithDB(iavlStoreKey, types.StoreTypeIAVL, nil)
|
||||
err := store.LoadVersion(0)
|
||||
require.NoError(t, err)
|
||||
|
||||
iavlStore := store.GetCommitStore(iavlStoreKey).(*iavl.Store)
|
||||
iavlStore.Set([]byte("MYKEY"), []byte("MYVALUE"))
|
||||
cid := store.Commit() // Commit with empty iavl store.
|
||||
|
||||
// Get Proof
|
||||
res, err := store.Query(&types.RequestQuery{
|
||||
Path: "/iavlStoreKey/key", // required path to get key/value+proof
|
||||
Data: []byte("MYABSENTKEY"),
|
||||
Prove: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, res.ProofOps)
|
||||
|
||||
// Verify proof.
|
||||
prt := DefaultProofRuntime()
|
||||
err = prt.VerifyAbsence(res.ProofOps, cid.Hash, "/iavlStoreKey/MYABSENTKEY")
|
||||
require.Nil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
prt = DefaultProofRuntime()
|
||||
err = prt.VerifyAbsence(res.ProofOps, cid.Hash, "/MYABSENTKEY")
|
||||
require.NotNil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
prt = DefaultProofRuntime()
|
||||
err = prt.VerifyValue(res.ProofOps, cid.Hash, "/iavlStoreKey/MYABSENTKEY", []byte(""))
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
@@ -0,0 +1,321 @@
|
||||
package rootmulti_test
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
dbm "github.com/cosmos/cosmos-db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/log"
|
||||
"cosmossdk.io/store/iavl"
|
||||
"cosmossdk.io/store/metrics"
|
||||
"cosmossdk.io/store/rootmulti"
|
||||
"cosmossdk.io/store/snapshots"
|
||||
snapshottypes "cosmossdk.io/store/snapshots/types"
|
||||
"cosmossdk.io/store/types"
|
||||
)
|
||||
|
||||
func newMultiStoreWithGeneratedData(db dbm.DB, stores uint8, storeKeys uint64) *rootmulti.Store {
|
||||
multiStore := rootmulti.NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
|
||||
r := rand.New(rand.NewSource(49872768940)) // Fixed seed for deterministic tests
|
||||
|
||||
keys := []*types.KVStoreKey{}
|
||||
for i := uint8(0); i < stores; i++ {
|
||||
key := types.NewKVStoreKey(fmt.Sprintf("store%v", i))
|
||||
multiStore.MountStoreWithDB(key, types.StoreTypeIAVL, nil)
|
||||
keys = append(keys, key)
|
||||
}
|
||||
err := multiStore.LoadLatestVersion()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, key := range keys {
|
||||
store := multiStore.GetCommitKVStore(key).(*iavl.Store)
|
||||
for i := uint64(0); i < storeKeys; i++ {
|
||||
k := make([]byte, 8)
|
||||
v := make([]byte, 1024)
|
||||
binary.BigEndian.PutUint64(k, i)
|
||||
_, err := r.Read(v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
store.Set(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
multiStore.Commit()
|
||||
err = multiStore.LoadLatestVersion()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return multiStore
|
||||
}
|
||||
|
||||
func newMultiStoreWithMixedMounts(db dbm.DB) *rootmulti.Store {
|
||||
store := rootmulti.NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
|
||||
store.MountStoreWithDB(types.NewKVStoreKey("iavl1"), types.StoreTypeIAVL, nil)
|
||||
store.MountStoreWithDB(types.NewKVStoreKey("iavl2"), types.StoreTypeIAVL, nil)
|
||||
store.MountStoreWithDB(types.NewKVStoreKey("iavl3"), types.StoreTypeIAVL, nil)
|
||||
store.MountStoreWithDB(types.NewTransientStoreKey("trans1"), types.StoreTypeTransient, nil)
|
||||
if err := store.LoadLatestVersion(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return store
|
||||
}
|
||||
|
||||
func newMultiStoreWithMixedMountsAndBasicData(db dbm.DB) *rootmulti.Store {
|
||||
store := newMultiStoreWithMixedMounts(db)
|
||||
store1 := store.GetStoreByName("iavl1").(types.CommitKVStore)
|
||||
store2 := store.GetStoreByName("iavl2").(types.CommitKVStore)
|
||||
trans1 := store.GetStoreByName("trans1").(types.KVStore)
|
||||
|
||||
store1.Set([]byte("a"), []byte{1})
|
||||
store1.Set([]byte("b"), []byte{1})
|
||||
store2.Set([]byte("X"), []byte{255})
|
||||
store2.Set([]byte("A"), []byte{101})
|
||||
trans1.Set([]byte("x1"), []byte{91})
|
||||
store.Commit()
|
||||
|
||||
store1.Set([]byte("b"), []byte{2})
|
||||
store1.Set([]byte("c"), []byte{3})
|
||||
store2.Set([]byte("B"), []byte{102})
|
||||
store.Commit()
|
||||
|
||||
store2.Set([]byte("C"), []byte{103})
|
||||
store2.Delete([]byte("X"))
|
||||
trans1.Set([]byte("x2"), []byte{92})
|
||||
store.Commit()
|
||||
|
||||
return store
|
||||
}
|
||||
|
||||
func assertStoresEqual(t *testing.T, expect, actual types.CommitKVStore, msgAndArgs ...interface{}) {
|
||||
t.Helper()
|
||||
assert.Equal(t, expect.LastCommitID(), actual.LastCommitID())
|
||||
expectIter := expect.Iterator(nil, nil)
|
||||
expectMap := map[string][]byte{}
|
||||
for ; expectIter.Valid(); expectIter.Next() {
|
||||
expectMap[string(expectIter.Key())] = expectIter.Value()
|
||||
}
|
||||
require.NoError(t, expectIter.Error())
|
||||
|
||||
actualIter := expect.Iterator(nil, nil)
|
||||
actualMap := map[string][]byte{}
|
||||
for ; actualIter.Valid(); actualIter.Next() {
|
||||
actualMap[string(actualIter.Key())] = actualIter.Value()
|
||||
}
|
||||
require.NoError(t, actualIter.Error())
|
||||
|
||||
assert.Equal(t, expectMap, actualMap, msgAndArgs...)
|
||||
}
|
||||
|
||||
func TestMultistoreSnapshot_Checksum(t *testing.T) {
|
||||
// Chunks from different nodes must fit together, so all nodes must produce identical chunks.
|
||||
// This checksum test makes sure that the byte stream remains identical. If the test fails
|
||||
// without having changed the data (e.g. because the Protobuf or zlib encoding changes),
|
||||
// snapshottypes.CurrentFormat must be bumped.
|
||||
store := newMultiStoreWithGeneratedData(dbm.NewMemDB(), 5, 10000)
|
||||
version := uint64(store.LastCommitID().Version)
|
||||
|
||||
testcases := []struct {
|
||||
format uint32
|
||||
chunkHashes []string
|
||||
}{
|
||||
{1, []string{
|
||||
"503e5b51b657055b77e88169fadae543619368744ad15f1de0736c0a20482f24",
|
||||
"e1a0daaa738eeb43e778aefd2805e3dd720798288a410b06da4b8459c4d8f72e",
|
||||
"aa048b4ee0f484965d7b3b06822cf0772cdcaad02f3b1b9055e69f2cb365ef3c",
|
||||
"7921eaa3ed4921341e504d9308a9877986a879fe216a099c86e8db66fcba4c63",
|
||||
"a4a864e6c02c9fca5837ec80dc84f650b25276ed7e4820cf7516ced9f9901b86",
|
||||
"980925390cc50f14998ecb1e87de719ca9dd7e72f5fefbe445397bf670f36c31",
|
||||
}},
|
||||
}
|
||||
for _, tc := range testcases {
|
||||
tc := tc
|
||||
t.Run(fmt.Sprintf("Format %v", tc.format), func(t *testing.T) {
|
||||
ch := make(chan io.ReadCloser)
|
||||
go func() {
|
||||
streamWriter := snapshots.NewStreamWriter(ch)
|
||||
defer streamWriter.Close()
|
||||
require.NotNil(t, streamWriter)
|
||||
err := store.Snapshot(version, streamWriter)
|
||||
require.NoError(t, err)
|
||||
}()
|
||||
hashes := []string{}
|
||||
hasher := sha256.New()
|
||||
for chunk := range ch {
|
||||
hasher.Reset()
|
||||
_, err := io.Copy(hasher, chunk)
|
||||
require.NoError(t, err)
|
||||
hashes = append(hashes, hex.EncodeToString(hasher.Sum(nil)))
|
||||
}
|
||||
assert.Equal(t, tc.chunkHashes, hashes,
|
||||
"Snapshot output for format %v has changed", tc.format)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultistoreSnapshot_Errors(t *testing.T) {
|
||||
store := newMultiStoreWithMixedMountsAndBasicData(dbm.NewMemDB())
|
||||
|
||||
testcases := map[string]struct {
|
||||
height uint64
|
||||
expectType error
|
||||
}{
|
||||
"0 height": {0, nil},
|
||||
"unknown height": {9, nil},
|
||||
}
|
||||
for name, tc := range testcases {
|
||||
tc := tc
|
||||
t.Run(name, func(t *testing.T) {
|
||||
err := store.Snapshot(tc.height, nil)
|
||||
require.Error(t, err)
|
||||
if tc.expectType != nil {
|
||||
assert.True(t, errors.Is(err, tc.expectType))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultistoreSnapshotRestore(t *testing.T) {
|
||||
source := newMultiStoreWithMixedMountsAndBasicData(dbm.NewMemDB())
|
||||
target := newMultiStoreWithMixedMounts(dbm.NewMemDB())
|
||||
version := uint64(source.LastCommitID().Version)
|
||||
require.EqualValues(t, 3, version)
|
||||
dummyExtensionItem := snapshottypes.SnapshotItem{
|
||||
Item: &snapshottypes.SnapshotItem_Extension{
|
||||
Extension: &snapshottypes.SnapshotExtensionMeta{
|
||||
Name: "test",
|
||||
Format: 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
chunks := make(chan io.ReadCloser, 100)
|
||||
go func() {
|
||||
streamWriter := snapshots.NewStreamWriter(chunks)
|
||||
require.NotNil(t, streamWriter)
|
||||
defer streamWriter.Close()
|
||||
err := source.Snapshot(version, streamWriter)
|
||||
require.NoError(t, err)
|
||||
// write an extension metadata
|
||||
err = streamWriter.WriteMsg(&dummyExtensionItem)
|
||||
require.NoError(t, err)
|
||||
}()
|
||||
|
||||
streamReader, err := snapshots.NewStreamReader(chunks)
|
||||
require.NoError(t, err)
|
||||
nextItem, err := target.Restore(version, snapshottypes.CurrentFormat, streamReader)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, *dummyExtensionItem.GetExtension(), *nextItem.GetExtension())
|
||||
|
||||
assert.Equal(t, source.LastCommitID(), target.LastCommitID())
|
||||
for _, key := range source.StoreKeysByName() {
|
||||
sourceStore := source.GetStoreByName(key.Name()).(types.CommitKVStore)
|
||||
targetStore := target.GetStoreByName(key.Name()).(types.CommitKVStore)
|
||||
switch sourceStore.GetStoreType() {
|
||||
case types.StoreTypeTransient:
|
||||
assert.False(t, targetStore.Iterator(nil, nil).Valid(),
|
||||
"transient store %v not empty", key.Name())
|
||||
default:
|
||||
assertStoresEqual(t, sourceStore, targetStore, "store %q not equal", key.Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkMultistoreSnapshot(b *testing.B, stores uint8, storeKeys uint64) {
|
||||
b.Helper()
|
||||
b.Skip("Noisy with slow setup time, please see https://github.com/cosmos/cosmos-sdk/issues/8855.")
|
||||
|
||||
b.ReportAllocs()
|
||||
b.StopTimer()
|
||||
source := newMultiStoreWithGeneratedData(dbm.NewMemDB(), stores, storeKeys)
|
||||
version := source.LastCommitID().Version
|
||||
require.EqualValues(b, 1, version)
|
||||
b.StartTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
target := rootmulti.NewStore(dbm.NewMemDB(), log.NewNopLogger(), metrics.NewNoOpMetrics())
|
||||
for _, key := range source.StoreKeysByName() {
|
||||
target.MountStoreWithDB(key, types.StoreTypeIAVL, nil)
|
||||
}
|
||||
err := target.LoadLatestVersion()
|
||||
require.NoError(b, err)
|
||||
require.EqualValues(b, 0, target.LastCommitID().Version)
|
||||
|
||||
chunks := make(chan io.ReadCloser)
|
||||
go func() {
|
||||
streamWriter := snapshots.NewStreamWriter(chunks)
|
||||
require.NotNil(b, streamWriter)
|
||||
err := source.Snapshot(uint64(version), streamWriter)
|
||||
require.NoError(b, err)
|
||||
}()
|
||||
for reader := range chunks {
|
||||
_, err := io.Copy(io.Discard, reader)
|
||||
require.NoError(b, err)
|
||||
err = reader.Close()
|
||||
require.NoError(b, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkMultistoreSnapshotRestore(b *testing.B, stores uint8, storeKeys uint64) {
|
||||
b.Helper()
|
||||
b.Skip("Noisy with slow setup time, please see https://github.com/cosmos/cosmos-sdk/issues/8855.")
|
||||
|
||||
b.ReportAllocs()
|
||||
b.StopTimer()
|
||||
source := newMultiStoreWithGeneratedData(dbm.NewMemDB(), stores, storeKeys)
|
||||
version := uint64(source.LastCommitID().Version)
|
||||
require.EqualValues(b, 1, version)
|
||||
b.StartTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
target := rootmulti.NewStore(dbm.NewMemDB(), log.NewNopLogger(), metrics.NewNoOpMetrics())
|
||||
for _, key := range source.StoreKeysByName() {
|
||||
target.MountStoreWithDB(key, types.StoreTypeIAVL, nil)
|
||||
}
|
||||
err := target.LoadLatestVersion()
|
||||
require.NoError(b, err)
|
||||
require.EqualValues(b, 0, target.LastCommitID().Version)
|
||||
|
||||
chunks := make(chan io.ReadCloser)
|
||||
go func() {
|
||||
writer := snapshots.NewStreamWriter(chunks)
|
||||
require.NotNil(b, writer)
|
||||
err := source.Snapshot(version, writer)
|
||||
require.NoError(b, err)
|
||||
}()
|
||||
reader, err := snapshots.NewStreamReader(chunks)
|
||||
require.NoError(b, err)
|
||||
_, err = target.Restore(version, snapshottypes.CurrentFormat, reader)
|
||||
require.NoError(b, err)
|
||||
require.Equal(b, source.LastCommitID(), target.LastCommitID())
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMultistoreSnapshot100K(b *testing.B) {
|
||||
benchmarkMultistoreSnapshot(b, 10, 10000)
|
||||
}
|
||||
|
||||
func BenchmarkMultistoreSnapshot1M(b *testing.B) {
|
||||
benchmarkMultistoreSnapshot(b, 10, 100000)
|
||||
}
|
||||
|
||||
func BenchmarkMultistoreSnapshotRestore100K(b *testing.B) {
|
||||
benchmarkMultistoreSnapshotRestore(b, 10, 10000)
|
||||
}
|
||||
|
||||
func BenchmarkMultistoreSnapshotRestore1M(b *testing.B) {
|
||||
benchmarkMultistoreSnapshotRestore(b, 10, 100000)
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,992 @@
|
||||
package rootmulti
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
dbm "github.com/cosmos/cosmos-db"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/errors"
|
||||
"cosmossdk.io/log"
|
||||
"cosmossdk.io/store/cachemulti"
|
||||
"cosmossdk.io/store/iavl"
|
||||
sdkmaps "cosmossdk.io/store/internal/maps"
|
||||
"cosmossdk.io/store/metrics"
|
||||
pruningtypes "cosmossdk.io/store/pruning/types"
|
||||
"cosmossdk.io/store/types"
|
||||
)
|
||||
|
||||
func TestStoreType(t *testing.T) {
|
||||
db := dbm.NewMemDB()
|
||||
store := NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
|
||||
store.MountStoreWithDB(types.NewKVStoreKey("store1"), types.StoreTypeIAVL, db)
|
||||
}
|
||||
|
||||
func TestGetCommitKVStore(t *testing.T) {
|
||||
var db dbm.DB = dbm.NewMemDB()
|
||||
ms := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningDefault))
|
||||
err := ms.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
|
||||
key := ms.keysByName["store1"]
|
||||
|
||||
store1 := ms.GetCommitKVStore(key)
|
||||
require.NotNil(t, store1)
|
||||
require.IsType(t, &iavl.Store{}, store1)
|
||||
|
||||
store2 := ms.GetCommitStore(key)
|
||||
require.NotNil(t, store2)
|
||||
require.IsType(t, &iavl.Store{}, store2)
|
||||
}
|
||||
|
||||
func TestStoreMount(t *testing.T) {
|
||||
db := dbm.NewMemDB()
|
||||
store := NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
|
||||
|
||||
key1 := types.NewKVStoreKey("store1")
|
||||
key2 := types.NewKVStoreKey("store2")
|
||||
dup1 := types.NewKVStoreKey("store1")
|
||||
|
||||
require.NotPanics(t, func() { store.MountStoreWithDB(key1, types.StoreTypeIAVL, db) })
|
||||
require.NotPanics(t, func() { store.MountStoreWithDB(key2, types.StoreTypeIAVL, db) })
|
||||
|
||||
require.Panics(t, func() { store.MountStoreWithDB(key1, types.StoreTypeIAVL, db) })
|
||||
require.Panics(t, func() { store.MountStoreWithDB(nil, types.StoreTypeIAVL, db) })
|
||||
require.Panics(t, func() { store.MountStoreWithDB(dup1, types.StoreTypeIAVL, db) })
|
||||
}
|
||||
|
||||
func TestCacheMultiStore(t *testing.T) {
|
||||
var db dbm.DB = dbm.NewMemDB()
|
||||
ms := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
|
||||
cacheMulti := ms.CacheMultiStore()
|
||||
require.IsType(t, cachemulti.Store{}, cacheMulti)
|
||||
}
|
||||
|
||||
func TestCacheMultiStoreWithVersion(t *testing.T) {
|
||||
var db dbm.DB = dbm.NewMemDB()
|
||||
ms := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
err := ms.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
|
||||
emptyHash := sha256.Sum256([]byte{})
|
||||
appHash := emptyHash[:]
|
||||
commitID := types.CommitID{Hash: appHash}
|
||||
checkStore(t, ms, commitID, commitID)
|
||||
|
||||
k, v := []byte("wind"), []byte("blows")
|
||||
|
||||
store1 := ms.GetStoreByName("store1").(types.KVStore)
|
||||
store1.Set(k, v)
|
||||
|
||||
cID := ms.Commit()
|
||||
require.Equal(t, int64(1), cID.Version)
|
||||
|
||||
// require no failure when given an invalid or pruned version
|
||||
_, err = ms.CacheMultiStoreWithVersion(cID.Version + 1)
|
||||
require.Error(t, err)
|
||||
|
||||
// require a valid version can be cache-loaded
|
||||
cms, err := ms.CacheMultiStoreWithVersion(cID.Version)
|
||||
require.NoError(t, err)
|
||||
|
||||
// require a valid key lookup yields the correct value
|
||||
kvStore := cms.GetKVStore(ms.keysByName["store1"])
|
||||
require.NotNil(t, kvStore)
|
||||
require.Equal(t, kvStore.Get(k), v)
|
||||
|
||||
// add new module stores (store4 and store5) to multi stores and commit
|
||||
ms.MountStoreWithDB(types.NewKVStoreKey("store4"), types.StoreTypeIAVL, nil)
|
||||
ms.MountStoreWithDB(types.NewKVStoreKey("store5"), types.StoreTypeIAVL, nil)
|
||||
err = ms.LoadLatestVersionAndUpgrade(&types.StoreUpgrades{Added: []string{"store4", "store5"}})
|
||||
require.NoError(t, err)
|
||||
ms.Commit()
|
||||
|
||||
// cache multistore of version before adding store4 should works
|
||||
_, err = ms.CacheMultiStoreWithVersion(1)
|
||||
require.NoError(t, err)
|
||||
|
||||
// require we cannot commit (write) to a cache-versioned multi-store
|
||||
require.Panics(t, func() {
|
||||
kvStore.Set(k, []byte("newValue"))
|
||||
cms.Write()
|
||||
})
|
||||
}
|
||||
|
||||
func TestHashStableWithEmptyCommit(t *testing.T) {
|
||||
var db dbm.DB = dbm.NewMemDB()
|
||||
ms := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
err := ms.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
|
||||
emptyHash := sha256.Sum256([]byte{})
|
||||
appHash := emptyHash[:]
|
||||
commitID := types.CommitID{Hash: appHash}
|
||||
checkStore(t, ms, commitID, commitID)
|
||||
|
||||
k, v := []byte("wind"), []byte("blows")
|
||||
|
||||
store1 := ms.GetStoreByName("store1").(types.KVStore)
|
||||
store1.Set(k, v)
|
||||
|
||||
workingHash := ms.WorkingHash()
|
||||
cID := ms.Commit()
|
||||
require.Equal(t, int64(1), cID.Version)
|
||||
hash := cID.Hash
|
||||
require.Equal(t, workingHash, hash)
|
||||
|
||||
// make an empty commit, it should update version, but not affect hash
|
||||
workingHash = ms.WorkingHash()
|
||||
cID = ms.Commit()
|
||||
require.Equal(t, workingHash, cID.Hash)
|
||||
require.Equal(t, int64(2), cID.Version)
|
||||
require.Equal(t, hash, cID.Hash)
|
||||
}
|
||||
|
||||
func TestMultistoreCommitLoad(t *testing.T) {
|
||||
var db dbm.DB = dbm.NewMemDB()
|
||||
store := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
err := store.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
|
||||
emptyHash := sha256.Sum256([]byte{})
|
||||
appHash := emptyHash[:]
|
||||
// New store has empty last commit.
|
||||
commitID := types.CommitID{Hash: appHash}
|
||||
checkStore(t, store, commitID, commitID)
|
||||
|
||||
// Make sure we can get stores by name.
|
||||
s1 := store.GetStoreByName("store1")
|
||||
require.NotNil(t, s1)
|
||||
s3 := store.GetStoreByName("store3")
|
||||
require.NotNil(t, s3)
|
||||
s77 := store.GetStoreByName("store77")
|
||||
require.Nil(t, s77)
|
||||
|
||||
// Make a few commits and check them.
|
||||
nCommits := int64(3)
|
||||
for i := int64(0); i < nCommits; i++ {
|
||||
workingHash := store.WorkingHash()
|
||||
commitID = store.Commit()
|
||||
require.Equal(t, workingHash, commitID.Hash)
|
||||
expectedCommitID := getExpectedCommitID(store, i+1)
|
||||
checkStore(t, store, expectedCommitID, commitID)
|
||||
}
|
||||
|
||||
// Load the latest multistore again and check version.
|
||||
store = newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
err = store.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
commitID = getExpectedCommitID(store, nCommits)
|
||||
checkStore(t, store, commitID, commitID)
|
||||
|
||||
// Commit and check version.
|
||||
workingHash := store.WorkingHash()
|
||||
commitID = store.Commit()
|
||||
require.Equal(t, workingHash, commitID.Hash)
|
||||
expectedCommitID := getExpectedCommitID(store, nCommits+1)
|
||||
checkStore(t, store, expectedCommitID, commitID)
|
||||
|
||||
// Load an older multistore and check version.
|
||||
ver := nCommits - 1
|
||||
store = newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
err = store.LoadVersion(ver)
|
||||
require.Nil(t, err)
|
||||
commitID = getExpectedCommitID(store, ver)
|
||||
checkStore(t, store, commitID, commitID)
|
||||
}
|
||||
|
||||
func TestMultistoreLoadWithUpgrade(t *testing.T) {
|
||||
var db dbm.DB = dbm.NewMemDB()
|
||||
store := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
err := store.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
|
||||
// write some data in all stores
|
||||
k1, v1 := []byte("first"), []byte("store")
|
||||
s1, _ := store.GetStoreByName("store1").(types.KVStore)
|
||||
require.NotNil(t, s1)
|
||||
s1.Set(k1, v1)
|
||||
|
||||
k2, v2 := []byte("second"), []byte("restore")
|
||||
s2, _ := store.GetStoreByName("store2").(types.KVStore)
|
||||
require.NotNil(t, s2)
|
||||
s2.Set(k2, v2)
|
||||
|
||||
k3, v3 := []byte("third"), []byte("dropped")
|
||||
s3, _ := store.GetStoreByName("store3").(types.KVStore)
|
||||
require.NotNil(t, s3)
|
||||
s3.Set(k3, v3)
|
||||
|
||||
s4, _ := store.GetStoreByName("store4").(types.KVStore)
|
||||
require.Nil(t, s4)
|
||||
|
||||
// do one commit
|
||||
workingHash := store.WorkingHash()
|
||||
commitID := store.Commit()
|
||||
require.Equal(t, workingHash, commitID.Hash)
|
||||
expectedCommitID := getExpectedCommitID(store, 1)
|
||||
checkStore(t, store, expectedCommitID, commitID)
|
||||
|
||||
ci, err := store.GetCommitInfo(1)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(1), ci.Version)
|
||||
require.Equal(t, 3, len(ci.StoreInfos))
|
||||
checkContains(t, ci.StoreInfos, []string{"store1", "store2", "store3"})
|
||||
|
||||
// Load without changes and make sure it is sensible
|
||||
store = newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
|
||||
err = store.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
commitID = getExpectedCommitID(store, 1)
|
||||
checkStore(t, store, commitID, commitID)
|
||||
|
||||
// let's query data to see it was saved properly
|
||||
s2, _ = store.GetStoreByName("store2").(types.KVStore)
|
||||
require.NotNil(t, s2)
|
||||
require.Equal(t, v2, s2.Get(k2))
|
||||
|
||||
// now, let's load with upgrades...
|
||||
restore, upgrades := newMultiStoreWithModifiedMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
err = restore.LoadLatestVersionAndUpgrade(upgrades)
|
||||
require.Nil(t, err)
|
||||
|
||||
// s1 was not changed
|
||||
s1, _ = restore.GetStoreByName("store1").(types.KVStore)
|
||||
require.NotNil(t, s1)
|
||||
require.Equal(t, v1, s1.Get(k1))
|
||||
|
||||
// store3 is mounted, but data deleted are gone
|
||||
s3, _ = restore.GetStoreByName("store3").(types.KVStore)
|
||||
require.NotNil(t, s3)
|
||||
require.Nil(t, s3.Get(k3)) // data was deleted
|
||||
|
||||
// store4 is mounted, with empty data
|
||||
s4, _ = restore.GetStoreByName("store4").(types.KVStore)
|
||||
require.NotNil(t, s4)
|
||||
|
||||
iterator := s4.Iterator(nil, nil)
|
||||
|
||||
values := 0
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
values++
|
||||
}
|
||||
require.Zero(t, values)
|
||||
|
||||
require.NoError(t, iterator.Close())
|
||||
|
||||
// write something inside store4
|
||||
k4, v4 := []byte("fourth"), []byte("created")
|
||||
s4.Set(k4, v4)
|
||||
|
||||
// store2 is no longer mounted
|
||||
st2 := restore.GetStoreByName("store2")
|
||||
require.Nil(t, st2)
|
||||
|
||||
// restore2 has the old data
|
||||
rs2, _ := restore.GetStoreByName("restore2").(types.KVStore)
|
||||
require.NotNil(t, rs2)
|
||||
require.Equal(t, v2, rs2.Get(k2))
|
||||
|
||||
// store this migrated data, and load it again without migrations
|
||||
migratedID := restore.Commit()
|
||||
require.Equal(t, migratedID.Version, int64(2))
|
||||
|
||||
reload, _ := newMultiStoreWithModifiedMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
// unmount store3 since store3 was deleted
|
||||
unmountStore(reload, "store3")
|
||||
|
||||
rs3, _ := reload.GetStoreByName("store3").(types.KVStore)
|
||||
require.Nil(t, rs3)
|
||||
|
||||
err = reload.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, migratedID, reload.LastCommitID())
|
||||
|
||||
// query this new store
|
||||
rl1, _ := reload.GetStoreByName("store1").(types.KVStore)
|
||||
require.NotNil(t, rl1)
|
||||
require.Equal(t, v1, rl1.Get(k1))
|
||||
|
||||
rl2, _ := reload.GetStoreByName("restore2").(types.KVStore)
|
||||
require.NotNil(t, rl2)
|
||||
require.Equal(t, v2, rl2.Get(k2))
|
||||
|
||||
rl4, _ := reload.GetStoreByName("store4").(types.KVStore)
|
||||
require.NotNil(t, rl4)
|
||||
require.Equal(t, v4, rl4.Get(k4))
|
||||
|
||||
// check commitInfo in storage
|
||||
ci, err = reload.GetCommitInfo(2)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(2), ci.Version)
|
||||
require.Equal(t, 3, len(ci.StoreInfos), ci.StoreInfos)
|
||||
checkContains(t, ci.StoreInfos, []string{"store1", "restore2", "store4"})
|
||||
}
|
||||
|
||||
func TestParsePath(t *testing.T) {
|
||||
_, _, err := parsePath("foo")
|
||||
require.Error(t, err)
|
||||
|
||||
store, subpath, err := parsePath("/foo")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, store, "foo")
|
||||
require.Equal(t, subpath, "")
|
||||
|
||||
store, subpath, err = parsePath("/fizz/bang/baz")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, store, "fizz")
|
||||
require.Equal(t, subpath, "/bang/baz")
|
||||
|
||||
substore, subsubpath, err := parsePath(subpath)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, substore, "bang")
|
||||
require.Equal(t, subsubpath, "/baz")
|
||||
}
|
||||
|
||||
func TestMultiStoreRestart(t *testing.T) {
|
||||
db := dbm.NewMemDB()
|
||||
pruning := pruningtypes.NewCustomPruningOptions(2, 1)
|
||||
multi := newMultiStoreWithMounts(db, pruning)
|
||||
err := multi.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
|
||||
initCid := multi.LastCommitID()
|
||||
|
||||
k, v := "wind", "blows"
|
||||
k2, v2 := "water", "flows"
|
||||
k3, v3 := "fire", "burns"
|
||||
|
||||
for i := 1; i < 3; i++ {
|
||||
// Set and commit data in one store.
|
||||
store1 := multi.GetStoreByName("store1").(types.KVStore)
|
||||
store1.Set([]byte(k), []byte(fmt.Sprintf("%s:%d", v, i)))
|
||||
|
||||
// ... and another.
|
||||
store2 := multi.GetStoreByName("store2").(types.KVStore)
|
||||
store2.Set([]byte(k2), []byte(fmt.Sprintf("%s:%d", v2, i)))
|
||||
|
||||
// ... and another.
|
||||
store3 := multi.GetStoreByName("store3").(types.KVStore)
|
||||
store3.Set([]byte(k3), []byte(fmt.Sprintf("%s:%d", v3, i)))
|
||||
|
||||
multi.Commit()
|
||||
|
||||
cinfo, err := multi.GetCommitInfo(int64(i))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(i), cinfo.Version)
|
||||
}
|
||||
|
||||
// Set and commit data in one store.
|
||||
store1 := multi.GetStoreByName("store1").(types.KVStore)
|
||||
store1.Set([]byte(k), []byte(fmt.Sprintf("%s:%d", v, 3)))
|
||||
|
||||
// ... and another.
|
||||
store2 := multi.GetStoreByName("store2").(types.KVStore)
|
||||
store2.Set([]byte(k2), []byte(fmt.Sprintf("%s:%d", v2, 3)))
|
||||
|
||||
multi.Commit()
|
||||
|
||||
flushedCinfo, err := multi.GetCommitInfo(3)
|
||||
require.Nil(t, err)
|
||||
require.NotEqual(t, initCid, flushedCinfo, "CID is different after flush to disk")
|
||||
|
||||
// ... and another.
|
||||
store3 := multi.GetStoreByName("store3").(types.KVStore)
|
||||
store3.Set([]byte(k3), []byte(fmt.Sprintf("%s:%d", v3, 3)))
|
||||
|
||||
multi.Commit()
|
||||
|
||||
postFlushCinfo, err := multi.GetCommitInfo(4)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(4), postFlushCinfo.Version, "Commit changed after in-memory commit")
|
||||
|
||||
multi = newMultiStoreWithMounts(db, pruning)
|
||||
err = multi.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
|
||||
reloadedCid := multi.LastCommitID()
|
||||
require.Equal(t, int64(4), reloadedCid.Version, "Reloaded CID is not the same as last flushed CID")
|
||||
|
||||
// Check that store1 and store2 retained date from 3rd commit
|
||||
store1 = multi.GetStoreByName("store1").(types.KVStore)
|
||||
val := store1.Get([]byte(k))
|
||||
require.Equal(t, []byte(fmt.Sprintf("%s:%d", v, 3)), val, "Reloaded value not the same as last flushed value")
|
||||
|
||||
store2 = multi.GetStoreByName("store2").(types.KVStore)
|
||||
val2 := store2.Get([]byte(k2))
|
||||
require.Equal(t, []byte(fmt.Sprintf("%s:%d", v2, 3)), val2, "Reloaded value not the same as last flushed value")
|
||||
|
||||
// Check that store3 still has data from last commit even though update happened on 2nd commit
|
||||
store3 = multi.GetStoreByName("store3").(types.KVStore)
|
||||
val3 := store3.Get([]byte(k3))
|
||||
require.Equal(t, []byte(fmt.Sprintf("%s:%d", v3, 3)), val3, "Reloaded value not the same as last flushed value")
|
||||
}
|
||||
|
||||
func TestMultiStoreQuery(t *testing.T) {
|
||||
db := dbm.NewMemDB()
|
||||
multi := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
err := multi.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
|
||||
k, v := []byte("wind"), []byte("blows")
|
||||
k2, v2 := []byte("water"), []byte("flows")
|
||||
// v3 := []byte("is cold")
|
||||
|
||||
// Commit the multistore.
|
||||
_ = multi.Commit()
|
||||
|
||||
// Make sure we can get by name.
|
||||
garbage := multi.GetStoreByName("bad-name")
|
||||
require.Nil(t, garbage)
|
||||
|
||||
// Set and commit data in one store.
|
||||
store1 := multi.GetStoreByName("store1").(types.KVStore)
|
||||
store1.Set(k, v)
|
||||
|
||||
// ... and another.
|
||||
store2 := multi.GetStoreByName("store2").(types.KVStore)
|
||||
store2.Set(k2, v2)
|
||||
|
||||
// Commit the multistore.
|
||||
cid := multi.Commit()
|
||||
ver := cid.Version
|
||||
|
||||
// Reload multistore from database
|
||||
multi = newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
err = multi.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
|
||||
// Test bad path.
|
||||
query := types.RequestQuery{Path: "/key", Data: k, Height: ver}
|
||||
_, err = multi.Query(&query)
|
||||
codespace, code, _ := errors.ABCIInfo(err, false)
|
||||
require.EqualValues(t, types.ErrUnknownRequest.ABCICode(), code)
|
||||
require.EqualValues(t, types.ErrUnknownRequest.Codespace(), codespace)
|
||||
|
||||
query.Path = "h897fy32890rf63296r92"
|
||||
_, err = multi.Query(&query)
|
||||
codespace, code, _ = errors.ABCIInfo(err, false)
|
||||
require.EqualValues(t, types.ErrUnknownRequest.ABCICode(), code)
|
||||
require.EqualValues(t, types.ErrUnknownRequest.Codespace(), codespace)
|
||||
|
||||
// Test invalid store name.
|
||||
query.Path = "/garbage/key"
|
||||
_, err = multi.Query(&query)
|
||||
codespace, code, _ = errors.ABCIInfo(err, false)
|
||||
require.EqualValues(t, types.ErrUnknownRequest.ABCICode(), code)
|
||||
require.EqualValues(t, types.ErrUnknownRequest.Codespace(), codespace)
|
||||
|
||||
// Test valid query with data.
|
||||
query.Path = "/store1/key"
|
||||
qres, err := multi.Query(&query)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, v, qres.Value)
|
||||
|
||||
// Test valid but empty query.
|
||||
query.Path = "/store2/key"
|
||||
query.Prove = true
|
||||
qres, err = multi.Query(&query)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, qres.Value)
|
||||
|
||||
// Test store2 data.
|
||||
// Since we are using the request as a reference, the path will be modified.
|
||||
query.Data = k2
|
||||
query.Path = "/store2/key"
|
||||
qres, err = multi.Query(&query)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, v2, qres.Value)
|
||||
}
|
||||
|
||||
func TestMultiStore_Pruning(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
numVersions int64
|
||||
po pruningtypes.PruningOptions
|
||||
deleted []int64
|
||||
saved []int64
|
||||
}{
|
||||
{"prune nothing", 10, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing), nil, []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
|
||||
{"prune everything", 12, pruningtypes.NewPruningOptions(pruningtypes.PruningEverything), []int64{1, 2, 3, 4, 5, 6, 7}, []int64{8, 9, 10, 11, 12}},
|
||||
{"prune some; no batch", 10, pruningtypes.NewCustomPruningOptions(2, 1), []int64{1, 2, 3, 4, 6, 5, 7}, []int64{8, 9, 10}},
|
||||
{"prune some; small batch", 10, pruningtypes.NewCustomPruningOptions(2, 3), []int64{1, 2, 3, 4, 5, 6}, []int64{7, 8, 9, 10}},
|
||||
{"prune some; large batch", 10, pruningtypes.NewCustomPruningOptions(2, 11), nil, []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
db := dbm.NewMemDB()
|
||||
ms := newMultiStoreWithMounts(db, tc.po)
|
||||
require.NoError(t, ms.LoadLatestVersion())
|
||||
|
||||
for i := int64(0); i < tc.numVersions; i++ {
|
||||
ms.Commit()
|
||||
}
|
||||
|
||||
for _, v := range tc.saved {
|
||||
_, err := ms.CacheMultiStoreWithVersion(v)
|
||||
require.NoError(t, err, "expected no error when loading height: %d", v)
|
||||
}
|
||||
|
||||
for _, v := range tc.deleted {
|
||||
_, err := ms.CacheMultiStoreWithVersion(v)
|
||||
require.Error(t, err, "expected error when loading height: %d", v)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiStore_Pruning_SameHeightsTwice(t *testing.T) {
|
||||
const (
|
||||
numVersions int64 = 10
|
||||
keepRecent uint64 = 2
|
||||
interval uint64 = 10
|
||||
)
|
||||
|
||||
db := dbm.NewMemDB()
|
||||
|
||||
ms := newMultiStoreWithMounts(db, pruningtypes.NewCustomPruningOptions(keepRecent, interval))
|
||||
require.NoError(t, ms.LoadLatestVersion())
|
||||
|
||||
var lastCommitInfo types.CommitID
|
||||
for i := int64(0); i < numVersions; i++ {
|
||||
lastCommitInfo = ms.Commit()
|
||||
}
|
||||
|
||||
require.Equal(t, numVersions, lastCommitInfo.Version)
|
||||
|
||||
for v := int64(1); v < numVersions-int64(keepRecent); v++ {
|
||||
err := ms.LoadVersion(v)
|
||||
require.Error(t, err, "expected error when loading pruned height: %d", v)
|
||||
}
|
||||
|
||||
for v := (numVersions - int64(keepRecent)); v < numVersions; v++ {
|
||||
err := ms.LoadVersion(v)
|
||||
require.NoError(t, err, "expected no error when loading height: %d", v)
|
||||
}
|
||||
|
||||
// Get latest
|
||||
err := ms.LoadVersion(numVersions - 1)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Ensure already pruned snapshot heights were loaded
|
||||
require.NoError(t, ms.pruningManager.LoadSnapshotHeights(db))
|
||||
|
||||
// Test pruning the same heights again
|
||||
lastCommitInfo = ms.Commit()
|
||||
require.Equal(t, numVersions, lastCommitInfo.Version)
|
||||
|
||||
// Ensure that can commit one more height with no panic
|
||||
lastCommitInfo = ms.Commit()
|
||||
require.Equal(t, numVersions+1, lastCommitInfo.Version)
|
||||
}
|
||||
|
||||
func TestMultiStore_PruningRestart(t *testing.T) {
|
||||
db := dbm.NewMemDB()
|
||||
ms := newMultiStoreWithMounts(db, pruningtypes.NewCustomPruningOptions(2, 11))
|
||||
require.NoError(t, ms.LoadLatestVersion())
|
||||
|
||||
// Commit enough to build up heights to prune, where on the next block we should
|
||||
// batch delete.
|
||||
for i := int64(0); i < 10; i++ {
|
||||
ms.Commit()
|
||||
}
|
||||
|
||||
actualHeightToPrune := ms.pruningManager.GetPruningHeight(ms.LatestVersion())
|
||||
require.Equal(t, int64(0), actualHeightToPrune)
|
||||
|
||||
// "restart"
|
||||
ms = newMultiStoreWithMounts(db, pruningtypes.NewCustomPruningOptions(2, 11))
|
||||
err := ms.LoadLatestVersion()
|
||||
require.NoError(t, err)
|
||||
|
||||
actualHeightToPrune = ms.pruningManager.GetPruningHeight(ms.LatestVersion())
|
||||
require.Equal(t, int64(0), actualHeightToPrune)
|
||||
|
||||
// commit one more block and ensure the heights have been pruned
|
||||
ms.Commit()
|
||||
|
||||
actualHeightToPrune = ms.pruningManager.GetPruningHeight(ms.LatestVersion())
|
||||
require.Equal(t, int64(8), actualHeightToPrune)
|
||||
|
||||
for v := int64(1); v <= actualHeightToPrune; v++ {
|
||||
_, err := ms.CacheMultiStoreWithVersion(v)
|
||||
require.Error(t, err, "expected error when loading height: %d", v)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUnevenStoresHeightCheck tests if loading root store correctly errors when
|
||||
// there's any module store with the wrong height
|
||||
func TestUnevenStoresHeightCheck(t *testing.T) {
|
||||
var db dbm.DB = dbm.NewMemDB()
|
||||
store := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
err := store.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
|
||||
// commit to increment store's height
|
||||
store.Commit()
|
||||
|
||||
// mount store4 to root store
|
||||
store.MountStoreWithDB(types.NewKVStoreKey("store4"), types.StoreTypeIAVL, nil)
|
||||
|
||||
// load the stores without upgrades
|
||||
err = store.LoadLatestVersion()
|
||||
require.Error(t, err)
|
||||
|
||||
// now, let's load with upgrades...
|
||||
upgrades := &types.StoreUpgrades{
|
||||
Added: []string{"store4"},
|
||||
}
|
||||
err = store.LoadLatestVersionAndUpgrade(upgrades)
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestSetInitialVersion(t *testing.T) {
|
||||
db := dbm.NewMemDB()
|
||||
multi := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
|
||||
require.NoError(t, multi.LoadLatestVersion())
|
||||
|
||||
err := multi.SetInitialVersion(5)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(5), multi.initialVersion)
|
||||
|
||||
multi.Commit()
|
||||
require.Equal(t, int64(5), multi.LastCommitID().Version)
|
||||
|
||||
ckvs := multi.GetCommitKVStore(multi.keysByName["store1"])
|
||||
iavlStore, ok := ckvs.(*iavl.Store)
|
||||
require.True(t, ok)
|
||||
require.True(t, iavlStore.VersionExists(5))
|
||||
}
|
||||
|
||||
func TestAddListenersAndListeningEnabled(t *testing.T) {
|
||||
db := dbm.NewMemDB()
|
||||
multi := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
testKey := types.NewKVStoreKey("listening_test_key")
|
||||
enabled := multi.ListeningEnabled(testKey)
|
||||
require.False(t, enabled)
|
||||
|
||||
wrongTestKey := types.NewKVStoreKey("wrong_listening_test_key")
|
||||
multi.AddListeners([]types.StoreKey{testKey})
|
||||
enabled = multi.ListeningEnabled(wrongTestKey)
|
||||
require.False(t, enabled)
|
||||
|
||||
enabled = multi.ListeningEnabled(testKey)
|
||||
require.True(t, enabled)
|
||||
}
|
||||
|
||||
func TestCacheWraps(t *testing.T) {
|
||||
db := dbm.NewMemDB()
|
||||
multi := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
|
||||
cacheWrapper := multi.CacheWrap()
|
||||
require.IsType(t, cachemulti.Store{}, cacheWrapper)
|
||||
|
||||
cacheWrappedWithTrace := multi.CacheWrapWithTrace(nil, nil)
|
||||
require.IsType(t, cachemulti.Store{}, cacheWrappedWithTrace)
|
||||
}
|
||||
|
||||
func TestTraceConcurrency(t *testing.T) {
|
||||
db := dbm.NewMemDB()
|
||||
multi := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
err := multi.LoadLatestVersion()
|
||||
require.NoError(t, err)
|
||||
|
||||
b := &bytes.Buffer{}
|
||||
key := multi.keysByName["store1"]
|
||||
tc := types.TraceContext(map[string]interface{}{"blockHeight": 64})
|
||||
|
||||
multi.SetTracer(b)
|
||||
multi.SetTracingContext(tc)
|
||||
|
||||
cms := multi.CacheMultiStore()
|
||||
store1 := cms.GetKVStore(key)
|
||||
cw := store1.CacheWrapWithTrace(b, tc)
|
||||
_ = cw
|
||||
require.NotNil(t, store1)
|
||||
|
||||
stop := make(chan struct{})
|
||||
stopW := make(chan struct{})
|
||||
|
||||
go func(stop chan struct{}) {
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
default:
|
||||
store1.Set([]byte{1}, []byte{1})
|
||||
cms.Write()
|
||||
}
|
||||
}
|
||||
}(stop)
|
||||
|
||||
go func(stop chan struct{}) {
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
default:
|
||||
multi.SetTracingContext(tc)
|
||||
}
|
||||
}
|
||||
}(stopW)
|
||||
|
||||
time.Sleep(3 * time.Second)
|
||||
stop <- struct{}{}
|
||||
stopW <- struct{}{}
|
||||
}
|
||||
|
||||
func TestCommitOrdered(t *testing.T) {
|
||||
var db dbm.DB = dbm.NewMemDB()
|
||||
multi := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
err := multi.LoadLatestVersion()
|
||||
require.Nil(t, err)
|
||||
|
||||
emptyHash := sha256.Sum256([]byte{})
|
||||
appHash := emptyHash[:]
|
||||
commitID := types.CommitID{Hash: appHash}
|
||||
checkStore(t, multi, commitID, commitID)
|
||||
|
||||
k, v := []byte("wind"), []byte("blows")
|
||||
k2, v2 := []byte("water"), []byte("flows")
|
||||
k3, v3 := []byte("fire"), []byte("burns")
|
||||
|
||||
store1 := multi.GetStoreByName("store1").(types.KVStore)
|
||||
store1.Set(k, v)
|
||||
|
||||
store2 := multi.GetStoreByName("store2").(types.KVStore)
|
||||
store2.Set(k2, v2)
|
||||
|
||||
store3 := multi.GetStoreByName("store3").(types.KVStore)
|
||||
store3.Set(k3, v3)
|
||||
|
||||
typeID := multi.Commit()
|
||||
require.Equal(t, int64(1), typeID.Version)
|
||||
|
||||
ci, err := multi.GetCommitInfo(1)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int64(1), ci.Version)
|
||||
require.Equal(t, 3, len(ci.StoreInfos))
|
||||
for i, s := range ci.StoreInfos {
|
||||
require.Equal(t, s.Name, fmt.Sprintf("store%d", i+1))
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// utils
|
||||
|
||||
var (
|
||||
testStoreKey1 = types.NewKVStoreKey("store1")
|
||||
testStoreKey2 = types.NewKVStoreKey("store2")
|
||||
testStoreKey3 = types.NewKVStoreKey("store3")
|
||||
)
|
||||
|
||||
func newMultiStoreWithMounts(db dbm.DB, pruningOpts pruningtypes.PruningOptions) *Store {
|
||||
store := NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
|
||||
store.SetPruning(pruningOpts)
|
||||
|
||||
store.MountStoreWithDB(testStoreKey1, types.StoreTypeIAVL, nil)
|
||||
store.MountStoreWithDB(testStoreKey2, types.StoreTypeIAVL, nil)
|
||||
store.MountStoreWithDB(testStoreKey3, types.StoreTypeIAVL, nil)
|
||||
|
||||
return store
|
||||
}
|
||||
|
||||
func newMultiStoreWithModifiedMounts(db dbm.DB, pruningOpts pruningtypes.PruningOptions) (*Store, *types.StoreUpgrades) {
|
||||
store := NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
|
||||
store.SetPruning(pruningOpts)
|
||||
|
||||
store.MountStoreWithDB(types.NewKVStoreKey("store1"), types.StoreTypeIAVL, nil)
|
||||
store.MountStoreWithDB(types.NewKVStoreKey("restore2"), types.StoreTypeIAVL, nil)
|
||||
store.MountStoreWithDB(types.NewKVStoreKey("store3"), types.StoreTypeIAVL, nil)
|
||||
store.MountStoreWithDB(types.NewKVStoreKey("store4"), types.StoreTypeIAVL, nil)
|
||||
|
||||
upgrades := &types.StoreUpgrades{
|
||||
Added: []string{"store4"},
|
||||
Renamed: []types.StoreRename{{
|
||||
OldKey: "store2",
|
||||
NewKey: "restore2",
|
||||
}},
|
||||
Deleted: []string{"store3"},
|
||||
}
|
||||
|
||||
return store, upgrades
|
||||
}
|
||||
|
||||
func unmountStore(rootStore *Store, storeKeyName string) {
|
||||
sk := rootStore.keysByName[storeKeyName]
|
||||
delete(rootStore.stores, sk)
|
||||
delete(rootStore.storesParams, sk)
|
||||
delete(rootStore.keysByName, storeKeyName)
|
||||
}
|
||||
|
||||
func checkStore(t *testing.T, store *Store, expect, got types.CommitID) {
|
||||
t.Helper()
|
||||
require.Equal(t, expect, got)
|
||||
require.Equal(t, expect, store.LastCommitID())
|
||||
}
|
||||
|
||||
func checkContains(tb testing.TB, info []types.StoreInfo, wanted []string) {
|
||||
tb.Helper()
|
||||
|
||||
for _, want := range wanted {
|
||||
checkHas(tb, info, want)
|
||||
}
|
||||
}
|
||||
|
||||
func checkHas(tb testing.TB, info []types.StoreInfo, want string) {
|
||||
tb.Helper()
|
||||
for _, i := range info {
|
||||
if i.Name == want {
|
||||
return
|
||||
}
|
||||
}
|
||||
tb.Fatalf("storeInfo doesn't contain %s", want)
|
||||
}
|
||||
|
||||
func getExpectedCommitID(store *Store, ver int64) types.CommitID {
|
||||
return types.CommitID{
|
||||
Version: ver,
|
||||
Hash: hashStores(store.stores),
|
||||
}
|
||||
}
|
||||
|
||||
func hashStores(stores map[types.StoreKey]types.CommitKVStore) []byte {
|
||||
m := make(map[string][]byte, len(stores))
|
||||
for key, store := range stores {
|
||||
name := key.Name()
|
||||
m[name] = types.StoreInfo{
|
||||
Name: name,
|
||||
CommitId: store.LastCommitID(),
|
||||
}.GetHash()
|
||||
}
|
||||
return sdkmaps.HashFromMap(m)
|
||||
}
|
||||
|
||||
type MockListener struct {
|
||||
stateCache []types.StoreKVPair
|
||||
}
|
||||
|
||||
func (tl *MockListener) OnWrite(storeKey types.StoreKey, key, value []byte, delete bool) error {
|
||||
tl.stateCache = append(tl.stateCache, types.StoreKVPair{
|
||||
StoreKey: storeKey.Name(),
|
||||
Key: key,
|
||||
Value: value,
|
||||
Delete: delete,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestStateListeners(t *testing.T) {
|
||||
var db dbm.DB = dbm.NewMemDB()
|
||||
ms := newMultiStoreWithMounts(db, pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
|
||||
require.Empty(t, ms.listeners)
|
||||
|
||||
ms.AddListeners([]types.StoreKey{testStoreKey1})
|
||||
require.Equal(t, 1, len(ms.listeners))
|
||||
|
||||
require.NoError(t, ms.LoadLatestVersion())
|
||||
cacheMulti := ms.CacheMultiStore()
|
||||
|
||||
store := cacheMulti.GetKVStore(testStoreKey1)
|
||||
store.Set([]byte{1}, []byte{1})
|
||||
require.Empty(t, ms.PopStateCache())
|
||||
|
||||
// writes are observed when cache store commit.
|
||||
cacheMulti.Write()
|
||||
require.Equal(t, 1, len(ms.PopStateCache()))
|
||||
|
||||
// test no listening on unobserved store
|
||||
store = cacheMulti.GetKVStore(testStoreKey2)
|
||||
store.Set([]byte{1}, []byte{1})
|
||||
require.Empty(t, ms.PopStateCache())
|
||||
|
||||
// writes are not observed when cache store commit
|
||||
cacheMulti.Write()
|
||||
require.Empty(t, ms.PopStateCache())
|
||||
}
|
||||
|
||||
type commitKVStoreStub struct {
|
||||
types.CommitKVStore
|
||||
Committed int
|
||||
}
|
||||
|
||||
func (stub *commitKVStoreStub) Commit() types.CommitID {
|
||||
commitID := stub.CommitKVStore.Commit()
|
||||
stub.Committed++
|
||||
return commitID
|
||||
}
|
||||
|
||||
func prepareStoreMap() (map[types.StoreKey]types.CommitKVStore, error) {
|
||||
var db dbm.DB = dbm.NewMemDB()
|
||||
store := NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
|
||||
store.MountStoreWithDB(types.NewKVStoreKey("iavl1"), types.StoreTypeIAVL, nil)
|
||||
store.MountStoreWithDB(types.NewKVStoreKey("iavl2"), types.StoreTypeIAVL, nil)
|
||||
store.MountStoreWithDB(types.NewTransientStoreKey("trans1"), types.StoreTypeTransient, nil)
|
||||
if err := store.LoadLatestVersion(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[types.StoreKey]types.CommitKVStore{
|
||||
testStoreKey1: &commitKVStoreStub{
|
||||
CommitKVStore: store.GetStoreByName("iavl1").(types.CommitKVStore),
|
||||
},
|
||||
testStoreKey2: &commitKVStoreStub{
|
||||
CommitKVStore: store.GetStoreByName("iavl2").(types.CommitKVStore),
|
||||
},
|
||||
testStoreKey3: &commitKVStoreStub{
|
||||
CommitKVStore: store.GetStoreByName("trans1").(types.CommitKVStore),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestCommitStores(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
committed int
|
||||
exptectCommit int
|
||||
}{
|
||||
{
|
||||
"when upgrade not get interrupted",
|
||||
0,
|
||||
1,
|
||||
},
|
||||
{
|
||||
"when upgrade get interrupted once",
|
||||
1,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"when upgrade get interrupted twice",
|
||||
2,
|
||||
0,
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
storeMap, err := prepareStoreMap()
|
||||
require.NoError(t, err)
|
||||
store := storeMap[testStoreKey1].(*commitKVStoreStub)
|
||||
for i := tc.committed; i > 0; i-- {
|
||||
store.Commit()
|
||||
}
|
||||
store.Committed = 0
|
||||
var version int64 = 1
|
||||
removalMap := map[types.StoreKey]bool{}
|
||||
res := commitStores(version, storeMap, removalMap)
|
||||
for _, s := range res.StoreInfos {
|
||||
require.Equal(t, version, s.CommitId.Version)
|
||||
}
|
||||
require.Equal(t, version, res.Version)
|
||||
require.Equal(t, tc.exptectCommit, store.Committed)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user