2022-12-02 04:39:12 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2022 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
import
|
2023-10-18 02:16:11 +00:00
|
|
|
std/[options],
|
2022-12-02 04:39:12 +00:00
|
|
|
eth/common,
|
2023-09-13 02:32:38 +00:00
|
|
|
stew/endians2,
|
2022-12-02 04:39:12 +00:00
|
|
|
json_serialization,
|
|
|
|
../utils/utils,
|
|
|
|
./evmforks
|
|
|
|
|
2023-01-31 01:32:17 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
type
|
|
|
|
ConsensusType* {.pure.} = enum
|
|
|
|
# Proof of Work
|
|
|
|
# algorithm: Ethash
|
|
|
|
POW
|
|
|
|
|
|
|
|
# Proof of Authority
|
|
|
|
# algorithm: Clique
|
|
|
|
POA
|
|
|
|
|
|
|
|
# Proof of Stake
|
|
|
|
# algorithm: Casper
|
|
|
|
POS
|
|
|
|
|
|
|
|
HardFork* = enum
|
|
|
|
Frontier
|
|
|
|
Homestead
|
|
|
|
DAOFork
|
|
|
|
Tangerine # a.k.a. EIP150
|
|
|
|
Spurious # a.k.a. EIP158
|
|
|
|
Byzantium
|
|
|
|
Constantinople
|
|
|
|
Petersburg # a.k.a. ConstantinopleFix
|
|
|
|
Istanbul
|
|
|
|
MuirGlacier
|
|
|
|
Berlin
|
|
|
|
London
|
|
|
|
ArrowGlacier
|
|
|
|
GrayGlacier
|
|
|
|
MergeFork # a.k.a. Paris
|
|
|
|
Shanghai
|
|
|
|
Cancun
|
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
const lastPurelyBlockNumberBasedFork* = GrayGlacier
|
|
|
|
# MergeFork is special because of TTD.
|
|
|
|
const firstTimeBasedFork* = Shanghai
|
|
|
|
|
|
|
|
|
|
|
|
type
|
2022-12-02 04:39:12 +00:00
|
|
|
CliqueOptions* = object
|
|
|
|
epoch* : Option[int]
|
|
|
|
period*: Option[int]
|
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
MergeForkTransitionThreshold* = object
|
|
|
|
blockNumber*: Option[BlockNumber]
|
|
|
|
ttd*: Option[DifficultyInt]
|
2023-08-06 07:55:11 +00:00
|
|
|
ttdPassed*: Option[bool]
|
2023-02-16 11:40:07 +00:00
|
|
|
|
|
|
|
ForkTransitionTable* = object
|
|
|
|
blockNumberThresholds*: array[Frontier..GrayGlacier, Option[BlockNumber]]
|
|
|
|
mergeForkTransitionThreshold*: MergeForkTransitionThreshold
|
|
|
|
timeThresholds*: array[Shanghai..Cancun, Option[EthTime]]
|
|
|
|
|
|
|
|
# Starting with Shanghai, forking is based on timestamp
|
|
|
|
# rather than block number.
|
|
|
|
#
|
|
|
|
# I'm not sure what to call this type, but we used to pass
|
|
|
|
# just the block number into various places that need to
|
|
|
|
# determine which fork we're on, and now we need to pass
|
|
|
|
# around both block number and also time. And the config
|
|
|
|
# info for each individual fork will be either a block
|
|
|
|
# number or a time.
|
|
|
|
#
|
|
|
|
# Note that time and TD are optional. TD being optional
|
|
|
|
# is because it's perfectly fine, if mergeForkBlock is
|
|
|
|
# set, to not bother with TTD anymore. But I'm not sure
|
|
|
|
# it makes sense to allow time to be optional. See the
|
|
|
|
# comment below on blockNumberToForkDeterminationInfo.
|
|
|
|
ForkDeterminationInfo* = object
|
|
|
|
blockNumber*: BlockNumber
|
|
|
|
time*: Option[EthTime]
|
|
|
|
td*: Option[DifficultyInt]
|
|
|
|
|
|
|
|
func blockNumberToForkDeterminationInfo*(n: BlockNumber): ForkDeterminationInfo =
|
|
|
|
# FIXME: All callers of this function are suspect; I'm guess we should
|
|
|
|
# always be using both block number and time. But we have a few places,
|
|
|
|
# like various tests, where we only have block number and the tests are
|
|
|
|
# meant for pre-Merge forks, so maybe those are okay.
|
|
|
|
ForkDeterminationInfo(blockNumber: n, time: none[EthTime](), td: none[DifficultyInt]())
|
|
|
|
|
|
|
|
func forkDeterminationInfo*(n: BlockNumber, t: EthTime): ForkDeterminationInfo =
|
|
|
|
ForkDeterminationInfo(blockNumber: n, time: some(t), td: none[DifficultyInt]())
|
|
|
|
|
|
|
|
# FIXME: Is this called anywhere?
|
|
|
|
func forkDeterminationInfoIncludingTd*(n: BlockNumber, t: EthTime, td: DifficultyInt): ForkDeterminationInfo =
|
|
|
|
ForkDeterminationInfo(blockNumber: n, time: some(t), td: some(td))
|
|
|
|
|
|
|
|
proc adjustForNextBlock*(n: BlockNumber): BlockNumber =
|
|
|
|
n + 1
|
|
|
|
|
|
|
|
func adjustForNextBlock*(t: EthTime): EthTime =
|
|
|
|
# FIXME-Adam: what's the right thing to do here?
|
|
|
|
# How do we calculate "the timestamp for the block
|
|
|
|
# after this one"?
|
|
|
|
#
|
|
|
|
# If this makes no sense, what should the callers
|
|
|
|
# do instead?
|
2023-10-18 02:16:11 +00:00
|
|
|
t + 12
|
2023-02-16 11:40:07 +00:00
|
|
|
|
|
|
|
func adjustForNextBlock*(f: ForkDeterminationInfo): ForkDeterminationInfo =
|
|
|
|
ForkDeterminationInfo(
|
|
|
|
blockNumber: adjustForNextBlock(f.blockNumber),
|
|
|
|
time: f.time.map(adjustForNextBlock),
|
|
|
|
td: f.td
|
|
|
|
)
|
|
|
|
|
|
|
|
# This function is awkward because there are various different ways now of
|
|
|
|
# doing a hard-fork transition (block number, ttd, time, block number *or*
|
|
|
|
# time). We used to have a simple array called forkToBlock that mapped each
|
|
|
|
# HardFork to a BlockNumber; now we have this ForkTransitionTable, which
|
|
|
|
# contains a couple of arrays and also special cases for MergeBlock and
|
|
|
|
# Shanghai.
|
|
|
|
func isGTETransitionThreshold*(map: ForkTransitionTable, forkDeterminer: ForkDeterminationInfo, fork: HardFork): bool =
|
|
|
|
if fork <= lastPurelyBlockNumberBasedFork:
|
|
|
|
map.blockNumberThresholds[fork].isSome and forkDeterminer.blockNumber >= map.blockNumberThresholds[fork].get
|
|
|
|
elif fork == MergeFork:
|
|
|
|
# MergeFork is a special case that can use either block number or ttd;
|
2023-08-06 07:55:11 +00:00
|
|
|
# ttdPassed > block number > ttd takes precedence.
|
2023-02-16 11:40:07 +00:00
|
|
|
let t = map.mergeForkTransitionThreshold
|
2023-08-06 07:55:11 +00:00
|
|
|
if t.ttdPassed.isSome:
|
|
|
|
t.ttdPassed.get
|
|
|
|
elif t.blockNumber.isSome:
|
2023-02-16 11:40:07 +00:00
|
|
|
forkDeterminer.blockNumber >= t.blockNumber.get
|
|
|
|
elif t.ttd.isSome and forkDeterminer.td.isSome:
|
|
|
|
forkDeterminer.td.get >= t.ttd.get
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
elif fork <= HardFork.high:
|
|
|
|
map.timeThresholds[fork].isSome and forkDeterminer.time.isSome and forkDeterminer.time.get >= map.timeThresholds[fork].get
|
|
|
|
else:
|
|
|
|
raise newException(Defect, "Why is this hard fork not in one of the above categories?")
|
|
|
|
|
|
|
|
type
|
2022-12-02 04:39:12 +00:00
|
|
|
# if you add more fork block
|
|
|
|
# please update forkBlockField constant too
|
|
|
|
ChainConfig* = ref object
|
|
|
|
chainId* : ChainId
|
|
|
|
homesteadBlock* : Option[BlockNumber]
|
|
|
|
daoForkBlock* : Option[BlockNumber]
|
|
|
|
daoForkSupport* : bool
|
|
|
|
eip150Block* : Option[BlockNumber]
|
|
|
|
eip150Hash* : Hash256
|
|
|
|
eip155Block* : Option[BlockNumber]
|
|
|
|
eip158Block* : Option[BlockNumber]
|
|
|
|
byzantiumBlock* : Option[BlockNumber]
|
|
|
|
constantinopleBlock*: Option[BlockNumber]
|
|
|
|
petersburgBlock* : Option[BlockNumber]
|
|
|
|
istanbulBlock* : Option[BlockNumber]
|
|
|
|
muirGlacierBlock* : Option[BlockNumber]
|
|
|
|
berlinBlock* : Option[BlockNumber]
|
|
|
|
londonBlock* : Option[BlockNumber]
|
|
|
|
arrowGlacierBlock* : Option[BlockNumber]
|
|
|
|
grayGlacierBlock* : Option[BlockNumber]
|
|
|
|
mergeForkBlock* : Option[BlockNumber]
|
2023-02-16 11:40:07 +00:00
|
|
|
shanghaiTime* : Option[EthTime]
|
|
|
|
cancunTime* : Option[EthTime]
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
clique* : CliqueOptions
|
|
|
|
terminalTotalDifficulty*: Option[UInt256]
|
2023-08-06 07:55:11 +00:00
|
|
|
terminalTotalDifficultyPassed*: Option[bool]
|
2022-12-02 04:39:12 +00:00
|
|
|
consensusType*
|
|
|
|
{.dontSerialize.} : ConsensusType
|
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
# These are used for checking that the values of the fields
|
|
|
|
# are in a valid order.
|
|
|
|
BlockNumberBasedForkOptional* = object
|
2022-12-02 04:39:12 +00:00
|
|
|
name*: string
|
|
|
|
number*: Option[BlockNumber]
|
2023-02-16 11:40:07 +00:00
|
|
|
TimeBasedForkOptional* = object
|
|
|
|
name*: string
|
|
|
|
time*: Option[EthTime]
|
|
|
|
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
# this table is used for generate
|
|
|
|
# code at compile time to check
|
|
|
|
# the order of blok number in ChainConfig
|
|
|
|
forkBlockField* = [
|
|
|
|
"homesteadBlock",
|
|
|
|
"daoForkBlock",
|
|
|
|
"eip150Block",
|
|
|
|
"eip155Block",
|
|
|
|
"eip158Block",
|
|
|
|
"byzantiumBlock",
|
|
|
|
"constantinopleBlock",
|
|
|
|
"petersburgBlock",
|
|
|
|
"istanbulBlock",
|
|
|
|
"muirGlacierBlock",
|
|
|
|
"berlinBlock",
|
|
|
|
"londonBlock",
|
|
|
|
"arrowGlacierBlock",
|
|
|
|
"grayGlacierBlock",
|
|
|
|
"mergeForkBlock",
|
|
|
|
]
|
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
forkTimeField* = [
|
|
|
|
"shanghaiTime",
|
|
|
|
"cancunTime",
|
2022-12-02 04:39:12 +00:00
|
|
|
]
|
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
|
|
|
|
func mergeForkTransitionThreshold*(conf: ChainConfig): MergeForkTransitionThreshold =
|
2023-08-06 07:55:11 +00:00
|
|
|
MergeForkTransitionThreshold(
|
|
|
|
blockNumber: conf.mergeForkBlock,
|
|
|
|
ttd: conf.terminalTotalDifficulty,
|
|
|
|
ttdPassed: conf.terminalTotalDifficultyPassed
|
|
|
|
)
|
2023-02-16 11:40:07 +00:00
|
|
|
|
|
|
|
proc toForkTransitionTable*(conf: ChainConfig): ForkTransitionTable =
|
|
|
|
# We used to auto-generate this code from a list of
|
|
|
|
# field names, but it doesn't seem worthwhile anymore
|
|
|
|
# (now that there's irregularity due to block-based vs
|
|
|
|
# timestamp-based forking).
|
|
|
|
result.blockNumberThresholds[Frontier ] = some(0.toBlockNumber)
|
|
|
|
result.blockNumberThresholds[Homestead ] = conf.homesteadBlock
|
|
|
|
result.blockNumberThresholds[DAOFork ] = conf.daoForkBlock
|
|
|
|
result.blockNumberThresholds[Tangerine ] = conf.eip150Block
|
|
|
|
result.blockNumberThresholds[Spurious ] = conf.eip158Block
|
|
|
|
result.blockNumberThresholds[Byzantium ] = conf.byzantiumBlock
|
|
|
|
result.blockNumberThresholds[Constantinople] = conf.constantinopleBlock
|
|
|
|
result.blockNumberThresholds[Petersburg ] = conf.petersburgBlock
|
|
|
|
result.blockNumberThresholds[Istanbul ] = conf.istanbulBlock
|
|
|
|
result.blockNumberThresholds[MuirGlacier ] = conf.muirGlacierBlock
|
|
|
|
result.blockNumberThresholds[Berlin ] = conf.berlinBlock
|
|
|
|
result.blockNumberThresholds[London ] = conf.londonBlock
|
|
|
|
result.blockNumberThresholds[ArrowGlacier ] = conf.arrowGlacierBlock
|
|
|
|
result.blockNumberThresholds[GrayGlacier ] = conf.grayGlacierBlock
|
|
|
|
result.mergeForkTransitionThreshold = conf.mergeForkTransitionThreshold
|
|
|
|
result.timeThresholds[Shanghai] = conf.shanghaiTime
|
|
|
|
result.timeThresholds[Cancun] = conf.cancunTime
|
|
|
|
|
|
|
|
proc populateFromForkTransitionTable*(conf: ChainConfig, t: ForkTransitionTable) =
|
|
|
|
conf.homesteadBlock = t.blockNumberThresholds[HardFork.Homestead]
|
|
|
|
conf.daoForkBlock = t.blockNumberThresholds[HardFork.DAOFork]
|
|
|
|
conf.eip150Block = t.blockNumberThresholds[HardFork.Tangerine]
|
|
|
|
conf.eip155Block = t.blockNumberThresholds[HardFork.Spurious]
|
|
|
|
conf.eip158Block = t.blockNumberThresholds[HardFork.Spurious]
|
|
|
|
conf.byzantiumBlock = t.blockNumberThresholds[HardFork.Byzantium]
|
|
|
|
conf.constantinopleBlock = t.blockNumberThresholds[HardFork.Constantinople]
|
|
|
|
conf.petersburgBlock = t.blockNumberThresholds[HardFork.Petersburg]
|
|
|
|
conf.istanbulBlock = t.blockNumberThresholds[HardFork.Istanbul]
|
|
|
|
conf.muirGlacierBlock = t.blockNumberThresholds[HardFork.MuirGlacier]
|
|
|
|
conf.berlinBlock = t.blockNumberThresholds[HardFork.Berlin]
|
|
|
|
conf.londonBlock = t.blockNumberThresholds[HardFork.London]
|
|
|
|
conf.arrowGlacierBlock = t.blockNumberThresholds[HardFork.ArrowGlacier]
|
|
|
|
conf.grayGlacierBlock = t.blockNumberThresholds[HardFork.GrayGlacier]
|
|
|
|
|
|
|
|
conf.mergeForkBlock = t.mergeForkTransitionThreshold.blockNumber
|
|
|
|
conf.terminalTotalDifficulty = t.mergeForkTransitionThreshold.ttd
|
2023-08-06 07:55:11 +00:00
|
|
|
conf.terminalTotalDifficultyPassed = t.mergeForkTransitionThreshold.ttdPassed
|
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
conf.shanghaiTime = t.timeThresholds[HardFork.Shanghai]
|
|
|
|
conf.cancunTime = t.timeThresholds[HardFork.Cancun]
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Map HardFork to EVM/EVMC Fork
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const
|
|
|
|
ToEVMFork*: array[HardFork, EVMFork] = [
|
|
|
|
FkFrontier, # Frontier
|
|
|
|
FkHomestead, # Homestead
|
|
|
|
FkHomestead, # DAOFork
|
|
|
|
FkTangerine, # Tangerine
|
|
|
|
FkSpurious, # Spurious
|
|
|
|
FkByzantium, # Byzantium
|
|
|
|
FkConstantinople, # Constantinople
|
|
|
|
FkPetersburg, # Petersburg
|
|
|
|
FkIstanbul, # Istanbul
|
|
|
|
FkIstanbul, # MuirGlacier
|
|
|
|
FkBerlin, # Berlin
|
|
|
|
FkLondon, # London
|
|
|
|
FkLondon, # ArrowGlacier
|
|
|
|
FkLondon, # GrayGlacier
|
|
|
|
FkParis, # MergeFork
|
|
|
|
FkShanghai, # Shanghai
|
|
|
|
FkCancun, # Cancun
|
|
|
|
]
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Block reward helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func eth(n: int): UInt256 {.compileTime.} =
|
|
|
|
n.u256 * pow(10.u256, 18)
|
|
|
|
|
|
|
|
const
|
|
|
|
eth5 = 5.eth
|
|
|
|
eth3 = 3.eth
|
|
|
|
eth2 = 2.eth
|
|
|
|
eth0 = 0.u256
|
|
|
|
|
|
|
|
BlockRewards*: array[HardFork, UInt256] = [
|
|
|
|
eth5, # Frontier
|
|
|
|
eth5, # Homestead
|
|
|
|
eth5, # DAOFork
|
|
|
|
eth5, # Tangerine
|
|
|
|
eth5, # Spurious
|
|
|
|
eth3, # Byzantium
|
|
|
|
eth2, # Constantinople
|
|
|
|
eth2, # Petersburg
|
|
|
|
eth2, # Istanbul
|
|
|
|
eth2, # MuirGlacier
|
|
|
|
eth2, # Berlin
|
|
|
|
eth2, # London
|
|
|
|
eth2, # ArrowGlacier
|
|
|
|
eth2, # GrayGlacier
|
|
|
|
eth0, # MergeFork
|
|
|
|
eth0, # Shanghai
|
|
|
|
eth0, # Cancun
|
|
|
|
]
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Fork ID helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func toNextFork(n: Option[BlockNumber]): uint64 =
|
|
|
|
if n.isSome:
|
|
|
|
n.get.truncate(uint64)
|
|
|
|
else:
|
|
|
|
0'u64
|
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
# EIP-6122: ForkID now works with timestamps too.
|
|
|
|
func toNextFork(t: Option[EthTime]): uint64 =
|
|
|
|
if t.isSome:
|
2023-10-18 02:16:11 +00:00
|
|
|
t.get.uint64
|
2023-02-16 11:40:07 +00:00
|
|
|
else:
|
|
|
|
0'u64
|
|
|
|
|
|
|
|
func arrayMappingHardForkToNextFork(c: ChainConfig): array[HardFork, uint64] =
|
|
|
|
return [
|
2022-12-02 04:39:12 +00:00
|
|
|
0'u64,
|
|
|
|
toNextFork(c.homesteadBlock),
|
|
|
|
toNextFork(c.daoForkBlock),
|
|
|
|
toNextFork(c.eip150Block),
|
|
|
|
toNextFork(c.eip158Block),
|
|
|
|
toNextFork(c.byzantiumBlock),
|
|
|
|
toNextFork(c.constantinopleBlock),
|
|
|
|
toNextFork(c.petersburgBlock),
|
|
|
|
toNextFork(c.istanbulBlock),
|
|
|
|
toNextFork(c.muirGlacierBlock),
|
|
|
|
toNextFork(c.berlinBlock),
|
|
|
|
toNextFork(c.londonBlock),
|
|
|
|
toNextFork(c.arrowGlacierBlock),
|
|
|
|
toNextFork(c.grayGlacierBlock),
|
|
|
|
toNextFork(c.mergeForkBlock),
|
2023-02-16 11:40:07 +00:00
|
|
|
toNextFork(c.shanghaiTime),
|
|
|
|
toNextFork(c.cancunTime),
|
2022-12-02 04:39:12 +00:00
|
|
|
]
|
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
func getNextFork(next: array[HardFork, uint64], fork: HardFork): uint64 =
|
2022-12-02 04:39:12 +00:00
|
|
|
if fork == high(HardFork):
|
|
|
|
result = 0
|
|
|
|
return
|
|
|
|
|
|
|
|
result = next[fork]
|
|
|
|
for x in fork..high(HardFork):
|
|
|
|
if result != next[x]:
|
|
|
|
result = next[x]
|
|
|
|
break
|
|
|
|
|
2023-02-16 11:40:07 +00:00
|
|
|
func calculateForkId(next: array[HardFork, uint64], fork: HardFork,
|
2022-12-02 04:39:12 +00:00
|
|
|
prevCRC: uint32, prevFork: uint64): ForkID =
|
2023-02-16 11:40:07 +00:00
|
|
|
result.nextFork = getNextFork(next, fork)
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
if result.nextFork != prevFork:
|
|
|
|
result.crc = crc32(prevCRC, toBytesBE(prevFork))
|
|
|
|
else:
|
|
|
|
result.crc = prevCRC
|
|
|
|
|
|
|
|
func calculateForkIds*(c: ChainConfig,
|
|
|
|
genesisCRC: uint32): array[HardFork, ForkID] =
|
2023-02-16 11:40:07 +00:00
|
|
|
let next = arrayMappingHardForkToNextFork(c)
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
var prevCRC = genesisCRC
|
2023-02-16 11:40:07 +00:00
|
|
|
var prevFork = getNextFork(next, Frontier)
|
2022-12-02 04:39:12 +00:00
|
|
|
|
|
|
|
for fork in HardFork:
|
2023-02-16 11:40:07 +00:00
|
|
|
result[fork] = calculateForkId(next, fork, prevCRC, prevFork)
|
2022-12-02 04:39:12 +00:00
|
|
|
prevFork = result[fork].nextFork
|
|
|
|
prevCRC = result[fork].crc
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|