2018-04-06 14:52:10 +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.
|
|
|
|
|
|
|
|
|
2019-09-04 06:37:33 +00:00
|
|
|
import
|
|
|
|
strformat, times, options,
|
|
|
|
eth/[common, rlp],
|
2020-04-15 11:09:49 +00:00
|
|
|
./difficulty, ../constants,
|
2021-05-13 09:01:58 +00:00
|
|
|
../chain_config
|
2018-01-17 14:16:00 +00:00
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
export BlockHeader
|
2018-02-13 17:18:08 +00:00
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc hasUncles*(header: BlockHeader): bool = header.ommersHash != EMPTY_UNCLE_HASH
|
2018-04-05 17:51:42 +00:00
|
|
|
|
2018-04-14 10:40:41 +00:00
|
|
|
proc `$`*(header: BlockHeader): string =
|
2018-05-30 16:11:15 +00:00
|
|
|
result = &"BlockHeader(timestamp: {header.timestamp} difficulty: {header.difficulty} blockNumber: {header.blockNumber} gasLimit: {header.gasLimit})"
|
2018-02-14 16:38:01 +00:00
|
|
|
|
2021-08-24 07:34:58 +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.
|
|
|
|
func computeGasLimit*(parentGasUsed, parentGasLimit, gasFloor, gasCeil: GasInt): GasInt =
|
|
|
|
# contrib = (parentGasUsed * 3 / 2) / 1024
|
|
|
|
let contrib = (parentGasUsed + parentGasUsed div 2) div GAS_LIMIT_ADJUSTMENT_FACTOR
|
|
|
|
|
|
|
|
# decay = parentGasLimit / 1024 -1
|
|
|
|
let decay = parentGasLimit div GAS_LIMIT_ADJUSTMENT_FACTOR - 1
|
|
|
|
|
|
|
|
#[
|
|
|
|
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.
|
|
|
|
]#
|
|
|
|
|
|
|
|
var limit = parentGasLimit - decay + contrib
|
|
|
|
if limit < GAS_LIMIT_MINIMUM:
|
|
|
|
limit = GAS_LIMIT_MINIMUM
|
|
|
|
|
|
|
|
# If we're outside our allowed gas range, we try to hone towards them
|
|
|
|
if limit < gasFloor:
|
|
|
|
limit = parentGasLimit + decay
|
|
|
|
if limit > gasFloor:
|
|
|
|
limit = gasFloor
|
|
|
|
|
|
|
|
elif limit > gasCeil:
|
|
|
|
limit = parentGasLimit - decay
|
|
|
|
if limit < gasCeil:
|
|
|
|
limit = gasCeil
|
|
|
|
|
|
|
|
return limit
|
|
|
|
|
2020-04-12 10:33:17 +00:00
|
|
|
proc generateHeaderFromParentHeader*(config: ChainConfig, parent: BlockHeader,
|
|
|
|
coinbase: EthAddress, timestamp: Option[EthTime],
|
2021-08-24 11:30:52 +00:00
|
|
|
gasLimit: GasInt, extraData: Blob, baseFee: Option[Uint256]): BlockHeader =
|
2019-09-04 06:37:33 +00:00
|
|
|
|
|
|
|
var lcTimestamp: EthTime
|
|
|
|
if timestamp.isNone:
|
|
|
|
lcTimeStamp = max(getTime(), parent.timestamp + 1.milliseconds) # Note: Py-evm uses +1 second, not ms
|
|
|
|
else:
|
|
|
|
lcTimestamp = timestamp.get()
|
|
|
|
|
|
|
|
if lcTimestamp <= parent.timestamp:
|
|
|
|
raise newException(ValueError, "header.timestamp should be higher than parent.timestamp")
|
|
|
|
|
2018-04-14 10:40:41 +00:00
|
|
|
result = BlockHeader(
|
2019-09-04 06:37:33 +00:00
|
|
|
timestamp: lcTimestamp,
|
2018-06-26 14:09:50 +00:00
|
|
|
blockNumber: (parent.blockNumber + 1),
|
2020-04-12 10:33:17 +00:00
|
|
|
difficulty: config.calcDifficulty(lcTimestamp, parent),
|
2021-08-24 11:30:52 +00:00
|
|
|
gasLimit: gasLimit,
|
2018-05-30 16:11:15 +00:00
|
|
|
stateRoot: parent.stateRoot,
|
2018-04-05 17:51:42 +00:00
|
|
|
coinbase: coinbase,
|
2019-09-04 06:37:33 +00:00
|
|
|
extraData: extraData,
|
2021-06-30 13:30:39 +00:00
|
|
|
fee: baseFee
|
2018-04-05 17:51:42 +00:00
|
|
|
)
|
2021-08-18 12:40:33 +00:00
|
|
|
|
|
|
|
# CalcGasLimit1559 calculates the next block gas limit under 1559 rules.
|
|
|
|
func calcGasLimit1559*(parentGasLimit, desiredLimit: GasInt): GasInt =
|
2021-08-24 11:30:52 +00:00
|
|
|
let delta = parentGasLimit div GAS_LIMIT_ADJUSTMENT_FACTOR - 1.GasInt
|
2021-08-18 12:40:33 +00:00
|
|
|
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
|