basic structure in place

This commit is contained in:
rigelrozanski 2018-11-13 11:30:06 -05:00
parent 2f73cf4193
commit 956d351f68
2 changed files with 19 additions and 6 deletions

View File

@ -427,20 +427,17 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
// 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.
blockGasMeter := sdk.NewGasMeter(app.maximumBlockGas)
if app.deliverState == nil {
app.setDeliverState(req.Header)
app.deliverState.ctx = app.deliverState.ctx.
WithBlockGasMeter(blockGasMeter)
} 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).
WithBlockGasMeter(blockGasMeter)
WithBlockHeight(req.Header.Height)
}
app.deliverState.ctx = app.deliverState.ctx.
WithBlockGasMeter(sdk.NewGasMeter(app.maximumBlockGas))
if app.beginBlocker != nil {
res = app.beginBlocker(app.deliverState.ctx, req)
@ -607,6 +604,13 @@ func (app *BaseApp) initializeContext(ctx sdk.Context, mode runTxMode) sdk.Conte
// anteHandler. txBytes may be nil in some cases, eg. in tests. Also, in the
// future we may support "internal" transactions.
func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk.Result) {
// only run the tx if there is block gas remaining
if ctx.BlockGasMeter.PastLimit() {
result = sdk.ErrOutOfGas("no block gas left to run tx").Result()
return
}
// NOTE: GasWanted should be returned by the AnteHandler. GasUsed is
// determined by the GasMeter. We need access to the context to get the gas
// meter so we initialize upfront.

View File

@ -29,6 +29,7 @@ type ErrorOutOfGas struct {
type GasMeter interface {
GasConsumed() Gas
ConsumeGas(amount Gas, descriptor string)
PastLimit() bool
}
type basicGasMeter struct {
@ -55,6 +56,10 @@ func (g *basicGasMeter) ConsumeGas(amount Gas, descriptor string) {
}
}
func (g *basicGasMeter) PastLimit() bool {
return g.consumed > g.limit
}
type infiniteGasMeter struct {
consumed Gas
}
@ -74,6 +79,10 @@ func (g *infiniteGasMeter) ConsumeGas(amount Gas, descriptor string) {
g.consumed += amount
}
func (g *infiniteGasMeter) PastLimit() bool {
return false
}
// GasConfig defines gas cost for each operation on KVStores
type GasConfig struct {
HasCost Gas