implement calcEIP1559GasLimit

CalcGasLimit1559 calculates the next block gas limit under 1559 rules.
this function is needed in upcoming sealing engine implementation
This commit is contained in:
jangko 2021-08-18 19:40:33 +07:00
parent f4d9421836
commit 18b26a0089
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 67 additions and 5 deletions

View File

@ -80,3 +80,26 @@ proc generateHeaderFromParentHeader*(config: ChainConfig, parent: BlockHeader,
extraData: extraData,
fee: baseFee
)
# CalcGasLimit1559 calculates the next block gas limit under 1559 rules.
func calcGasLimit1559*(parentGasLimit, desiredLimit: GasInt): GasInt =
let delta = parentGasLimit div GAS_LIMIT_EMA_DENOMINATOR - 1.GasInt
var limit = parentGasLimit
var desiredLimit = desiredLimit
if desiredLimit < GAS_LIMIT_MINIMUM:
desiredLimit = GAS_LIMIT_MINIMUM
# If we're outside our allowed gas range, we try to hone towards them
if limit < desiredLimit:
limit = parentGasLimit + delta
if limit > desiredLimit:
limit = desiredLimit
return limit
if limit > desiredLimit:
limit = parentGasLimit - delta
if limit < desiredLimit:
limit = desiredLimit
return limit

View File

@ -4,7 +4,8 @@ import
eth/common/eth_types,
eth/p2p,
../nimbus/vm_internals,
../nimbus/config
../nimbus/config,
../nimbus/utils/header
func toAddress(n: int): EthAddress =
result[19] = n.byte
@ -60,5 +61,43 @@ proc miscMain*() =
check conf.net.networkId == 123.NetworkId
test "calcGasLimitEIP1559":
type
GLT = object
limit: GasInt
max : GasInt
min : GasInt
const testData = [
GLT(limit: 20000000, max: 20019530, min: 19980470),
GLT(limit: 40000000, max: 40039061, min: 39960939)
]
for x in testData:
# Increase
var have = calcGasLimit1559(x.limit, 2*x.limit)
var want = x.max
check have == want
# Decrease
have = calcGasLimit1559(x.limit, 0)
want = x.min
check have == want
# Small decrease
have = calcGasLimit1559(x.limit, x.limit-1)
want = x.limit-1
check have == want
# Small increase
have = calcGasLimit1559(x.limit, x.limit+1)
want = x.limit+1
check have == want
# No change
have = calcGasLimit1559(x.limit, x.limit)
want = x.limit
check have == want
when isMainModule:
miscMain()