Added gas calculations, fleshed out generateHeaderFromParentHeader
This commit is contained in:
parent
b40b3e078c
commit
cafbc7f46d
|
@ -1,65 +1,113 @@
|
||||||
import ../constants, ttmath, strformat
|
import ../constants, ttmath, strformat, times, ../validation
|
||||||
|
|
||||||
type
|
type
|
||||||
|
EthTime = Time
|
||||||
Header* = ref object
|
Header* = ref object
|
||||||
timestamp*: int
|
timestamp*: EthTime
|
||||||
difficulty*: UInt256
|
difficulty*: UInt256
|
||||||
blockNumber*: UInt256
|
blockNumber*: UInt256
|
||||||
hash*: string
|
hash*: string
|
||||||
|
unclesHash*: string
|
||||||
coinbase*: string
|
coinbase*: string
|
||||||
gasLimit*: UInt256
|
|
||||||
stateRoot*: string
|
stateRoot*: string
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
|
|
||||||
|
proc hasUncles*(header: Header): bool = header.uncles_hash != EMPTY_UNCLE_HASH
|
||||||
|
|
||||||
|
proc gasUsed*(header: Header): UInt256 =
|
||||||
|
# TODO
|
||||||
|
# Should this be calculated/a proc? Parity and Py-Evm just have it as a field.
|
||||||
|
0.u256
|
||||||
|
|
||||||
|
proc gasLimit*(header: Header): UInt256 =
|
||||||
|
# TODO
|
||||||
|
0.u256
|
||||||
|
|
||||||
proc `$`*(header: Header): string =
|
proc `$`*(header: Header): string =
|
||||||
if header.isNil:
|
if header.isNil:
|
||||||
result = "nil"
|
result = "nil"
|
||||||
else:
|
else:
|
||||||
result = &"Header(timestamp: {header.timestamp} difficulty: {header.difficulty} blockNumber: {header.blockNumber} gasLimit: {header.gasLimit})"
|
result = &"Header(timestamp: {header.timestamp} difficulty: {header.difficulty} blockNumber: {header.blockNumber} gasLimit: {header.gasLimit})"
|
||||||
|
|
||||||
|
proc gasLimitBounds*(parent: Header): (UInt256, UInt256) =
|
||||||
|
## Compute the boundaries for the block gas limit based on the parent block.
|
||||||
|
let
|
||||||
|
boundary_range = parent.gasLimit div GAS_LIMIT_ADJUSTMENT_FACTOR
|
||||||
|
upper_bound = parent.gas_limit + boundary_range
|
||||||
|
lower_bound = max(GAS_LIMIT_MINIMUM, parent.gas_limit - boundary_range)
|
||||||
|
return (lower_bound, upper_bound)
|
||||||
|
|
||||||
|
#[
|
||||||
|
proc validate_gaslimit(header: Header):
|
||||||
|
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))
|
||||||
|
]#
|
||||||
|
|
||||||
|
proc computeGasLimit*(parent: Header, gasLimitFloor: UInt256): UInt256 =
|
||||||
|
#[
|
||||||
|
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.
|
||||||
|
]#
|
||||||
|
if gas_limit_floor < GAS_LIMIT_MINIMUM:
|
||||||
|
raise newException(ValueError,
|
||||||
|
&"""
|
||||||
|
The `gas_limit_floor` value must be greater than the GAS_LIMIT_MINIMUM.
|
||||||
|
Got {gasLimitFloor}. Must be greater than {GAS_LIMIT_MINIMUM}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
let decay = parent.gasLimit div GAS_LIMIT_EMA_DENOMINATOR
|
||||||
|
var usageIncrease = u256(0)
|
||||||
|
|
||||||
|
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
|
||||||
|
elif gas_limit < gas_limit_floor:
|
||||||
|
return parent.gas_limit + decay
|
||||||
|
else:
|
||||||
|
return gas_limit
|
||||||
|
|
||||||
proc generateHeaderFromParentHeader*(
|
proc generateHeaderFromParentHeader*(
|
||||||
computeDifficultyFn: proc(parentHeader: Header, timestamp: int): int,
|
computeDifficultyFn: proc(parentHeader: Header, timestamp: int): int,
|
||||||
parentHeader: Header,
|
parent: Header,
|
||||||
coinbase: string,
|
coinbase: string,
|
||||||
timestamp: int = -1,
|
timestamp: int = -1,
|
||||||
extraData: string = string""): Header =
|
extraData: string = ""): Header =
|
||||||
Header()
|
# TODO: validateGt(timestamp, parent.timestamp)
|
||||||
# Generate BlockHeader from state_root and parent_header
|
result = Header(
|
||||||
# if timestamp is None:
|
timestamp: max(getTime(), parent.timestamp + 1.milliseconds), # Note: Py-evm uses +1 second, not ms
|
||||||
# timestamp = max(int(time.time()), parent_header.timestamp + 1)
|
block_number: (parent.block_number + u256(1)),
|
||||||
# elif timestamp <= parent_header.timestamp:
|
# TODO: difficulty: parent.computeDifficulty(parent.timestamp),
|
||||||
# raise ValueError(
|
#[TODO: Make field? Or do we need to keep as a proc?
|
||||||
# "header.timestamp ({}) should be higher than"
|
gas_limit: computeGasLimit(
|
||||||
# "parent_header.timestamp ({})".format(
|
parent,
|
||||||
# timestamp,
|
gas_limit_floor=GENESIS_GAS_LIMIT,
|
||||||
# parent_header.timestamp,
|
),]#
|
||||||
# )
|
hash: parent.hash,
|
||||||
# )
|
state_root: parent.state_root,
|
||||||
# header = BlockHeader(
|
coinbase: coinbase,
|
||||||
# difficulty=compute_difficulty_fn(parent_header, timestamp),
|
# TODO: data: extraData,
|
||||||
# block_number=(parent_header.block_number + 1),
|
)
|
||||||
# gas_limit=compute_gas_limit(
|
|
||||||
# parent_header,
|
|
||||||
# gas_limit_floor=GENESIS_GAS_LIMIT,
|
|
||||||
# ),
|
|
||||||
# timestamp=timestamp,
|
|
||||||
# parent_hash=parent_header.hash,
|
|
||||||
# state_root=parent_header.state_root,
|
|
||||||
# coinbase=coinbase,
|
|
||||||
# extra_data=extra_data,
|
|
||||||
# )
|
|
||||||
|
|
||||||
# return header
|
|
||||||
|
|
||||||
proc computeGasLimit*(header: Header, gasLimitFloor: UInt256): UInt256 =
|
|
||||||
# TODO
|
|
||||||
gasLimitFloor
|
|
||||||
|
|
||||||
proc gasUsed*(header: Header): UInt256 =
|
|
||||||
# TODO
|
|
||||||
0.u256
|
|
||||||
|
|
||||||
proc gasLimit*(header: Header): UInt256 =
|
|
||||||
# TODO
|
|
||||||
0.u256
|
|
||||||
|
|
Loading…
Reference in New Issue