2016-04-14 16:18:24 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-10-19 14:08:17 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2015-11-27 14:40:29 +00:00
|
|
|
)
|
|
|
|
|
2018-08-29 09:21:12 +00:00
|
|
|
// CalcGasLimit computes the gas limit of the next block after parent. It aims
|
|
|
|
// to keep the baseline gas above the provided floor, and increase it towards the
|
|
|
|
// ceil if the blocks are full. If the ceil is exceeded, it will always decrease
|
|
|
|
// the gas allowance.
|
2021-03-23 09:00:07 +00:00
|
|
|
func CalcGasLimit(parentGasUsed, parentGasLimit, gasFloor, gasCeil uint64) uint64 {
|
2015-11-27 14:40:29 +00:00
|
|
|
// contrib = (parentGasUsed * 3 / 2) / 1024
|
2021-03-23 09:00:07 +00:00
|
|
|
contrib := (parentGasUsed + parentGasUsed/2) / params.GasLimitBoundDivisor
|
2015-11-27 14:40:29 +00:00
|
|
|
|
|
|
|
// decay = parentGasLimit / 1024 -1
|
2021-03-23 09:00:07 +00:00
|
|
|
decay := parentGasLimit/params.GasLimitBoundDivisor - 1
|
2015-11-27 14:40:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
strategy: gasLimit of block-to-mine is set based on parent's
|
|
|
|
gasUsed value. if parentGasUsed > parentGasLimit * (2/3) then we
|
|
|
|
increase it, otherwise lower it (or leave it unchanged if it's right
|
|
|
|
at that usage) the amount increased/decreased depends on how far away
|
|
|
|
from parentGasLimit * (2/3) parentGasUsed is.
|
|
|
|
*/
|
2021-03-23 09:00:07 +00:00
|
|
|
limit := parentGasLimit - decay + contrib
|
2017-11-13 11:47:27 +00:00
|
|
|
if limit < params.MinGasLimit {
|
|
|
|
limit = params.MinGasLimit
|
|
|
|
}
|
2018-08-29 09:21:12 +00:00
|
|
|
// If we're outside our allowed gas range, we try to hone towards them
|
|
|
|
if limit < gasFloor {
|
2021-03-23 09:00:07 +00:00
|
|
|
limit = parentGasLimit + decay
|
2018-08-29 09:21:12 +00:00
|
|
|
if limit > gasFloor {
|
|
|
|
limit = gasFloor
|
|
|
|
}
|
|
|
|
} else if limit > gasCeil {
|
2021-03-23 09:00:07 +00:00
|
|
|
limit = parentGasLimit - decay
|
2018-08-29 09:21:12 +00:00
|
|
|
if limit < gasCeil {
|
|
|
|
limit = gasCeil
|
2017-11-13 11:47:27 +00:00
|
|
|
}
|
2015-11-27 14:40:29 +00:00
|
|
|
}
|
2017-11-13 11:47:27 +00:00
|
|
|
return limit
|
2015-11-27 14:40:29 +00:00
|
|
|
}
|