Skip to content

Commit 32039d5

Browse files
authored
use fork aware deadline functions (#7744)
* use fork aware deadline functions * fix
1 parent 688b3ee commit 32039d5

File tree

12 files changed

+166
-58
lines changed

12 files changed

+166
-58
lines changed

beacon_chain/fork_choice/fork_choice.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ proc process_block*(self: var ForkChoice,
295295
# Add proposer score boost if the block is timely
296296
let slot = self.checkpoints.time.slotOrZero(dag.timeParams)
297297
if slot == blck.slot and
298-
self.checkpoints.time < slot.attestation_deadline(dag.timeParams) and
298+
self.checkpoints.time < slot.attestation_deadline(
299+
dag.timeParams, typeof(blck).kind) and
299300
self.checkpoints.proposer_boost_root == ZERO_HASH:
300301
self.checkpoints.proposer_boost_root = blckRef.root
301302

beacon_chain/gossip_processing/eth2_processor.nim

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,11 @@ proc processAttestation*(
546546
return errIgnore("Attestation before genesis")
547547

548548
# Potential under/overflows are fine; would just create odd metrics and logs
549-
let delay = wallTime -
550-
attestation.data.slot.attestation_deadline(self.dag.timeParams)
549+
let
550+
consensusFork =
551+
self.dag.cfg.consensusForkAtEpoch(attestation.data.slot.epoch)
552+
delay = wallTime - attestation.data.slot.attestation_deadline(
553+
self.dag.timeParams, consensusFork)
551554
debug "Attestation received", delay
552555

553556
let v = when attestation is phase0.Attestation:
@@ -614,7 +617,7 @@ proc processSignedAggregateAndProof*(
614617
# Potential under/overflows are fine; would just create odd logs
615618
let
616619
slot = signedAggregateAndProof.message.aggregate.data.slot
617-
delay = wallTime - slot.aggregate_deadline(self.dag.timeParams)
620+
delay = wallTime - slot.aggregate_deadline(self.dag.timeParams, fork)
618621
debug "Aggregate received", delay
619622

620623
let v = await self.attestationPool.validateAggregate(

beacon_chain/spec/beacon_time.nim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,32 @@ func sync_contribution_deadline*(
223223
s.start_beacon_time(timeParams) +
224224
timeParams.syncContributionSlotOffset
225225

226+
# Gloas
227+
func attestation_deadline_gloas*(
228+
s: Slot, timeParams: TimeParams): BeaconTime =
229+
s.start_beacon_time(timeParams) +
230+
timeParams.attestationSlotOffsetGloas
231+
232+
func aggregate_deadline_gloas*(
233+
s: Slot, timeParams: TimeParams): BeaconTime =
234+
s.start_beacon_time(timeParams) +
235+
timeParams.aggregateSlotOffsetGloas
236+
237+
func sync_committee_message_deadline_gloas*(
238+
s: Slot, timeParams: TimeParams): BeaconTime =
239+
s.start_beacon_time(timeParams) +
240+
timeParams.syncCommitteeMessageSlotOffsetGloas
241+
242+
func sync_contribution_deadline_gloas*(
243+
s: Slot, timeParams: TimeParams): BeaconTime =
244+
s.start_beacon_time(timeParams) +
245+
timeParams.syncContributionSlotOffsetGloas
246+
247+
func payload_attestation_deadline*(
248+
s: Slot, timeParams: TimeParams): BeaconTime =
249+
s.start_beacon_time(timeParams) +
250+
timeParams.payloadAttestationSlotOffset
251+
226252
func light_client_finality_update_time*(
227253
s: Slot, timeParams: TimeParams): BeaconTime =
228254
s.start_beacon_time(timeParams) +

beacon_chain/spec/helpers.nim

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,3 +583,35 @@ func is_builder_payment_withdrawable*(
583583
# https://github.com/ethereum/consensus-specs/blob/v1.6.0-beta.0/specs/gloas/beacon-chain.md#new-is_parent_block_full
584584
func is_parent_block_full*(state: gloas.BeaconState): bool =
585585
state.latest_execution_payload_bid.block_hash == state.latest_block_hash
586+
587+
func attestation_deadline*(
588+
s: Slot, timeParams: TimeParams,
589+
consensusFork: ConsensusFork): BeaconTime =
590+
if consensusFork >= ConsensusFork.Gloas:
591+
attestation_deadline_gloas(s, timeParams)
592+
else:
593+
attestation_deadline(s, timeParams)
594+
595+
func aggregate_deadline*(
596+
s: Slot, timeParams: TimeParams,
597+
consensusFork: ConsensusFork): BeaconTime =
598+
if consensusFork >= ConsensusFork.Gloas:
599+
aggregate_deadline_gloas(s, timeParams)
600+
else:
601+
aggregate_deadline(s, timeParams)
602+
603+
func sync_committee_message_deadline*(
604+
s: Slot, timeParams: TimeParams,
605+
consensusFork: ConsensusFork): BeaconTime =
606+
if consensusFork >= ConsensusFork.Gloas:
607+
sync_committee_message_deadline_gloas(s, timeParams)
608+
else:
609+
sync_committee_message_deadline(s, timeParams)
610+
611+
func sync_contribution_deadline*(
612+
s: Slot, timeParams: TimeParams,
613+
consensusFork: ConsensusFork): BeaconTime =
614+
if consensusFork >= ConsensusFork.Gloas:
615+
sync_contribution_deadline_gloas(s, timeParams)
616+
else:
617+
sync_contribution_deadline(s, timeParams)

beacon_chain/validator_client/attestation_service.nim

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ proc serveAttestation(
6363
raise exc
6464

6565
logScope:
66-
delay = vc.getDelay(attestationSlot.attestation_deadline(vc.timeParams))
66+
delay = vc.getDelay(attestationSlot.attestation_deadline(
67+
vc.timeParams, consensusFork))
6768

6869
debug "Sending attestation"
6970

@@ -92,7 +93,8 @@ proc serveAttestation(
9293
submitAttestation(attestation)
9394

9495
if res:
95-
let delay = vc.getDelay(attestationSlot.attestation_deadline(vc.timeParams))
96+
let delay = vc.getDelay(attestationSlot.attestation_deadline(
97+
vc.timeParams, consensusFork))
9698
beacon_attestations_sent.inc()
9799
beacon_attestation_sent_delay.observe(delay.toFloatSeconds())
98100
notice "Attestation published"
@@ -144,7 +146,7 @@ proc serveAggregateAndProofV2*(
144146
raiseAssert "Unsupported SignedAggregateAndProof"
145147

146148
logScope:
147-
delay = vc.getDelay(slot.aggregate_deadline(vc.timeParams))
149+
delay = vc.getDelay(slot.aggregate_deadline(vc.timeParams, consensusFork))
148150

149151
debug "Sending aggregated attestation", fork = consensusFork
150152

@@ -258,8 +260,10 @@ proc produceAndPublishAttestationsV2*(
258260
else:
259261
inc(errored)
260262
(succeed, errored, failed)
261-
262-
delay = vc.getDelay(slot.attestation_deadline(vc.timeParams))
263+
264+
consensusFork = vc.getConsensusFork(fork)
265+
delay = vc.getDelay(slot.attestation_deadline(
266+
vc.timeParams, consensusFork))
263267

264268
debug "Attestation statistics", total = len(pendingAttestations),
265269
succeed = statistics[0], failed_to_deliver = statistics[1],
@@ -372,7 +376,10 @@ proc produceAndPublishAggregatesV2(
372376
inc(errored)
373377
(succeed, errored, failed)
374378

375-
let delay = vc.getDelay(slot.aggregate_deadline(vc.timeParams))
379+
let
380+
consensusFork = vc.getConsensusFork(vc.forkAtEpoch(slot.epoch))
381+
delay = vc.getDelay(
382+
slot.aggregate_deadline(vc.timeParams, consensusFork))
376383
debug "Aggregated attestation statistics", total = len(aggregates),
377384
succeed = statistics[0], failed_to_deliver = statistics[1],
378385
not_accepted = statistics[2], delay = delay, slot = slot,
@@ -387,7 +394,10 @@ proc publishAttestationsAndAggregatesV2(
387394
vc = service.client
388395

389396
block:
390-
let delay = vc.getDelay(slot.attestation_deadline(vc.timeParams))
397+
let
398+
consensusFork = vc.getConsensusFork(vc.forkAtEpoch(slot.epoch))
399+
delay = vc.getDelay(slot.attestation_deadline(
400+
vc.timeParams, consensusFork))
391401
debug "Producing attestations", delay = delay, slot = slot,
392402
duties_count = len(duties)
393403

@@ -402,14 +412,17 @@ proc publishAttestationsAndAggregatesV2(
402412
debug "Publish attestation request was interrupted"
403413
raise exc
404414

405-
let aggregateTime = vc.beaconClock.fromNow(
406-
slot.aggregate_deadline(vc.timeParams))
415+
let
416+
consensusFork = vc.getConsensusFork(vc.forkAtEpoch(slot.epoch))
417+
aggregateTime = vc.beaconClock.fromNow(
418+
slot.aggregate_deadline(vc.timeParams, consensusFork))
407419
if aggregateTime.inFuture:
408420
await sleepAsync(aggregateTime.offset)
409421

410422
block:
411423
let
412-
delay = vc.getDelay(slot.aggregate_deadline(vc.timeParams))
424+
delay = vc.getDelay(
425+
slot.aggregate_deadline(vc.timeParams, consensusFork))
413426
dutiesByCommittee = getAttesterDutiesByCommittee(duties)
414427
debug "Producing aggregate and proofs", delay = delay
415428
var tasks: seq[Future[void].Raising([CancelledError])]

beacon_chain/validator_client/block_service.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,10 @@ proc runBlockPollMonitor(service: BlockServiceRef,
598598
let
599599
currentTime = vc.beaconClock.now()
600600
afterSlot = currentTime.slotOrZero(vc.timeParams)
601+
consensusFork = vc.getConsensusFork(vc.forkAtEpoch(afterSlot.epoch))
601602

602-
if currentTime > afterSlot.attestation_deadline(vc.timeParams):
603+
if currentTime > afterSlot.attestation_deadline(
604+
vc.timeParams, consensusFork):
603605
# Attestation time already, lets wait for next slot.
604606
continue
605607

beacon_chain/validator_client/common.nim

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,24 @@ proc getCurrentSlot*(vc: ValidatorClientRef): Opt[Slot] =
879879
else:
880880
Opt.none(Slot)
881881

882+
proc getConsensusFork*(vc: ValidatorClientRef, fork: Fork): ConsensusFork =
883+
doAssert(vc.forkConfig.isSome())
884+
for key, value in vc.forkConfig.get().pairs():
885+
if value.version == fork.current_version:
886+
return key
887+
raiseAssert "ForkConfig missing fork [" & $fork.current_version & "]"
888+
889+
proc forkAtEpoch*(vc: ValidatorClientRef, epoch: Epoch): Fork =
890+
# If schedule is present, it MUST not be empty.
891+
doAssert(len(vc.forks) > 0)
892+
var res: Fork
893+
for item in vc.forks:
894+
if item.epoch <= epoch:
895+
res = item
896+
else:
897+
break
898+
res
899+
882900
proc getAttesterDutiesForSlot*(vc: ValidatorClientRef,
883901
slot: Slot): seq[DutyAndProof] =
884902
## Returns all `DutyAndProof` for the given `slot`.
@@ -903,7 +921,10 @@ proc getSyncCommitteeDutiesForSlot*(vc: ValidatorClientRef,
903921
proc getDurationToNextAttestation*(vc: ValidatorClientRef,
904922
slot: Slot): string =
905923
var minSlot = FAR_FUTURE_SLOT
906-
let currentEpoch = slot.epoch()
924+
let
925+
currentEpoch = slot.epoch()
926+
consensusFork = vc.getConsensusFork(vc.forkAtEpoch(currentEpoch))
927+
907928
for epoch in [currentEpoch, currentEpoch + 1'u64]:
908929
for key, item in vc.attesters:
909930
let duty = item.duties.getOrDefault(epoch, DefaultDutyAndProof)
@@ -916,7 +937,7 @@ proc getDurationToNextAttestation*(vc: ValidatorClientRef,
916937
if minSlot == FAR_FUTURE_SLOT:
917938
"<unknown>"
918939
else:
919-
$(minSlot.attestation_deadline(vc.timeParams) -
940+
$(minSlot.attestation_deadline(vc.timeParams, consensusFork) -
920941
slot.start_beacon_time(vc.timeParams))
921942

922943
proc getDurationToNextBlock*(vc: ValidatorClientRef, slot: Slot): string =
@@ -979,24 +1000,6 @@ proc getValidatorForDuties*(vc: ValidatorClientRef,
9791000
slashingSafe = false): Opt[AttachedValidator] =
9801001
vc.attachedValidators[].getValidatorForDuties(key, slot, slashingSafe)
9811002

982-
proc forkAtEpoch*(vc: ValidatorClientRef, epoch: Epoch): Fork =
983-
# If schedule is present, it MUST not be empty.
984-
doAssert(len(vc.forks) > 0)
985-
var res: Fork
986-
for item in vc.forks:
987-
if item.epoch <= epoch:
988-
res = item
989-
else:
990-
break
991-
res
992-
993-
proc getConsensusFork*(vc: ValidatorClientRef, fork: Fork): ConsensusFork =
994-
doAssert(vc.forkConfig.isSome())
995-
for key, value in vc.forkConfig.get().pairs():
996-
if value.version == fork.current_version:
997-
return key
998-
raiseAssert "ForkConfig missing fork [" & $fork.current_version & "]"
999-
10001003
proc isPastElectraFork*(vc: ValidatorClientRef, epoch: Epoch): bool =
10011004
doAssert(len(vc.forks) > 0)
10021005
doAssert(vc.forkConfig.isSome())
@@ -1492,12 +1495,13 @@ proc waitForBlock*(
14921495
shortLog(blocks[0])
14931496
else:
14941497
"[" & blocks.mapIt(shortLog(it)).join(", ") & "]"
1498+
consensusFork = vc.getConsensusFork(vc.forkAtEpoch(slot.epoch))
14951499

14961500
debug "Block proposal awaited", duration = dur,
14971501
block_root = blockRoot
14981502

14991503
try:
1500-
await waitAfterBlockCutoff(vc.beaconClock, slot)
1504+
await waitAfterBlockCutoff(vc.beaconClock, slot, consensusFork)
15011505
except CancelledError as exc:
15021506
let dur = Moment.now() - startTime
15031507
debug "Waiting for block cutoff was interrupted", duration = dur

beacon_chain/validator_client/sync_committee_service.nim

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ proc serveSyncCommitteeMessage*(
3737
vc = service.client
3838
startTime = Moment.now()
3939
fork = vc.forkAtEpoch(slot.epoch)
40+
currentFork = vc.getConsensusFork(fork)
4041
genesisValidatorsRoot = vc.beaconGenesis.genesis_validators_root
4142
vindex = duty.validator_index
4243
validator = vc.getValidatorForDuties(
@@ -63,7 +64,8 @@ proc serveSyncCommitteeMessage*(
6364

6465
debug "Sending sync committee message",
6566
delay = vc.getDelay(
66-
message.slot.sync_committee_message_deadline(vc.timeParams))
67+
message.slot.sync_committee_message_deadline(
68+
vc.timeParams, currentFork))
6769

6870
let res =
6971
try:
@@ -78,7 +80,8 @@ proc serveSyncCommitteeMessage*(
7880

7981
let
8082
delay = vc.getDelay(
81-
message.slot.sync_committee_message_deadline(vc.timeParams))
83+
message.slot.sync_committee_message_deadline(
84+
vc.timeParams, currentFork))
8285
dur = Moment.now() - startTime
8386

8487
if res:
@@ -133,7 +136,9 @@ proc produceAndPublishSyncCommitteeMessages(
133136
(succeed, errored, failed)
134137

135138
let
136-
delay = vc.getDelay(slot.attestation_deadline(vc.timeParams))
139+
consensusFork = vc.getConsensusFork(vc.forkAtEpoch(slot.epoch))
140+
delay = vc.getDelay(slot.attestation_deadline(
141+
vc.timeParams, consensusFork))
137142
dur = Moment.now() - startTime
138143

139144
debug "Sync committee message statistics",
@@ -154,6 +159,7 @@ proc serveContributionAndProof*(
154159
slot = proof.contribution.slot
155160
genesisRoot = vc.beaconGenesis.genesis_validators_root
156161
fork = vc.forkAtEpoch(slot.epoch)
162+
consensusFork = vc.getConsensusFork(fork)
157163

158164
logScope:
159165
validator = validatorLog(validator)
@@ -176,7 +182,8 @@ proc serveContributionAndProof*(
176182
res.get()
177183

178184
debug "Sending sync contribution",
179-
delay = vc.getDelay(slot.sync_contribution_deadline(vc.timeParams))
185+
delay = vc.getDelay(slot.sync_contribution_deadline(
186+
vc.timeParams, consensusFork))
180187

181188
let restSignedProof = RestSignedContributionAndProof.init(
182189
proof, signature)
@@ -328,7 +335,8 @@ proc produceAndPublishContributions(
328335
(succeed, errored, failed)
329336

330337
let
331-
delay = vc.getDelay(slot.aggregate_deadline(vc.timeParams))
338+
consensusFork = vc.getConsensusFork(vc.forkAtEpoch(slot.epoch))
339+
delay = vc.getDelay(slot.aggregate_deadline(vc.timeParams, consensusFork))
332340
dur = Moment.now() - startTime
333341

334342
debug "Sync message contribution statistics",
@@ -355,8 +363,10 @@ proc publishSyncMessagesAndContributions(
355363
slot = slot
356364

357365
block:
358-
let delay = vc.getDelay(
359-
slot.sync_committee_message_deadline(vc.timeParams))
366+
let
367+
currentFork = vc.getConsensusFork(vc.forkAtEpoch(slot.epoch))
368+
delay = vc.getDelay(
369+
slot.sync_committee_message_deadline(vc.timeParams, currentFork))
360370
debug "Producing sync committee messages", delay = delay,
361371
duties_count = len(duties)
362372

@@ -396,15 +406,19 @@ proc publishSyncMessagesAndContributions(
396406
return
397407

398408
let currentTime = vc.beaconClock.now()
399-
if slot.sync_contribution_deadline(vc.timeParams) > currentTime:
409+
let consensusFork = vc.getConsensusFork(vc.forkAtEpoch(slot.epoch))
410+
if slot.sync_contribution_deadline(
411+
vc.timeParams, consensusFork) > currentTime:
400412
let waitDur = nanoseconds((
401-
slot.sync_contribution_deadline(vc.timeParams) - currentTime).nanoseconds)
413+
slot.sync_contribution_deadline(
414+
vc.timeParams, consensusFork) - currentTime).nanoseconds)
402415
# Sleeping until `sync_contribution_deadline`.
403416
debug "Waiting for sync contribution deadline", wait_time = waitDur
404417
await sleepAsync(waitDur)
405418

406419
block:
407-
let delay = vc.getDelay(slot.sync_contribution_deadline(vc.timeParams))
420+
let delay = vc.getDelay(
421+
slot.sync_contribution_deadline(vc.timeParams, consensusFork))
408422
debug "Producing contribution and proofs", delay = delay
409423

410424
try:

0 commit comments

Comments
 (0)