cosmos-sdk/app/app.go

316 lines
6.8 KiB
Go
Raw Normal View History

package app
import (
2017-12-01 09:10:17 -08:00
"bytes"
"fmt"
"os"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
abci "github.com/tendermint/abci/types"
2017-12-26 17:04:48 -08:00
cmn "github.com/tendermint/tmlibs/common"
2017-12-01 09:10:17 -08:00
"github.com/tendermint/tmlibs/log"
2018-01-12 13:59:19 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
)
2017-12-26 17:04:48 -08:00
var mainHeaderKey = []byte("header")
2017-12-01 09:10:17 -08:00
2017-12-19 21:04:40 -08:00
// App - The ABCI application
type App struct {
2017-12-01 09:10:17 -08:00
logger log.Logger
// App name from abci.Info
name string
2017-12-21 23:30:44 -08:00
// Main (uncached) state
2018-01-12 13:59:19 -08:00
ms sdk.CommitMultiStore
2017-12-01 09:10:17 -08:00
2018-01-12 13:59:19 -08:00
// Unmarshal []byte into sdk.Tx
txDecoder sdk.TxDecoder
// Ante handler for fee and auth.
2018-01-12 13:59:19 -08:00
defaultAnteHandler sdk.AnteHandler
// Handle any kind of message.
router Router
//--------------------
// Volatile
2017-12-26 17:04:48 -08:00
// CheckTx state, a cache-wrap of `.ms`.
2018-01-12 13:59:19 -08:00
msCheck sdk.CacheMultiStore
2017-12-01 09:10:17 -08:00
2017-12-26 17:04:48 -08:00
// DeliverTx state, a cache-wrap of `.ms`.
2018-01-12 13:59:19 -08:00
msDeliver sdk.CacheMultiStore
2017-12-21 23:30:44 -08:00
// Current block header
2017-12-26 17:04:48 -08:00
header abci.Header
2017-12-01 09:10:17 -08:00
// Cached validator changes from DeliverTx.
2017-12-20 17:34:51 -08:00
valUpdates []abci.Validator
}
2017-12-19 21:04:40 -08:00
var _ abci.Application = &App{}
2018-01-12 13:59:19 -08:00
func NewApp(name string, ms sdk.CommitMultiStore) *App {
2017-12-19 21:04:40 -08:00
return &App{
logger: makeDefaultLogger(),
name: name,
ms: ms,
router: NewRouter(),
2017-12-01 09:10:17 -08:00
}
2017-12-19 21:04:40 -08:00
}
2017-12-21 23:30:44 -08:00
func (app *App) Name() string {
return app.name
}
2018-01-12 13:59:19 -08:00
func (app *App) SetTxDecoder(txDecoder sdk.TxDecoder) {
app.txDecoder = txDecoder
2017-12-19 21:04:40 -08:00
}
2018-01-12 13:59:19 -08:00
func (app *App) SetDefaultAnteHandler(ah sdk.AnteHandler) {
app.defaultAnteHandler = ah
2018-01-03 17:20:21 -08:00
}
func (app *App) Router() Router {
return app.router
2017-12-19 21:04:40 -08:00
}
2017-12-01 09:10:17 -08:00
/* TODO consider:
func (app *App) SetBeginBlocker(...) {}
func (app *App) SetEndBlocker(...) {}
func (app *App) SetInitStater(...) {}
*/
2018-01-12 13:59:19 -08:00
func (app *App) LoadLatestVersion(mainKey sdk.SubstoreKey) error {
2017-12-26 17:04:48 -08:00
app.ms.LoadLatestVersion()
2018-01-12 13:59:19 -08:00
return app.initFromStore(mainKey)
2017-12-19 21:04:40 -08:00
}
2018-01-12 13:59:19 -08:00
func (app *App) LoadVersion(version int64, mainKey sdk.SubstoreKey) error {
2017-12-26 17:04:48 -08:00
app.ms.LoadVersion(version)
2018-01-12 13:59:19 -08:00
return app.initFromStore(mainKey)
2017-12-20 17:34:51 -08:00
}
2017-12-26 17:04:48 -08:00
// The last CommitID of the multistore.
2018-01-12 13:59:19 -08:00
func (app *App) LastCommitID() sdk.CommitID {
2017-12-26 17:04:48 -08:00
return app.ms.LastCommitID()
}
// The last commited block height.
func (app *App) LastBlockHeight() int64 {
return app.ms.LastCommitID().Version
}
// Initializes the remaining logic from app.ms.
2018-01-12 13:59:19 -08:00
func (app *App) initFromStore(mainKey sdk.SubstoreKey) error {
2017-12-26 17:04:48 -08:00
lastCommitID := app.ms.LastCommitID()
2018-01-12 13:59:19 -08:00
main := app.ms.GetKVStore(mainKey)
2017-12-26 17:04:48 -08:00
header := abci.Header{}
2017-12-01 09:10:17 -08:00
2017-12-19 21:04:40 -08:00
// Main store should exist.
2018-01-12 13:59:19 -08:00
if main == nil {
2017-12-19 21:04:40 -08:00
return errors.New("App expects MultiStore with 'main' KVStore")
}
2017-12-21 23:30:44 -08:00
// If we've committed before, we expect main://<mainHeaderKey>.
2017-12-01 09:10:17 -08:00
if !lastCommitID.IsZero() {
2017-12-21 23:30:44 -08:00
headerBytes := main.Get(mainHeaderKey)
if len(headerBytes) == 0 {
errStr := fmt.Sprintf("Version > 0 but missing key %s", mainHeaderKey)
2017-12-19 21:04:40 -08:00
return errors.New(errStr)
2017-12-01 09:10:17 -08:00
}
2017-12-26 17:04:48 -08:00
err := proto.Unmarshal(headerBytes, &header)
2017-12-01 09:10:17 -08:00
if err != nil {
2017-12-19 21:04:40 -08:00
return errors.Wrap(err, "Failed to parse Header")
2017-12-01 09:10:17 -08:00
}
2017-12-21 23:30:44 -08:00
lastVersion := lastCommitID.Version
if header.Height != lastVersion {
errStr := fmt.Sprintf("Expected main://%s.Height %v but got %v", mainHeaderKey, lastVersion, header.Height)
2017-12-19 21:04:40 -08:00
return errors.New(errStr)
2017-12-01 09:10:17 -08:00
}
}
2017-12-20 17:34:51 -08:00
// Set App state.
app.header = header
2017-12-26 17:04:48 -08:00
app.msCheck = nil
app.msDeliver = nil
2017-12-20 17:34:51 -08:00
app.valUpdates = nil
return nil
2017-12-01 09:10:17 -08:00
}
//----------------------------------------
2017-12-21 20:19:44 -08:00
// Implements ABCI
func (app *App) Info(req abci.RequestInfo) abci.ResponseInfo {
2017-12-26 17:04:48 -08:00
lastCommitID := app.ms.LastCommitID()
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-12-21 20:19:44 -08:00
}
2017-12-21 20:19:44 -08:00
// Implements ABCI
2017-12-21 23:30:44 -08:00
func (app *App) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOption) {
// TODO: Implement
return
2017-12-21 20:19:44 -08:00
}
// Implements ABCI
2017-12-21 23:30:44 -08:00
func (app *App) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) {
2017-12-21 20:19:44 -08:00
// TODO: Use req.Validators
2017-12-21 23:30:44 -08:00
return
2017-12-21 20:19:44 -08:00
}
// Implements ABCI
2017-12-21 23:30:44 -08:00
func (app *App) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
2017-12-21 20:19:44 -08:00
// TODO: See app/query.go
2017-12-21 23:30:44 -08:00
return
2017-12-21 20:19:44 -08:00
}
// Implements ABCI
2017-12-21 23:30:44 -08:00
func (app *App) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
2017-12-21 20:19:44 -08:00
app.header = req.Header
2017-12-26 17:04:48 -08:00
app.msDeliver = app.ms.CacheMultiStore()
app.msCheck = app.ms.CacheMultiStore()
2017-12-21 23:30:44 -08:00
return
}
2017-12-21 20:19:44 -08:00
// Implements ABCI
2017-12-21 23:30:44 -08:00
func (app *App) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {
2017-12-01 09:10:17 -08:00
result := app.runTx(true, txBytes)
2018-01-03 17:20:21 -08:00
2017-12-26 17:04:48 -08:00
return abci.ResponseCheckTx{
Code: result.Code,
Data: result.Data,
Log: result.Log,
2017-12-26 17:04:48 -08:00
GasWanted: result.GasWanted,
Fee: cmn.KI64Pair{
[]byte(result.FeeDenom),
result.FeeAmount,
},
Tags: result.Tags,
}
}
2017-12-21 20:19:44 -08:00
// Implements ABCI
2017-12-21 23:30:44 -08:00
func (app *App) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {
2017-12-01 09:10:17 -08:00
result := app.runTx(false, txBytes)
2017-12-21 20:19:44 -08:00
// After-handler hooks.
2017-12-26 17:04:48 -08:00
if result.Code == abci.CodeTypeOK {
app.valUpdates = append(app.valUpdates, result.ValidatorUpdates...)
2017-12-21 20:19:44 -08:00
} else {
// Even though the Code is not OK, there will be some side
// effects, like those caused by fee deductions or sequence
// incrementations.
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{
2017-12-26 17:04:48 -08:00
Code: result.Code,
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-01-12 13:59:19 -08:00
func (app *App) runTx(isCheckTx bool, txBytes []byte) (result sdk.Result) {
// Handle any panics.
defer func() {
if r := recover(); r != nil {
2018-01-12 13:59:19 -08:00
result = sdk.Result{
Code: 1, // TODO
Log: fmt.Sprintf("Recovered: %v\n", r),
}
}
}()
2018-01-12 13:59:19 -08:00
var store sdk.MultiStore
if isCheckTx {
store = app.msCheck
} else {
store = app.msDeliver
}
// Initialize arguments to Handler.
2018-01-12 13:59:19 -08:00
var ctx = sdk.NewContext(
store,
app.header,
isCheckTx,
txBytes,
)
// Decode the Tx.
2018-01-12 13:59:19 -08:00
tx, err := app.txDecoder(txBytes)
if err != nil {
2018-01-12 13:59:19 -08:00
return sdk.Result{
Code: 1, // TODO
}
}
// Run the ante handler.
2018-01-12 13:59:19 -08:00
result, abort := app.defaultAnteHandler(ctx, tx)
if isCheckTx || abort {
return result
}
// Match and run route.
msgType := tx.Type()
handler := app.router.Route(msgType)
result = handler(ctx, tx)
return result
}
2017-12-21 20:19:44 -08:00
// Implements ABCI
func (app *App) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
res.ValidatorUpdates = app.valUpdates
app.valUpdates = nil
return
2017-12-01 09:10:17 -08:00
}
2017-12-21 20:19:44 -08:00
// Implements ABCI
func (app *App) Commit() (res abci.ResponseCommit) {
2017-12-26 17:04:48 -08:00
app.msDeliver.Write()
commitID := app.ms.Commit()
app.logger.Debug("Commit synced",
"commit", commitID,
)
2017-12-26 17:04:48 -08:00
return abci.ResponseCommit{
Data: commitID.Hash,
}
2017-12-01 09:10:17 -08:00
}
2017-12-21 20:19:44 -08:00
//----------------------------------------
// Misc.
2017-12-01 09:10:17 -08:00
// Return index of list with validator of same PubKey, or -1 if no match
2017-12-01 09:10:17 -08:00
func pubKeyIndex(val *abci.Validator, list []*abci.Validator) int {
for i, v := range list {
if bytes.Equal(val.PubKey, v.PubKey) {
return i
}
}
return -1
}
// Make a simple default logger
// TODO: Make log capturable for each transaction, and return it in
// ResponseDeliverTx.Log and ResponseCheckTx.Log.
func makeDefaultLogger() log.Logger {
return log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app")
}