nimbus-eth1/nimbus/core/gaslimit.nim

115 lines
3.9 KiB
Nim
Raw Normal View History

# Nimbus
# Copyright (c) 2018 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
# http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
import
2022-12-02 04:35:41 +00:00
../common/common,
std/strformat,
stew/results,
2022-12-02 04:35:41 +00:00
eth/[eip1559]
export
eip1559
2022-10-20 02:44:16 +00:00
{.push raises: [].}
# ------------------------------------------------------------------------------
# Pre Eip 1559 gas limit validation
# ------------------------------------------------------------------------------
proc validateGasLimit(header: BlockHeader; limit: GasInt): Result[void,string] =
let diff = if limit > header.gasLimit:
limit - header.gasLimit
else:
header.gasLimit - limit
let upperLimit = limit div GAS_LIMIT_ADJUSTMENT_FACTOR
if diff >= upperLimit:
try:
return err(&"invalid gas limit: have {header.gasLimit}, want {limit} +-= {upperLimit-1}")
except ValueError:
# TODO deprecate-strformat
raiseAssert "strformat cannot fail"
if header.gasLimit < GAS_LIMIT_MINIMUM:
return err("invalid gas limit below 5000")
ok()
proc validateGasLimit(com: CommonRef; header: BlockHeader): Result[void, string] =
let parent = try:
2022-12-02 04:35:41 +00:00
com.db.getBlockHeader(header.parentHash)
except CatchableError:
return err "Parent block not in database"
header.validateGasLimit(parent.gasLimit)
# ------------------------------------------------------------------------------
# Eip 1559 support
# ------------------------------------------------------------------------------
# consensus/misc/eip1559.go(55): func CalcBaseFee(config [..]
2022-12-02 04:35:41 +00:00
proc calcEip1599BaseFee*(com: CommonRef; parent: BlockHeader): UInt256 =
## calculates the basefee of the header.
# If the current block is the first EIP-1559 block, return the
# initial base fee.
2022-12-02 04:35:41 +00:00
if com.isLondon(parent.blockNumber):
eip1559.calcEip1599BaseFee(parent.gasLimit, parent.gasUsed, parent.baseFee)
else:
EIP1559_INITIAL_BASE_FEE
# consensus/misc/eip1559.go(32): func VerifyEip1559Header(config [..]
2022-12-02 04:35:41 +00:00
proc verifyEip1559Header(com: CommonRef;
parent, header: BlockHeader): Result[void, string]
{.raises: [].} =
## Verify that the gas limit remains within allowed bounds
2022-12-02 04:35:41 +00:00
let limit = if com.isLondon(parent.blockNumber):
parent.gasLimit
else:
parent.gasLimit * EIP1559_ELASTICITY_MULTIPLIER
let rc = header.validateGasLimit(limit)
if rc.isErr:
return rc
let headerBaseFee = header.baseFee
# Verify the header is not malformed
if headerBaseFee.isZero:
return err("Post EIP-1559 header expected to have base fee")
# Verify the baseFee is correct based on the parent header.
2022-12-02 04:35:41 +00:00
var expectedBaseFee = com.calcEip1599BaseFee(parent)
if headerBaseFee != expectedBaseFee:
try:
return err(&"invalid baseFee: have {expectedBaseFee}, "&
&"want {header.baseFee}, " &
&"parent.baseFee {parent.baseFee}, "&
&"parent.gasUsed {parent.gasUsed}")
except ValueError:
# TODO deprecate-strformat
raiseAssert "strformat cannot fail"
return ok()
2022-12-02 04:35:41 +00:00
proc validateGasLimitOrBaseFee*(com: CommonRef;
header, parent: BlockHeader): Result[void, string] =
2022-12-02 04:35:41 +00:00
if not com.isLondon(header.blockNumber):
# Verify BaseFee not present before EIP-1559 fork.
if not header.baseFee.isZero:
return err("invalid baseFee before London fork: have " & $header.baseFee & ", want <0>")
2022-12-02 04:35:41 +00:00
let rc = com.validateGasLimit(header)
if rc.isErr:
return rc
else:
2022-12-02 04:35:41 +00:00
let rc = com.verifyEip1559Header(parent = parent,
header = header)
if rc.isErr:
return rc
return ok()