2021-06-29 07:41:01 +00:00
|
|
|
# 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
|
|
|
|
std/strformat,
|
|
|
|
stew/results,
|
2022-08-18 20:41:03 +00:00
|
|
|
eth/[common, eip1559],
|
2021-06-29 07:41:01 +00:00
|
|
|
../db/db_chain,
|
|
|
|
../constants,
|
|
|
|
../chain_config,
|
2021-09-11 14:58:01 +00:00
|
|
|
../forks
|
2021-06-29 07:41:01 +00:00
|
|
|
|
2022-08-18 20:41:03 +00:00
|
|
|
export
|
|
|
|
eip1559
|
|
|
|
|
2021-06-29 07:41:01 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Pre Eip 1559 gas limit validation
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
proc validateGasLimit(header: BlockHeader; limit: GasInt): Result[void, string]
|
|
|
|
{.raises: [Defect].} =
|
2021-06-29 07:41:01 +00:00
|
|
|
let diff = if limit > header.gasLimit:
|
|
|
|
limit - header.gasLimit
|
|
|
|
else:
|
|
|
|
header.gasLimit - limit
|
|
|
|
|
|
|
|
let upperLimit = limit div GAS_LIMIT_ADJUSTMENT_FACTOR
|
|
|
|
|
|
|
|
if diff >= upperLimit:
|
2021-10-05 23:31:35 +00:00
|
|
|
try:
|
|
|
|
return err(&"invalid gas limit: have {header.gasLimit}, want {limit} +-= {upperLimit-1}")
|
|
|
|
except ValueError:
|
|
|
|
# TODO deprecate-strformat
|
|
|
|
raiseAssert "strformat cannot fail"
|
2021-06-29 07:41:01 +00:00
|
|
|
if header.gasLimit < GAS_LIMIT_MINIMUM:
|
|
|
|
return err("invalid gas limit below 5000")
|
|
|
|
ok()
|
|
|
|
|
2021-10-05 23:31:35 +00:00
|
|
|
proc validateGasLimit(c: BaseChainDB; header: BlockHeader): Result[void, string]
|
|
|
|
{.raises: [Defect].} =
|
|
|
|
let parent = try:
|
|
|
|
c.getBlockHeader(header.parentHash)
|
|
|
|
except CatchableError:
|
|
|
|
return err "Parent block not in database"
|
2021-06-29 07:41:01 +00:00
|
|
|
header.validateGasLimit(parent.gasLimit)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Eip 1559 support
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# params/config.go(450): func (c *ChainConfig) IsLondon(num [..]
|
|
|
|
proc isLondonOrLater*(c: ChainConfig; number: BlockNumber): bool =
|
|
|
|
c.toFork(number) >= FkLondon
|
|
|
|
|
|
|
|
# consensus/misc/eip1559.go(55): func CalcBaseFee(config [..]
|
2021-08-24 07:34:58 +00:00
|
|
|
proc calcEip1599BaseFee*(c: ChainConfig; parent: BlockHeader): UInt256 =
|
2021-06-29 07:41:01 +00:00
|
|
|
## calculates the basefee of the header.
|
|
|
|
|
|
|
|
# If the current block is the first EIP-1559 block, return the
|
|
|
|
# initial base fee.
|
2022-08-18 20:41:03 +00:00
|
|
|
if c.isLondonOrLater(parent.blockNumber):
|
|
|
|
eip1559.calcEip1599BaseFee(parent.gasLimit, parent.gasUsed, parent.baseFee)
|
2021-06-29 07:41:01 +00:00
|
|
|
else:
|
2022-08-18 20:41:03 +00:00
|
|
|
EIP1559_INITIAL_BASE_FEE
|
2021-06-29 07:41:01 +00:00
|
|
|
|
|
|
|
# consensus/misc/eip1559.go(32): func VerifyEip1559Header(config [..]
|
|
|
|
proc verifyEip1559Header(c: ChainConfig;
|
2021-10-05 23:31:35 +00:00
|
|
|
parent, header: BlockHeader): Result[void, string]
|
|
|
|
{.raises: [Defect].} =
|
2021-06-29 07:41:01 +00:00
|
|
|
## Verify that the gas limit remains within allowed bounds
|
|
|
|
let limit = if c.isLondonOrLater(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.
|
|
|
|
var expectedBaseFee = c.calcEip1599BaseFee(parent)
|
|
|
|
if headerBaseFee != expectedBaseFee:
|
2021-10-05 23:31:35 +00:00
|
|
|
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"
|
2021-06-29 07:41:01 +00:00
|
|
|
|
|
|
|
return ok()
|
|
|
|
|
|
|
|
proc validateGasLimitOrBaseFee*(c: BaseChainDB;
|
2021-10-05 23:31:35 +00:00
|
|
|
header, parent: BlockHeader): Result[void, string]
|
|
|
|
{.gcsafe, raises: [Defect].} =
|
2021-06-29 07:41:01 +00:00
|
|
|
|
|
|
|
if not c.config.isLondonOrLater(header.blockNumber):
|
|
|
|
# Verify BaseFee not present before EIP-1559 fork.
|
|
|
|
if not header.baseFee.isZero:
|
2021-10-05 23:31:35 +00:00
|
|
|
return err("invalid baseFee before London fork: have " & $header.baseFee & ", want <0>")
|
2021-06-29 07:41:01 +00:00
|
|
|
let rc = c.validateGasLimit(header)
|
|
|
|
if rc.isErr:
|
|
|
|
return rc
|
|
|
|
else:
|
|
|
|
let rc = c.config.verifyEip1559Header(parent = parent,
|
|
|
|
header = header)
|
|
|
|
if rc.isErr:
|
|
|
|
return rc
|
|
|
|
|
|
|
|
return ok()
|