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.
import
std/[options],
std/[options, strutils],
eth/common,
stew/endians2,
json_serialization,
@ -189,34 +189,44 @@ type
name*: string
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
# 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",
]
forkTimeField* = [
"shanghaiTime",
"cancunTime",
]
forkBlockField* = collectBlockFields()
forkTimeField* = collectTimeFields()
func mergeForkTransitionThreshold*(conf: ChainConfig): MergeForkTransitionThreshold =