cosmos-sdk/store/rootmultistore.go

423 lines
10 KiB
Go
Raw Normal View History

2017-10-31 13:45:57 -07:00
package store
import (
"fmt"
2018-01-30 06:20:38 -08:00
"strings"
2017-10-31 13:45:57 -07:00
2018-01-30 06:20:38 -08:00
"golang.org/x/crypto/ripemd160"
abci "github.com/tendermint/abci/types"
2017-10-31 13:45:57 -07:00
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/merkle"
sdk "github.com/cosmos/cosmos-sdk/types"
2017-10-31 13:45:57 -07:00
)
const (
latestVersionKey = "s/latest"
commitInfoKeyFmt = "s/%d" // s/<version>
2017-10-31 13:45:57 -07:00
)
2017-12-03 22:55:15 -08:00
// rootMultiStore is composed of many CommitStores.
2017-12-01 08:52:54 -08:00
// Name contrasts with cacheMultiStore which is for cache-wrapping
// other MultiStores.
2017-10-31 13:45:57 -07:00
// Implements MultiStore.
2017-11-29 07:57:47 -08:00
type rootMultiStore struct {
2017-10-31 13:45:57 -07:00
db dbm.DB
2017-12-03 23:17:10 -08:00
lastCommitID CommitID
storesParams map[StoreKey]storeParams
stores map[StoreKey]CommitStore
keysByName map[string]StoreKey
2017-10-31 13:45:57 -07:00
}
2017-12-26 17:04:48 -08:00
var _ CommitMultiStore = (*rootMultiStore)(nil)
2018-01-30 06:20:38 -08:00
var _ Queryable = (*rootMultiStore)(nil)
2017-12-26 17:04:48 -08:00
2018-04-18 21:49:24 -07:00
// nolint
2018-01-03 17:20:21 -08:00
func NewCommitMultiStore(db dbm.DB) *rootMultiStore {
2017-11-29 07:57:47 -08:00
return &rootMultiStore{
2017-10-31 13:45:57 -07:00
db: db,
storesParams: make(map[StoreKey]storeParams),
stores: make(map[StoreKey]CommitStore),
keysByName: make(map[string]StoreKey),
2017-10-31 13:45:57 -07:00
}
}
// Implements Store.
func (rs *rootMultiStore) GetStoreType() StoreType {
return sdk.StoreTypeMulti
}
2017-12-26 17:04:48 -08:00
// Implements CommitMultiStore.
func (rs *rootMultiStore) MountStoreWithDB(key StoreKey, typ StoreType, db dbm.DB) {
if key == nil {
panic("MountIAVLStore() key cannot be nil")
}
if _, ok := rs.storesParams[key]; ok {
panic(fmt.Sprintf("rootMultiStore duplicate store key %v", key))
}
rs.storesParams[key] = storeParams{
2018-04-12 15:56:41 -07:00
key: key,
typ: typ,
2018-04-12 15:56:41 -07:00
db: db,
2017-10-31 13:45:57 -07:00
}
rs.keysByName[key.Name()] = key
2017-10-31 13:45:57 -07:00
}
2017-12-26 17:04:48 -08:00
// Implements CommitMultiStore.
func (rs *rootMultiStore) GetCommitStore(key StoreKey) CommitStore {
return rs.stores[key]
2017-12-26 17:04:48 -08:00
}
2018-01-31 19:15:32 -08:00
// Implements CommitMultiStore.
func (rs *rootMultiStore) GetCommitKVStore(key StoreKey) CommitKVStore {
return rs.stores[key].(CommitKVStore)
}
2017-12-26 17:04:48 -08:00
// Implements CommitMultiStore.
2017-11-29 07:57:47 -08:00
func (rs *rootMultiStore) LoadLatestVersion() error {
2017-12-01 08:52:54 -08:00
ver := getLatestVersion(rs.db)
2017-12-09 12:35:51 -08:00
return rs.LoadVersion(ver)
2017-10-31 13:45:57 -07:00
}
2017-12-26 17:04:48 -08:00
// Implements CommitMultiStore.
2017-11-29 07:57:47 -08:00
func (rs *rootMultiStore) LoadVersion(ver int64) error {
2017-10-31 13:45:57 -07:00
// Special logic for version 0
if ver == 0 {
for key, storeParams := range rs.storesParams {
id := CommitID{}
store, err := rs.loadCommitStoreFromParams(id, storeParams)
2017-10-31 13:45:57 -07:00
if err != nil {
2017-11-29 07:57:47 -08:00
return fmt.Errorf("Failed to load rootMultiStore: %v", err)
2017-10-31 13:45:57 -07:00
}
rs.stores[key] = store
2017-10-31 13:45:57 -07:00
}
rs.lastCommitID = CommitID{}
2017-10-31 13:45:57 -07:00
return nil
}
// Otherwise, version is 1 or greater
// Get commitInfo
cInfo, err := getCommitInfo(rs.db, ver)
2017-12-09 12:35:51 -08:00
if err != nil {
return err
}
2017-10-31 13:45:57 -07:00
// Load each Store
var newStores = make(map[StoreKey]CommitStore)
for _, storeInfo := range cInfo.StoreInfos {
key, commitID := rs.nameToKey(storeInfo.Name), storeInfo.Core.CommitID
storeParams := rs.storesParams[key]
store, err := rs.loadCommitStoreFromParams(commitID, storeParams)
2017-10-31 13:45:57 -07:00
if err != nil {
2017-11-29 07:57:47 -08:00
return fmt.Errorf("Failed to load rootMultiStore: %v", err)
2017-10-31 13:45:57 -07:00
}
newStores[key] = store
2017-10-31 13:45:57 -07:00
}
2017-12-03 22:55:15 -08:00
// If any CommitStoreLoaders were not used, return error.
for key := range rs.storesParams {
if _, ok := newStores[key]; !ok {
2018-01-12 13:48:54 -08:00
return fmt.Errorf("Unused CommitStoreLoader: %v", key)
2017-10-31 13:45:57 -07:00
}
}
2017-11-29 07:57:47 -08:00
// Success.
rs.lastCommitID = cInfo.CommitID()
rs.stores = newStores
2017-10-31 13:45:57 -07:00
return nil
}
2017-12-26 17:04:48 -08:00
//----------------------------------------
// +CommitStore
2018-01-26 04:19:33 -08:00
// Implements Committer/CommitStore.
func (rs *rootMultiStore) LastCommitID() CommitID {
return rs.lastCommitID
}
// Implements Committer/CommitStore.
2017-12-01 08:52:54 -08:00
func (rs *rootMultiStore) Commit() CommitID {
// Commit stores.
version := rs.lastCommitID.Version + 1
commitInfo := commitStores(version, rs.stores)
2017-12-01 08:52:54 -08:00
// Need to update atomically.
2017-12-01 08:52:54 -08:00
batch := rs.db.NewBatch()
setCommitInfo(batch, version, commitInfo)
setLatestVersion(batch, version)
2017-10-31 13:45:57 -07:00
batch.Write()
// Prepare for next version.
2017-12-03 23:17:10 -08:00
commitID := CommitID{
2017-12-01 08:52:54 -08:00
Version: version,
Hash: commitInfo.Hash(),
2017-12-01 08:52:54 -08:00
}
2017-12-03 23:17:10 -08:00
rs.lastCommitID = commitID
return commitID
2017-12-03 22:55:15 -08:00
}
2017-12-01 08:52:54 -08:00
2018-01-26 04:19:33 -08:00
// Implements CacheWrapper/Store/CommitStore.
2017-12-04 00:56:25 -08:00
func (rs *rootMultiStore) CacheWrap() CacheWrap {
return rs.CacheMultiStore().(CacheWrap)
2017-12-01 08:52:54 -08:00
}
2017-12-26 17:04:48 -08:00
//----------------------------------------
// +MultiStore
// Implements MultiStore.
2017-11-29 07:57:47 -08:00
func (rs *rootMultiStore) CacheMultiStore() CacheMultiStore {
2017-12-04 00:23:10 -08:00
return newCacheMultiStoreFromRMS(rs)
2017-10-31 13:45:57 -07:00
}
2017-12-26 17:04:48 -08:00
// Implements MultiStore.
func (rs *rootMultiStore) GetStore(key StoreKey) Store {
return rs.stores[key]
2017-10-31 13:45:57 -07:00
}
2017-12-26 17:04:48 -08:00
// Implements MultiStore.
func (rs *rootMultiStore) GetKVStore(key StoreKey) KVStore {
return rs.stores[key].(KVStore)
}
2018-05-11 08:46:50 -07:00
// Implements MultiStore.
func (rs *rootMultiStore) GetKVStoreWithGas(meter sdk.GasMeter, key StoreKey) KVStore {
return NewGasKVStore(meter, rs.GetKVStore(key))
}
// getStoreByName will first convert the original name to
// a special key, before looking up the CommitStore.
// This is not exposed to the extensions (which will need the
// StoreKey), but is useful in main, and particularly app.Query,
// in order to convert human strings into CommitStores.
func (rs *rootMultiStore) getStoreByName(name string) Store {
key := rs.keysByName[name]
if key == nil {
return nil
}
return rs.stores[key]
}
2018-01-30 06:20:38 -08:00
//---------------------- Query ------------------
2018-02-06 14:18:22 -08:00
// Query calls substore.Query with the same `req` where `req.Path` is
// modified to remove the substore prefix.
// Ie. `req.Path` here is `/<substore>/<path>`, and trimmed to `/<path>` for the substore.
// TODO: add proof for `multistore -> substore`.
2018-01-30 06:20:38 -08:00
func (rs *rootMultiStore) Query(req abci.RequestQuery) abci.ResponseQuery {
// Query just routes this to a substore.
path := req.Path
storeName, subpath, err := parsePath(path)
if err != nil {
return err.QueryResult()
2018-01-30 06:20:38 -08:00
}
store := rs.getStoreByName(storeName)
2018-01-30 06:20:38 -08:00
if store == nil {
msg := fmt.Sprintf("no such store: %s", storeName)
return sdk.ErrUnknownRequest(msg).QueryResult()
2018-01-30 06:20:38 -08:00
}
2018-02-06 14:18:22 -08:00
queryable, ok := store.(Queryable)
2018-01-30 06:20:38 -08:00
if !ok {
msg := fmt.Sprintf("store %s doesn't support queries", storeName)
return sdk.ErrUnknownRequest(msg).QueryResult()
2018-01-30 06:20:38 -08:00
}
// trim the path and make the query
req.Path = subpath
2018-02-06 14:18:22 -08:00
res := queryable.Query(req)
2018-01-30 06:20:38 -08:00
return res
}
// parsePath expects a format like /<storeName>[/<subpath>]
// Must start with /, subpath may be empty
// Returns error if it doesn't start with /
func parsePath(path string) (storeName string, subpath string, err sdk.Error) {
if !strings.HasPrefix(path, "/") {
err = sdk.ErrUnknownRequest(fmt.Sprintf("invalid path: %s", path))
return
}
paths := strings.SplitN(path[1:], "/", 2)
storeName = paths[0]
if len(paths) == 2 {
2018-01-30 06:30:25 -08:00
subpath = "/" + paths[1]
2018-01-30 06:20:38 -08:00
}
return
}
//----------------------------------------
func (rs *rootMultiStore) loadCommitStoreFromParams(id CommitID, params storeParams) (store CommitStore, err error) {
2018-04-12 15:56:41 -07:00
var db dbm.DB
if params.db != nil {
db = dbm.NewPrefixDB(params.db, []byte("s/_/"))
2018-04-12 15:56:41 -07:00
} else {
2018-04-12 16:22:44 -07:00
db = dbm.NewPrefixDB(rs.db, []byte("s/k:"+params.key.Name()+"/"))
}
switch params.typ {
case sdk.StoreTypeMulti:
panic("recursive MultiStores not yet supported")
// TODO: id?
// return NewCommitMultiStore(db, id)
case sdk.StoreTypeIAVL:
store, err = LoadIAVLStore(db, id)
return
case sdk.StoreTypeDB:
panic("dbm.DB is not a CommitStore")
default:
panic(fmt.Sprintf("unrecognized store type %v", params.typ))
}
}
func (rs *rootMultiStore) nameToKey(name string) StoreKey {
2018-04-18 21:49:24 -07:00
for key := range rs.storesParams {
if key.Name() == name {
return key
}
}
panic("Unknown name " + name)
}
//----------------------------------------
// storeParams
type storeParams struct {
2018-04-12 15:56:41 -07:00
key StoreKey
db dbm.DB
typ StoreType
2017-10-31 13:45:57 -07:00
}
//----------------------------------------
// commitInfo
2017-10-31 13:45:57 -07:00
// NOTE: Keep commitInfo a simple immutable struct.
type commitInfo struct {
2017-10-31 13:45:57 -07:00
2017-12-01 08:52:54 -08:00
// Version
Version int64
2017-10-31 13:45:57 -07:00
// Store info for
StoreInfos []storeInfo
2017-10-31 13:45:57 -07:00
}
// Hash returns the simple merkle root hash of the stores sorted by name.
func (ci commitInfo) Hash() []byte {
// TODO cache to ci.hash []byte
m := make(map[string]merkle.Hasher, len(ci.StoreInfos))
for _, storeInfo := range ci.StoreInfos {
m[storeInfo.Name] = storeInfo
2017-10-31 13:45:57 -07:00
}
2017-12-01 08:52:54 -08:00
return merkle.SimpleHashFromMap(m)
2017-10-31 13:45:57 -07:00
}
func (ci commitInfo) CommitID() CommitID {
2017-12-01 08:52:54 -08:00
return CommitID{
Version: ci.Version,
Hash: ci.Hash(),
2017-10-31 13:45:57 -07:00
}
}
2017-11-29 07:57:47 -08:00
2017-12-01 08:52:54 -08:00
//----------------------------------------
// storeInfo
2017-12-01 08:52:54 -08:00
// storeInfo contains the name and core reference for an
// underlying store. It is the leaf of the rootMultiStores top
// level simple merkle tree.
type storeInfo struct {
2017-12-01 08:52:54 -08:00
Name string
Core storeCore
2017-11-29 07:57:47 -08:00
}
type storeCore struct {
// StoreType StoreType
2017-12-01 08:52:54 -08:00
CommitID CommitID
// ... maybe add more state
2017-11-29 07:57:47 -08:00
}
2018-01-21 19:21:17 -08:00
// Implements merkle.Hasher.
func (si storeInfo) Hash() []byte {
2018-01-21 19:21:17 -08:00
// Doesn't write Name, since merkle.SimpleHashFromMap() will
// include them via the keys.
bz, _ := cdc.MarshalBinary(si.Core) // Does not error
2017-12-01 08:52:54 -08:00
hasher := ripemd160.New()
2018-01-21 19:21:17 -08:00
hasher.Write(bz)
2017-12-01 08:52:54 -08:00
return hasher.Sum(nil)
2017-11-29 07:57:47 -08:00
}
2017-12-01 08:52:54 -08:00
//----------------------------------------
2017-12-26 17:04:48 -08:00
// Misc.
2017-12-01 08:52:54 -08:00
func getLatestVersion(db dbm.DB) int64 {
var latest int64
2017-12-09 12:35:51 -08:00
latestBytes := db.Get([]byte(latestVersionKey))
2017-12-01 08:52:54 -08:00
if latestBytes == nil {
return 0
}
2018-01-21 19:21:17 -08:00
err := cdc.UnmarshalBinary(latestBytes, &latest)
2017-12-01 08:52:54 -08:00
if err != nil {
panic(err)
}
return latest
2017-11-29 07:57:47 -08:00
}
// Set the latest version.
func setLatestVersion(batch dbm.Batch, version int64) {
2018-01-21 19:21:17 -08:00
latestBytes, _ := cdc.MarshalBinary(version) // Does not error
batch.Set([]byte(latestVersionKey), latestBytes)
}
// Commits each store and returns a new commitInfo.
func commitStores(version int64, storeMap map[StoreKey]CommitStore) commitInfo {
storeInfos := make([]storeInfo, 0, len(storeMap))
for key, store := range storeMap {
// Commit
commitID := store.Commit()
// Record CommitID
si := storeInfo{}
si.Name = key.Name()
si.Core.CommitID = commitID
// si.Core.StoreType = store.GetStoreType()
storeInfos = append(storeInfos, si)
}
ci := commitInfo{
Version: version,
StoreInfos: storeInfos,
}
return ci
}
// Gets commitInfo from disk.
func getCommitInfo(db dbm.DB, ver int64) (commitInfo, error) {
// Get from DB.
cInfoKey := fmt.Sprintf(commitInfoKeyFmt, ver)
cInfoBytes := db.Get([]byte(cInfoKey))
if cInfoBytes == nil {
return commitInfo{}, fmt.Errorf("Failed to get rootMultiStore: no data")
}
// Parse bytes.
var cInfo commitInfo
err := cdc.UnmarshalBinary(cInfoBytes, &cInfo)
if err != nil {
return commitInfo{}, fmt.Errorf("Failed to get rootMultiStore: %v", err)
}
return cInfo, nil
}
// Set a commitInfo for given version.
func setCommitInfo(batch dbm.Batch, version int64, cInfo commitInfo) {
cInfoBytes, err := cdc.MarshalBinary(cInfo)
if err != nil {
panic(err)
}
cInfoKey := fmt.Sprintf(commitInfoKeyFmt, version)
batch.Set([]byte(cInfoKey), cInfoBytes)
}