2018-01-20 20:08:00 -08:00
|
|
|
package baseapp
|
2017-10-16 10:21:43 -07:00
|
|
|
|
|
|
|
import (
|
2017-12-01 09:10:17 -08:00
|
|
|
"fmt"
|
2018-01-26 04:19:33 -08:00
|
|
|
"runtime/debug"
|
2018-05-09 13:25:13 -07:00
|
|
|
"strings"
|
2017-12-01 09:10:17 -08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-02-14 08:16:06 -08:00
|
|
|
|
2017-10-16 10:21:43 -07:00
|
|
|
abci "github.com/tendermint/abci/types"
|
2017-12-26 17:04:48 -08:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2018-01-22 05:44:24 -08:00
|
|
|
dbm "github.com/tendermint/tmlibs/db"
|
2017-12-01 09:10:17 -08:00
|
|
|
"github.com/tendermint/tmlibs/log"
|
2017-10-16 10:21:43 -07:00
|
|
|
|
2018-01-22 05:44:24 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
2018-01-12 13:59:19 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-04-26 19:59:30 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/wire"
|
2018-05-23 19:26:54 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2017-10-16 10:21:43 -07:00
|
|
|
)
|
|
|
|
|
2018-02-27 19:23:56 -08:00
|
|
|
// Key to store the header in the DB itself.
|
|
|
|
// Use the db directly instead of a store to avoid
|
|
|
|
// conflicts with handlers writing to the store
|
|
|
|
// and to avoid affecting the Merkle root.
|
|
|
|
var dbHeaderKey = []byte("header")
|
2017-12-01 09:10:17 -08:00
|
|
|
|
2018-05-15 17:18:25 -07:00
|
|
|
// Enum mode for app.runTx
|
|
|
|
type runTxMode uint8
|
2018-05-15 17:06:17 -07:00
|
|
|
|
|
|
|
const (
|
|
|
|
// Check a transaction
|
2018-05-15 17:18:25 -07:00
|
|
|
runTxModeCheck runTxMode = iota
|
2018-05-15 17:06:17 -07:00
|
|
|
// Simulate a transaction
|
2018-05-15 17:18:25 -07:00
|
|
|
runTxModeSimulate runTxMode = iota
|
2018-05-15 17:06:17 -07:00
|
|
|
// Deliver a transaction
|
2018-05-15 17:18:25 -07:00
|
|
|
runTxModeDeliver runTxMode = iota
|
2018-05-15 17:06:17 -07:00
|
|
|
)
|
|
|
|
|
2018-02-04 12:20:49 -08:00
|
|
|
// The ABCI application
|
2018-01-20 20:08:00 -08:00
|
|
|
type BaseApp struct {
|
2018-02-17 15:10:55 -08:00
|
|
|
// initialized on creation
|
2018-04-17 19:16:21 -07:00
|
|
|
Logger log.Logger
|
|
|
|
name string // application name from abci.Info
|
2018-05-11 13:06:53 -07:00
|
|
|
cdc *wire.Codec // Amino codec
|
2018-04-17 19:16:21 -07:00
|
|
|
db dbm.DB // common DB backend
|
|
|
|
cms sdk.CommitMultiStore // Main (uncached) state
|
|
|
|
router Router // handle any kind of message
|
|
|
|
codespacer *sdk.Codespacer // handle module codespacing
|
2018-02-17 15:10:55 -08:00
|
|
|
|
2018-02-17 16:24:40 -08:00
|
|
|
// must be set
|
|
|
|
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx
|
|
|
|
anteHandler sdk.AnteHandler // ante handler for fee and auth
|
|
|
|
|
2018-02-17 15:10:55 -08:00
|
|
|
// may be nil
|
2018-05-15 07:00:17 -07:00
|
|
|
initChainer sdk.InitChainer // initialize state with validators and state blob
|
|
|
|
beginBlocker sdk.BeginBlocker // logic to run before any txs
|
|
|
|
endBlocker sdk.EndBlocker // logic to run after all txs, and to determine valset changes
|
|
|
|
addrPeerFilter sdk.PeerFilter // filter peers by address and port
|
|
|
|
pubkeyPeerFilter sdk.PeerFilter // filter peers by public key
|
2018-01-12 11:49:53 -08:00
|
|
|
|
|
|
|
//--------------------
|
|
|
|
// Volatile
|
2018-02-27 20:46:27 -08:00
|
|
|
// checkState is set on initialization and reset on Commit.
|
|
|
|
// deliverState is set in InitChain and BeginBlock and cleared on Commit.
|
|
|
|
// See methods setCheckState and setDeliverState.
|
2018-02-25 12:59:11 -08:00
|
|
|
// .valUpdates accumulate in DeliverTx and are reset in BeginBlock.
|
2018-02-27 20:46:27 -08:00
|
|
|
// QUESTION: should we put valUpdates in the deliverState.ctx?
|
2018-05-23 13:25:56 -07:00
|
|
|
checkState *state // for CheckTx
|
|
|
|
deliverState *state // for DeliverTx
|
|
|
|
valUpdates []abci.Validator // cached validator changes from DeliverTx
|
|
|
|
absentValidators [][]byte // absent validators from begin block
|
2017-10-16 10:21:43 -07:00
|
|
|
}
|
|
|
|
|
2018-02-25 12:59:11 -08:00
|
|
|
var _ abci.Application = (*BaseApp)(nil)
|
2017-10-16 10:21:43 -07:00
|
|
|
|
2018-02-04 12:20:49 -08:00
|
|
|
// Create and name new BaseApp
|
2018-03-28 05:33:48 -07:00
|
|
|
// NOTE: The db is used to store the version number for now.
|
2018-05-15 17:31:52 -07:00
|
|
|
func NewBaseApp(name string, cdc *wire.Codec, logger log.Logger, db dbm.DB) *BaseApp {
|
2018-04-17 19:16:21 -07:00
|
|
|
app := &BaseApp{
|
|
|
|
Logger: logger,
|
|
|
|
name: name,
|
2018-05-11 13:06:53 -07:00
|
|
|
cdc: cdc,
|
2018-04-17 19:16:21 -07:00
|
|
|
db: db,
|
|
|
|
cms: store.NewCommitMultiStore(db),
|
|
|
|
router: NewRouter(),
|
|
|
|
codespacer: sdk.NewCodespacer(),
|
2018-04-26 19:59:30 -07:00
|
|
|
txDecoder: defaultTxDecoder(cdc),
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
2018-04-17 19:16:21 -07:00
|
|
|
// Register the undefined & root codespaces, which should not be used by any modules
|
|
|
|
app.codespacer.RegisterOrPanic(sdk.CodespaceUndefined)
|
|
|
|
app.codespacer.RegisterOrPanic(sdk.CodespaceRoot)
|
|
|
|
return app
|
2017-12-19 21:04:40 -08:00
|
|
|
}
|
|
|
|
|
2018-02-04 12:20:49 -08:00
|
|
|
// BaseApp Name
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) Name() string {
|
2017-12-21 23:30:44 -08:00
|
|
|
return app.name
|
|
|
|
}
|
|
|
|
|
2018-04-17 19:16:21 -07:00
|
|
|
// Register the next available codespace through the baseapp's codespacer, starting from a default
|
|
|
|
func (app *BaseApp) RegisterCodespace(codespace sdk.CodespaceType) sdk.CodespaceType {
|
|
|
|
return app.codespacer.RegisterNext(codespace)
|
|
|
|
}
|
|
|
|
|
2018-02-14 17:09:00 -08:00
|
|
|
// Mount a store to the provided key in the BaseApp multistore
|
|
|
|
func (app *BaseApp) MountStoresIAVL(keys ...*sdk.KVStoreKey) {
|
|
|
|
for _, key := range keys {
|
|
|
|
app.MountStore(key, sdk.StoreTypeIAVL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 03:44:20 -07:00
|
|
|
// Mount a store to the provided key in the BaseApp multistore, using a specified DB
|
2018-03-29 11:44:56 -07:00
|
|
|
func (app *BaseApp) MountStoreWithDB(key sdk.StoreKey, typ sdk.StoreType, db dbm.DB) {
|
2018-03-28 05:33:48 -07:00
|
|
|
app.cms.MountStoreWithDB(key, typ, db)
|
2018-01-24 11:47:51 -08:00
|
|
|
}
|
|
|
|
|
2018-04-13 03:44:20 -07:00
|
|
|
// Mount a store to the provided key in the BaseApp multistore, using the default DB
|
2018-01-22 05:44:24 -08:00
|
|
|
func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) {
|
2018-04-13 03:44:20 -07:00
|
|
|
app.cms.MountStoreWithDB(key, typ, nil)
|
2018-01-24 11:47:51 -08:00
|
|
|
}
|
|
|
|
|
2018-04-26 19:59:30 -07:00
|
|
|
// Set the txDecoder function
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) {
|
2018-01-12 11:49:53 -08:00
|
|
|
app.txDecoder = txDecoder
|
2017-12-19 21:04:40 -08:00
|
|
|
}
|
2018-04-26 19:59:30 -07:00
|
|
|
|
|
|
|
// default custom logic for transaction decoding
|
|
|
|
func defaultTxDecoder(cdc *wire.Codec) sdk.TxDecoder {
|
|
|
|
return func(txBytes []byte) (sdk.Tx, sdk.Error) {
|
2018-05-23 19:26:54 -07:00
|
|
|
var tx = auth.StdTx{}
|
2018-04-26 19:59:30 -07:00
|
|
|
|
|
|
|
if len(txBytes) == 0 {
|
|
|
|
return nil, sdk.ErrTxDecode("txBytes are empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// StdTx.Msg is an interface. The concrete types
|
|
|
|
// are registered by MakeTxCodec
|
|
|
|
err := cdc.UnmarshalBinary(txBytes, &tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrTxDecode("").Trace(err.Error())
|
|
|
|
}
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint - Set functions
|
2018-02-16 17:15:38 -08:00
|
|
|
func (app *BaseApp) SetInitChainer(initChainer sdk.InitChainer) {
|
|
|
|
app.initChainer = initChainer
|
2018-02-02 17:09:20 -08:00
|
|
|
}
|
2018-02-18 10:48:36 -08:00
|
|
|
func (app *BaseApp) SetBeginBlocker(beginBlocker sdk.BeginBlocker) {
|
|
|
|
app.beginBlocker = beginBlocker
|
|
|
|
}
|
|
|
|
func (app *BaseApp) SetEndBlocker(endBlocker sdk.EndBlocker) {
|
|
|
|
app.endBlocker = endBlocker
|
|
|
|
}
|
2018-02-14 17:09:00 -08:00
|
|
|
func (app *BaseApp) SetAnteHandler(ah sdk.AnteHandler) {
|
|
|
|
app.anteHandler = ah
|
2018-01-03 17:20:21 -08:00
|
|
|
}
|
2018-05-15 07:00:17 -07:00
|
|
|
func (app *BaseApp) SetAddrPeerFilter(pf sdk.PeerFilter) {
|
|
|
|
app.addrPeerFilter = pf
|
|
|
|
}
|
|
|
|
func (app *BaseApp) SetPubKeyPeerFilter(pf sdk.PeerFilter) {
|
|
|
|
app.pubkeyPeerFilter = pf
|
|
|
|
}
|
2018-02-14 17:09:00 -08:00
|
|
|
func (app *BaseApp) Router() Router { return app.router }
|
2017-12-01 09:10:17 -08:00
|
|
|
|
2018-02-14 08:16:06 -08:00
|
|
|
// load latest application version
|
2018-01-22 05:44:24 -08:00
|
|
|
func (app *BaseApp) LoadLatestVersion(mainKey sdk.StoreKey) error {
|
2018-01-26 04:19:33 -08:00
|
|
|
app.cms.LoadLatestVersion()
|
2018-01-12 13:59:19 -08:00
|
|
|
return app.initFromStore(mainKey)
|
2017-12-19 21:04:40 -08:00
|
|
|
}
|
|
|
|
|
2018-02-14 08:16:06 -08:00
|
|
|
// load application version
|
2018-01-22 05:44:24 -08:00
|
|
|
func (app *BaseApp) LoadVersion(version int64, mainKey sdk.StoreKey) error {
|
2018-01-26 04:19:33 -08:00
|
|
|
app.cms.LoadVersion(version)
|
2018-01-12 13:59:19 -08:00
|
|
|
return app.initFromStore(mainKey)
|
2017-12-20 17:34:51 -08:00
|
|
|
}
|
|
|
|
|
2018-02-14 08:16:06 -08:00
|
|
|
// the last CommitID of the multistore
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) LastCommitID() sdk.CommitID {
|
2018-01-26 04:19:33 -08:00
|
|
|
return app.cms.LastCommitID()
|
2017-12-26 17:04:48 -08:00
|
|
|
}
|
|
|
|
|
2018-02-14 08:16:06 -08:00
|
|
|
// the last commited block height
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) LastBlockHeight() int64 {
|
2018-01-26 04:19:33 -08:00
|
|
|
return app.cms.LastCommitID().Version
|
2017-12-26 17:04:48 -08:00
|
|
|
}
|
|
|
|
|
2018-02-14 08:16:06 -08:00
|
|
|
// initializes the remaining logic from app.cms
|
2018-01-22 05:44:24 -08:00
|
|
|
func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {
|
2017-12-01 09:10:17 -08:00
|
|
|
|
2018-02-14 08:16:06 -08:00
|
|
|
// main store should exist.
|
2018-02-27 19:23:56 -08:00
|
|
|
// TODO: we don't actually need the main store here
|
|
|
|
main := app.cms.GetKVStore(mainKey)
|
2018-01-12 13:59:19 -08:00
|
|
|
if main == nil {
|
2018-01-20 20:08:00 -08:00
|
|
|
return errors.New("BaseApp expects MultiStore with 'main' KVStore")
|
2017-12-19 21:04:40 -08:00
|
|
|
}
|
|
|
|
|
2018-02-27 20:07:54 -08:00
|
|
|
// XXX: Do we really need the header? What does it have that we want
|
|
|
|
// here that's not already in the CommitID ? If an app wants to have it,
|
|
|
|
// they can do so in their BeginBlocker. If we force it in baseapp,
|
|
|
|
// then either we force the AppHash to change with every block (since the header
|
|
|
|
// will be in the merkle store) or we can't write the state and the header to the
|
|
|
|
// db atomically without doing some surgery on the store interfaces ...
|
|
|
|
|
2018-02-27 19:23:56 -08:00
|
|
|
// if we've committed before, we expect <dbHeaderKey> to exist in the db
|
2018-02-27 20:07:54 -08:00
|
|
|
/*
|
|
|
|
var lastCommitID = app.cms.LastCommitID()
|
|
|
|
var header abci.Header
|
|
|
|
|
|
|
|
if !lastCommitID.IsZero() {
|
|
|
|
headerBytes := app.db.Get(dbHeaderKey)
|
|
|
|
if len(headerBytes) == 0 {
|
|
|
|
errStr := fmt.Sprintf("Version > 0 but missing key %s", dbHeaderKey)
|
|
|
|
return errors.New(errStr)
|
|
|
|
}
|
|
|
|
err := proto.Unmarshal(headerBytes, &header)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to parse Header")
|
|
|
|
}
|
|
|
|
lastVersion := lastCommitID.Version
|
|
|
|
if header.Height != lastVersion {
|
|
|
|
errStr := fmt.Sprintf("Expected db://%s.Height %v but got %v", dbHeaderKey, lastVersion, header.Height)
|
|
|
|
return errors.New(errStr)
|
|
|
|
}
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
2018-02-27 20:07:54 -08:00
|
|
|
*/
|
2017-12-20 17:34:51 -08:00
|
|
|
|
2018-02-17 15:10:55 -08:00
|
|
|
// initialize Check state
|
2018-02-27 20:46:27 -08:00
|
|
|
app.setCheckState(abci.Header{})
|
2017-12-20 17:34:51 -08:00
|
|
|
|
|
|
|
return nil
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
|
2018-02-17 15:10:55 -08:00
|
|
|
// NewContext returns a new Context with the correct store, the given header, and nil txBytes.
|
|
|
|
func (app *BaseApp) NewContext(isCheckTx bool, header abci.Header) sdk.Context {
|
|
|
|
if isCheckTx {
|
2018-05-28 17:26:17 -07:00
|
|
|
return sdk.NewContext(app.checkState.ms, header, true, nil, app.Logger)
|
2018-02-27 20:46:27 -08:00
|
|
|
}
|
2018-05-28 17:26:17 -07:00
|
|
|
return sdk.NewContext(app.deliverState.ms, header, false, nil, app.Logger)
|
2018-02-27 20:46:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type state struct {
|
|
|
|
ms sdk.CacheMultiStore
|
|
|
|
ctx sdk.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *state) CacheMultiStore() sdk.CacheMultiStore {
|
|
|
|
return st.ms.CacheMultiStore()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *BaseApp) setCheckState(header abci.Header) {
|
|
|
|
ms := app.cms.CacheMultiStore()
|
|
|
|
app.checkState = &state{
|
|
|
|
ms: ms,
|
2018-05-28 17:26:17 -07:00
|
|
|
ctx: sdk.NewContext(ms, header, true, nil, app.Logger),
|
2018-02-27 20:46:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *BaseApp) setDeliverState(header abci.Header) {
|
|
|
|
ms := app.cms.CacheMultiStore()
|
|
|
|
app.deliverState = &state{
|
|
|
|
ms: ms,
|
2018-05-28 17:26:17 -07:00
|
|
|
ctx: sdk.NewContext(ms, header, false, nil, app.Logger),
|
2018-02-17 15:10:55 -08:00
|
|
|
}
|
2018-02-16 17:15:38 -08:00
|
|
|
}
|
|
|
|
|
2018-04-26 19:59:30 -07:00
|
|
|
//______________________________________________________________________________
|
2018-04-27 04:38:11 -07:00
|
|
|
|
2018-02-14 08:16:06 -08:00
|
|
|
// ABCI
|
2017-12-01 09:10:17 -08:00
|
|
|
|
2018-02-04 12:20:49 -08:00
|
|
|
// Implements ABCI
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo {
|
2018-01-26 04:19:33 -08:00
|
|
|
lastCommitID := app.cms.LastCommitID()
|
2017-12-14 06:28:37 -08:00
|
|
|
|
2017-12-21 20:19:44 -08:00
|
|
|
return abci.ResponseInfo{
|
2017-12-21 23:30:44 -08:00
|
|
|
Data: app.name,
|
2017-12-21 20:19:44 -08:00
|
|
|
LastBlockHeight: lastCommitID.Version,
|
|
|
|
LastBlockAppHash: lastCommitID.Hash,
|
2017-10-16 10:21:43 -07:00
|
|
|
}
|
2017-12-21 20:19:44 -08:00
|
|
|
}
|
2017-12-14 06:28:37 -08:00
|
|
|
|
2018-02-04 12:20:49 -08:00
|
|
|
// Implements ABCI
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOption) {
|
2017-12-21 23:30:44 -08:00
|
|
|
// TODO: Implement
|
|
|
|
return
|
2017-12-21 20:19:44 -08:00
|
|
|
}
|
|
|
|
|
2018-02-04 12:20:49 -08:00
|
|
|
// Implements ABCI
|
2018-02-16 17:58:51 -08:00
|
|
|
// InitChain runs the initialization logic directly on the CommitMultiStore and commits it.
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) {
|
2018-02-16 17:15:38 -08:00
|
|
|
if app.initChainer == nil {
|
2018-02-15 06:35:46 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-27 20:46:27 -08:00
|
|
|
// Initialize the deliver state and run initChain
|
|
|
|
app.setDeliverState(abci.Header{})
|
|
|
|
app.initChainer(app.deliverState.ctx, req) // no error
|
2018-02-16 17:58:51 -08:00
|
|
|
|
2018-02-27 20:07:54 -08:00
|
|
|
// NOTE: we don't commit, but BeginBlock for block 1
|
2018-02-27 20:46:27 -08:00
|
|
|
// starts from this deliverState
|
2017-12-21 23:30:44 -08:00
|
|
|
return
|
2017-12-21 20:19:44 -08:00
|
|
|
}
|
|
|
|
|
2018-05-15 07:00:17 -07:00
|
|
|
// Filter peers by address / port
|
|
|
|
func (app *BaseApp) FilterPeerByAddrPort(info string) abci.ResponseQuery {
|
|
|
|
if app.addrPeerFilter != nil {
|
|
|
|
return app.addrPeerFilter(info)
|
|
|
|
}
|
|
|
|
return abci.ResponseQuery{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter peers by public key
|
|
|
|
func (app *BaseApp) FilterPeerByPubKey(info string) abci.ResponseQuery {
|
|
|
|
if app.pubkeyPeerFilter != nil {
|
|
|
|
return app.pubkeyPeerFilter(info)
|
|
|
|
}
|
|
|
|
return abci.ResponseQuery{}
|
|
|
|
}
|
|
|
|
|
2018-01-24 11:47:51 -08:00
|
|
|
// Implements ABCI.
|
2018-01-30 11:01:25 -08:00
|
|
|
// Delegates to CommitMultiStore if it implements Queryable
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
|
2018-05-15 17:06:17 -07:00
|
|
|
path := strings.Split(req.Path, "/")
|
|
|
|
// first element is empty string
|
|
|
|
if len(path) > 0 && path[0] == "" {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
2018-05-10 11:20:34 -07:00
|
|
|
// "/app" prefix for special application queries
|
2018-05-15 17:06:17 -07:00
|
|
|
if len(path) >= 2 && path[0] == "app" {
|
2018-05-09 13:25:13 -07:00
|
|
|
var result sdk.Result
|
2018-05-15 17:06:17 -07:00
|
|
|
switch path[1] {
|
|
|
|
case "simulate":
|
2018-05-09 13:25:13 -07:00
|
|
|
txBytes := req.Data
|
|
|
|
tx, err := app.txDecoder(txBytes)
|
|
|
|
if err != nil {
|
|
|
|
result = err.Result()
|
2018-05-11 11:02:05 -07:00
|
|
|
} else {
|
|
|
|
result = app.Simulate(tx)
|
2018-05-09 13:25:13 -07:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
result = sdk.ErrUnknownRequest(fmt.Sprintf("Unknown query: %s", path)).Result()
|
|
|
|
}
|
2018-05-11 13:06:53 -07:00
|
|
|
value := app.cdc.MustMarshalBinary(result)
|
2018-05-09 13:25:13 -07:00
|
|
|
return abci.ResponseQuery{
|
|
|
|
Code: uint32(sdk.ABCICodeOK),
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 11:20:34 -07:00
|
|
|
// "/store" prefix for store queries
|
2018-05-15 17:06:17 -07:00
|
|
|
if len(path) >= 1 && path[0] == "store" {
|
2018-05-10 11:20:34 -07:00
|
|
|
queryable, ok := app.cms.(sdk.Queryable)
|
|
|
|
if !ok {
|
|
|
|
msg := "multistore doesn't support queries"
|
|
|
|
return sdk.ErrUnknownRequest(msg).QueryResult()
|
|
|
|
}
|
2018-05-15 17:06:17 -07:00
|
|
|
req.Path = "/" + strings.Join(path[1:], "/")
|
2018-05-10 11:20:34 -07:00
|
|
|
return queryable.Query(req)
|
|
|
|
}
|
2018-05-15 07:00:17 -07:00
|
|
|
// "/p2p" prefix for p2p queries
|
2018-05-15 17:06:17 -07:00
|
|
|
if len(path) >= 4 && path[0] == "p2p" {
|
|
|
|
if path[1] == "filter" {
|
|
|
|
if path[2] == "addr" {
|
|
|
|
return app.FilterPeerByAddrPort(path[3])
|
2018-05-15 07:00:17 -07:00
|
|
|
}
|
2018-05-15 17:06:17 -07:00
|
|
|
if path[2] == "pubkey" {
|
|
|
|
return app.FilterPeerByPubKey(path[3])
|
2018-05-15 07:00:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 11:20:34 -07:00
|
|
|
msg := "unknown query path"
|
|
|
|
return sdk.ErrUnknownRequest(msg).QueryResult()
|
2017-12-21 20:19:44 -08:00
|
|
|
}
|
|
|
|
|
2018-02-04 12:20:49 -08:00
|
|
|
// Implements ABCI
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
|
2018-02-27 20:07:54 -08:00
|
|
|
// Initialize the DeliverTx state.
|
2018-02-27 20:46:27 -08:00
|
|
|
// If this is the first block, it should already
|
|
|
|
// be initialized in InitChain. It may also be nil
|
|
|
|
// if this is a test and InitChain was never called.
|
|
|
|
if app.deliverState == nil {
|
|
|
|
app.setDeliverState(req.Header)
|
2018-02-27 20:07:54 -08:00
|
|
|
}
|
2018-01-24 12:11:14 -08:00
|
|
|
app.valUpdates = nil
|
2018-02-17 15:10:55 -08:00
|
|
|
if app.beginBlocker != nil {
|
2018-02-27 20:46:27 -08:00
|
|
|
res = app.beginBlocker(app.deliverState.ctx, req)
|
2018-02-17 15:10:55 -08:00
|
|
|
}
|
2018-05-23 13:25:56 -07:00
|
|
|
// set the absent validators for addition to context in deliverTx
|
|
|
|
app.absentValidators = req.AbsentValidators
|
2017-12-21 23:30:44 -08:00
|
|
|
return
|
2017-10-16 10:21:43 -07:00
|
|
|
}
|
|
|
|
|
2018-02-04 12:20:49 -08:00
|
|
|
// Implements ABCI
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {
|
2018-01-26 04:19:33 -08:00
|
|
|
// Decode the Tx.
|
|
|
|
var result sdk.Result
|
|
|
|
var tx, err = app.txDecoder(txBytes)
|
|
|
|
if err != nil {
|
|
|
|
result = err.Result()
|
|
|
|
} else {
|
2018-05-15 17:18:25 -07:00
|
|
|
result = app.runTx(runTxModeCheck, txBytes, tx)
|
2018-01-26 04:19:33 -08:00
|
|
|
}
|
2018-01-03 17:20:21 -08:00
|
|
|
|
2017-12-26 17:04:48 -08:00
|
|
|
return abci.ResponseCheckTx{
|
2018-01-26 06:22:56 -08:00
|
|
|
Code: uint32(result.Code),
|
2017-12-03 21:25:37 -08:00
|
|
|
Data: result.Data,
|
|
|
|
Log: result.Log,
|
2017-12-26 17:04:48 -08:00
|
|
|
GasWanted: result.GasWanted,
|
2018-05-07 11:49:34 -07:00
|
|
|
GasUsed: result.GasUsed,
|
2017-12-26 17:04:48 -08:00
|
|
|
Fee: cmn.KI64Pair{
|
|
|
|
[]byte(result.FeeDenom),
|
|
|
|
result.FeeAmount,
|
|
|
|
},
|
|
|
|
Tags: result.Tags,
|
2017-10-16 10:21:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 12:20:49 -08:00
|
|
|
// Implements ABCI
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {
|
2018-01-26 04:19:33 -08:00
|
|
|
// Decode the Tx.
|
|
|
|
var result sdk.Result
|
|
|
|
var tx, err = app.txDecoder(txBytes)
|
|
|
|
if err != nil {
|
|
|
|
result = err.Result()
|
|
|
|
} else {
|
2018-05-15 17:18:25 -07:00
|
|
|
result = app.runTx(runTxModeDeliver, txBytes, tx)
|
2018-01-26 04:19:33 -08:00
|
|
|
}
|
2017-12-21 20:19:44 -08:00
|
|
|
|
|
|
|
// After-handler hooks.
|
2018-01-26 06:22:56 -08:00
|
|
|
if result.IsOK() {
|
2017-12-26 17:04:48 -08:00
|
|
|
app.valUpdates = append(app.valUpdates, result.ValidatorUpdates...)
|
2017-12-21 20:19:44 -08:00
|
|
|
} else {
|
2018-04-10 20:51:09 -07:00
|
|
|
// Even though the Result.Code is not OK, there are still effects,
|
|
|
|
// namely fee deductions and sequence incrementing.
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
|
2017-12-21 20:19:44 -08:00
|
|
|
// Tell the blockchain engine (i.e. Tendermint).
|
|
|
|
return abci.ResponseDeliverTx{
|
2018-01-26 06:22:56 -08:00
|
|
|
Code: uint32(result.Code),
|
2017-12-26 17:04:48 -08:00
|
|
|
Data: result.Data,
|
|
|
|
Log: result.Log,
|
|
|
|
GasWanted: result.GasWanted,
|
|
|
|
GasUsed: result.GasUsed,
|
|
|
|
Tags: result.Tags,
|
2017-12-21 20:19:44 -08:00
|
|
|
}
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
|
2018-05-09 08:54:04 -07:00
|
|
|
// nolint - Mostly for testing
|
2018-02-17 13:19:34 -08:00
|
|
|
func (app *BaseApp) Check(tx sdk.Tx) (result sdk.Result) {
|
2018-05-15 17:18:25 -07:00
|
|
|
return app.runTx(runTxModeCheck, nil, tx)
|
2018-02-17 13:19:34 -08:00
|
|
|
}
|
2018-05-09 08:54:04 -07:00
|
|
|
|
|
|
|
// nolint - full tx execution
|
2018-05-11 11:02:05 -07:00
|
|
|
func (app *BaseApp) Simulate(tx sdk.Tx) (result sdk.Result) {
|
2018-05-15 17:18:25 -07:00
|
|
|
return app.runTx(runTxModeSimulate, nil, tx)
|
2018-05-09 08:54:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// nolint
|
2018-02-17 13:19:34 -08:00
|
|
|
func (app *BaseApp) Deliver(tx sdk.Tx) (result sdk.Result) {
|
2018-05-15 17:18:25 -07:00
|
|
|
return app.runTx(runTxModeDeliver, nil, tx)
|
2018-02-17 13:19:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// txBytes may be nil in some cases, eg. in tests.
|
|
|
|
// Also, in the future we may support "internal" transactions.
|
2018-05-15 17:18:25 -07:00
|
|
|
func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk.Result) {
|
2018-01-12 11:49:53 -08:00
|
|
|
// Handle any panics.
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2018-05-08 08:46:02 -07:00
|
|
|
switch r.(type) {
|
|
|
|
case sdk.ErrorOutOfGas:
|
|
|
|
log := fmt.Sprintf("Out of gas in location: %v", r.(sdk.ErrorOutOfGas).Descriptor)
|
|
|
|
result = sdk.ErrOutOfGas(log).Result()
|
|
|
|
default:
|
|
|
|
log := fmt.Sprintf("Recovered: %v\nstack:\n%v", r, string(debug.Stack()))
|
|
|
|
result = sdk.ErrInternal(log).Result()
|
|
|
|
}
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-01-26 06:22:56 -08:00
|
|
|
// Get the Msg.
|
|
|
|
var msg = tx.GetMsg()
|
|
|
|
if msg == nil {
|
|
|
|
return sdk.ErrInternal("Tx.GetMsg() returned nil").Result()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the Msg.
|
|
|
|
err := msg.ValidateBasic()
|
2018-01-12 11:49:53 -08:00
|
|
|
if err != nil {
|
2018-04-17 19:16:21 -07:00
|
|
|
err = err.WithDefaultCodespace(sdk.CodespaceRoot)
|
2018-01-26 04:19:33 -08:00
|
|
|
return err.Result()
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
|
|
|
|
2018-02-17 15:10:55 -08:00
|
|
|
// Get the context
|
|
|
|
var ctx sdk.Context
|
2018-05-15 17:18:25 -07:00
|
|
|
if mode == runTxModeCheck || mode == runTxModeSimulate {
|
2018-02-27 20:46:27 -08:00
|
|
|
ctx = app.checkState.ctx.WithTxBytes(txBytes)
|
2018-02-17 15:10:55 -08:00
|
|
|
} else {
|
2018-02-27 20:46:27 -08:00
|
|
|
ctx = app.deliverState.ctx.WithTxBytes(txBytes)
|
2018-05-23 13:25:56 -07:00
|
|
|
ctx = ctx.WithAbsentValidators(app.absentValidators)
|
2018-02-17 15:10:55 -08:00
|
|
|
}
|
2018-01-26 04:19:33 -08:00
|
|
|
|
2018-05-09 08:54:04 -07:00
|
|
|
// Simulate a DeliverTx for gas calculation
|
2018-05-15 17:18:25 -07:00
|
|
|
if mode == runTxModeSimulate {
|
2018-05-09 08:54:04 -07:00
|
|
|
ctx = ctx.WithIsCheckTx(false)
|
|
|
|
}
|
|
|
|
|
2018-01-12 11:49:53 -08:00
|
|
|
// Run the ante handler.
|
2018-02-28 19:17:48 -08:00
|
|
|
if app.anteHandler != nil {
|
|
|
|
newCtx, result, abort := app.anteHandler(ctx, tx)
|
|
|
|
if abort {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
if !newCtx.IsZero() {
|
|
|
|
ctx = newCtx
|
|
|
|
}
|
2018-01-26 05:11:01 -08:00
|
|
|
}
|
|
|
|
|
2018-03-13 11:48:51 -07:00
|
|
|
// Match route.
|
|
|
|
msgType := msg.Type()
|
|
|
|
handler := app.router.Route(msgType)
|
|
|
|
if handler == nil {
|
|
|
|
return sdk.ErrUnknownRequest("Unrecognized Msg type: " + msgType).Result()
|
|
|
|
}
|
|
|
|
|
2018-02-25 12:59:11 -08:00
|
|
|
// Get the correct cache
|
|
|
|
var msCache sdk.CacheMultiStore
|
2018-05-15 17:18:25 -07:00
|
|
|
if mode == runTxModeCheck || mode == runTxModeSimulate {
|
2018-02-27 20:46:27 -08:00
|
|
|
// CacheWrap app.checkState.ms in case it fails.
|
|
|
|
msCache = app.checkState.CacheMultiStore()
|
2018-02-25 12:59:11 -08:00
|
|
|
ctx = ctx.WithMultiStore(msCache)
|
|
|
|
} else {
|
2018-02-27 20:46:27 -08:00
|
|
|
// CacheWrap app.deliverState.ms in case it fails.
|
|
|
|
msCache = app.deliverState.CacheMultiStore()
|
2018-02-25 12:59:11 -08:00
|
|
|
ctx = ctx.WithMultiStore(msCache)
|
|
|
|
}
|
2018-01-12 11:49:53 -08:00
|
|
|
|
2018-01-26 06:22:56 -08:00
|
|
|
result = handler(ctx, msg)
|
2018-01-12 11:49:53 -08:00
|
|
|
|
2018-05-07 11:49:34 -07:00
|
|
|
// Set gas utilized
|
|
|
|
result.GasUsed = ctx.GasMeter().GasConsumed()
|
|
|
|
|
2018-05-09 12:57:30 -07:00
|
|
|
// If not a simulated run and result was successful, write to app.checkState.ms or app.deliverState.ms
|
2018-05-15 17:18:25 -07:00
|
|
|
if mode != runTxModeSimulate && result.IsOK() {
|
2018-01-26 05:11:01 -08:00
|
|
|
msCache.Write()
|
|
|
|
}
|
|
|
|
|
2018-01-12 11:49:53 -08:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2018-02-04 12:20:49 -08:00
|
|
|
// Implements ABCI
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
|
2018-02-17 15:10:55 -08:00
|
|
|
if app.endBlocker != nil {
|
2018-02-27 20:46:27 -08:00
|
|
|
res = app.endBlocker(app.deliverState.ctx, req)
|
2018-02-17 15:10:55 -08:00
|
|
|
} else {
|
|
|
|
res.ValidatorUpdates = app.valUpdates
|
|
|
|
}
|
2017-12-21 20:19:44 -08:00
|
|
|
return
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
|
2018-02-04 12:20:49 -08:00
|
|
|
// Implements ABCI
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) Commit() (res abci.ResponseCommit) {
|
2018-02-27 20:46:27 -08:00
|
|
|
header := app.deliverState.ctx.BlockHeader()
|
2018-02-27 20:07:54 -08:00
|
|
|
/*
|
|
|
|
// Write the latest Header to the store
|
|
|
|
headerBytes, err := proto.Marshal(&header)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
app.db.SetSync(dbHeaderKey, headerBytes)
|
|
|
|
*/
|
2018-02-27 19:23:56 -08:00
|
|
|
|
2018-02-17 15:10:55 -08:00
|
|
|
// Write the Deliver state and commit the MultiStore
|
2018-02-27 20:46:27 -08:00
|
|
|
app.deliverState.ms.Write()
|
2018-01-26 04:19:33 -08:00
|
|
|
commitID := app.cms.Commit()
|
2018-02-14 11:04:34 -08:00
|
|
|
app.Logger.Debug("Commit synced",
|
2017-12-14 08:18:18 -08:00
|
|
|
"commit", commitID,
|
|
|
|
)
|
2018-02-17 15:10:55 -08:00
|
|
|
|
2018-02-27 20:46:27 -08:00
|
|
|
// Reset the Check state to the latest committed
|
2018-02-17 15:10:55 -08:00
|
|
|
// NOTE: safe because Tendermint holds a lock on the mempool for Commit.
|
|
|
|
// Use the header from this latest block.
|
2018-02-27 20:46:27 -08:00
|
|
|
app.setCheckState(header)
|
|
|
|
|
2018-04-10 20:51:09 -07:00
|
|
|
// Empty the Deliver state
|
2018-02-27 20:46:27 -08:00
|
|
|
app.deliverState = nil
|
2018-02-17 15:10:55 -08:00
|
|
|
|
2017-12-26 17:04:48 -08:00
|
|
|
return abci.ResponseCommit{
|
|
|
|
Data: commitID.Hash,
|
|
|
|
}
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|