Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Store Refactor #2344

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (app *BaseApp) Name() string {
// SetCommitMultiStoreTracer sets the store tracer on the BaseApp's underlying
// CommitMultiStore.
func (app *BaseApp) SetCommitMultiStoreTracer(w io.Writer) {
app.cms.WithTracer(w)
app.cms.GetTracer().Writer = w
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial API call was bad and that's my fault. Can we just do app.cms.SetTracer(w)?

}

// Register the next available codespace through the baseapp's codespacer, starting from a default
Expand All @@ -124,31 +124,31 @@ func (app *BaseApp) RegisterCodespace(codespace sdk.CodespaceType) sdk.Codespace
}

// Mount IAVL stores to the provided keys in the BaseApp multistore
func (app *BaseApp) MountStoresIAVL(keys ...*sdk.KVStoreKey) {
func (app *BaseApp) MountStoresIAVL(keys ...sdk.KVStoreKey) {
for _, key := range keys {
app.MountStore(key, sdk.StoreTypeIAVL)
app.MountStore(key)
}
}

// Mount stores to the provided keys in the BaseApp multistore
func (app *BaseApp) MountStoresTransient(keys ...*sdk.TransientStoreKey) {
for _, key := range keys {
app.MountStore(key, sdk.StoreTypeTransient)
app.MountStore(key)
}
}

// Mount a store to the provided key in the BaseApp multistore, using a specified DB
func (app *BaseApp) MountStoreWithDB(key sdk.StoreKey, typ sdk.StoreType, db dbm.DB) {
app.cms.MountStoreWithDB(key, typ, db)
func (app *BaseApp) MountStoreWithDB(key sdk.KVStoreKey, db dbm.DB) {
app.cms.MountKVStoreWithDB(key, db)
}

// Mount a store to the provided key in the BaseApp multistore, using the default DB
func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) {
app.cms.MountStoreWithDB(key, typ, nil)
func (app *BaseApp) MountStore(key sdk.KVStoreKey) {
app.cms.MountKVStoreWithDB(key, nil)
}

// load latest application version
func (app *BaseApp) LoadLatestVersion(mainKey sdk.StoreKey) error {
func (app *BaseApp) LoadLatestVersion(mainKey sdk.KVStoreKey) error {
err := app.cms.LoadLatestVersion()
if err != nil {
return err
Expand All @@ -157,8 +157,8 @@ func (app *BaseApp) LoadLatestVersion(mainKey sdk.StoreKey) error {
}

// load application version
func (app *BaseApp) LoadVersion(version int64, mainKey sdk.StoreKey) error {
err := app.cms.LoadVersion(version)
func (app *BaseApp) LoadVersion(version int64, mainKey sdk.KVStoreKey) error {
err := app.cms.LoadMultiStoreVersion(version)
if err != nil {
return err
}
Expand All @@ -176,7 +176,7 @@ func (app *BaseApp) LastBlockHeight() int64 {
}

// initializes the remaining logic from app.cms
func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {
func (app *BaseApp) initFromStore(mainKey sdk.KVStoreKey) error {
// main store should exist.
// TODO: we don't actually need the main store here
main := app.cms.GetKVStore(mainKey)
Expand Down Expand Up @@ -208,19 +208,19 @@ type state struct {
}

func (st *state) CacheMultiStore() sdk.CacheMultiStore {
return st.ms.CacheMultiStore()
return st.ms.CacheWrap()
}

func (app *BaseApp) setCheckState(header abci.Header) {
ms := app.cms.CacheMultiStore()
ms := app.cms.CacheWrap()
app.checkState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, true, app.Logger).WithMinimumFees(app.minimumFees),
}
}

func (app *BaseApp) setDeliverState(header abci.Header) {
ms := app.cms.CacheMultiStore()
ms := app.cms.CacheWrap()
app.deliverState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, false, app.Logger),
Expand Down Expand Up @@ -297,7 +297,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
path := splitPath(req.Path)
if len(path) == 0 {
msg := "no query path provided"
return sdk.ErrUnknownRequest(msg).QueryResult()
return store.ErrUnknownRequest(msg).QueryResult()
}
switch path[0] {
// "/app" prefix for special application queries
Expand All @@ -312,7 +312,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
}

msg := "unknown query path"
return sdk.ErrUnknownRequest(msg).QueryResult()
return store.ErrUnknownRequest(msg).QueryResult()
}

func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) (res abci.ResponseQuery) {
Expand Down Expand Up @@ -344,15 +344,15 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) (res abc
}
}
msg := "Expected second parameter to be either simulate or version, neither was present"
return sdk.ErrUnknownRequest(msg).QueryResult()
return store.ErrUnknownRequest(msg).QueryResult()
}

func handleQueryStore(app *BaseApp, path []string, req abci.RequestQuery) (res abci.ResponseQuery) {
// "/store" prefix for store queries
queryable, ok := app.cms.(sdk.Queryable)
if !ok {
msg := "multistore doesn't support queries"
return sdk.ErrUnknownRequest(msg).QueryResult()
return store.ErrUnknownRequest(msg).QueryResult()
}
req.Path = "/" + strings.Join(path[1:], "/")
return queryable.Query(req)
Expand All @@ -373,26 +373,26 @@ func handleQueryP2P(app *BaseApp, path []string, req abci.RequestQuery) (res abc
}
} else {
msg := "Expected second parameter to be filter"
return sdk.ErrUnknownRequest(msg).QueryResult()
return store.ErrUnknownRequest(msg).QueryResult()
}
}

msg := "Expected path is p2p filter <addr|pubkey> <parameter>"
return sdk.ErrUnknownRequest(msg).QueryResult()
return store.ErrUnknownRequest(msg).QueryResult()
}

func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) (res abci.ResponseQuery) {
// path[0] should be "custom" because "/custom" prefix is required for keeper queries.
// the queryRouter routes using path[1]. For example, in the path "custom/gov/proposal", queryRouter routes using "gov"
if len(path) < 2 || path[1] == "" {
return sdk.ErrUnknownRequest("No route for custom query specified").QueryResult()
return store.ErrUnknownRequest("No route for custom query specified").QueryResult()
}
querier := app.queryRouter.Route(path[1])
if querier == nil {
return sdk.ErrUnknownRequest(fmt.Sprintf("no custom querier found for route %s", path[1])).QueryResult()
return store.ErrUnknownRequest(fmt.Sprintf("no custom querier found for route %s", path[1])).QueryResult()
}

ctx := sdk.NewContext(app.cms.CacheMultiStore(), app.checkState.ctx.BlockHeader(), true, app.Logger).
ctx := sdk.NewContext(app.cms.CacheWrap(), app.checkState.ctx.BlockHeader(), true, app.Logger).
WithMinimumFees(app.minimumFees)
// Passes the rest of the path as an argument to the querier.
// For example, in the path "custom/gov/proposal/test", the gov querier gets []string{"proposal", "test"} as the path
Expand All @@ -411,9 +411,10 @@ func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) (res

// BeginBlock implements the ABCI application interface.
func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
if app.cms.TracingEnabled() {
app.cms.ResetTraceContext()
app.cms.WithTracingContext(sdk.TraceContext(
tracer := app.cms.GetTracer()
if tracer.Enabled() {
tracer.ResetContext()
tracer.SetContext(sdk.TraceContext(
map[string]interface{}{"blockHeight": req.Header.Height},
))
}
Expand Down Expand Up @@ -644,10 +645,11 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
// Keep the state in a transient CacheWrap in case processing the messages
// fails.
msCache = getState(app, mode).CacheMultiStore()
if msCache.TracingEnabled() {
msCache = msCache.WithTracingContext(sdk.TraceContext(
tracer := msCache.GetTracer()
if tracer.Enabled() {
tracer.SetContext(sdk.TraceContext(
map[string]interface{}{"txHash": cmn.HexBytes(tmhash.Sum(txBytes)).String()},
)).(sdk.CacheMultiStore)
))
}

ctx = ctx.WithMultiStore(msCache)
Expand All @@ -664,8 +666,9 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk

// EndBlock implements the ABCI application interface.
func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
if app.deliverState.ms.TracingEnabled() {
app.deliverState.ms = app.deliverState.ms.ResetTraceContext().(sdk.CacheMultiStore)
tracer := app.deliverState.ms.GetTracer()
if tracer.Enabled() {
tracer.ResetContext()
}

if app.endBlocker != nil {
Expand Down
4 changes: 2 additions & 2 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func testTxDecoder(cdc *codec.Codec) sdk.TxDecoder {
}
}

func anteHandlerTxTest(t *testing.T, capKey *sdk.KVStoreKey, storeKey []byte) sdk.AnteHandler {
func anteHandlerTxTest(t *testing.T, capKey sdk.KVStoreKey, storeKey []byte) sdk.AnteHandler {
return func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, res sdk.Result, abort bool) {
store := ctx.KVStore(capKey)
msgCounter := tx.(txTest).Counter
Expand All @@ -375,7 +375,7 @@ func anteHandlerTxTest(t *testing.T, capKey *sdk.KVStoreKey, storeKey []byte) sd
}
}

func handlerMsgCounter(t *testing.T, capKey *sdk.KVStoreKey, deliverKey []byte) sdk.Handler {
func handlerMsgCounter(t *testing.T, capKey sdk.KVStoreKey, deliverKey []byte) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
store := ctx.KVStore(capKey)
var msgCount int64
Expand Down
12 changes: 6 additions & 6 deletions client/context/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
tmliteErr "github.com/tendermint/tendermint/lite/errors"
Expand Down Expand Up @@ -209,23 +209,23 @@ func (ctx CLIContext) verifyProof(_ string, resp abci.ResponseQuery) error {
return err
}

var multiStoreProof store.MultiStoreProof
var multiStoreProof rootmulti.Proof
cdc := codec.New()

err = cdc.UnmarshalBinary(resp.Proof, &multiStoreProof)
if err != nil {
return errors.Wrap(err, "failed to unmarshalBinary rangeProof")
}

// verify the substore commit hash against trusted appHash
substoreCommitHash, err := store.VerifyMultiStoreCommitInfo(
// Verify the substore commit hash against trusted appHash
substoreCommitHash, err := rootmulti.VerifyCommitInfo(
multiStoreProof.StoreName, multiStoreProof.StoreInfos, commit.Header.AppHash,
)
if err != nil {
return errors.Wrap(err, "failed in verifying the proof against appHash")
}

err = store.VerifyRangeProof(resp.Key, resp.Value, substoreCommitHash, &multiStoreProof.RangeProof)
err = rootmulti.VerifyRangeProof(resp.Key, resp.Value, substoreCommitHash, &multiStoreProof.RangeProof)
if err != nil {
return errors.Wrap(err, "failed in the range proof verification")
}
Expand All @@ -252,7 +252,7 @@ func isQueryStoreWithProof(path string) bool {
return false
}

if store.RequireProof("/" + paths[2]) {
if rootmulti.RequireProof("/" + paths[2]) {
return true
}

Expand Down
18 changes: 9 additions & 9 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ type GaiaApp struct {
cdc *codec.Codec

// keys to access the substores
keyMain *sdk.KVStoreKey
keyAccount *sdk.KVStoreKey
keyStake *sdk.KVStoreKey
keyMain *sdk.IAVLStoreKey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhmmm, why did we rename these? This assumes IAVL usage, although it probably won't change for Gaia.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want us to have this key... an IAVLStore is a KVStore... no need to create key types for specific types of KVStores. In the future we may have other store types like HeapStores.

keyAccount *sdk.IAVLStoreKey
keyStake *sdk.IAVLStoreKey
tkeyStake *sdk.TransientStoreKey
keySlashing *sdk.KVStoreKey
keyMint *sdk.KVStoreKey
keyDistr *sdk.KVStoreKey
keySlashing *sdk.IAVLStoreKey
keyMint *sdk.IAVLStoreKey
keyDistr *sdk.IAVLStoreKey
tkeyDistr *sdk.TransientStoreKey
keyGov *sdk.KVStoreKey
keyFeeCollection *sdk.KVStoreKey
keyParams *sdk.KVStoreKey
keyGov *sdk.IAVLStoreKey
keyFeeCollection *sdk.IAVLStoreKey
keyParams *sdk.IAVLStoreKey
tkeyParams *sdk.TransientStoreKey

// Manage getting and setting accounts
Expand Down
12 changes: 6 additions & 6 deletions cmd/gaia/cmd/gaiadebug/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ type GaiaApp struct {
cdc *codec.Codec

// keys to access the substores
keyMain *sdk.KVStoreKey
keyAccount *sdk.KVStoreKey
keyStake *sdk.KVStoreKey
keyMain *sdk.IAVLStoreKey
keyAccount *sdk.IAVLStoreKey
keyStake *sdk.IAVLStoreKey
tkeyStake *sdk.TransientStoreKey
keySlashing *sdk.KVStoreKey
keyParams *sdk.KVStoreKey
keySlashing *sdk.IAVLStoreKey
keyParams *sdk.IAVLStoreKey
tkeyParams *sdk.TransientStoreKey

// Manage getting and setting accounts
Expand Down Expand Up @@ -189,7 +189,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp
app.SetEndBlocker(app.EndBlocker)
app.SetAnteHandler(auth.NewAnteHandler(app.accountKeeper, app.feeCollectionKeeper))
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyStake, app.keySlashing, app.keyParams)
app.MountStore(app.tkeyParams, sdk.StoreTypeTransient)
app.MountStore(app.tkeyParams)
err := app.LoadLatestVersion(app.keyMain)
if err != nil {
cmn.Exit(err.Error())
Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/core/examples/app1.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (msg MsgSend) Tags() sdk.Tags {
// Handle MsgSend.
// NOTE: msg.From, msg.To, and msg.Amount were already validated
// in ValidateBasic().
func handleMsgSend(key *sdk.KVStoreKey) sdk.Handler {
func handleMsgSend(key sdk.KVStoreKey) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
sendMsg, ok := msg.(MsgSend)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/core/examples/app2.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (msg MsgIssue) Tags() sdk.Tags {
// Handler for the message

// Handle MsgIssue.
func handleMsgIssue(keyIssue *sdk.KVStoreKey, keyAcc *sdk.KVStoreKey) sdk.Handler {
func handleMsgIssue(keyIssue sdk.KVStoreKey, keyAcc sdk.KVStoreKey) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
issueMsg, ok := msg.(MsgIssue)
if !ok {
Expand Down
6 changes: 3 additions & 3 deletions examples/basecoin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ type BasecoinApp struct {
cdc *codec.Codec

// keys to access the multistore
keyMain *sdk.KVStoreKey
keyAccount *sdk.KVStoreKey
keyIBC *sdk.KVStoreKey
keyMain *sdk.IAVLStoreKey
keyAccount *sdk.IAVLStoreKey
keyIBC *sdk.IAVLStoreKey

// manage getting and setting accounts
accountKeeper auth.AccountKeeper
Expand Down
10 changes: 5 additions & 5 deletions examples/democoin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ type DemocoinApp struct {
cdc *codec.Codec

// keys to access the substores
capKeyMainStore *sdk.KVStoreKey
capKeyAccountStore *sdk.KVStoreKey
capKeyPowStore *sdk.KVStoreKey
capKeyIBCStore *sdk.KVStoreKey
capKeyStakingStore *sdk.KVStoreKey
capKeyMainStore *sdk.IAVLStoreKey
capKeyAccountStore *sdk.IAVLStoreKey
capKeyPowStore *sdk.IAVLStoreKey
capKeyIBCStore *sdk.IAVLStoreKey
capKeyStakingStore *sdk.IAVLStoreKey

// keepers
feeCollectionKeeper auth.FeeCollectionKeeper
Expand Down
7 changes: 4 additions & 3 deletions examples/democoin/x/assoc/validator_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/examples/democoin/mock"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func defaultContext(key sdk.StoreKey) sdk.Context {
func defaultContext(key sdk.KVStoreKey) sdk.Context {
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db)
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
cms.MountKVStoreWithDB(key, db)
cms.LoadLatestVersion()
ctx := sdk.NewContext(cms, abci.Header{}, false, nil)
return ctx
Expand All @@ -36,7 +37,7 @@ func TestValidatorSet(t *testing.T) {
{addr2, sdk.NewDec(2)},
}}

valset := NewValidatorSet(codec.New(), ctx.KVStore(key).Prefix([]byte("assoc")), base, 1, 5)
valset := NewValidatorSet(codec.New(), prefix.NewStore(ctx.KVStore(key), []byte("assoc")), base, 1, 5)

require.Equal(t, base.Validator(ctx, addr1), valset.Validator(ctx, addr1))
require.Equal(t, base.Validator(ctx, addr2), valset.Validator(ctx, addr2))
Expand Down
Loading