Compiletime automatic collect fork field from ChainConfig

This commit is contained in:
jangko 2023-10-24 12:51:37 +07:00
parent 6ef5fd78b7
commit 0a01ab1fe0
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
1 changed files with 33 additions and 23 deletions

View File

@ -8,7 +8,7 @@
# those terms. # those terms.
import import
std/[options], std/[options, strutils],
eth/common, eth/common,
stew/endians2, stew/endians2,
json_serialization, json_serialization,
@ -189,34 +189,44 @@ type
name*: string name*: string
time*: Option[EthTime] time*: Option[EthTime]
func countTimeFields(): int {.compileTime.} =
var z = Chainconfig()
for name, _ in fieldPairs(z[]):
if name.endsWith("Time"):
inc result
func countBlockFields(): int {.compileTime.} =
var z = Chainconfig()
for name, _ in fieldPairs(z[]):
if name.endsWith("Block"):
inc result
const
timeFieldsCount = countTimeFields()
blockFieldsCount = countBlockFields()
func collectTimeFields(): array[timeFieldsCount, string] =
var z = Chainconfig()
var i = 0
for name, _ in fieldPairs(z[]):
if name.endsWith("Time"):
result[i] = name
inc i
func collectBlockFields(): array[blockFieldsCount, string] =
var z = Chainconfig()
var i = 0
for name, _ in fieldPairs(z[]):
if name.endsWith("Block"):
result[i] = name
inc i
const const
# this table is used for generate # this table is used for generate
# code at compile time to check # code at compile time to check
# the order of blok number in ChainConfig # the order of blok number in ChainConfig
forkBlockField* = [ forkBlockField* = collectBlockFields()
"homesteadBlock", forkTimeField* = collectTimeFields()
"daoForkBlock",
"eip150Block",
"eip155Block",
"eip158Block",
"byzantiumBlock",
"constantinopleBlock",
"petersburgBlock",
"istanbulBlock",
"muirGlacierBlock",
"berlinBlock",
"londonBlock",
"arrowGlacierBlock",
"grayGlacierBlock",
"mergeForkBlock",
]
forkTimeField* = [
"shanghaiTime",
"cancunTime",
]
func mergeForkTransitionThreshold*(conf: ChainConfig): MergeForkTransitionThreshold = func mergeForkTransitionThreshold*(conf: ChainConfig): MergeForkTransitionThreshold =