Skip to content

Commit c7d5ccf

Browse files
authored
Rework ForkId to get rid of ChainForkId and its conversions (#3810)
* Rework ForkId to get rid of ChainForkId and its conversions And be more consistent in the naming + use fields as per EIPs
1 parent def221d commit c7d5ccf

File tree

12 files changed

+200
-236
lines changed

12 files changed

+200
-236
lines changed

execution_chain/common/common.nim

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -350,20 +350,20 @@ func isLondonOrLater*(com: CommonRef, number: BlockNumber): bool =
350350
# TODO: Fixme, use only London comparator
351351
com.toHardFork(number.forkDeterminationInfo) >= London
352352

353-
func forkId*(com: CommonRef, head, time: uint64): ForkID {.gcsafe.} =
354-
## Get ForkId for given block number / timestamp (EIP 2364/2124)
355-
com.forkIdCalculator.newID(head, time)
353+
func forkId*(com: CommonRef, head, time: uint64): ForkId {.gcsafe.} =
354+
## Get ForkId for given block number / timestamp (EIP-2124/2364/6122)
355+
com.forkIdCalculator.calculateForkId(head, time)
356356

357-
func forkId*(com: CommonRef, forkActivationTime: EthTime): ForkID {.gcsafe.} =
358-
## Get ForkId for given timestamp (EIP 2364/2124)
357+
func forkId*(com: CommonRef, forkActivationTime: EthTime): ForkId {.gcsafe.} =
358+
## Get ForkId for given timestamp (EIP-2124/2364/6122)
359359
## Only works for timestamp based forks
360-
com.forkIdCalculator.newID(0'u64, forkActivationTime.uint64)
360+
com.forkIdCalculator.calculateForkId(0'u64, forkActivationTime.uint64)
361361

362-
func forkId*(com: CommonRef, head: BlockNumber, time: EthTime): ForkID {.gcsafe.} =
363-
## Get ForkId for given block number / timestamp (EIP-2124 + EIP-6122)
364-
com.forkIdCalculator.newID(head, time.uint64)
362+
func forkId*(com: CommonRef, head: BlockNumber, time: EthTime): ForkId {.gcsafe.} =
363+
## Get ForkId for given block number / timestamp (EIP-2124/2364/6122)
364+
com.forkIdCalculator.calculateForkId(head, time.uint64)
365365

366-
func compatibleForkId*(com: CommonRef, forkId: ForkID, blockNumber: BlockNumber, time: EthTime): bool =
366+
func compatibleForkId*(com: CommonRef, forkId: ForkId, blockNumber: BlockNumber, time: EthTime): bool =
367367
## Check if a fork ID is compatible at a specific head position
368368
com.forkIdCalculator.compatible(forkId, blockNumber, time.uint64)
369369

execution_chain/common/hardforks.nim

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
{.push raises: [].}
1111

1212
import
13-
std/[strutils],
14-
eth/common/[headers],
13+
std/strutils,
14+
eth/common/[base, headers],
1515
stew/endians2,
1616
json_serialization,
1717
../utils/utils,
1818
./evmforks
1919

20+
export base
21+
2022
type
2123
HardFork* = enum
2224
Frontier
@@ -348,40 +350,40 @@ type
348350
byBlock: seq[uint64]
349351
byTime: seq[uint64]
350352
genesisCRC: uint32
351-
forkHistory: seq[ForkID]
353+
forkHistory: seq[ForkId]
352354

353-
func newID*(calc: ForkIdCalculator, head, time: uint64): ForkID =
355+
func calculateForkId*(calc: ForkIdCalculator, head, time: uint64): ForkId =
354356
## Create a fork ID for a specific block height and timestamp
355357
var crc = calc.genesisCRC
356358
for fork in calc.byBlock:
357359
if fork <= head:
358360
# Fork already passed, checksum the previous crc and the fork number
359361
crc = crc32(crc, fork.toBytesBE)
360362
continue
361-
return (crc, fork)
363+
return ForkId(hash: crc.to(Bytes4), next: fork)
362364

363365
for fork in calc.byTime:
364366
if fork <= time:
365367
# Fork already passed, checksum the previous crc and fork timestamp
366368
crc = crc32(crc, fork.toBytesBE)
367369
continue
368-
return (crc, fork)
370+
return ForkId(hash: crc.to(Bytes4), next: fork)
369371

370-
(crc, 0'u64)
372+
ForkId(hash: crc.to(Bytes4), next: 0'u64)
371373

372-
func compatible*(calc: ForkIdCalculator, forkId: ForkID, number: uint64, time: uint64): bool =
374+
func compatible*(calc: ForkIdCalculator, forkId: ForkId, number: uint64, time: uint64): bool =
373375
## Check forkId compatibility at a specific head position according to EIP-2124 / EIP-6122.
374376
## The number and time represent the local chain state at which compatibility needs to be checked
375377
## In regular use this should be the current header block number and timestamp, but for testing
376378
## arbitrary points in history can be used.
377379

378380
# Calculate our current local fork ID at the given head position (= block and/or time)
379-
let localId = calc.newID(number, time)
381+
let localId = calc.calculateForkId(number, time)
380382

381383
# Calculate position of local fork ID in history
382384
var localForkPos = -1
383385
for i, historicalId in calc.forkHistory:
384-
if localId.crc == historicalId.crc:
386+
if localId.hash == historicalId.hash:
385387
localForkPos = i
386388
break
387389

@@ -393,9 +395,9 @@ func compatible*(calc: ForkIdCalculator, forkId: ForkID, number: uint64, time: u
393395
number
394396

395397
# 1: local and remote FORK_HASH matches
396-
if forkId.crc == localId.crc:
397-
if forkId.nextFork != 0: # announced fork
398-
if head >= forkId.nextFork:
398+
if forkId.hash == localId.hash:
399+
if forkId.next != 0: # announced fork
400+
if head >= forkId.next:
399401
# 1a - next fork already passed locally
400402
return false
401403
else:
@@ -408,19 +410,19 @@ func compatible*(calc: ForkIdCalculator, forkId: ForkID, number: uint64, time: u
408410

409411
# 2 + 3: FORK_HASH is subset or superset of local past forks
410412
for i, historicalId in calc.forkHistory:
411-
if forkId.crc == historicalId.crc:
413+
if forkId.hash == historicalId.hash:
412414
# Need to know if remote (=forkId) is ahead or behind localId
413415
if i > localForkPos:
414416
# 3: Remote is at a future fork we also know about
415417
# (local is syncing)
416418
return true
417419
# TODO: Should we still check its next fork?
418-
if forkId.nextFork == historicalId.nextFork:
419-
# 2: Remote is at a past fork we also know about and its nextFork matches the one for that past fork
420+
if forkId.next == historicalId.next:
421+
# 2: Remote is at a past fork we also know about and its next fork matches the one for that past fork
420422
# (remote is syncing)
421423
return true
422424
else:
423-
# 4: Remote is at a past fork we also know about but its nextFork doesn't match
425+
# 4: Remote is at a past fork we also know about but its next fork doesn't match
424426
return false
425427

426428
# 4: incompatible
@@ -474,20 +476,20 @@ func init*(
474476
forksByTime.delete(0)
475477

476478
# Calculate the fork ids for the full fork history
477-
var forkHistory = newSeqOfCap[ForkID](forksByBlock.len + forksByTime.len + 1)
479+
var forkHistory = newSeqOfCap[ForkId](forksByBlock.len + forksByTime.len + 1)
478480
var crc = genesisCRC
479481

480482
# Each entry is (crc before fork, fork number)
481483
for fork in forksByBlock:
482-
forkHistory.add((crc, fork))
484+
forkHistory.add(ForkId(hash: crc.to(Bytes4), next: fork))
483485
crc = crc32(crc, fork.toBytesBE)
484486

485487
for fork in forksByTime:
486-
forkHistory.add((crc, fork))
488+
forkHistory.add(ForkId(hash: crc.to(Bytes4), next: fork))
487489
crc = crc32(crc, fork.toBytesBE)
488490

489491
# Add last fork ID (after all forks, next=0)
490-
forkHistory.add((crc, 0'u64))
492+
forkHistory.add(ForkId(hash: crc.to(Bytes4), next: 0'u64))
491493

492494
ForkIdCalculator(
493495
genesisCRC: genesisCRC,

execution_chain/networking/chain_forkid.nim

Lines changed: 0 additions & 28 deletions
This file was deleted.

execution_chain/networking/eth1_discovery.nim

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import
1919
eth/enode/enode_utils,
2020
./discoveryv5,
2121
./discoveryv4,
22-
./chain_forkid,
2322
./bootnodes
2423

2524
export
@@ -40,7 +39,7 @@ type
4039
AddressV4 = discoveryv4.Address
4140
AddressV5 = discoveryv5.Address
4241

43-
CompatibleForkIdProc* = proc(id: ForkID): bool {.noSideEffect, raises: [].}
42+
CompatibleForkIdProc* = proc(id: ForkId): bool {.noSideEffect, raises: [].}
4443

4544
Eth1Discovery* = ref object
4645
discv4: DiscV4
@@ -106,12 +105,12 @@ func eligibleNode(proto: Eth1Discovery, rec: Record): bool =
106105
let
107106
ethValue =
108107
try:
109-
rlp.decode(bytes, array[1, ChainForkId])
108+
rlp.decode(bytes, array[1, ForkId])
110109
except RlpError:
111110
return false
112-
chainForkId = ethValue[0]
111+
forkId = ethValue[0]
113112

114-
proto.compatibleForkId(chainForkId.to(ForkID))
113+
proto.compatibleForkId(forkId)
115114

116115
#------------------------------------------------------------------------------
117116
# Public functions
@@ -225,11 +224,11 @@ proc getRandomBootnode*(proto: Eth1Discovery): Opt[NodeV4] =
225224
return Opt.none(NodeV4)
226225
return Opt.some(newNode(enode))
227226

228-
func updateForkID*(proto: Eth1Discovery, forkId: ForkID) =
227+
func updateForkId*(proto: Eth1Discovery, forkId: ForkId) =
229228
# https://github.com/ethereum/devp2p/blob/bc76b9809a30e6dc5c8dcda996273f0f9bcf7108/enr-entries/eth.md
230229
if proto.discv5.isNil.not:
231230
let
232-
list = [forkId.to(ChainForkId)]
231+
list = [forkId]
233232
bytes = rlp.encode(list)
234233
kv = ("eth", bytes)
235234
proto.discv5.updateRecord([kv]).isOkOr:

execution_chain/networking/peer_pool.nim

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
import
1616
std/[os, tables, times, sets],
1717
chronos, chronicles,
18+
eth/common/base,
1819
./p2p_metrics,
1920
./[eth1_discovery, p2p_peers]
2021

21-
from eth/common/base import ForkID
22-
23-
export sets, tables, CompatibleForkIdProc
22+
export sets, tables, CompatibleForkIdProc, base
2423

2524
logScope:
2625
topics = "p2p peer_pool"
@@ -32,7 +31,7 @@ type
3231

3332
WorkerFuture = Future[void].Raising([CancelledError])
3433

35-
ForkIdProc* = proc(): ForkID {.noSideEffect, raises: [].}
34+
ForkIdProc* = proc(): ForkId {.noSideEffect, raises: [].}
3635

3736
# Usually Network generic param is instantiated with EthereumNode
3837
PeerPoolRef*[Network] = ref object
@@ -44,7 +43,7 @@ type
4443
discovery: Eth1Discovery
4544
workers: seq[WorkerFuture]
4645
forkId: ForkIdProc
47-
lastForkId: ForkID
46+
lastForkId: ForkId
4847
connectTimer: Future[void].Raising([CancelledError])
4948
updateTimer: Future[void].Raising([CancelledError])
5049
connectingNodes*: HashSet[Node]
@@ -162,22 +161,22 @@ proc lookupPeers(p: PeerPoolRef) {.async: (raises: [CancelledError]).} =
162161
# to be later processed by connection worker
163162
await p.discovery.lookupRandomNode(p.connQueue)
164163

165-
func updateForkID(p: PeerPoolRef) =
164+
func updateForkId(p: PeerPoolRef) =
166165
if p.forkId.isNil:
167166
return
168167

169168
let forkId = p.forkId()
170169
if p.lastForkId == forkId:
171170
return
172171

173-
p.discovery.updateForkID(forkId)
172+
p.discovery.updateForkId(forkId)
174173
p.lastForkId = forkId
175174

176175
proc run(p: PeerPoolRef) {.async: (raises: [CancelledError]).} =
177176
trace "Running PeerPool..."
178177

179178
# initial cycle
180-
p.updateForkID()
179+
p.updateForkId()
181180
await p.discovery.start()
182181
await p.lookupPeers()
183182

@@ -199,7 +198,7 @@ proc run(p: PeerPoolRef) {.async: (raises: [CancelledError]).} =
199198
await p.lookupPeers()
200199

201200
if res == p.updateTimer:
202-
p.updateForkID()
201+
p.updateForkId()
203202

204203
#------------------------------------------------------------------------------
205204
# Private functions

execution_chain/nimbus_execution_client.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ proc setupP2P(nimbus: NimbusNode, config: ExecutionClientConf, com: CommonRef) =
104104
bootstrapNodes = config.getBootstrapNodes()
105105
fc = nimbus.fc
106106

107-
func forkIdProc(): ForkID =
107+
func forkIdProc(): ForkId =
108108
let header = fc.latestHeader()
109109
com.forkId(header.number, header.timestamp)
110110

111-
func compatibleForkIdProc(id: ForkID): bool =
111+
func compatibleForkIdProc(id: ForkId): bool =
112112
let header = fc.latestHeader()
113113
com.compatibleForkId(id, header.number, header.timestamp)
114114

execution_chain/rpc/rpc_utils.nim

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
{.push raises: [].}
1111

1212
import
13-
stew/endians2,
1413
std/[sequtils, algorithm],
1514
./rpc_types,
1615
./params,
@@ -341,15 +340,15 @@ proc populateConfigObject*(com: CommonRef, fork: HardFork): ConfigObject =
341340

342341
configObject.activationTime = Number com.activationTime(fork).get(EthTime(0))
343342
configObject.chainId = com.chainId
344-
configObject.forkId = FixedBytes[4] com.forkId(
343+
configObject.forkId = com.forkId(
345344
com.activationTime(fork).get(EthTime(0))
346-
).crc.toBytesBE
345+
).hash
347346
configObject.blobSchedule.max = Number com.maxBlobsPerBlock(fork)
348347
configObject.blobSchedule.target = Number com.targetBlobsPerBlock(fork)
349348
configObject.blobSchedule.baseFeeUpdateFraction = Number com.baseFeeUpdateFraction(fork)
350349

351350
# Precompiles
352-
let
351+
let
353352
evmFork = ToEVMFork[fork]
354353
lastPrecompile = getMaxPrecompile(evmFork)
355354

@@ -369,7 +368,7 @@ proc populateConfigObject*(com: CommonRef, fork: HardFork): ConfigObject =
369368

370369
return configObject
371370

372-
proc getEthConfigObject*(com: CommonRef,
371+
proc getEthConfigObject*(com: CommonRef,
373372
chain: ForkedChainRef,
374373
fork: HardFork,
375374
nextFork: Opt[HardFork],

execution_chain/sync/wire_protocol/handler.nim

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import
1414
chronicles, chronos,
15-
stew/endians2,
1615
./types,
1716
./requester,
1817
./broadcast,
@@ -64,10 +63,8 @@ proc getStatus68*(ctx: EthWireRef): Eth68State =
6463
totalDifficulty: txFrame.headTotalDifficulty,
6564
genesisHash: com.genesisHash,
6665
bestBlockHash: ctx.chain.latestHash,
67-
forkId: ChainForkId(
68-
forkHash: forkId.crc.toBytesBE,
69-
forkNext: forkId.nextFork
70-
))
66+
forkId: forkId,
67+
)
7168

7269
proc getStatus69*(ctx: EthWireRef): Eth69State =
7370
let
@@ -77,7 +74,7 @@ proc getStatus69*(ctx: EthWireRef): Eth69State =
7774

7875
Eth69State(
7976
genesisHash: com.genesisHash,
80-
forkId: forkId.to(ChainForkId),
77+
forkId: forkId,
8178
earliest: 0,
8279
latest: bestBlock.number,
8380
latestHash: ctx.chain.latestHash,

0 commit comments

Comments
 (0)