<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9362 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [X] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [X] added `!` to the type prefix if API or client breaking change - [X] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [X] provided a link to the relevant issue or specification - [X] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [X] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [X] added a changelog entry to `CHANGELOG.md` - [X] included comments for [documenting Go code](https://blog.golang.org/godoc) - [X] updated the relevant documentation or specification - [X] reviewed "Files changed" and left comments if necessary - [X] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
152 lines
4.2 KiB
Go
152 lines
4.2 KiB
Go
package mock
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
"github.com/tendermint/tendermint/types"
|
|
|
|
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/tx"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/middleware"
|
|
)
|
|
|
|
func testTxHandler(options middleware.TxHandlerOptions) tx.Handler {
|
|
return middleware.ComposeMiddlewares(
|
|
middleware.NewRunMsgsTxHandler(options.MsgServiceRouter, options.LegacyRouter),
|
|
middleware.GasTxMiddleware,
|
|
middleware.RecoveryTxMiddleware,
|
|
middleware.NewIndexEventsTxMiddleware(options.IndexEvents),
|
|
)
|
|
}
|
|
|
|
// NewApp creates a simple mock kvstore app for testing. It should work
|
|
// similar to a real app. Make sure rootDir is empty before running the test,
|
|
// in order to guarantee consistent results
|
|
func NewApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
|
db, err := sdk.NewLevelDB("mock", filepath.Join(rootDir, "data"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Capabilities key to access the main KVStore.
|
|
capKeyMainStore := sdk.NewKVStoreKey("main")
|
|
|
|
// Create BaseApp.
|
|
baseApp := bam.NewBaseApp("kvstore", logger, db, decodeTx)
|
|
|
|
// Set mounts for BaseApp's MultiStore.
|
|
baseApp.MountStores(capKeyMainStore)
|
|
|
|
baseApp.SetInitChainer(InitChainer(capKeyMainStore))
|
|
|
|
// Set a Route.
|
|
encCfg := simapp.MakeTestEncodingConfig()
|
|
legacyRouter := middleware.NewLegacyRouter()
|
|
// We're adding a test legacy route here, which accesses the kvstore
|
|
// and simply sets the Msg's key/value pair in the kvstore.
|
|
legacyRouter.AddRoute(sdk.NewRoute("kvstore", KVStoreHandler(capKeyMainStore)))
|
|
txHandler := testTxHandler(
|
|
middleware.TxHandlerOptions{
|
|
LegacyRouter: legacyRouter,
|
|
MsgServiceRouter: middleware.NewMsgServiceRouter(encCfg.InterfaceRegistry),
|
|
},
|
|
)
|
|
baseApp.SetTxHandler(txHandler)
|
|
|
|
// Load latest version.
|
|
if err := baseApp.LoadLatestVersion(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return baseApp, nil
|
|
}
|
|
|
|
// KVStoreHandler is a simple handler that takes kvstoreTx and writes
|
|
// them to the db
|
|
func KVStoreHandler(storeKey storetypes.StoreKey) sdk.Handler {
|
|
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
|
|
dTx, ok := msg.(kvstoreTx)
|
|
if !ok {
|
|
return nil, errors.New("KVStoreHandler should only receive kvstoreTx")
|
|
}
|
|
|
|
// tx is already unmarshalled
|
|
key := dTx.key
|
|
value := dTx.value
|
|
|
|
store := ctx.KVStore(storeKey)
|
|
store.Set(key, value)
|
|
|
|
return &sdk.Result{
|
|
Log: fmt.Sprintf("set %s=%s", key, value),
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
// basic KV structure
|
|
type KV struct {
|
|
Key string `json:"key"`
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
// What Genesis JSON is formatted as
|
|
type GenesisJSON struct {
|
|
Values []KV `json:"values"`
|
|
}
|
|
|
|
// InitChainer returns a function that can initialize the chain
|
|
// with key/value pairs
|
|
func InitChainer(key storetypes.StoreKey) func(sdk.Context, abci.RequestInitChain) abci.ResponseInitChain {
|
|
return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
|
|
stateJSON := req.AppStateBytes
|
|
|
|
genesisState := new(GenesisJSON)
|
|
err := json.Unmarshal(stateJSON, genesisState)
|
|
if err != nil {
|
|
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
|
|
// return sdk.ErrGenesisParse("").TraceCause(err, "")
|
|
}
|
|
|
|
for _, val := range genesisState.Values {
|
|
store := ctx.KVStore(key)
|
|
store.Set([]byte(val.Key), []byte(val.Value))
|
|
}
|
|
return abci.ResponseInitChain{}
|
|
}
|
|
}
|
|
|
|
// AppGenState can be passed into InitCmd, returns a static string of a few
|
|
// key-values that can be parsed by InitChainer
|
|
func AppGenState(_ *codec.LegacyAmino, _ types.GenesisDoc, _ []json.RawMessage) (appState json.
|
|
RawMessage, err error) {
|
|
appState = json.RawMessage(`{
|
|
"values": [
|
|
{
|
|
"key": "hello",
|
|
"value": "goodbye"
|
|
},
|
|
{
|
|
"key": "foo",
|
|
"value": "bar"
|
|
}
|
|
]
|
|
}`)
|
|
return
|
|
}
|
|
|
|
// AppGenStateEmpty returns an empty transaction state for mocking.
|
|
func AppGenStateEmpty(_ *codec.LegacyAmino, _ types.GenesisDoc, _ []json.RawMessage) (
|
|
appState json.RawMessage, err error) {
|
|
appState = json.RawMessage(``)
|
|
return
|
|
}
|