cosmos-sdk/app/base.go
2017-10-25 19:56:48 +02:00

80 lines
1.7 KiB
Go

package app
import (
abci "github.com/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk"
"github.com/cosmos/cosmos-sdk/errors"
"github.com/cosmos/cosmos-sdk/util"
)
// BaseApp - The ABCI application
type BaseApp struct {
*StoreApp
handler sdk.Handler
clock sdk.Ticker
}
var _ abci.Application = &BaseApp{}
// NewBaseApp extends a StoreApp with a handler and a ticker,
// which it binds to the proper abci calls
func NewBaseApp(store *StoreApp, handler sdk.Handler, clock sdk.Ticker) *BaseApp {
return &BaseApp{
StoreApp: store,
handler: handler,
clock: clock,
}
}
// DeliverTx - ABCI - dispatches to the handler
func (app *BaseApp) DeliverTx(txBytes []byte) abci.Result {
// TODO: use real context on refactor
ctx := util.MockContext(
app.GetChainID(),
app.WorkingHeight(),
)
// Note: first decorator must parse bytes
res, err := app.handler.DeliverTx(ctx, app.Append(), txBytes)
if err != nil {
return errors.Result(err)
}
app.AddValChange(res.Diff)
return sdk.ToABCI(res)
}
// CheckTx - ABCI - dispatches to the handler
func (app *BaseApp) CheckTx(txBytes []byte) abci.Result {
// TODO: use real context on refactor
ctx := util.MockContext(
app.GetChainID(),
app.WorkingHeight(),
)
// Note: first decorator must parse bytes
res, err := app.handler.CheckTx(ctx, app.Check(), txBytes)
if err != nil {
return errors.Result(err)
}
return sdk.ToABCI(res)
}
// BeginBlock - ABCI - triggers Tick actions
func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) {
// execute tick if present
if app.clock != nil {
ctx := util.MockContext(
app.GetChainID(),
app.WorkingHeight(),
)
diff, err := app.clock.Tick(ctx, app.Append())
if err != nil {
panic(err)
}
app.AddValChange(diff)
}
}