2017-12-12 20:13:51 -08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-02-02 07:22:49 -08:00
|
|
|
abci "github.com/tendermint/abci/types"
|
2017-12-12 20:13:51 -08:00
|
|
|
dbm "github.com/tendermint/tmlibs/db"
|
|
|
|
"github.com/tendermint/tmlibs/merkle"
|
2018-01-15 17:10:46 -08:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2017-12-12 20:13:51 -08:00
|
|
|
)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
const useDebugDB = false
|
|
|
|
|
2017-12-12 20:13:51 -08:00
|
|
|
func TestMultistoreCommitLoad(t *testing.T) {
|
2018-04-13 02:04:31 -07:00
|
|
|
var db dbm.DB = dbm.NewMemDB()
|
|
|
|
if useDebugDB {
|
|
|
|
db = dbm.NewDebugDB("CMS", db)
|
|
|
|
}
|
2018-01-22 05:44:24 -08:00
|
|
|
store := newMultiStoreWithMounts(db)
|
2017-12-12 20:13:51 -08:00
|
|
|
err := store.LoadLatestVersion()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// New store has empty last commit.
|
2017-12-12 20:13:51 -08:00
|
|
|
commitID := CommitID{}
|
|
|
|
checkStore(t, store, commitID, commitID)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Make sure we can get stores by name.
|
2018-02-02 06:43:03 -08:00
|
|
|
s1 := store.getStoreByName("store1")
|
2018-01-30 06:30:25 -08:00
|
|
|
assert.NotNil(t, s1)
|
2018-02-02 06:43:03 -08:00
|
|
|
s3 := store.getStoreByName("store3")
|
2018-01-30 06:30:25 -08:00
|
|
|
assert.NotNil(t, s3)
|
2018-02-02 06:43:03 -08:00
|
|
|
s77 := store.getStoreByName("store77")
|
2018-01-30 06:30:25 -08:00
|
|
|
assert.Nil(t, s77)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Make a few commits and check them.
|
2017-12-12 20:13:51 -08:00
|
|
|
nCommits := int64(3)
|
|
|
|
for i := int64(0); i < nCommits; i++ {
|
|
|
|
commitID = store.Commit()
|
|
|
|
expectedCommitID := getExpectedCommitID(store, i+1)
|
|
|
|
checkStore(t, store, expectedCommitID, commitID)
|
|
|
|
}
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Load the latest multistore again and check version.
|
2018-01-22 05:44:24 -08:00
|
|
|
store = newMultiStoreWithMounts(db)
|
2017-12-12 20:13:51 -08:00
|
|
|
err = store.LoadLatestVersion()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
commitID = getExpectedCommitID(store, nCommits)
|
|
|
|
checkStore(t, store, commitID, commitID)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Commit and check version.
|
2017-12-12 20:13:51 -08:00
|
|
|
commitID = store.Commit()
|
|
|
|
expectedCommitID := getExpectedCommitID(store, nCommits+1)
|
|
|
|
checkStore(t, store, expectedCommitID, commitID)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Load an older multistore and check version.
|
2017-12-12 20:13:51 -08:00
|
|
|
ver := nCommits - 1
|
2018-01-22 05:44:24 -08:00
|
|
|
store = newMultiStoreWithMounts(db)
|
2017-12-12 20:13:51 -08:00
|
|
|
err = store.LoadVersion(ver)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
commitID = getExpectedCommitID(store, ver)
|
|
|
|
checkStore(t, store, commitID, commitID)
|
|
|
|
|
|
|
|
// XXX: commit this older version
|
|
|
|
commitID = store.Commit()
|
|
|
|
expectedCommitID = getExpectedCommitID(store, ver+1)
|
|
|
|
checkStore(t, store, expectedCommitID, commitID)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// XXX: confirm old commit is overwritten and we have rolled back
|
|
|
|
// LatestVersion
|
2018-01-22 05:44:24 -08:00
|
|
|
store = newMultiStoreWithMounts(db)
|
2017-12-12 20:13:51 -08:00
|
|
|
err = store.LoadLatestVersion()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
commitID = getExpectedCommitID(store, ver+1)
|
|
|
|
checkStore(t, store, commitID, commitID)
|
|
|
|
}
|
|
|
|
|
2018-01-30 06:30:25 -08:00
|
|
|
func TestParsePath(t *testing.T) {
|
|
|
|
_, _, err := parsePath("foo")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
store, subpath, err := parsePath("/foo")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, store, "foo")
|
|
|
|
assert.Equal(t, subpath, "")
|
|
|
|
|
|
|
|
store, subpath, err = parsePath("/fizz/bang/baz")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, store, "fizz")
|
|
|
|
assert.Equal(t, subpath, "/bang/baz")
|
|
|
|
|
|
|
|
substore, subsubpath, err := parsePath(subpath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, substore, "bang")
|
|
|
|
assert.Equal(t, subsubpath, "/baz")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-02 07:22:49 -08:00
|
|
|
func TestMultiStoreQuery(t *testing.T) {
|
|
|
|
db := dbm.NewMemDB()
|
|
|
|
multi := newMultiStoreWithMounts(db)
|
|
|
|
err := multi.LoadLatestVersion()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
k, v := []byte("wind"), []byte("blows")
|
|
|
|
k2, v2 := []byte("water"), []byte("flows")
|
|
|
|
// v3 := []byte("is cold")
|
|
|
|
|
|
|
|
cid := multi.Commit()
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Make sure we can get by name.
|
2018-02-02 07:22:49 -08:00
|
|
|
garbage := multi.getStoreByName("bad-name")
|
|
|
|
assert.Nil(t, garbage)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Set and commit data in one store.
|
2018-02-02 07:22:49 -08:00
|
|
|
store1 := multi.getStoreByName("store1").(KVStore)
|
|
|
|
store1.Set(k, v)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// ... and another.
|
2018-02-02 07:22:49 -08:00
|
|
|
store2 := multi.getStoreByName("store2").(KVStore)
|
|
|
|
store2.Set(k2, v2)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Commit the multistore.
|
2018-02-02 07:22:49 -08:00
|
|
|
cid = multi.Commit()
|
|
|
|
ver := cid.Version
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Test bad path.
|
2018-02-02 07:22:49 -08:00
|
|
|
query := abci.RequestQuery{Path: "/key", Data: k, Height: ver}
|
|
|
|
qres := multi.Query(query)
|
|
|
|
assert.Equal(t, uint32(sdk.CodeUnknownRequest), qres.Code)
|
|
|
|
|
|
|
|
query.Path = "h897fy32890rf63296r92"
|
|
|
|
qres = multi.Query(query)
|
|
|
|
assert.Equal(t, uint32(sdk.CodeUnknownRequest), qres.Code)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Test invalid store name.
|
2018-02-02 07:22:49 -08:00
|
|
|
query.Path = "/garbage/key"
|
|
|
|
qres = multi.Query(query)
|
|
|
|
assert.Equal(t, uint32(sdk.CodeUnknownRequest), qres.Code)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Test valid query with data.
|
2018-02-02 07:22:49 -08:00
|
|
|
query.Path = "/store1/key"
|
|
|
|
qres = multi.Query(query)
|
|
|
|
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
|
|
|
|
assert.Equal(t, v, qres.Value)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Test valid but empty query.
|
2018-02-02 07:22:49 -08:00
|
|
|
query.Path = "/store2/key"
|
|
|
|
query.Prove = true
|
|
|
|
qres = multi.Query(query)
|
|
|
|
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
|
|
|
|
assert.Nil(t, qres.Value)
|
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
// Test store2 data.
|
2018-02-02 07:22:49 -08:00
|
|
|
query.Data = k2
|
|
|
|
qres = multi.Query(query)
|
|
|
|
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
|
|
|
|
assert.Equal(t, v2, qres.Value)
|
|
|
|
}
|
|
|
|
|
2017-12-12 20:13:51 -08:00
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
// utils
|
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
func newMultiStoreWithMounts(db dbm.DB) *rootMultiStore {
|
2018-01-06 12:53:31 -08:00
|
|
|
store := NewCommitMultiStore(db)
|
2018-01-22 05:44:24 -08:00
|
|
|
store.MountStoreWithDB(
|
|
|
|
sdk.NewKVStoreKey("store1"), sdk.StoreTypeIAVL, db)
|
|
|
|
store.MountStoreWithDB(
|
|
|
|
sdk.NewKVStoreKey("store2"), sdk.StoreTypeIAVL, db)
|
|
|
|
store.MountStoreWithDB(
|
|
|
|
sdk.NewKVStoreKey("store3"), sdk.StoreTypeIAVL, db)
|
2017-12-12 20:13:51 -08:00
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkStore(t *testing.T, store *rootMultiStore, expect, got CommitID) {
|
|
|
|
assert.Equal(t, expect, got)
|
|
|
|
assert.Equal(t, expect, store.LastCommitID())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getExpectedCommitID(store *rootMultiStore, ver int64) CommitID {
|
|
|
|
return CommitID{
|
|
|
|
Version: ver,
|
2018-01-22 05:44:24 -08:00
|
|
|
Hash: hashStores(store.stores),
|
2017-12-12 20:13:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
func hashStores(stores map[StoreKey]CommitStore) []byte {
|
2018-01-21 19:21:17 -08:00
|
|
|
m := make(map[string]merkle.Hasher, len(stores))
|
2018-01-15 17:10:46 -08:00
|
|
|
for key, store := range stores {
|
|
|
|
name := key.Name()
|
2018-01-22 05:44:24 -08:00
|
|
|
m[name] = storeInfo{
|
2017-12-12 20:13:51 -08:00
|
|
|
Name: name,
|
2018-01-22 05:44:24 -08:00
|
|
|
Core: storeCore{
|
|
|
|
CommitID: store.LastCommitID(),
|
|
|
|
// StoreType: store.GetStoreType(),
|
2017-12-12 20:13:51 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return merkle.SimpleHashFromMap(m)
|
|
|
|
}
|