1010{.push raises : [].}
1111
1212import
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+
2022type
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,
0 commit comments