Skip to content

Commit

Permalink
Require BeginBlock for NewContext
Browse files Browse the repository at this point in the history
  • Loading branch information
jaekwon committed Jan 26, 2018
1 parent a5fa187 commit ac7fa3f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
17 changes: 11 additions & 6 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type BaseApp struct {
msDeliver sdk.CacheMultiStore

// Current block header
header abci.Header
header *abci.Header

// Cached validator changes from DeliverTx.
valUpdates []abci.Validator
Expand Down Expand Up @@ -155,9 +155,9 @@ func (app *BaseApp) LastBlockHeight() int64 {

// Initializes the remaining logic from app.ms.
func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {
lastCommitID := app.ms.LastCommitID()
main := app.ms.GetKVStore(mainKey)
header := abci.Header{}
var lastCommitID = app.ms.LastCommitID()
var main = app.ms.GetKVStore(mainKey)
var header *abci.Header

// Main store should exist.
if main == nil {
Expand All @@ -171,7 +171,7 @@ func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {
errStr := fmt.Sprintf("Version > 0 but missing key %s", mainHeaderKey)
return errors.New(errStr)
}
err := proto.Unmarshal(headerBytes, &header)
err := proto.Unmarshal(headerBytes, header)
if err != nil {
return errors.Wrap(err, "Failed to parse Header")
}
Expand Down Expand Up @@ -225,9 +225,11 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {

// Implements ABCI.
func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) {
app.header = req.Header
// NOTE: For consistency we should unset these upon EndBlock.
app.header = &req.Header
app.msDeliver = app.ms.CacheMultiStore()
app.msCheck = app.ms.CacheMultiStore()
app.valUpdates = nil
return
}

Expand Down Expand Up @@ -318,6 +320,9 @@ func (app *BaseApp) runTx(isCheckTx bool, txBytes []byte) (result sdk.Result) {
func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
res.ValidatorUpdates = app.valUpdates
app.valUpdates = nil
app.header = nil
app.msDeliver = nil
app.msCheck = nil
return
}

Expand Down
8 changes: 7 additions & 1 deletion baseapp/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ func (app *BaseApp) NewContext(isCheckTx bool, txBytes []byte) sdk.Context {
} else {
store = app.msDeliver
}
if store == nil {
panic("BaseApp.NewContext() requires BeginBlock(): missing store")
}
if app.header == nil {
panic("BaseApp.NewContext() requires BeginBlock(): missing header")
}

// Initialize arguments to Handler.
var ctx = sdk.NewContext(
store,
app.header,
*app.header,
isCheckTx,
txBytes,
)
Expand Down

0 comments on commit ac7fa3f

Please sign in to comment.