nimbus-eth1/nimbus/utils/header.nim

92 lines
3.4 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
strformat, times, options,
eth/[common, rlp],
2020-04-15 11:09:49 +00:00
./difficulty, ../constants,
../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
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})"
2019-09-24 13:05:21 +00:00
proc gasLimitBounds*(parent: BlockHeader): (GasInt, GasInt) =
## Compute the boundaries for the block gas limit based on the parent block.
let
2019-09-24 13:05:21 +00:00
boundaryRange = (parent.gasLimit div GAS_LIMIT_ADJUSTMENT_FACTOR)
upperBound = if GAS_LIMIT_MAXIMUM - boundaryRange < parent.gasLimit:
GAS_LIMIT_MAXIMUM else: parent.gasLimit + boundaryRange
lowerBound = max(GAS_LIMIT_MINIMUM, parent.gasLimit - boundaryRange)
2019-09-09 06:05:16 +00:00
2018-05-30 16:11:15 +00:00
return (lowerBound, upperBound)
2018-05-30 16:11:15 +00:00
proc computeGasLimit*(parent: BlockHeader, gasLimitFloor: GasInt): GasInt =
#[
For each block:
- decrease by 1/1024th of the gas limit from the previous block
- increase by 50% of the total gas used by the previous block
If the value is less than the given `gas_limit_floor`:
- increase the gas limit by 1/1024th of the gas limit from the previous block.
If the value is less than the GAS_LIMIT_MINIMUM:
- use the GAS_LIMIT_MINIMUM as the new gas limit.
]#
2018-05-30 16:11:15 +00:00
if gasLimitFloor < GAS_LIMIT_MINIMUM:
raise newException(ValueError,
&"""
2018-05-30 16:11:15 +00:00
The `gasLimitFloor` value must be greater than the GAS_LIMIT_MINIMUM.
Got {gasLimitFloor}. Must be greater than {GAS_LIMIT_MINIMUM}
"""
)
2018-01-17 14:16:00 +00:00
let decay = parent.gasLimit div GAS_LIMIT_EMA_DENOMINATOR
2018-05-30 16:11:15 +00:00
var usageIncrease: GasInt
2018-01-17 14:16:00 +00:00
if parent.gasUsed > 0:
usageIncrease = (
parent.gas_used * GAS_LIMIT_USAGE_ADJUSTMENT_NUMERATOR
) div GAS_LIMIT_USAGE_ADJUSTMENT_DENOMINATOR div GAS_LIMIT_EMA_DENOMINATOR
let gasLimit = max(
GAS_LIMIT_MINIMUM,
2019-09-25 13:05:33 +00:00
parent.gasLimit - decay + usageIncrease
)
2019-09-25 13:05:33 +00:00
if gasLimit < GAS_LIMIT_MINIMUM:
return GAS_LIMIT_MINIMUM
elif gasLimit < gasLimitFloor:
return parent.gasLimit + decay
else:
2019-09-25 13:05:33 +00:00
return gasLimit
2020-04-12 10:33:17 +00:00
proc generateHeaderFromParentHeader*(config: ChainConfig, parent: BlockHeader,
coinbase: EthAddress, timestamp: Option[EthTime],
2019-09-25 13:05:33 +00:00
gasLimit: Option[GasInt], extraData: Blob): BlockHeader =
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")
result = BlockHeader(
timestamp: lcTimestamp,
blockNumber: (parent.blockNumber + 1),
2020-04-12 10:33:17 +00:00
difficulty: config.calcDifficulty(lcTimestamp, parent),
2019-09-25 13:05:33 +00:00
gasLimit: if gasLimit.isSome: gasLimit.get() else: computeGasLimit(parent, gasLimitFloor = GENESIS_GAS_LIMIT),
2018-05-30 16:11:15 +00:00
stateRoot: parent.stateRoot,
coinbase: coinbase,
extraData: extraData,
)