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
|
2022-12-02 04:35:41 +00:00
|
|
|
std/[strformat, times],
|
|
|
|
./difficulty
|
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
|
|
|
|
|
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
|