2018-02-21 07:04:54 -08:00
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-03-27 03:14:19 -07:00
|
|
|
"path/filepath"
|
2018-02-21 07:04:54 -08:00
|
|
|
|
|
|
|
abci "github.com/tendermint/abci/types"
|
2018-04-20 16:55:22 -07:00
|
|
|
crypto "github.com/tendermint/go-crypto"
|
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2018-02-21 07:04:54 -08:00
|
|
|
dbm "github.com/tendermint/tmlibs/db"
|
|
|
|
"github.com/tendermint/tmlibs/log"
|
|
|
|
|
|
|
|
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-04-21 19:26:46 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
2018-02-21 07:04:54 -08:00
|
|
|
)
|
|
|
|
|
2018-04-21 19:26:46 -07:00
|
|
|
// 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,
|
2018-02-21 07:04:54 -08:00
|
|
|
// in order to guarantee consistent results
|
2018-02-21 09:56:04 -08:00
|
|
|
func NewApp(rootDir string, logger log.Logger) (abci.Application, error) {
|
2018-03-27 03:14:19 -07:00
|
|
|
db, err := dbm.NewGoLevelDB("mock", filepath.Join(rootDir, "data"))
|
2018-02-21 07:04:54 -08:00
|
|
|
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)
|
|
|
|
|
|
|
|
// Set mounts for BaseApp's MultiStore.
|
|
|
|
baseApp.MountStoresIAVL(capKeyMainStore)
|
|
|
|
|
|
|
|
// Set Tx decoder
|
|
|
|
baseApp.SetTxDecoder(decodeTx)
|
|
|
|
|
|
|
|
baseApp.SetInitChainer(InitChainer(capKeyMainStore))
|
|
|
|
|
|
|
|
// Set a handler Route.
|
2018-04-02 08:13:37 -07:00
|
|
|
baseApp.Router().AddRoute("kvstore", KVStoreHandler(capKeyMainStore))
|
2018-02-21 07:04:54 -08:00
|
|
|
|
|
|
|
// Load latest version.
|
|
|
|
if err := baseApp.LoadLatestVersion(capKeyMainStore); 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 sdk.StoreKey) sdk.Handler {
|
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
|
|
|
dTx, ok := msg.(kvstoreTx)
|
|
|
|
if !ok {
|
|
|
|
panic("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{
|
|
|
|
Code: 0,
|
|
|
|
Log: fmt.Sprintf("set %s=%s", key, value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 19:40:39 -08:00
|
|
|
// basic KV structure
|
2018-02-21 07:04:54 -08:00
|
|
|
type KV struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
2018-02-28 19:40:39 -08:00
|
|
|
// What Genesis JSON is formatted as
|
2018-02-21 07:04:54 -08:00
|
|
|
type GenesisJSON struct {
|
|
|
|
Values []KV `json:"values"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitChainer returns a function that can initialize the chain
|
|
|
|
// with key/value pairs
|
|
|
|
func InitChainer(key sdk.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{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 13:12:14 -07:00
|
|
|
// AppGenState can be passed into InitCmd, returns a static string of a few
|
2018-04-20 16:55:22 -07:00
|
|
|
// key-values that can be parsed by InitChainer
|
2018-04-25 13:12:14 -07:00
|
|
|
func AppGenState(_ *wire.Codec, _ []json.RawMessage) (appState json.RawMessage, err error) {
|
2018-04-21 19:26:46 -07:00
|
|
|
appState = json.RawMessage(`{
|
2018-02-21 07:04:54 -08:00
|
|
|
"values": [
|
|
|
|
{
|
|
|
|
"key": "hello",
|
|
|
|
"value": "goodbye"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "foo",
|
|
|
|
"value": "bar"
|
|
|
|
}
|
|
|
|
]
|
2018-04-21 19:26:46 -07:00
|
|
|
}`)
|
2018-04-20 16:55:22 -07:00
|
|
|
return
|
2018-02-21 07:04:54 -08:00
|
|
|
}
|
2018-04-25 13:12:14 -07:00
|
|
|
|
|
|
|
// Return a validator, not much else
|
|
|
|
func AppGenTx(_ *wire.Codec, pk crypto.PubKey) (
|
|
|
|
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) {
|
|
|
|
|
|
|
|
validator = tmtypes.GenesisValidator{
|
|
|
|
PubKey: pk,
|
|
|
|
Power: 10,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|