Skip to content

Commit ca58ca6

Browse files
authored
Fix ForkId compatible bug (#3796)
* Fix ForkId compatible bug * Cleanup + remove additional test for other PR as it is still faulty
1 parent e449b1f commit ca58ca6

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

execution_chain/common/hardforks.nim

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ type
351351
cache: seq[ForkID]
352352

353353
func newID*(calc: ForkIdCalculator, head, time: uint64): ForkID =
354+
# Create a fork ID for a specific block height and timestamp:
354355
var crc = calc.genesisCRC
355356
for fork in calc.byBlock:
356357
if fork <= head:
@@ -370,15 +371,21 @@ func newID*(calc: ForkIdCalculator, head, time: uint64): ForkID =
370371

371372
func compatible*(calc: var ForkIdCalculator, forkId: ForkID): bool =
372373
if calc.cache.len == 0:
373-
calc.cache = newSeqOfCap[ForkID](calc.byBlock.len + calc.byTime.len)
374+
calc.cache = newSeqOfCap[ForkID](calc.byBlock.len + calc.byTime.len + 1)
374375
var crc = calc.genesisCRC
376+
377+
# Build cache of all valid ForkIds
378+
# Each entry is (crc before fork, fork number)
375379
for fork in calc.byBlock:
376-
crc = crc32(crc, fork.toBytesBE)
377380
calc.cache.add( (crc, fork) )
381+
crc = crc32(crc, fork.toBytesBE)
378382

379383
for fork in calc.byTime:
380-
crc = crc32(crc, fork.toBytesBE)
381384
calc.cache.add( (crc, fork) )
385+
crc = crc32(crc, fork.toBytesBE)
386+
387+
# Add last fork ID (after all forks, next=0)
388+
calc.cache.add( (crc, 0'u64) )
382389

383390
for id in calc.cache:
384391
if id == forkId:
@@ -389,6 +396,7 @@ func compatible*(calc: var ForkIdCalculator, forkId: ForkID): bool =
389396
func initForkIdCalculator*(map: ForkTransitionTable,
390397
genesisCRC: uint32,
391398
genesisTime: uint64): ForkIdCalculator =
399+
# Build ForkIdCalculator from the chain's fork configuration
392400

393401
# Extract the fork rule block number aggregate it
394402
var forksByBlock: seq[uint64]

execution_chain/networking/eth1_discovery.nim

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,12 @@ func eligibleNode(proto: Eth1Discovery, rec: Record): bool =
104104
return true
105105

106106
let
107-
chainForkIds = try: rlp.decode(bytes, array[1, ChainForkId])
108-
except RlpError: return false
109-
chainForkId = chainForkIds[0]
107+
ethValue =
108+
try:
109+
rlp.decode(bytes, array[1, ChainForkId])
110+
except RlpError:
111+
return false
112+
chainForkId = ethValue[0]
110113

111114
proto.compatibleForkId(chainForkId.to(ForkID))
112115

tests/test_forkid.nim

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,19 @@ const
8383
(number: 123'u64, time: 1762955545'u64, id: (crc: 0x23AA1351'u32, next: 0'u64)), # Future BPO2 time
8484
]
8585

86-
template runTest(network: untyped, name: string) =
86+
template runForkIdTest(network: untyped, name: string) =
8787
test name:
8888
var
8989
params = networkParams(network)
9090
com = CommonRef.new(newCoreDbRef DefaultDbMemory, nil, network, params)
9191

92-
for i, x in `network IDs`:
93-
let id = com.forkId(x.number, x.time)
94-
check toHex(id.crc) == toHex(x.id.crc)
95-
check id.nextFork == x.id.next
92+
for x in `network IDs`:
93+
let computedId = com.forkId(x.number, x.time)
94+
check toHex(computedId.crc) == toHex(x.id.crc)
95+
check computedId.nextFork == x.id.next
96+
97+
# The computed ID should be compatible with the CommonRef ForkIdCalculator itself
98+
check com.compatibleForkId(computedId) == true
9699

97100
func config(shanghai, cancun: uint64): ChainConfig =
98101
ChainConfig(
@@ -143,8 +146,8 @@ template runGenesisTimeIdTests() =
143146
check get.nextFork == x.want.next
144147

145148
suite "Fork ID tests":
146-
runTest(MainNet, "MainNet")
147-
runTest(SepoliaNet, "SepoliaNet")
148-
runTest(HoodiNet, "HoodiNet")
149+
runForkIdTest(MainNet, "MainNet")
150+
runForkIdTest(SepoliaNet, "SepoliaNet")
151+
runForkIdTest(HoodiNet, "HoodiNet")
149152
test "Genesis Time Fork ID":
150153
runGenesisTimeIdTests()

0 commit comments

Comments
 (0)