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-02-05 19:15:50 +00:00
|
|
|
import eth/[common, rlp], ../constants, strformat, times, ../validation
|
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
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc gasLimitBounds*(parent: BlockHeader): (GasInt, GasInt) =
|
2018-04-05 17:51:42 +00:00
|
|
|
## Compute the boundaries for the block gas limit based on the parent block.
|
|
|
|
let
|
2018-05-30 16:11:15 +00:00
|
|
|
boundaryRange = parent.gasLimit div GAS_LIMIT_ADJUSTMENT_FACTOR
|
|
|
|
upperBound = parent.gasLimit + boundaryRange
|
|
|
|
lowerBound = max(GAS_LIMIT_MINIMUM, parent.gasLimit - boundaryRange)
|
|
|
|
return (lowerBound, upperBound)
|
2018-01-17 12:57:50 +00:00
|
|
|
|
2018-04-05 17:51:42 +00:00
|
|
|
#[
|
2018-04-14 10:40:41 +00:00
|
|
|
proc validate_gaslimit(header: BlockHeader):
|
2018-04-05 17:51:42 +00:00
|
|
|
let parent_header = getBlockHeaderByHash(header.parent_hash)
|
|
|
|
low_bound, high_bound = compute_gas_limit_bounds(parent_header)
|
|
|
|
if header.gas_limit < low_bound:
|
|
|
|
raise ValidationError(
|
|
|
|
"The gas limit on block {0} is too low: {1}. It must be at least {2}".format(
|
|
|
|
encode_hex(header.hash), header.gas_limit, low_bound))
|
|
|
|
elif header.gas_limit > high_bound:
|
|
|
|
raise ValidationError(
|
|
|
|
"The gas limit on block {0} is too high: {1}. It must be at most {2}".format(
|
|
|
|
encode_hex(header.hash), header.gas_limit, high_bound))
|
|
|
|
]#
|
2018-01-17 14:16:00 +00:00
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc computeGasLimit*(parent: BlockHeader, gasLimitFloor: GasInt): GasInt =
|
2018-04-05 17:51:42 +00:00
|
|
|
#[
|
|
|
|
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:
|
2018-04-05 17:51:42 +00:00
|
|
|
raise newException(ValueError,
|
|
|
|
&"""
|
2018-05-30 16:11:15 +00:00
|
|
|
The `gasLimitFloor` value must be greater than the GAS_LIMIT_MINIMUM.
|
2018-04-05 17:51:42 +00:00
|
|
|
Got {gasLimitFloor}. Must be greater than {GAS_LIMIT_MINIMUM}
|
|
|
|
"""
|
|
|
|
)
|
2018-01-17 14:16:00 +00:00
|
|
|
|
2018-04-05 17:51:42 +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
|
|
|
|
2018-04-05 17:51:42 +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,
|
|
|
|
parent.gasLimit - decay + usage_increase
|
|
|
|
)
|
|
|
|
|
|
|
|
if gas_limit < GAS_LIMIT_MINIMUM:
|
|
|
|
return GAS_LIMIT_MINIMUM
|
2018-05-30 16:11:15 +00:00
|
|
|
elif gas_limit < gasLimitFloor:
|
2018-04-05 17:51:42 +00:00
|
|
|
return parent.gas_limit + decay
|
|
|
|
else:
|
|
|
|
return gas_limit
|
|
|
|
|
|
|
|
proc generateHeaderFromParentHeader*(
|
2018-04-14 10:40:41 +00:00
|
|
|
computeDifficultyFn: proc(parentHeader: BlockHeader, timestamp: int): int,
|
|
|
|
parent: BlockHeader,
|
2018-05-30 16:11:15 +00:00
|
|
|
coinbase: EthAddress,
|
2018-04-05 17:51:42 +00:00
|
|
|
timestamp: int = -1,
|
2018-04-14 10:40:41 +00:00
|
|
|
extraData: string = ""): BlockHeader =
|
2018-04-05 17:51:42 +00:00
|
|
|
# TODO: validateGt(timestamp, parent.timestamp)
|
2018-04-14 10:40:41 +00:00
|
|
|
result = BlockHeader(
|
2018-04-05 17:51:42 +00:00
|
|
|
timestamp: max(getTime(), parent.timestamp + 1.milliseconds), # Note: Py-evm uses +1 second, not ms
|
2018-06-26 14:09:50 +00:00
|
|
|
blockNumber: (parent.blockNumber + 1),
|
2018-04-06 14:52:10 +00:00
|
|
|
# TODO: difficulty: parent.computeDifficulty(parent.timestamp),
|
2018-05-30 16:11:15 +00:00
|
|
|
gasLimit: computeGasLimit(parent, gasLimitFloor = GENESIS_GAS_LIMIT),
|
|
|
|
stateRoot: parent.stateRoot,
|
2018-04-05 17:51:42 +00:00
|
|
|
coinbase: coinbase,
|
|
|
|
# TODO: data: extraData,
|
|
|
|
)
|
2018-05-30 16:11:15 +00:00
|
|
|
|
2018-06-15 09:11:25 +00:00
|
|
|
import nimcrypto
|
|
|
|
# TODO: required otherwise
|
2019-02-05 19:15:50 +00:00
|
|
|
# eth/common/rlp_serialization.nim(18, 12) template/generic instantiation from here
|
2018-06-15 09:11:25 +00:00
|
|
|
# nimcrypto/hash.nim(46, 6) Error: attempting to call undeclared routine: 'init'
|
2018-05-30 16:11:15 +00:00
|
|
|
proc hash*(b: BlockHeader): Hash256 {.inline.} = rlpHash(b)
|