From a5fa1874c54d07ba33f7b1b480e7ba03ea7b670d Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Wed, 24 Jan 2018 11:47:51 -0800 Subject: [PATCH] Expose way to get Context --- baseapp/baseapp.go | 54 ++++++++++++++++++++++++++-------------------- baseapp/context.go | 23 ++++++++++++++++++++ 2 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 baseapp/context.go diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 85ae034e25..836fd056e4 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -95,14 +95,34 @@ func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) { app.ms.MountStoreWithDB(key, typ, app.db) } +func (app *BaseApp) TxDecoder() sdk.TxDecoder { + return app.txDecoder +} + func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) { app.txDecoder = txDecoder } +func (app *BaseApp) DefaultAnteHandler() sdk.AnteHandler { + return app.defaultAnteHandler +} + func (app *BaseApp) SetDefaultAnteHandler(ah sdk.AnteHandler) { app.defaultAnteHandler = ah } +func (app *BaseApp) MultiStore() sdk.MultiStore { + return app.ms +} + +func (app *BaseApp) MultiStoreCheck() sdk.MultiStore { + return app.msCheck +} + +func (app *BaseApp) MultiStoreDeliver() sdk.MultiStore { + return app.msDeliver +} + func (app *BaseApp) Router() Router { return app.router } @@ -173,7 +193,7 @@ func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error { //---------------------------------------- -// Implements ABCI +// Implements ABCI. func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo { lastCommitID := app.ms.LastCommitID() @@ -185,25 +205,25 @@ func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo { } } -// Implements ABCI +// Implements ABCI. func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOption) { // TODO: Implement return } -// Implements ABCI +// Implements ABCI. func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) { // TODO: Use req.Validators return } -// Implements ABCI +// Implements ABCI. func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) { // TODO: See app/query.go return } -// Implements ABCI +// Implements ABCI. func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) { app.header = req.Header app.msDeliver = app.ms.CacheMultiStore() @@ -211,7 +231,7 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg return } -// Implements ABCI +// Implements ABCI. func (app *BaseApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) { result := app.runTx(true, txBytes) @@ -230,7 +250,7 @@ func (app *BaseApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) { } -// Implements ABCI +// Implements ABCI. func (app *BaseApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) { result := app.runTx(false, txBytes) @@ -267,20 +287,8 @@ func (app *BaseApp) runTx(isCheckTx bool, txBytes []byte) (result sdk.Result) { } }() - var store sdk.MultiStore - if isCheckTx { - store = app.msCheck - } else { - store = app.msDeliver - } - - // Initialize arguments to Handler. - var ctx = sdk.NewContext( - store, - app.header, - isCheckTx, - txBytes, - ) + // Construct a Context. + var ctx = app.NewContext(isCheckTx, txBytes) // Decode the Tx. tx, err := app.txDecoder(txBytes) @@ -306,14 +314,14 @@ func (app *BaseApp) runTx(isCheckTx bool, txBytes []byte) (result sdk.Result) { return result } -// Implements ABCI +// Implements ABCI. func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) { res.ValidatorUpdates = app.valUpdates app.valUpdates = nil return } -// Implements ABCI +// Implements ABCI. func (app *BaseApp) Commit() (res abci.ResponseCommit) { app.msDeliver.Write() commitID := app.ms.Commit() diff --git a/baseapp/context.go b/baseapp/context.go new file mode 100644 index 0000000000..33038d60da --- /dev/null +++ b/baseapp/context.go @@ -0,0 +1,23 @@ +package baseapp + +import sdk "github.com/cosmos/cosmos-sdk/types" + +// NOTE: Unstable. +// Returns a new Context suitable for AnteHandler (and indirectly Handler) processing. +func (app *BaseApp) NewContext(isCheckTx bool, txBytes []byte) sdk.Context { + var store sdk.MultiStore + if isCheckTx { + store = app.msCheck + } else { + store = app.msDeliver + } + + // Initialize arguments to Handler. + var ctx = sdk.NewContext( + store, + app.header, + isCheckTx, + txBytes, + ) + return ctx +}