2018-01-20 20:08:00 -08:00
|
|
|
package baseapp
|
2017-10-16 10:21:43 -07:00
|
|
|
|
|
|
|
import (
|
2019-08-06 05:59:22 -07:00
|
|
|
"encoding/json"
|
2017-12-01 09:10:17 -08:00
|
|
|
"fmt"
|
2019-08-06 05:59:22 -07:00
|
|
|
"io/ioutil"
|
2019-04-23 06:33:11 -07:00
|
|
|
"os"
|
2019-02-07 17:52:24 -08:00
|
|
|
"reflect"
|
2018-01-26 04:19:33 -08:00
|
|
|
"runtime/debug"
|
2019-05-16 08:25:32 -07:00
|
|
|
"sort"
|
2018-05-09 13:25:13 -07:00
|
|
|
"strings"
|
2017-12-01 09:10:17 -08:00
|
|
|
|
2019-02-08 11:37:46 -08:00
|
|
|
"errors"
|
|
|
|
|
2018-11-24 18:10:59 -08:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-02-14 08:16:06 -08:00
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2018-07-12 16:58:51 -07:00
|
|
|
"github.com/tendermint/tendermint/crypto/tmhash"
|
2018-07-02 13:34:06 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2019-08-02 06:20:39 -07:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
2017-10-16 10:21:43 -07:00
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-01-22 05:44:24 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
2019-08-06 05:59:22 -07:00
|
|
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
2018-01-12 13:59:19 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2017-10-16 10:21:43 -07:00
|
|
|
)
|
|
|
|
|
2018-11-20 16:44:49 -08:00
|
|
|
// Key to store the consensus params in the main store.
|
|
|
|
var mainConsensusParamsKey = []byte("consensus_params")
|
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-12-19 11:58:30 -08:00
|
|
|
|
|
|
|
// MainStoreKey is the string representation of the main store
|
|
|
|
MainStoreKey = "main"
|
2018-05-15 17:06:17 -07:00
|
|
|
)
|
|
|
|
|
2019-08-06 05:59:22 -07:00
|
|
|
// StoreLoader defines a customizable function to control how we load the CommitMultiStore
|
|
|
|
// from disk. This is useful for state migration, when loading a datastore written with
|
|
|
|
// an older version of the software. In particular, if a module changed the substore key name
|
|
|
|
// (or removed a substore) between two versions of the software.
|
|
|
|
type StoreLoader func(ms sdk.CommitMultiStore) error
|
|
|
|
|
2018-07-12 16:58:51 -07:00
|
|
|
// BaseApp reflects the ABCI application implementation.
|
2018-01-20 20:08:00 -08:00
|
|
|
type BaseApp struct {
|
2018-02-17 15:10:55 -08:00
|
|
|
// initialized on creation
|
2019-02-07 17:52:24 -08:00
|
|
|
logger log.Logger
|
2018-07-31 18:48:32 -07:00
|
|
|
name string // application name from abci.Info
|
|
|
|
db dbm.DB // common DB backend
|
|
|
|
cms sdk.CommitMultiStore // Main (uncached) state
|
2019-08-06 05:59:22 -07:00
|
|
|
storeLoader StoreLoader // function to handle store loading, may be overridden with SetStoreLoader()
|
2019-05-16 08:25:32 -07:00
|
|
|
router sdk.Router // handle any kind of message
|
|
|
|
queryRouter sdk.QueryRouter // router for redirecting query calls
|
2018-07-31 18:48:32 -07:00
|
|
|
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx
|
2018-07-18 16:24:16 -07:00
|
|
|
|
2018-11-20 16:44:49 -08:00
|
|
|
// set upon LoadVersion or LoadLatestVersion.
|
2019-02-07 17:52:24 -08:00
|
|
|
baseKey *sdk.KVStoreKey // Main KVStore in cms
|
2018-02-17 16:24:40 -08:00
|
|
|
|
2019-02-07 17:52:24 -08:00
|
|
|
anteHandler sdk.AnteHandler // ante handler for fee and auth
|
|
|
|
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
|
|
|
|
idPeerFilter sdk.PeerFilter // filter peers by node ID
|
|
|
|
fauxMerkleMode bool // if true, IAVL MountStores uses MountStoresDB for simulation speed.
|
2018-01-12 11:49:53 -08:00
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// --------------------
|
|
|
|
// Volatile state
|
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-10-03 08:48:23 -07:00
|
|
|
checkState *state // for CheckTx
|
|
|
|
deliverState *state // for DeliverTx
|
|
|
|
voteInfos []abci.VoteInfo // absent validators from begin block
|
2018-07-31 21:23:25 -07:00
|
|
|
|
2018-11-20 16:44:49 -08:00
|
|
|
// consensus params
|
2019-02-05 19:11:57 -08:00
|
|
|
// TODO: Move this in the future to baseapp param store on main store.
|
2018-11-20 16:44:49 -08:00
|
|
|
consensusParams *abci.ConsensusParams
|
|
|
|
|
2019-01-18 08:45:20 -08:00
|
|
|
// The minimum gas prices a validator is willing to accept for processing a
|
|
|
|
// transaction. This is mainly used for DoS and spam prevention.
|
|
|
|
minGasPrices sdk.DecCoins
|
2018-09-19 08:25:52 -07:00
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// flag for sealing options and parameters to a BaseApp
|
2018-07-31 21:23:25 -07:00
|
|
|
sealed bool
|
2019-04-23 06:33:11 -07:00
|
|
|
|
|
|
|
// height at which to halt the chain and gracefully shutdown
|
|
|
|
haltHeight uint64
|
2019-05-02 12:37:44 -07:00
|
|
|
|
|
|
|
// application's version string
|
|
|
|
appVersion string
|
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
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// NewBaseApp returns a reference to an initialized BaseApp. It accepts a
|
|
|
|
// variadic number of option functions, which act on the BaseApp to set
|
|
|
|
// configuration choices.
|
2018-07-12 16:58:51 -07:00
|
|
|
//
|
2018-03-28 05:33:48 -07:00
|
|
|
// NOTE: The db is used to store the version number for now.
|
2019-02-05 19:11:57 -08:00
|
|
|
func NewBaseApp(
|
|
|
|
name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecoder, options ...func(*BaseApp),
|
|
|
|
) *BaseApp {
|
|
|
|
|
2018-07-12 18:32:48 -07:00
|
|
|
app := &BaseApp{
|
2019-02-07 17:52:24 -08:00
|
|
|
logger: logger,
|
2018-11-27 00:14:22 -08:00
|
|
|
name: name,
|
|
|
|
db: db,
|
|
|
|
cms: store.NewCommitMultiStore(db),
|
2019-08-06 05:59:22 -07:00
|
|
|
storeLoader: DefaultStoreLoader,
|
2018-11-27 00:14:22 -08:00
|
|
|
router: NewRouter(),
|
|
|
|
queryRouter: NewQueryRouter(),
|
|
|
|
txDecoder: txDecoder,
|
|
|
|
fauxMerkleMode: false,
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
2018-07-10 12:10:59 -07:00
|
|
|
for _, option := range options {
|
|
|
|
option(app)
|
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-04-17 19:16:21 -07:00
|
|
|
return app
|
2017-12-19 21:04:40 -08:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// Name returns the name of the BaseApp.
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) Name() string {
|
2017-12-21 23:30:44 -08:00
|
|
|
return app.name
|
|
|
|
}
|
|
|
|
|
2019-05-02 12:37:44 -07:00
|
|
|
// AppVersion returns the application's version string.
|
|
|
|
func (app *BaseApp) AppVersion() string {
|
|
|
|
return app.appVersion
|
|
|
|
}
|
|
|
|
|
2019-02-07 17:52:24 -08:00
|
|
|
// Logger returns the logger of the BaseApp.
|
|
|
|
func (app *BaseApp) Logger() log.Logger {
|
|
|
|
return app.logger
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// MountStores mounts all IAVL or DB stores to the provided keys in the BaseApp
|
|
|
|
// multistore.
|
2019-02-07 17:52:24 -08:00
|
|
|
func (app *BaseApp) MountStores(keys ...sdk.StoreKey) {
|
2018-02-14 17:09:00 -08:00
|
|
|
for _, key := range keys {
|
2019-02-07 17:52:24 -08:00
|
|
|
switch key.(type) {
|
|
|
|
case *sdk.KVStoreKey:
|
|
|
|
if !app.fauxMerkleMode {
|
|
|
|
app.MountStore(key, sdk.StoreTypeIAVL)
|
|
|
|
} else {
|
|
|
|
// StoreTypeDB doesn't do anything upon commit, and it doesn't
|
|
|
|
// retain history, but it's useful for faster simulation.
|
|
|
|
app.MountStore(key, sdk.StoreTypeDB)
|
|
|
|
}
|
|
|
|
case *sdk.TransientStoreKey:
|
|
|
|
app.MountStore(key, sdk.StoreTypeTransient)
|
|
|
|
default:
|
|
|
|
panic("Unrecognized store key type " + reflect.TypeOf(key).Name())
|
2018-11-27 00:14:22 -08:00
|
|
|
}
|
2018-02-14 17:09:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 06:59:16 -07:00
|
|
|
// MountStores mounts all IAVL or DB stores to the provided keys in the BaseApp
|
|
|
|
// multistore.
|
|
|
|
func (app *BaseApp) MountKVStores(keys map[string]*sdk.KVStoreKey) {
|
|
|
|
for _, key := range keys {
|
|
|
|
if !app.fauxMerkleMode {
|
|
|
|
app.MountStore(key, sdk.StoreTypeIAVL)
|
|
|
|
} else {
|
|
|
|
// StoreTypeDB doesn't do anything upon commit, and it doesn't
|
|
|
|
// retain history, but it's useful for faster simulation.
|
|
|
|
app.MountStore(key, sdk.StoreTypeDB)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MountStores mounts all IAVL or DB stores to the provided keys in the BaseApp
|
|
|
|
// multistore.
|
|
|
|
func (app *BaseApp) MountTransientStores(keys map[string]*sdk.TransientStoreKey) {
|
|
|
|
for _, key := range keys {
|
|
|
|
app.MountStore(key, sdk.StoreTypeTransient)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// MountStoreWithDB mounts 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
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// MountStore mounts 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
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// LoadLatestVersion loads the latest application version. It will panic if
|
|
|
|
// called more than once on a running BaseApp.
|
2019-02-07 17:52:24 -08:00
|
|
|
func (app *BaseApp) LoadLatestVersion(baseKey *sdk.KVStoreKey) error {
|
2019-08-06 05:59:22 -07:00
|
|
|
err := app.storeLoader(app.cms)
|
2018-06-28 15:52:10 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-07 17:52:24 -08:00
|
|
|
return app.initFromMainStore(baseKey)
|
2017-12-19 21:04:40 -08:00
|
|
|
}
|
|
|
|
|
2019-08-06 05:59:22 -07:00
|
|
|
// DefaultStoreLoader will be used by default and loads the latest version
|
|
|
|
func DefaultStoreLoader(ms sdk.CommitMultiStore) error {
|
|
|
|
return ms.LoadLatestVersion()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreLoaderWithUpgrade is used to prepare baseapp with a fixed StoreLoader
|
|
|
|
// pattern. This is useful in test cases, or with custom upgrade loading logic.
|
|
|
|
func StoreLoaderWithUpgrade(upgrades *storetypes.StoreUpgrades) StoreLoader {
|
|
|
|
return func(ms sdk.CommitMultiStore) error {
|
|
|
|
return ms.LoadLatestVersionAndUpgrade(upgrades)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpgradeableStoreLoader can be configured by SetStoreLoader() to check for the
|
|
|
|
// existence of a given upgrade file - json encoded StoreUpgrades data.
|
|
|
|
//
|
|
|
|
// If not file is present, it will peform the default load (no upgrades to store).
|
|
|
|
//
|
|
|
|
// If the file is present, it will parse the file and execute those upgrades
|
|
|
|
// (rename or delete stores), while loading the data. It will also delete the
|
|
|
|
// upgrade file upon successful load, so that the upgrade is only applied once,
|
|
|
|
// and not re-applied on next restart
|
|
|
|
//
|
|
|
|
// This is useful for in place migrations when a store key is renamed between
|
|
|
|
// two versions of the software. (TODO: this code will move to x/upgrades
|
|
|
|
// when PR #4233 is merged, here mainly to help test the design)
|
|
|
|
func UpgradeableStoreLoader(upgradeInfoPath string) StoreLoader {
|
|
|
|
return func(ms sdk.CommitMultiStore) error {
|
|
|
|
_, err := os.Stat(upgradeInfoPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return DefaultStoreLoader(ms)
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// there is a migration file, let's execute
|
|
|
|
data, err := ioutil.ReadFile(upgradeInfoPath)
|
|
|
|
if err != nil {
|
2019-08-19 09:06:27 -07:00
|
|
|
return fmt.Errorf("cannot read upgrade file %s: %v", upgradeInfoPath, err)
|
2019-08-06 05:59:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var upgrades storetypes.StoreUpgrades
|
|
|
|
err = json.Unmarshal(data, &upgrades)
|
|
|
|
if err != nil {
|
2019-08-19 09:06:27 -07:00
|
|
|
return fmt.Errorf("cannot parse upgrade file: %v", err)
|
2019-08-06 05:59:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ms.LoadLatestVersionAndUpgrade(&upgrades)
|
|
|
|
if err != nil {
|
2019-08-19 09:06:27 -07:00
|
|
|
return fmt.Errorf("load and upgrade database: %v", err)
|
2019-08-06 05:59:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we have a successful load, we delete the file
|
|
|
|
err = os.Remove(upgradeInfoPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("deleting upgrade file %s: %v", upgradeInfoPath, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// LoadVersion loads the BaseApp application version. It will panic if called
|
|
|
|
// more than once on a running baseapp.
|
2019-02-07 17:52:24 -08:00
|
|
|
func (app *BaseApp) LoadVersion(version int64, baseKey *sdk.KVStoreKey) error {
|
2018-06-28 15:52:10 -07:00
|
|
|
err := app.cms.LoadVersion(version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-07 17:52:24 -08:00
|
|
|
return app.initFromMainStore(baseKey)
|
2017-12-20 17:34:51 -08:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// LastCommitID returns 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
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// LastBlockHeight returns the last committed 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
|
2019-02-07 17:52:24 -08:00
|
|
|
func (app *BaseApp) initFromMainStore(baseKey *sdk.KVStoreKey) error {
|
|
|
|
mainStore := app.cms.GetKVStore(baseKey)
|
2018-11-20 16:44:49 -08:00
|
|
|
if mainStore == nil {
|
2018-06-13 12:13:22 -07:00
|
|
|
return errors.New("baseapp expects MultiStore with 'main' KVStore")
|
2017-12-19 21:04:40 -08:00
|
|
|
}
|
2018-11-20 16:44:49 -08:00
|
|
|
|
2019-02-07 17:52:24 -08:00
|
|
|
// memoize baseKey
|
|
|
|
if app.baseKey != nil {
|
|
|
|
panic("app.baseKey expected to be nil; duplicate init?")
|
2018-11-20 16:44:49 -08:00
|
|
|
}
|
2019-02-07 17:52:24 -08:00
|
|
|
app.baseKey = baseKey
|
2018-11-20 16:44:49 -08:00
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// Load the consensus params from the main store. If the consensus params are
|
|
|
|
// nil, it will be saved later during InitChain.
|
|
|
|
//
|
|
|
|
// TODO: assert that InitChain hasn't yet been called.
|
2018-11-20 16:44:49 -08:00
|
|
|
consensusParamsBz := mainStore.Get(mainConsensusParamsKey)
|
|
|
|
if consensusParamsBz != nil {
|
|
|
|
var consensusParams = &abci.ConsensusParams{}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-11-24 18:10:59 -08:00
|
|
|
err := proto.Unmarshal(consensusParamsBz, consensusParams)
|
2018-11-20 16:44:49 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-11-20 16:44:49 -08:00
|
|
|
app.setConsensusParams(consensusParams)
|
|
|
|
}
|
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
// needed for the export command which inits from store but never calls initchain
|
2018-07-23 16:55:09 -07:00
|
|
|
app.setCheckState(abci.Header{})
|
2018-07-31 21:23:25 -07:00
|
|
|
app.Seal()
|
|
|
|
|
2017-12-20 17:34:51 -08:00
|
|
|
return nil
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
|
2019-01-18 08:45:20 -08:00
|
|
|
func (app *BaseApp) setMinGasPrices(gasPrices sdk.DecCoins) {
|
|
|
|
app.minGasPrices = gasPrices
|
|
|
|
}
|
2018-09-19 08:25:52 -07:00
|
|
|
|
2019-04-23 06:33:11 -07:00
|
|
|
func (app *BaseApp) setHaltHeight(height uint64) {
|
|
|
|
app.haltHeight = height
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// Router returns the router of the BaseApp.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (app *BaseApp) Router() sdk.Router {
|
2019-02-05 19:11:57 -08:00
|
|
|
if app.sealed {
|
|
|
|
// We cannot return a router when the app is sealed because we can't have
|
|
|
|
// any routes modified which would cause unexpected routing behavior.
|
|
|
|
panic("Router() on sealed BaseApp")
|
|
|
|
}
|
|
|
|
return app.router
|
2018-02-27 20:46:27 -08:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// QueryRouter returns the QueryRouter of a BaseApp.
|
2019-05-16 08:25:32 -07:00
|
|
|
func (app *BaseApp) QueryRouter() sdk.QueryRouter { return app.queryRouter }
|
2018-02-27 20:46:27 -08:00
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// Seal seals a BaseApp. It prohibits any further modifications to a BaseApp.
|
|
|
|
func (app *BaseApp) Seal() { app.sealed = true }
|
|
|
|
|
|
|
|
// IsSealed returns true if the BaseApp is sealed and false otherwise.
|
|
|
|
func (app *BaseApp) IsSealed() bool { return app.sealed }
|
2018-11-20 01:06:14 -08:00
|
|
|
|
2019-02-07 17:52:24 -08:00
|
|
|
// setCheckState sets checkState with the cached multistore and
|
|
|
|
// the context wrapping it.
|
|
|
|
// It is called by InitChain() and Commit()
|
2018-02-27 20:46:27 -08:00
|
|
|
func (app *BaseApp) setCheckState(header abci.Header) {
|
|
|
|
ms := app.cms.CacheMultiStore()
|
|
|
|
app.checkState = &state{
|
|
|
|
ms: ms,
|
2019-02-07 17:52:24 -08:00
|
|
|
ctx: sdk.NewContext(ms, header, true, app.logger).WithMinGasPrices(app.minGasPrices),
|
2018-02-27 20:46:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 17:52:24 -08:00
|
|
|
// setCheckState sets checkState with the cached multistore and
|
|
|
|
// the context wrapping it.
|
|
|
|
// It is called by InitChain() and BeginBlock(),
|
|
|
|
// and deliverState is set nil on Commit().
|
2018-02-27 20:46:27 -08:00
|
|
|
func (app *BaseApp) setDeliverState(header abci.Header) {
|
|
|
|
ms := app.cms.CacheMultiStore()
|
|
|
|
app.deliverState = &state{
|
|
|
|
ms: ms,
|
2019-02-07 17:52:24 -08:00
|
|
|
ctx: sdk.NewContext(ms, header, false, app.logger),
|
2018-02-17 15:10:55 -08:00
|
|
|
}
|
2018-02-16 17:15:38 -08:00
|
|
|
}
|
|
|
|
|
2018-11-20 16:44:49 -08:00
|
|
|
// setConsensusParams memoizes the consensus params.
|
|
|
|
func (app *BaseApp) setConsensusParams(consensusParams *abci.ConsensusParams) {
|
|
|
|
app.consensusParams = consensusParams
|
|
|
|
}
|
|
|
|
|
|
|
|
// setConsensusParams stores the consensus params to the main store.
|
|
|
|
func (app *BaseApp) storeConsensusParams(consensusParams *abci.ConsensusParams) {
|
2018-11-24 18:10:59 -08:00
|
|
|
consensusParamsBz, err := proto.Marshal(consensusParams)
|
2018-11-20 16:44:49 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-02-07 17:52:24 -08:00
|
|
|
mainStore := app.cms.GetKVStore(app.baseKey)
|
2018-11-20 16:44:49 -08:00
|
|
|
mainStore.Set(mainConsensusParamsKey, consensusParamsBz)
|
|
|
|
}
|
|
|
|
|
2019-03-07 21:52:19 -08:00
|
|
|
// getMaximumBlockGas gets the maximum gas from the consensus params. It panics
|
|
|
|
// if maximum block gas is less than negative one and returns zero if negative
|
|
|
|
// one.
|
|
|
|
func (app *BaseApp) getMaximumBlockGas() uint64 {
|
2019-03-29 10:13:45 -07:00
|
|
|
if app.consensusParams == nil || app.consensusParams.Block == nil {
|
2018-11-20 16:44:49 -08:00
|
|
|
return 0
|
|
|
|
}
|
2019-03-07 21:52:19 -08:00
|
|
|
|
2019-03-29 10:13:45 -07:00
|
|
|
maxGas := app.consensusParams.Block.MaxGas
|
2019-03-07 21:52:19 -08:00
|
|
|
switch {
|
|
|
|
case maxGas < -1:
|
|
|
|
panic(fmt.Sprintf("invalid maximum block gas: %d", maxGas))
|
|
|
|
|
|
|
|
case maxGas == -1:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
default:
|
|
|
|
return uint64(maxGas)
|
|
|
|
}
|
2018-11-20 16:44:49 -08:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// ----------------------------------------------------------------------------
|
2018-02-14 08:16:06 -08:00
|
|
|
// ABCI
|
2017-12-01 09:10:17 -08:00
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// Info implements the ABCI interface.
|
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
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// SetOption implements the ABCI interface.
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOption) {
|
2019-02-05 19:11:57 -08:00
|
|
|
// TODO: Implement!
|
2017-12-21 23:30:44 -08:00
|
|
|
return
|
2017-12-21 20:19:44 -08:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// InitChain implements the ABCI interface. It runs the initialization logic
|
|
|
|
// directly on the CommitMultiStore.
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) {
|
2019-02-05 19:11:57 -08:00
|
|
|
// stash the consensus params in the cms main store and memoize
|
2018-11-20 16:44:49 -08:00
|
|
|
if req.ConsensusParams != nil {
|
|
|
|
app.setConsensusParams(req.ConsensusParams)
|
|
|
|
app.storeConsensusParams(req.ConsensusParams)
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:37:39 -08:00
|
|
|
initHeader := abci.Header{ChainID: req.ChainId, Time: req.Time}
|
|
|
|
|
|
|
|
// initialize the deliver state and check state with a correct header
|
|
|
|
app.setDeliverState(initHeader)
|
|
|
|
app.setCheckState(initHeader)
|
2018-06-27 15:45:10 -07:00
|
|
|
|
2018-02-16 17:15:38 -08:00
|
|
|
if app.initChainer == nil {
|
2018-02-15 06:35:46 -08:00
|
|
|
return
|
|
|
|
}
|
2018-11-14 11:16:52 -08:00
|
|
|
|
|
|
|
// add block gas meter for any genesis transactions (allow infinite gas)
|
|
|
|
app.deliverState.ctx = app.deliverState.ctx.
|
|
|
|
WithBlockGasMeter(sdk.NewInfiniteGasMeter())
|
|
|
|
|
2018-07-18 23:39:40 -07:00
|
|
|
res = app.initChainer(app.deliverState.ctx, req)
|
2018-02-16 17:58:51 -08:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
// sanity check
|
|
|
|
if len(req.Validators) > 0 {
|
|
|
|
if len(req.Validators) != len(res.Validators) {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"len(RequestInitChain.Validators) != len(validators) (%d != %d)",
|
|
|
|
len(req.Validators), len(res.Validators)))
|
|
|
|
}
|
|
|
|
sort.Sort(abci.ValidatorUpdates(req.Validators))
|
|
|
|
sort.Sort(abci.ValidatorUpdates(res.Validators))
|
|
|
|
for i, val := range res.Validators {
|
|
|
|
if !val.Equal(req.Validators[i]) {
|
|
|
|
panic(fmt.Errorf("validators[%d] != req.Validators[%d] ", i, i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// NOTE: We don't commit, but BeginBlock for block 1 starts from this
|
|
|
|
// deliverState.
|
2017-12-21 23:30:44 -08:00
|
|
|
return
|
2017-12-21 20:19:44 -08:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// FilterPeerByAddrPort filters peers by address/port.
|
2018-05-15 07:00:17 -07:00
|
|
|
func (app *BaseApp) FilterPeerByAddrPort(info string) abci.ResponseQuery {
|
|
|
|
if app.addrPeerFilter != nil {
|
|
|
|
return app.addrPeerFilter(info)
|
|
|
|
}
|
|
|
|
return abci.ResponseQuery{}
|
|
|
|
}
|
|
|
|
|
2019-02-07 17:52:24 -08:00
|
|
|
// FilterPeerByIDfilters peers by node ID.
|
|
|
|
func (app *BaseApp) FilterPeerByID(info string) abci.ResponseQuery {
|
|
|
|
if app.idPeerFilter != nil {
|
|
|
|
return app.idPeerFilter(info)
|
2018-05-15 07:00:17 -07:00
|
|
|
}
|
|
|
|
return abci.ResponseQuery{}
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// Splits a string path using the delimiter '/'.
|
|
|
|
// e.g. "this/is/funny" becomes []string{"this", "is", "funny"}
|
2018-07-09 13:54:28 -07:00
|
|
|
func splitPath(requestPath string) (path []string) {
|
|
|
|
path = strings.Split(requestPath, "/")
|
2018-05-15 17:06:17 -07:00
|
|
|
// first element is empty string
|
|
|
|
if len(path) > 0 && path[0] == "" {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
2018-07-09 13:54:28 -07:00
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// Query implements the ABCI interface. It delegates to CommitMultiStore if it
|
|
|
|
// implements Queryable.
|
2018-07-09 13:54:28 -07:00
|
|
|
func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
|
|
|
|
path := splitPath(req.Path)
|
2018-07-09 14:44:01 -07:00
|
|
|
if len(path) == 0 {
|
|
|
|
msg := "no query path provided"
|
|
|
|
return sdk.ErrUnknownRequest(msg).QueryResult()
|
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-07-09 14:44:01 -07:00
|
|
|
switch path[0] {
|
2018-05-10 11:20:34 -07:00
|
|
|
// "/app" prefix for special application queries
|
2018-07-09 14:44:01 -07:00
|
|
|
case "app":
|
|
|
|
return handleQueryApp(app, path, req)
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-07-09 14:44:01 -07:00
|
|
|
case "store":
|
|
|
|
return handleQueryStore(app, path, req)
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-07-09 14:44:01 -07:00
|
|
|
case "p2p":
|
|
|
|
return handleQueryP2P(app, path, req)
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-07-31 18:48:32 -07:00
|
|
|
case "custom":
|
|
|
|
return handleQueryCustom(app, path, req)
|
2018-07-09 14:44:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := "unknown query path"
|
|
|
|
return sdk.ErrUnknownRequest(msg).QueryResult()
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) (res abci.ResponseQuery) {
|
|
|
|
if len(path) >= 2 {
|
2018-05-09 13:25:13 -07:00
|
|
|
var result sdk.Result
|
2019-02-05 19:11:57 -08:00
|
|
|
|
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 {
|
2019-02-04 15:35:12 -08:00
|
|
|
result = app.Simulate(txBytes, tx)
|
2018-05-09 13:25:13 -07:00
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-06-13 22:49:21 -07:00
|
|
|
case "version":
|
|
|
|
return abci.ResponseQuery{
|
2018-11-16 09:12:24 -08:00
|
|
|
Code: uint32(sdk.CodeOK),
|
|
|
|
Codespace: string(sdk.CodespaceRoot),
|
2019-07-25 17:45:13 -07:00
|
|
|
Height: req.Height,
|
2019-05-02 12:37:44 -07:00
|
|
|
Value: []byte(app.appVersion),
|
2018-06-13 22:49:21 -07:00
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-05-09 13:25:13 -07:00
|
|
|
default:
|
|
|
|
result = sdk.ErrUnknownRequest(fmt.Sprintf("Unknown query: %s", path)).Result()
|
|
|
|
}
|
2018-07-12 19:06:54 -07:00
|
|
|
|
2018-11-04 18:28:38 -08:00
|
|
|
value := codec.Cdc.MustMarshalBinaryLengthPrefixed(result)
|
2018-05-09 13:25:13 -07:00
|
|
|
return abci.ResponseQuery{
|
2018-11-16 09:12:24 -08:00
|
|
|
Code: uint32(sdk.CodeOK),
|
|
|
|
Codespace: string(sdk.CodespaceRoot),
|
2019-07-25 17:45:13 -07:00
|
|
|
Height: req.Height,
|
2018-11-16 09:12:24 -08:00
|
|
|
Value: value,
|
2018-05-09 13:25:13 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-07-09 14:44:01 -07:00
|
|
|
msg := "Expected second parameter to be either simulate or version, neither was present"
|
|
|
|
return sdk.ErrUnknownRequest(msg).QueryResult()
|
|
|
|
}
|
|
|
|
|
2019-07-25 17:45:13 -07:00
|
|
|
func handleQueryStore(app *BaseApp, path []string, req abci.RequestQuery) abci.ResponseQuery {
|
2018-05-10 11:20:34 -07:00
|
|
|
// "/store" prefix for store queries
|
2018-07-09 14:44:01 -07:00
|
|
|
queryable, ok := app.cms.(sdk.Queryable)
|
|
|
|
if !ok {
|
|
|
|
msg := "multistore doesn't support queries"
|
|
|
|
return sdk.ErrUnknownRequest(msg).QueryResult()
|
2018-05-10 11:20:34 -07:00
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-07-09 14:44:01 -07:00
|
|
|
req.Path = "/" + strings.Join(path[1:], "/")
|
2019-07-25 17:45:13 -07:00
|
|
|
|
2019-08-14 09:45:11 -07:00
|
|
|
// when a client did not provide a query height, manually inject the latest
|
|
|
|
if req.Height == 0 {
|
|
|
|
req.Height = app.LastBlockHeight()
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Height <= 1 && req.Prove {
|
|
|
|
return sdk.ErrInternal("cannot query with proof when height <= 1; please provide a valid height").QueryResult()
|
|
|
|
}
|
|
|
|
|
2019-07-25 17:45:13 -07:00
|
|
|
resp := queryable.Query(req)
|
|
|
|
resp.Height = req.Height
|
|
|
|
|
|
|
|
return resp
|
2018-07-09 14:44:01 -07:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
func handleQueryP2P(app *BaseApp, path []string, _ abci.RequestQuery) (res abci.ResponseQuery) {
|
2018-05-15 07:00:17 -07:00
|
|
|
// "/p2p" prefix for p2p queries
|
2018-07-09 14:44:01 -07:00
|
|
|
if len(path) >= 4 {
|
2019-02-07 17:52:24 -08:00
|
|
|
cmd, typ, arg := path[1], path[2], path[3]
|
|
|
|
switch cmd {
|
|
|
|
case "filter":
|
|
|
|
switch typ {
|
|
|
|
case "addr":
|
|
|
|
return app.FilterPeerByAddrPort(arg)
|
2019-07-25 17:45:13 -07:00
|
|
|
|
2019-02-07 17:52:24 -08:00
|
|
|
case "id":
|
|
|
|
return app.FilterPeerByID(arg)
|
2018-05-15 07:00:17 -07:00
|
|
|
}
|
2019-07-25 17:45:13 -07:00
|
|
|
|
2019-02-07 17:52:24 -08:00
|
|
|
default:
|
2018-07-09 14:44:01 -07:00
|
|
|
msg := "Expected second parameter to be filter"
|
|
|
|
return sdk.ErrUnknownRequest(msg).QueryResult()
|
2018-05-15 07:00:17 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-09 14:44:01 -07:00
|
|
|
|
2019-02-07 17:52:24 -08:00
|
|
|
msg := "Expected path is p2p filter <addr|id> <parameter>"
|
2018-05-10 11:20:34 -07:00
|
|
|
return sdk.ErrUnknownRequest(msg).QueryResult()
|
2017-12-21 20:19:44 -08:00
|
|
|
}
|
|
|
|
|
2018-07-31 18:48:32 -07:00
|
|
|
func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) (res abci.ResponseQuery) {
|
2019-02-05 19:11:57 -08:00
|
|
|
// 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".
|
2018-10-11 20:34:46 -07:00
|
|
|
if len(path) < 2 || path[1] == "" {
|
|
|
|
return sdk.ErrUnknownRequest("No route for custom query specified").QueryResult()
|
2018-08-16 18:35:17 -07:00
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-08-15 17:59:15 -07:00
|
|
|
querier := app.queryRouter.Route(path[1])
|
2018-08-17 00:16:17 -07:00
|
|
|
if querier == nil {
|
2018-10-11 20:34:46 -07:00
|
|
|
return sdk.ErrUnknownRequest(fmt.Sprintf("no custom querier found for route %s", path[1])).QueryResult()
|
2018-08-17 00:16:17 -07:00
|
|
|
}
|
|
|
|
|
2019-08-03 06:56:15 -07:00
|
|
|
// when a client did not provide a query height, manually inject the latest
|
|
|
|
if req.Height == 0 {
|
|
|
|
req.Height = app.LastBlockHeight()
|
|
|
|
}
|
|
|
|
|
2019-08-14 09:45:11 -07:00
|
|
|
if req.Height <= 1 && req.Prove {
|
|
|
|
return sdk.ErrInternal("cannot query with proof when height <= 1; please provide a valid height").QueryResult()
|
|
|
|
}
|
|
|
|
|
2019-08-03 06:56:15 -07:00
|
|
|
cacheMS, err := app.cms.CacheMultiStoreWithVersion(req.Height)
|
|
|
|
if err != nil {
|
|
|
|
return sdk.ErrInternal(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"failed to load state at height %d; %s (latest height: %d)",
|
|
|
|
req.Height, err, app.LastBlockHeight(),
|
|
|
|
),
|
|
|
|
).QueryResult()
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// cache wrap the commit-multistore for safety
|
2019-01-18 08:45:20 -08:00
|
|
|
ctx := sdk.NewContext(
|
2019-08-03 06:56:15 -07:00
|
|
|
cacheMS, app.checkState.ctx.BlockHeader(), true, app.logger,
|
2019-01-18 08:45:20 -08:00
|
|
|
).WithMinGasPrices(app.minGasPrices)
|
2018-11-04 20:44:43 -08:00
|
|
|
|
2018-08-15 17:59:15 -07:00
|
|
|
// Passes the rest of the path as an argument to the querier.
|
2019-02-05 19:11:57 -08:00
|
|
|
//
|
|
|
|
// For example, in the path "custom/gov/proposal/test", the gov querier gets
|
|
|
|
// []string{"proposal", "test"} as the path.
|
2019-08-03 06:56:15 -07:00
|
|
|
resBytes, queryErr := querier(ctx, path[2:], req)
|
|
|
|
if queryErr != nil {
|
2018-08-03 12:55:00 -07:00
|
|
|
return abci.ResponseQuery{
|
2019-08-03 06:56:15 -07:00
|
|
|
Code: uint32(queryErr.Code()),
|
|
|
|
Codespace: string(queryErr.Codespace()),
|
2019-07-25 17:45:13 -07:00
|
|
|
Height: req.Height,
|
2019-08-03 06:56:15 -07:00
|
|
|
Log: queryErr.ABCILog(),
|
2018-08-03 12:55:00 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-08-03 12:55:00 -07:00
|
|
|
return abci.ResponseQuery{
|
2019-07-25 17:45:13 -07:00
|
|
|
Code: uint32(sdk.CodeOK),
|
|
|
|
Height: req.Height,
|
|
|
|
Value: resBytes,
|
2018-08-03 12:55:00 -07:00
|
|
|
}
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
|
|
|
|
2019-03-07 21:52:19 -08:00
|
|
|
func (app *BaseApp) validateHeight(req abci.RequestBeginBlock) error {
|
|
|
|
if req.Header.Height < 1 {
|
|
|
|
return fmt.Errorf("invalid height: %d", req.Header.Height)
|
|
|
|
}
|
|
|
|
|
|
|
|
prevHeight := app.LastBlockHeight()
|
|
|
|
if req.Header.Height != prevHeight+1 {
|
|
|
|
return fmt.Errorf("invalid height: %d; expected: %d", req.Header.Height, prevHeight+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-12 16:58:51 -07:00
|
|
|
// BeginBlock implements the ABCI application interface.
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
|
2018-07-12 16:58:51 -07:00
|
|
|
if app.cms.TracingEnabled() {
|
2019-02-01 17:03:09 -08:00
|
|
|
app.cms.SetTracingContext(sdk.TraceContext(
|
2018-07-12 16:58:51 -07:00
|
|
|
map[string]interface{}{"blockHeight": req.Header.Height},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2019-03-07 21:52:19 -08:00
|
|
|
if err := app.validateHeight(req); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-07-12 16:58:51 -07:00
|
|
|
// Initialize the DeliverTx state. If this is the first block, it should
|
|
|
|
// already be initialized in InitChain. Otherwise app.deliverState will be
|
|
|
|
// nil, since it is reset on Commit.
|
2018-02-27 20:46:27 -08:00
|
|
|
if app.deliverState == nil {
|
|
|
|
app.setDeliverState(req.Header)
|
2018-06-27 15:45:10 -07:00
|
|
|
} else {
|
|
|
|
// In the first block, app.deliverState.ctx will already be initialized
|
|
|
|
// by InitChain. Context is now updated with Header information.
|
2018-11-12 20:12:09 -08:00
|
|
|
app.deliverState.ctx = app.deliverState.ctx.
|
|
|
|
WithBlockHeader(req.Header).
|
2018-11-13 08:30:06 -08:00
|
|
|
WithBlockHeight(req.Header.Height)
|
2018-11-14 11:07:46 -08:00
|
|
|
}
|
2018-11-13 11:30:24 -08:00
|
|
|
|
2018-11-14 11:07:46 -08:00
|
|
|
// add block gas meter
|
2018-11-15 08:13:18 -08:00
|
|
|
var gasMeter sdk.GasMeter
|
2018-11-20 16:44:49 -08:00
|
|
|
if maxGas := app.getMaximumBlockGas(); maxGas > 0 {
|
|
|
|
gasMeter = sdk.NewGasMeter(maxGas)
|
2018-11-14 11:07:46 -08:00
|
|
|
} else {
|
2018-11-15 08:13:18 -08:00
|
|
|
gasMeter = sdk.NewInfiniteGasMeter()
|
2018-11-13 11:30:24 -08:00
|
|
|
}
|
2019-01-08 15:56:53 -08:00
|
|
|
|
2018-11-15 08:13:18 -08:00
|
|
|
app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(gasMeter)
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2018-02-17 15:10:55 -08:00
|
|
|
if app.beginBlocker != nil {
|
2019-04-05 13:20:58 -07:00
|
|
|
res = app.beginBlocker(app.deliverState.ctx, req)
|
2018-02-17 15:10:55 -08:00
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2018-06-04 16:42:01 -07:00
|
|
|
// set the signed validators for addition to context in deliverTx
|
2018-10-03 08:48:23 -07:00
|
|
|
app.voteInfos = req.LastCommitInfo.GetVotes()
|
2017-12-21 23:30:44 -08:00
|
|
|
return
|
2017-10-16 10:21:43 -07:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// CheckTx implements the ABCI interface. It runs the "basic checks" to see
|
2019-07-30 09:11:09 -07:00
|
|
|
// whether or not a transaction can possibly be executed, first decoding and then
|
|
|
|
// the ante handler (which checks signatures/fees/ValidateBasic).
|
2019-02-05 19:11:57 -08:00
|
|
|
//
|
|
|
|
// NOTE:CheckTx does not run the actual Msg handler function(s).
|
2019-06-26 09:03:25 -07:00
|
|
|
func (app *BaseApp) CheckTx(req abci.RequestCheckTx) (res abci.ResponseCheckTx) {
|
2018-01-26 04:19:33 -08:00
|
|
|
var result sdk.Result
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2019-06-26 09:03:25 -07:00
|
|
|
tx, err := app.txDecoder(req.Tx)
|
2018-01-26 04:19:33 -08:00
|
|
|
if err != nil {
|
|
|
|
result = err.Result()
|
|
|
|
} else {
|
2019-06-26 09:03:25 -07:00
|
|
|
result = app.runTx(runTxModeCheck, req.Tx, 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,
|
2018-11-19 09:13:45 -08:00
|
|
|
GasWanted: int64(result.GasWanted), // TODO: Should type accept unsigned ints?
|
|
|
|
GasUsed: int64(result.GasUsed), // TODO: Should type accept unsigned ints?
|
2019-06-26 09:03:25 -07:00
|
|
|
Events: result.Events.ToABCIEvents(),
|
2017-10-16 10:21:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// DeliverTx implements the ABCI interface.
|
2019-06-26 09:03:25 -07:00
|
|
|
func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) (res abci.ResponseDeliverTx) {
|
2018-11-12 20:12:09 -08:00
|
|
|
var result sdk.Result
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2019-06-26 09:03:25 -07:00
|
|
|
tx, err := app.txDecoder(req.Tx)
|
2018-01-26 04:19:33 -08:00
|
|
|
if err != nil {
|
|
|
|
result = err.Result()
|
|
|
|
} else {
|
2019-06-26 09:03:25 -07:00
|
|
|
result = app.runTx(runTxModeDeliver, req.Tx, tx)
|
2018-01-26 04:19:33 -08:00
|
|
|
}
|
2017-12-21 20:19:44 -08:00
|
|
|
|
|
|
|
return abci.ResponseDeliverTx{
|
2018-01-26 06:22:56 -08:00
|
|
|
Code: uint32(result.Code),
|
2018-11-16 09:12:24 -08:00
|
|
|
Codespace: string(result.Codespace),
|
2017-12-26 17:04:48 -08:00
|
|
|
Data: result.Data,
|
|
|
|
Log: result.Log,
|
2018-11-19 09:13:45 -08:00
|
|
|
GasWanted: int64(result.GasWanted), // TODO: Should type accept unsigned ints?
|
|
|
|
GasUsed: int64(result.GasUsed), // TODO: Should type accept unsigned ints?
|
2019-06-26 09:03:25 -07:00
|
|
|
Events: result.Events.ToABCIEvents(),
|
2017-12-21 20:19:44 -08:00
|
|
|
}
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// validateBasicTxMsgs executes basic validator calls for messages.
|
2018-07-09 16:16:43 -07:00
|
|
|
func validateBasicTxMsgs(msgs []sdk.Msg) sdk.Error {
|
2019-08-26 05:05:43 -07:00
|
|
|
if len(msgs) == 0 {
|
2019-02-07 17:52:24 -08:00
|
|
|
return sdk.ErrUnknownRequest("Tx.GetMsgs() must return at least one message in list")
|
2018-07-09 16:16:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, msg := range msgs {
|
|
|
|
// Validate the Msg.
|
|
|
|
err := msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-06-26 18:53:41 -07:00
|
|
|
|
2018-07-09 16:16:43 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-20 01:06:14 -08:00
|
|
|
// retrieve the context for the tx w/ txBytes and other memoized values.
|
|
|
|
func (app *BaseApp) getContextForTx(mode runTxMode, txBytes []byte) (ctx sdk.Context) {
|
|
|
|
ctx = app.getState(mode).ctx.
|
|
|
|
WithTxBytes(txBytes).
|
2018-11-29 09:21:45 -08:00
|
|
|
WithVoteInfos(app.voteInfos).
|
|
|
|
WithConsensusParams(app.consensusParams)
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-11-20 01:06:14 -08:00
|
|
|
if mode == runTxModeSimulate {
|
|
|
|
ctx, _ = ctx.CacheContext()
|
2018-06-26 18:53:41 -07:00
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2018-06-26 18:53:41 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// runMsgs iterates through all the messages and executes them.
|
2019-05-16 18:32:20 -07:00
|
|
|
// nolint: gocyclo
|
2018-07-18 17:42:19 -07:00
|
|
|
func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (result sdk.Result) {
|
2019-03-25 17:54:23 -07:00
|
|
|
idxLogs := make([]sdk.ABCIMessageLog, 0, len(msgs)) // a list of JSON-encoded logs with msg index
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2019-06-26 09:03:25 -07:00
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
code sdk.CodeType
|
|
|
|
codespace sdk.CodespaceType
|
|
|
|
)
|
2019-02-05 19:11:57 -08:00
|
|
|
|
2019-06-26 09:03:25 -07:00
|
|
|
events := sdk.EmptyEvents()
|
|
|
|
|
|
|
|
// NOTE: GasWanted is determined by ante handler and GasUsed by the GasMeter.
|
2018-06-26 18:53:41 -07:00
|
|
|
for msgIdx, msg := range msgs {
|
2019-02-05 19:11:57 -08:00
|
|
|
// match message route
|
2018-10-23 12:23:55 -07:00
|
|
|
msgRoute := msg.Route()
|
|
|
|
handler := app.router.Route(msgRoute)
|
2018-06-26 18:53:41 -07:00
|
|
|
if handler == nil {
|
2018-10-23 12:23:55 -07:00
|
|
|
return sdk.ErrUnknownRequest("Unrecognized Msg type: " + msgRoute).Result()
|
2018-06-26 18:53:41 -07:00
|
|
|
}
|
|
|
|
|
2018-07-18 17:42:19 -07:00
|
|
|
var msgResult sdk.Result
|
2019-02-05 19:11:57 -08:00
|
|
|
|
|
|
|
// skip actual execution for CheckTx mode
|
2018-07-18 17:42:19 -07:00
|
|
|
if mode != runTxModeCheck {
|
|
|
|
msgResult = handler(ctx, msg)
|
|
|
|
}
|
2018-06-26 18:53:41 -07:00
|
|
|
|
2019-06-26 09:03:25 -07:00
|
|
|
// Each message result's Data must be length prefixed in order to separate
|
|
|
|
// each result.
|
2018-06-26 18:53:41 -07:00
|
|
|
data = append(data, msgResult.Data...)
|
2019-06-26 09:03:25 -07:00
|
|
|
|
|
|
|
// append events from the message's execution and a message action event
|
|
|
|
events = events.AppendEvent(sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyAction, msg.Type())))
|
|
|
|
events = events.AppendEvents(msgResult.Events)
|
2018-06-26 18:53:41 -07:00
|
|
|
|
2019-04-29 20:50:44 -07:00
|
|
|
idxLog := sdk.ABCIMessageLog{MsgIndex: uint16(msgIdx), Log: msgResult.Log}
|
2019-02-13 12:40:34 -08:00
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// stop execution and return on first failed message
|
2018-06-26 18:53:41 -07:00
|
|
|
if !msgResult.IsOK() {
|
2019-02-13 12:40:34 -08:00
|
|
|
idxLog.Success = false
|
2019-03-25 17:54:23 -07:00
|
|
|
idxLogs = append(idxLogs, idxLog)
|
2019-02-13 12:40:34 -08:00
|
|
|
|
2018-06-26 18:53:41 -07:00
|
|
|
code = msgResult.Code
|
2018-11-16 09:12:24 -08:00
|
|
|
codespace = msgResult.Codespace
|
2018-06-26 18:53:41 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-02-13 12:40:34 -08:00
|
|
|
idxLog.Success = true
|
2019-03-25 17:54:23 -07:00
|
|
|
idxLogs = append(idxLogs, idxLog)
|
2018-06-26 18:53:41 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 17:54:23 -07:00
|
|
|
logJSON := codec.Cdc.MustMarshalJSON(idxLogs)
|
2018-06-26 18:53:41 -07:00
|
|
|
result = sdk.Result{
|
2018-11-16 09:12:24 -08:00
|
|
|
Code: code,
|
|
|
|
Codespace: codespace,
|
|
|
|
Data: data,
|
2019-02-13 12:40:34 -08:00
|
|
|
Log: strings.TrimSpace(string(logJSON)),
|
2018-11-16 09:12:24 -08:00
|
|
|
GasUsed: ctx.GasMeter().GasConsumed(),
|
2019-06-26 09:03:25 -07:00
|
|
|
Events: events,
|
2018-06-26 18:53:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-03-25 17:54:23 -07:00
|
|
|
// Returns the applications's deliverState if app is in runTxModeDeliver,
|
2018-07-12 16:58:51 -07:00
|
|
|
// otherwise it returns the application's checkstate.
|
2018-11-16 10:33:47 -08:00
|
|
|
func (app *BaseApp) getState(mode runTxMode) *state {
|
2018-07-09 16:24:20 -07:00
|
|
|
if mode == runTxModeCheck || mode == runTxModeSimulate {
|
|
|
|
return app.checkState
|
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2018-07-09 16:27:51 -07:00
|
|
|
return app.deliverState
|
2018-07-09 16:24:20 -07:00
|
|
|
}
|
|
|
|
|
2018-11-20 01:06:14 -08:00
|
|
|
// cacheTxContext returns a new context based off of the provided context with
|
|
|
|
// a cache wrapped multi-store.
|
|
|
|
func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (
|
|
|
|
sdk.Context, sdk.CacheMultiStore) {
|
2018-11-16 10:33:47 -08:00
|
|
|
|
2018-11-20 01:06:14 -08:00
|
|
|
ms := ctx.MultiStore()
|
|
|
|
// TODO: https://github.com/cosmos/cosmos-sdk/issues/2824
|
|
|
|
msCache := ms.CacheMultiStore()
|
2018-11-16 10:33:47 -08:00
|
|
|
if msCache.TracingEnabled() {
|
2019-02-01 17:03:09 -08:00
|
|
|
msCache = msCache.SetTracingContext(
|
2018-11-16 10:33:47 -08:00
|
|
|
sdk.TraceContext(
|
|
|
|
map[string]interface{}{
|
|
|
|
"txHash": fmt.Sprintf("%X", tmhash.Sum(txBytes)),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
).(sdk.CacheMultiStore)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.WithMultiStore(msCache), msCache
|
|
|
|
}
|
|
|
|
|
2019-05-16 18:32:20 -07:00
|
|
|
// runTx processes a transaction. The transactions is processed via an
|
2018-11-16 10:33:47 -08:00
|
|
|
// anteHandler. The provided txBytes may be nil in some cases, eg. in tests. For
|
|
|
|
// further details on transaction execution, reference the BaseApp SDK
|
|
|
|
// documentation.
|
2018-05-15 17:18:25 -07:00
|
|
|
func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk.Result) {
|
2018-07-12 16:58:51 -07:00
|
|
|
// NOTE: GasWanted should be returned by the AnteHandler. GasUsed is
|
|
|
|
// determined by the GasMeter. We need access to the context to get the gas
|
|
|
|
// meter so we initialize upfront.
|
2018-11-19 09:13:45 -08:00
|
|
|
var gasWanted uint64
|
2018-11-16 10:33:47 -08:00
|
|
|
|
2018-11-20 01:06:14 -08:00
|
|
|
ctx := app.getContextForTx(mode, txBytes)
|
|
|
|
ms := ctx.MultiStore()
|
2018-07-06 07:47:07 -07:00
|
|
|
|
2018-11-13 11:27:15 -08:00
|
|
|
// only run the tx if there is block gas remaining
|
2018-11-20 20:07:30 -08:00
|
|
|
if mode == runTxModeDeliver && ctx.BlockGasMeter().IsOutOfGas() {
|
2019-05-16 18:32:20 -07:00
|
|
|
return sdk.ErrOutOfGas("no block gas left to run tx").Result()
|
2018-11-13 11:27:15 -08:00
|
|
|
}
|
|
|
|
|
2018-12-10 12:39:29 -08:00
|
|
|
var startingGas uint64
|
|
|
|
if mode == runTxModeDeliver {
|
|
|
|
startingGas = ctx.BlockGasMeter().GasConsumed()
|
|
|
|
}
|
|
|
|
|
2018-01-12 11:49:53 -08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2018-07-06 07:47:07 -07:00
|
|
|
switch rType := r.(type) {
|
2018-05-08 08:46:02 -07:00
|
|
|
case sdk.ErrorOutOfGas:
|
2019-02-04 15:35:12 -08:00
|
|
|
log := fmt.Sprintf(
|
|
|
|
"out of gas in location: %v; gasWanted: %d, gasUsed: %d",
|
|
|
|
rType.Descriptor, gasWanted, ctx.GasMeter().GasConsumed(),
|
|
|
|
)
|
2018-05-08 08:46:02 -07:00
|
|
|
result = sdk.ErrOutOfGas(log).Result()
|
|
|
|
default:
|
2018-06-13 12:13:22 -07:00
|
|
|
log := fmt.Sprintf("recovered: %v\nstack:\n%v", r, string(debug.Stack()))
|
2018-05-08 08:46:02 -07:00
|
|
|
result = sdk.ErrInternal(log).Result()
|
|
|
|
}
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2018-11-22 09:34:13 -08:00
|
|
|
result.GasWanted = gasWanted
|
|
|
|
result.GasUsed = ctx.GasMeter().GasConsumed()
|
|
|
|
}()
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// If BlockGasMeter() panics it will be caught by the above recover and will
|
|
|
|
// return an error - in any case BlockGasMeter will consume gas past the limit.
|
|
|
|
//
|
|
|
|
// NOTE: This must exist in a separate defer function for the above recovery
|
|
|
|
// to recover from this one.
|
2018-11-22 09:34:13 -08:00
|
|
|
defer func() {
|
2018-11-20 20:07:30 -08:00
|
|
|
if mode == runTxModeDeliver {
|
|
|
|
ctx.BlockGasMeter().ConsumeGas(
|
2019-01-08 15:56:53 -08:00
|
|
|
ctx.GasMeter().GasConsumedToLimit(),
|
|
|
|
"block gas meter",
|
|
|
|
)
|
|
|
|
|
2018-12-10 12:39:29 -08:00
|
|
|
if ctx.BlockGasMeter().GasConsumed() < startingGas {
|
2019-02-08 12:45:23 -08:00
|
|
|
panic(sdk.ErrorGasOverflow{Descriptor: "tx gas summation"})
|
2018-12-10 12:39:29 -08:00
|
|
|
}
|
2018-11-20 20:07:30 -08:00
|
|
|
}
|
2018-01-12 11:49:53 -08:00
|
|
|
}()
|
|
|
|
|
2018-06-21 15:05:25 -07:00
|
|
|
var msgs = tx.GetMsgs()
|
2018-08-22 04:38:55 -07:00
|
|
|
if err := validateBasicTxMsgs(msgs); err != nil {
|
2018-07-09 16:16:43 -07:00
|
|
|
return err.Result()
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
|
|
|
|
2018-02-28 19:17:48 -08:00
|
|
|
if app.anteHandler != nil {
|
2018-11-16 10:33:47 -08:00
|
|
|
var anteCtx sdk.Context
|
|
|
|
var msCache sdk.CacheMultiStore
|
|
|
|
|
|
|
|
// Cache wrap context before anteHandler call in case it aborts.
|
|
|
|
// This is required for both CheckTx and DeliverTx.
|
2019-01-08 15:56:53 -08:00
|
|
|
// Ref: https://github.com/cosmos/cosmos-sdk/issues/2772
|
|
|
|
//
|
2018-11-16 10:33:47 -08:00
|
|
|
// NOTE: Alternatively, we could require that anteHandler ensures that
|
|
|
|
// writes do not happen if aborted/failed. This may have some
|
|
|
|
// performance benefits, but it'll be more difficult to get right.
|
2018-11-20 01:06:14 -08:00
|
|
|
anteCtx, msCache = app.cacheTxContext(ctx, txBytes)
|
2018-11-16 10:33:47 -08:00
|
|
|
|
2019-03-25 17:54:23 -07:00
|
|
|
newCtx, result, abort := app.anteHandler(anteCtx, tx, mode == runTxModeSimulate)
|
2018-02-28 19:17:48 -08:00
|
|
|
if !newCtx.IsZero() {
|
2019-01-08 15:56:53 -08:00
|
|
|
// At this point, newCtx.MultiStore() is cache-wrapped, or something else
|
|
|
|
// replaced by the ante handler. We want the original multistore, not one
|
|
|
|
// which was cache-wrapped for the ante handler.
|
|
|
|
//
|
|
|
|
// Also, in the case of the tx aborting, we need to track gas consumed via
|
|
|
|
// the instantiated gas meter in the ante handler, so we update the context
|
|
|
|
// prior to returning.
|
2018-11-20 01:06:14 -08:00
|
|
|
ctx = newCtx.WithMultiStore(ms)
|
2018-02-28 19:17:48 -08:00
|
|
|
}
|
2019-01-08 15:56:53 -08:00
|
|
|
|
2019-02-07 17:52:24 -08:00
|
|
|
gasWanted = result.GasWanted
|
|
|
|
|
2019-01-08 15:56:53 -08:00
|
|
|
if abort {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2018-11-16 10:33:47 -08:00
|
|
|
msCache.Write()
|
2018-01-26 05:11:01 -08:00
|
|
|
}
|
|
|
|
|
2018-11-16 10:33:47 -08:00
|
|
|
// Create a new context based off of the existing context with a cache wrapped
|
|
|
|
// multi-store in case message processing fails.
|
2018-11-20 01:06:14 -08:00
|
|
|
runMsgCtx, msCache := app.cacheTxContext(ctx, txBytes)
|
2018-11-16 10:33:47 -08:00
|
|
|
result = app.runMsgs(runMsgCtx, msgs, mode)
|
2018-06-26 18:53:41 -07:00
|
|
|
result.GasWanted = gasWanted
|
2018-05-07 11:49:34 -07:00
|
|
|
|
2019-07-30 09:11:09 -07:00
|
|
|
// Safety check: don't write the cache state unless we're in DeliverTx.
|
|
|
|
if mode != runTxModeDeliver {
|
2019-05-16 18:32:20 -07:00
|
|
|
return result
|
2018-11-20 01:06:14 -08:00
|
|
|
}
|
|
|
|
|
2018-08-22 04:38:55 -07:00
|
|
|
// only update state if all messages pass
|
|
|
|
if result.IsOK() {
|
2018-01-26 05:11:01 -08:00
|
|
|
msCache.Write()
|
|
|
|
}
|
|
|
|
|
2019-05-16 18:32:20 -07:00
|
|
|
return result
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// EndBlock implements the ABCI interface.
|
2018-01-20 20:08:00 -08:00
|
|
|
func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
|
2018-07-12 16:58:51 -07:00
|
|
|
if app.deliverState.ms.TracingEnabled() {
|
2019-02-01 17:03:09 -08:00
|
|
|
app.deliverState.ms = app.deliverState.ms.SetTracingContext(nil).(sdk.CacheMultiStore)
|
2018-07-12 16:58:51 -07:00
|
|
|
}
|
|
|
|
|
2018-02-17 15:10:55 -08:00
|
|
|
if app.endBlocker != nil {
|
2019-04-05 13:20:58 -07:00
|
|
|
res = app.endBlocker(app.deliverState.ctx, req)
|
2018-02-17 15:10:55 -08:00
|
|
|
}
|
2018-07-12 16:58:51 -07:00
|
|
|
|
2017-12-21 20:19:44 -08:00
|
|
|
return
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
|
|
|
|
2019-04-23 06:33:11 -07:00
|
|
|
// Commit implements the ABCI interface. It will commit all state that exists in
|
|
|
|
// the deliver state's multi-store and includes the resulting commit ID in the
|
|
|
|
// returned abci.ResponseCommit. Commit will set the check state based on the
|
|
|
|
// latest header and reset the deliver state. Also, if a non-zero halt height is
|
|
|
|
// defined in config, Commit will execute a deferred function call to check
|
|
|
|
// against that height and gracefully halt if it matches the latest committed
|
|
|
|
// height.
|
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 19:23:56 -08:00
|
|
|
|
2019-02-05 19:11:57 -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()
|
2019-02-07 17:52:24 -08:00
|
|
|
app.logger.Debug("Commit synced", "commit", fmt.Sprintf("%X", commitID))
|
2019-02-05 19:11:57 -08:00
|
|
|
|
|
|
|
// Reset the Check state to the latest committed.
|
|
|
|
//
|
2019-04-23 06:33:11 -07:00
|
|
|
// NOTE: This is 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)
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// empty/reset the deliver state
|
2018-02-27 20:46:27 -08:00
|
|
|
app.deliverState = nil
|
2018-02-17 15:10:55 -08:00
|
|
|
|
2019-04-23 06:33:11 -07:00
|
|
|
defer func() {
|
|
|
|
if app.haltHeight > 0 && uint64(header.Height) == app.haltHeight {
|
|
|
|
app.logger.Info("halting node per configuration", "height", app.haltHeight)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-12-26 17:04:48 -08:00
|
|
|
return abci.ResponseCommit{
|
|
|
|
Data: commitID.Hash,
|
|
|
|
}
|
2017-12-01 09:10:17 -08:00
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// State
|
|
|
|
|
|
|
|
type state struct {
|
|
|
|
ms sdk.CacheMultiStore
|
|
|
|
ctx sdk.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *state) CacheMultiStore() sdk.CacheMultiStore {
|
|
|
|
return st.ms.CacheMultiStore()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *state) Context() sdk.Context {
|
|
|
|
return st.ctx
|
|
|
|
}
|