2019-09-04 10:33:32 -07:00
|
|
|
package baseapp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2019-09-09 07:08:10 -07:00
|
|
|
"syscall"
|
2019-09-04 10:33:32 -07:00
|
|
|
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2020-03-26 09:46:10 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-06-18 11:12:44 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/telemetry"
|
2019-09-04 10:33:32 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-12-27 09:57:54 -08:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2019-09-04 10:33:32 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// InitChain implements the ABCI interface. It runs the initialization logic
|
|
|
|
// directly on the CommitMultiStore.
|
|
|
|
func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) {
|
|
|
|
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)
|
|
|
|
|
2020-04-16 08:10:39 -07:00
|
|
|
// Store the consensus params in the BaseApp's paramstore. Note, this must be
|
|
|
|
// done after the deliver state and context have been set as it's persisted
|
|
|
|
// to state.
|
|
|
|
if req.ConsensusParams != nil {
|
2020-04-22 11:24:13 -07:00
|
|
|
app.StoreConsensusParams(app.deliverState.ctx, req.ConsensusParams)
|
2020-04-16 08:10:39 -07:00
|
|
|
}
|
|
|
|
|
2019-09-04 10:33:32 -07:00
|
|
|
if app.initChainer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add block gas meter for any genesis transactions (allow infinite gas)
|
|
|
|
app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter())
|
|
|
|
|
|
|
|
res = app.initChainer(app.deliverState.ctx, req)
|
|
|
|
|
|
|
|
// sanity check
|
|
|
|
if len(req.Validators) > 0 {
|
|
|
|
if len(req.Validators) != len(res.Validators) {
|
|
|
|
panic(
|
|
|
|
fmt.Errorf(
|
|
|
|
"len(RequestInitChain.Validators) != len(GenesisValidators) (%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("genesisValidators[%d] != req.Validators[%d] ", i, i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: We don't commit, but BeginBlock for block 1 starts from this
|
|
|
|
// deliverState.
|
2019-09-19 06:21:38 -07:00
|
|
|
return res
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info implements the ABCI interface.
|
|
|
|
func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo {
|
|
|
|
lastCommitID := app.cms.LastCommitID()
|
|
|
|
|
|
|
|
return abci.ResponseInfo{
|
|
|
|
Data: app.name,
|
|
|
|
LastBlockHeight: lastCommitID.Version,
|
|
|
|
LastBlockAppHash: lastCommitID.Hash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOption implements the ABCI interface.
|
|
|
|
func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOption) {
|
|
|
|
// TODO: Implement!
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterPeerByAddrPort filters peers by address/port.
|
|
|
|
func (app *BaseApp) FilterPeerByAddrPort(info string) abci.ResponseQuery {
|
|
|
|
if app.addrPeerFilter != nil {
|
|
|
|
return app.addrPeerFilter(info)
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-09-04 10:33:32 -07:00
|
|
|
return abci.ResponseQuery{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterPeerByIDfilters peers by node ID.
|
|
|
|
func (app *BaseApp) FilterPeerByID(info string) abci.ResponseQuery {
|
|
|
|
if app.idPeerFilter != nil {
|
|
|
|
return app.idPeerFilter(info)
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-09-04 10:33:32 -07:00
|
|
|
return abci.ResponseQuery{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeginBlock implements the ABCI application interface.
|
|
|
|
func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
|
2020-06-18 11:12:44 -07:00
|
|
|
defer telemetry.MeasureSince("abci", "begin_block")
|
|
|
|
|
2019-09-04 10:33:32 -07:00
|
|
|
if app.cms.TracingEnabled() {
|
|
|
|
app.cms.SetTracingContext(sdk.TraceContext(
|
|
|
|
map[string]interface{}{"blockHeight": req.Header.Height},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.validateHeight(req); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if app.deliverState == nil {
|
|
|
|
app.setDeliverState(req.Header)
|
|
|
|
} else {
|
|
|
|
// In the first block, app.deliverState.ctx will already be initialized
|
|
|
|
// by InitChain. Context is now updated with Header information.
|
|
|
|
app.deliverState.ctx = app.deliverState.ctx.
|
|
|
|
WithBlockHeader(req.Header).
|
|
|
|
WithBlockHeight(req.Header.Height)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add block gas meter
|
|
|
|
var gasMeter sdk.GasMeter
|
2020-04-16 08:10:39 -07:00
|
|
|
if maxGas := app.getMaximumBlockGas(app.deliverState.ctx); maxGas > 0 {
|
2019-09-04 10:33:32 -07:00
|
|
|
gasMeter = sdk.NewGasMeter(maxGas)
|
|
|
|
} else {
|
|
|
|
gasMeter = sdk.NewInfiniteGasMeter()
|
|
|
|
}
|
|
|
|
|
|
|
|
app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(gasMeter)
|
|
|
|
|
|
|
|
if app.beginBlocker != nil {
|
|
|
|
res = app.beginBlocker(app.deliverState.ctx, req)
|
|
|
|
}
|
|
|
|
// set the signed validators for addition to context in deliverTx
|
|
|
|
app.voteInfos = req.LastCommitInfo.GetVotes()
|
2019-09-19 06:21:38 -07:00
|
|
|
return res
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// EndBlock implements the ABCI interface.
|
|
|
|
func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
|
2020-06-18 11:12:44 -07:00
|
|
|
defer telemetry.MeasureSince("abci", "end_block")
|
|
|
|
|
2019-09-04 10:33:32 -07:00
|
|
|
if app.deliverState.ms.TracingEnabled() {
|
|
|
|
app.deliverState.ms = app.deliverState.ms.SetTracingContext(nil).(sdk.CacheMultiStore)
|
|
|
|
}
|
|
|
|
|
|
|
|
if app.endBlocker != nil {
|
|
|
|
res = app.endBlocker(app.deliverState.ctx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
// CheckTx implements the ABCI interface and executes a tx in CheckTx mode. In
|
|
|
|
// CheckTx mode, messages are not executed. This means messages are only validated
|
|
|
|
// and only the AnteHandler is executed. State is persisted to the BaseApp's
|
|
|
|
// internal CheckTx state if the AnteHandler passes. Otherwise, the ResponseCheckTx
|
|
|
|
// will contain releveant error information. Regardless of tx execution outcome,
|
|
|
|
// the ResponseCheckTx will contain relevant gas execution context.
|
|
|
|
func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
|
2020-06-18 11:12:44 -07:00
|
|
|
defer telemetry.MeasureSince("abci", "check_tx")
|
|
|
|
|
2019-09-04 10:33:32 -07:00
|
|
|
tx, err := app.txDecoder(req.Tx)
|
2019-12-27 09:57:54 -08:00
|
|
|
if err != nil {
|
2020-07-05 09:56:17 -07:00
|
|
|
return sdkerrors.ResponseCheckTx(err, 0, 0, app.trace)
|
2019-12-27 09:57:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var mode runTxMode
|
|
|
|
|
2019-10-23 07:43:58 -07:00
|
|
|
switch {
|
|
|
|
case req.Type == abci.CheckTxType_New:
|
2019-12-27 09:57:54 -08:00
|
|
|
mode = runTxModeCheck
|
|
|
|
|
2019-10-23 07:43:58 -07:00
|
|
|
case req.Type == abci.CheckTxType_Recheck:
|
2019-12-27 09:57:54 -08:00
|
|
|
mode = runTxModeReCheck
|
|
|
|
|
2019-10-23 07:43:58 -07:00
|
|
|
default:
|
2019-12-27 09:57:54 -08:00
|
|
|
panic(fmt.Sprintf("unknown RequestCheckTx type: %s", req.Type))
|
|
|
|
}
|
|
|
|
|
|
|
|
gInfo, result, err := app.runTx(mode, req.Tx, tx)
|
|
|
|
if err != nil {
|
2020-07-05 09:56:17 -07:00
|
|
|
return sdkerrors.ResponseCheckTx(err, gInfo.GasWanted, gInfo.GasUsed, app.trace)
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return abci.ResponseCheckTx{
|
2019-12-27 09:57:54 -08:00
|
|
|
GasWanted: int64(gInfo.GasWanted), // TODO: Should type accept unsigned ints?
|
|
|
|
GasUsed: int64(gInfo.GasUsed), // TODO: Should type accept unsigned ints?
|
2019-09-04 10:33:32 -07:00
|
|
|
Log: result.Log,
|
2019-12-27 09:57:54 -08:00
|
|
|
Data: result.Data,
|
2020-03-26 09:46:10 -07:00
|
|
|
Events: result.Events,
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
// DeliverTx implements the ABCI interface and executes a tx in DeliverTx mode.
|
|
|
|
// State only gets persisted if all messages are valid and get executed successfully.
|
|
|
|
// Otherwise, the ResponseDeliverTx will contain releveant error information.
|
|
|
|
// Regardless of tx execution outcome, the ResponseDeliverTx will contain relevant
|
|
|
|
// gas execution context.
|
|
|
|
func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
|
2020-06-18 11:12:44 -07:00
|
|
|
defer telemetry.MeasureSince("abci", "deliver_tx")
|
|
|
|
|
2019-09-04 10:33:32 -07:00
|
|
|
tx, err := app.txDecoder(req.Tx)
|
|
|
|
if err != nil {
|
2020-07-05 09:56:17 -07:00
|
|
|
return sdkerrors.ResponseDeliverTx(err, 0, 0, app.trace)
|
2019-12-27 09:57:54 -08:00
|
|
|
}
|
|
|
|
|
2020-06-18 11:12:44 -07:00
|
|
|
gInfo := sdk.GasInfo{}
|
|
|
|
resultStr := "successful"
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
telemetry.IncrCounter(1, "tx", "count")
|
|
|
|
telemetry.IncrCounter(1, "tx", resultStr)
|
|
|
|
telemetry.SetGauge(float32(gInfo.GasUsed), "tx", "gas", "used")
|
|
|
|
telemetry.SetGauge(float32(gInfo.GasWanted), "tx", "gas", "wanted")
|
|
|
|
}()
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
gInfo, result, err := app.runTx(runTxModeDeliver, req.Tx, tx)
|
|
|
|
if err != nil {
|
2020-06-18 11:12:44 -07:00
|
|
|
resultStr = "failed"
|
2020-07-05 09:56:17 -07:00
|
|
|
return sdkerrors.ResponseDeliverTx(err, gInfo.GasWanted, gInfo.GasUsed, app.trace)
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return abci.ResponseDeliverTx{
|
2019-12-27 09:57:54 -08:00
|
|
|
GasWanted: int64(gInfo.GasWanted), // TODO: Should type accept unsigned ints?
|
|
|
|
GasUsed: int64(gInfo.GasUsed), // TODO: Should type accept unsigned ints?
|
2019-09-04 10:33:32 -07:00
|
|
|
Log: result.Log,
|
2019-12-27 09:57:54 -08:00
|
|
|
Data: result.Data,
|
2020-03-26 09:46:10 -07:00
|
|
|
Events: result.Events,
|
2019-09-04 10:33:32 -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.
|
|
|
|
func (app *BaseApp) Commit() (res abci.ResponseCommit) {
|
2020-06-18 11:12:44 -07:00
|
|
|
defer telemetry.MeasureSince("abci", "commit")
|
|
|
|
|
2019-09-04 10:33:32 -07:00
|
|
|
header := app.deliverState.ctx.BlockHeader()
|
|
|
|
|
|
|
|
// Write the DeliverTx state which is cache-wrapped and commit the MultiStore.
|
|
|
|
// The write to the DeliverTx state writes all state transitions to the root
|
|
|
|
// MultiStore (app.cms) so when Commit() is called is persists those values.
|
|
|
|
app.deliverState.ms.Write()
|
|
|
|
commitID := app.cms.Commit()
|
|
|
|
app.logger.Debug("Commit synced", "commit", fmt.Sprintf("%X", commitID))
|
|
|
|
|
|
|
|
// Reset the Check state to the latest committed.
|
|
|
|
//
|
|
|
|
// NOTE: This is safe because Tendermint holds a lock on the mempool for
|
|
|
|
// Commit. Use the header from this latest block.
|
|
|
|
app.setCheckState(header)
|
|
|
|
|
|
|
|
// empty/reset the deliver state
|
|
|
|
app.deliverState = nil
|
|
|
|
|
2019-12-03 01:24:12 -08:00
|
|
|
var halt bool
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case app.haltHeight > 0 && uint64(header.Height) >= app.haltHeight:
|
|
|
|
halt = true
|
|
|
|
|
|
|
|
case app.haltTime > 0 && header.Time.Unix() >= int64(app.haltTime):
|
|
|
|
halt = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if halt {
|
|
|
|
// Halt the binary and allow Tendermint to receive the ResponseCommit
|
|
|
|
// response with the commit ID hash. This will allow the node to successfully
|
|
|
|
// restart and process blocks assuming the halt configuration has been
|
|
|
|
// reset or moved to a more distant value.
|
|
|
|
app.halt()
|
|
|
|
}
|
|
|
|
|
2019-09-04 10:33:32 -07:00
|
|
|
return abci.ResponseCommit{
|
|
|
|
Data: commitID.Hash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:08:10 -07:00
|
|
|
// halt attempts to gracefully shutdown the node via SIGINT and SIGTERM falling
|
|
|
|
// back on os.Exit if both fail.
|
|
|
|
func (app *BaseApp) halt() {
|
|
|
|
app.logger.Info("halting node per configuration", "height", app.haltHeight, "time", app.haltTime)
|
|
|
|
|
|
|
|
p, err := os.FindProcess(os.Getpid())
|
|
|
|
if err == nil {
|
|
|
|
// attempt cascading signals in case SIGINT fails (os dependent)
|
|
|
|
sigIntErr := p.Signal(syscall.SIGINT)
|
|
|
|
sigTermErr := p.Signal(syscall.SIGTERM)
|
|
|
|
|
|
|
|
if sigIntErr == nil || sigTermErr == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resort to exiting immediately if the process could not be found or killed
|
|
|
|
// via SIGINT/SIGTERM signals.
|
|
|
|
app.logger.Info("failed to send SIGINT/SIGTERM; exiting...")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2019-09-04 10:33:32 -07:00
|
|
|
// Query implements the ABCI interface. It delegates to CommitMultiStore if it
|
|
|
|
// implements Queryable.
|
2019-12-27 09:57:54 -08:00
|
|
|
func (app *BaseApp) Query(req abci.RequestQuery) abci.ResponseQuery {
|
2020-06-18 11:12:44 -07:00
|
|
|
defer telemetry.MeasureSince("abci", "query")
|
|
|
|
|
2020-06-04 11:48:06 -07:00
|
|
|
// handle gRPC routes first rather than calling splitPath because '/' characters
|
|
|
|
// are used as part of gRPC paths
|
|
|
|
if grpcHandler := app.grpcQueryRouter.Route(req.Path); grpcHandler != nil {
|
|
|
|
return app.handleQueryGRPC(grpcHandler, req)
|
|
|
|
}
|
|
|
|
|
2019-09-04 10:33:32 -07:00
|
|
|
path := splitPath(req.Path)
|
|
|
|
if len(path) == 0 {
|
2019-12-27 09:57:54 -08:00
|
|
|
sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no query path provided"))
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
switch path[0] {
|
|
|
|
// "/app" prefix for special application queries
|
|
|
|
case "app":
|
|
|
|
return handleQueryApp(app, path, req)
|
|
|
|
|
|
|
|
case "store":
|
|
|
|
return handleQueryStore(app, path, req)
|
|
|
|
|
|
|
|
case "p2p":
|
2019-12-27 09:57:54 -08:00
|
|
|
return handleQueryP2P(app, path)
|
2019-09-04 10:33:32 -07:00
|
|
|
|
|
|
|
case "custom":
|
|
|
|
return handleQueryCustom(app, path, req)
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown query path"))
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
2020-06-04 11:48:06 -07:00
|
|
|
func (app *BaseApp) handleQueryGRPC(handler GRPCQueryHandler, req abci.RequestQuery) abci.ResponseQuery {
|
|
|
|
ctx, err := app.createQueryContext(req)
|
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.QueryResult(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := handler(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
res = sdkerrors.QueryResult(err)
|
|
|
|
res.Height = req.Height
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *BaseApp) createQueryContext(req abci.RequestQuery) (sdk.Context, error) {
|
|
|
|
// 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.Context{},
|
|
|
|
sdkerrors.Wrap(
|
|
|
|
sdkerrors.ErrInvalidRequest,
|
|
|
|
"cannot query with proof when height <= 1; please provide a valid height",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheMS, err := app.cms.CacheMultiStoreWithVersion(req.Height)
|
|
|
|
if err != nil {
|
|
|
|
return sdk.Context{},
|
|
|
|
sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrInvalidRequest,
|
|
|
|
"failed to load state at height %d; %s (latest height: %d)", req.Height, err, app.LastBlockHeight(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache wrap the commit-multistore for safety
|
|
|
|
ctx := sdk.NewContext(
|
|
|
|
cacheMS, app.checkState.ctx.BlockHeader(), true, app.logger,
|
|
|
|
).WithMinGasPrices(app.minGasPrices)
|
|
|
|
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) abci.ResponseQuery {
|
2019-09-04 10:33:32 -07:00
|
|
|
if len(path) >= 2 {
|
|
|
|
switch path[1] {
|
|
|
|
case "simulate":
|
|
|
|
txBytes := req.Data
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2019-09-04 10:33:32 -07:00
|
|
|
tx, err := app.txDecoder(txBytes)
|
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.QueryResult(sdkerrors.Wrap(err, "failed to decode tx"))
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:03:49 -07:00
|
|
|
gInfo, res, err := app.Simulate(txBytes, tx)
|
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.QueryResult(sdkerrors.Wrap(err, "failed to simulate tx"))
|
|
|
|
}
|
|
|
|
|
2020-03-26 09:46:10 -07:00
|
|
|
simRes := &sdk.SimulationResponse{
|
2020-03-19 16:03:49 -07:00
|
|
|
GasInfo: gInfo,
|
|
|
|
Result: res,
|
|
|
|
}
|
2019-12-27 09:57:54 -08:00
|
|
|
|
2020-03-26 09:46:10 -07:00
|
|
|
bz, err := codec.ProtoMarshalJSON(simRes)
|
2020-03-24 13:45:34 -07:00
|
|
|
if err != nil {
|
|
|
|
return sdkerrors.QueryResult(sdkerrors.Wrap(err, "failed to JSON encode simulation response"))
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
return abci.ResponseQuery{
|
|
|
|
Codespace: sdkerrors.RootCodespace,
|
|
|
|
Height: req.Height,
|
2020-03-24 13:45:34 -07:00
|
|
|
Value: bz,
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
case "version":
|
|
|
|
return abci.ResponseQuery{
|
2019-12-27 09:57:54 -08:00
|
|
|
Codespace: sdkerrors.RootCodespace,
|
2019-09-04 10:33:32 -07:00
|
|
|
Height: req.Height,
|
|
|
|
Value: []byte(app.appVersion),
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query: %s", path))
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.QueryResult(
|
|
|
|
sdkerrors.Wrap(
|
|
|
|
sdkerrors.ErrUnknownRequest,
|
|
|
|
"expected second parameter to be either 'simulate' or 'version', neither was present",
|
|
|
|
),
|
|
|
|
)
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleQueryStore(app *BaseApp, path []string, req abci.RequestQuery) abci.ResponseQuery {
|
|
|
|
// "/store" prefix for store queries
|
|
|
|
queryable, ok := app.cms.(sdk.Queryable)
|
|
|
|
if !ok {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "multistore doesn't support queries"))
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
req.Path = "/" + strings.Join(path[1:], "/")
|
|
|
|
|
|
|
|
// 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 {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.QueryResult(
|
|
|
|
sdkerrors.Wrap(
|
|
|
|
sdkerrors.ErrInvalidRequest,
|
|
|
|
"cannot query with proof when height <= 1; please provide a valid height",
|
|
|
|
),
|
|
|
|
)
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
resp := queryable.Query(req)
|
|
|
|
resp.Height = req.Height
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
func handleQueryP2P(app *BaseApp, path []string) abci.ResponseQuery {
|
2019-09-04 10:33:32 -07:00
|
|
|
// "/p2p" prefix for p2p queries
|
|
|
|
if len(path) >= 4 {
|
|
|
|
cmd, typ, arg := path[1], path[2], path[3]
|
|
|
|
switch cmd {
|
|
|
|
case "filter":
|
|
|
|
switch typ {
|
|
|
|
case "addr":
|
|
|
|
return app.FilterPeerByAddrPort(arg)
|
|
|
|
|
|
|
|
case "id":
|
|
|
|
return app.FilterPeerByID(arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "expected second parameter to be 'filter'"))
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.QueryResult(
|
|
|
|
sdkerrors.Wrap(
|
|
|
|
sdkerrors.ErrUnknownRequest, "expected path is p2p filter <addr|id> <parameter>",
|
|
|
|
),
|
|
|
|
)
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) abci.ResponseQuery {
|
2019-09-04 10:33:32 -07:00
|
|
|
// path[0] should be "custom" because "/custom" prefix is required for keeper
|
|
|
|
// queries.
|
|
|
|
//
|
2019-10-14 15:05:10 -07:00
|
|
|
// The QueryRouter routes using path[1]. For example, in the path
|
|
|
|
// "custom/gov/proposal", QueryRouter routes using "gov".
|
2019-09-04 10:33:32 -07:00
|
|
|
if len(path) < 2 || path[1] == "" {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no route for custom query specified"))
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
querier := app.queryRouter.Route(path[1])
|
|
|
|
if querier == nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "no custom querier found for route %s", path[1]))
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
2020-06-04 11:48:06 -07:00
|
|
|
ctx, err := app.createQueryContext(req)
|
2019-09-04 10:33:32 -07:00
|
|
|
if err != nil {
|
2020-06-04 11:48:06 -07:00
|
|
|
return sdkerrors.QueryResult(err)
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Passes the rest of the path as an argument to the querier.
|
|
|
|
//
|
|
|
|
// For example, in the path "custom/gov/proposal/test", the gov querier gets
|
|
|
|
// []string{"proposal", "test"} as the path.
|
2019-12-27 09:57:54 -08:00
|
|
|
resBytes, err := querier(ctx, path[2:], req)
|
|
|
|
if err != nil {
|
2020-06-04 11:48:06 -07:00
|
|
|
res := sdkerrors.QueryResult(err)
|
|
|
|
res.Height = req.Height
|
|
|
|
return res
|
2019-09-04 10:33:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return abci.ResponseQuery{
|
|
|
|
Height: req.Height,
|
|
|
|
Value: resBytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitPath splits a string path using the delimiter '/'.
|
|
|
|
//
|
|
|
|
// e.g. "this/is/funny" becomes []string{"this", "is", "funny"}
|
|
|
|
func splitPath(requestPath string) (path []string) {
|
|
|
|
path = strings.Split(requestPath, "/")
|
|
|
|
|
|
|
|
// first element is empty string
|
|
|
|
if len(path) > 0 && path[0] == "" {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|