block gas meter working

This commit is contained in:
rigelrozanski 2018-11-12 23:12:09 -05:00
parent 0345f38639
commit 2f73cf4193
2 changed files with 28 additions and 4 deletions

View File

@ -68,8 +68,10 @@ type BaseApp struct {
deliverState *state // for DeliverTx
voteInfos []abci.VoteInfo // absent validators from begin block
// minimum fees for spam prevention
// spam prevention
minimumFees sdk.Coins
maximumBlockGas int64
deliverGas
// flag for sealing
sealed bool
@ -194,6 +196,9 @@ func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {
// SetMinimumFees sets the minimum fees.
func (app *BaseApp) SetMinimumFees(fees sdk.Coins) { app.minimumFees = fees }
// SetMaximumBlockGas sets the maximum gas allowable per block.
func (app *BaseApp) SetMaximumBlockGas(gas int64) { app.maximumBlockGas = gas }
// NewContext returns a new Context with the correct store, the given header, and nil txBytes.
func (app *BaseApp) NewContext(isCheckTx bool, header abci.Header) sdk.Context {
if isCheckTx {
@ -422,12 +427,19 @@ 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)
app.deliverState.ctx = app.deliverState.ctx.
WithBlockHeader(req.Header).
WithBlockHeight(req.Header.Height).
WithBlockGasMeter(blockGasMeter)
}
if app.beginBlocker != nil {
@ -467,9 +479,10 @@ func (app *BaseApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {
// Implements ABCI
func (app *BaseApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {
// Decode the Tx.
var result sdk.Result
var tx, err = app.txDecoder(txBytes)
var result sdk.Result
if err != nil {
result = err.Result()
} else {
@ -655,6 +668,10 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
result = app.runMsgs(ctx, msgs, mode)
result.GasWanted = gasWanted
// consume block gas
ctx.BlockGasMeter.ConsumeGas(
ctx.GasMeter().GasConsumed(), "block gas meter")
// only update state if all messages pass
if result.IsOK() {
msCache.Write()

View File

@ -140,6 +140,7 @@ const (
contextKeyLogger
contextKeyVoteInfos
contextKeyGasMeter
contextKeyBlockGasMeter
contextKeyMinimumFees
)
@ -170,6 +171,8 @@ func (c Context) VoteInfos() []abci.VoteInfo {
func (c Context) GasMeter() GasMeter { return c.Value(contextKeyGasMeter).(GasMeter) }
func (c Context) BlockGasMeter() GasMeter { return c.Value(contextKeyBlockGasMeter).(GasMeter) }
func (c Context) IsCheckTx() bool { return c.Value(contextKeyIsCheckTx).(bool) }
func (c Context) MinimumFees() Coins { return c.Value(contextKeyMinimumFees).(Coins) }
@ -219,6 +222,10 @@ func (c Context) WithVoteInfos(VoteInfos []abci.VoteInfo) Context {
func (c Context) WithGasMeter(meter GasMeter) Context { return c.withValue(contextKeyGasMeter, meter) }
func (c Context) WithBlockGasMeter(meter GasMeter) Context {
return c.withValue(contextKeyBlockGasMeter, meter)
}
func (c Context) WithIsCheckTx(isCheckTx bool) Context {
return c.withValue(contextKeyIsCheckTx, isCheckTx)
}