Skip to content

Commit aabec2a

Browse files
committed
integrate payload attestations into validator workflow
1 parent d9f6f8f commit aabec2a

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

beacon_chain/validators/beacon_validators.nim

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,89 @@ proc sendSyncCommitteeContributions(
838838
asyncSpawn signAndSendContribution(
839839
node, validator, subcommitteeIdx, head, slot)
840840

841+
proc checkPayloadPresent*(
842+
node: BeaconNode, beacon_block_root: Eth2Digest
843+
): bool =
844+
## Check if we received the payload envelope for this slot
845+
debugGloasComment"TODO - check for envelope availability"
846+
return true
847+
848+
proc checkBlobDataAvailable*(
849+
node: BeaconNode, beacon_block_root: Eth2Digest
850+
): bool =
851+
## Check if the blob(s) for this slot is/are available
852+
debugGloasComment"TODO - check for blob availability"
853+
return true
854+
855+
proc createAndSendPayloadAttestation(node: BeaconNode,
856+
fork: Fork,
857+
genesis_validators_root: Eth2Digest,
858+
validator: AttachedValidator,
859+
validator_index: ValidatorIndex,
860+
slot: Slot,
861+
beacon_block_root: Eth2Digest)
862+
{.async: (raises: [CancelledError]).} =
863+
let
864+
payload_present = node.checkPayloadPresent(beacon_block_root)
865+
blob_data_available = node.checkBlobDataAvailable(beacon_block_root)
866+
867+
let data = PayloadAttestationData(
868+
beacon_block_root: beacon_block_root,
869+
slot: slot,
870+
payload_present: payload_present,
871+
blob_data_available: blob_data_available
872+
)
873+
874+
let signature = block:
875+
let res = await validator.getPayloadAttestationSignature(
876+
fork, genesis_validators_root, data)
877+
if res.isErr():
878+
warn "Unble to sign payload attestation",
879+
validator = shortLog(validator),
880+
data = shortLog(data),
881+
error_msg = res.error()
882+
return
883+
res.get()
884+
885+
let message = PayloadAttestationMessage(
886+
validator_index: validator_index.uint64,
887+
data: data,
888+
signature: signature
889+
)
890+
891+
discard await node.router.routePayloadAttestationMessage(
892+
message, checkSignature = false, checkValidator = false)
893+
894+
proc sendPayloadAttestations(
895+
node: BeaconNode, head: BlockRef, slot: Slot) =
896+
## Perform payload attestation duties for PTC members
897+
898+
let consensusFork = node.dag.cfg.consensusForkAtEpoch(slot.epoch)
899+
if consensusFork < ConsensusFork.Gloas:
900+
return
901+
902+
# Get the beacon block root for the slot we are attesting to
903+
let target = head.atSlot(slot)
904+
if head != target.blck:
905+
notice "Payload attestation to a state in the past",
906+
attestationTarget = shortLog(target),
907+
head = shortLog(head)
908+
909+
let
910+
fork = node.dag.forkAtEpoch(slot.epoch)
911+
genesis_validators_root = node.dag.genesis_validators_root
912+
913+
withState(node.dag.headState):
914+
when consensusFork >= ConsensusFork.Gloas:
915+
var cache: StateCache
916+
for vidx in get_ptc(forkyState.data, slot, cache):
917+
let validator = node.getValidatorForDuties(vidx, slot).valueOr:
918+
continue
919+
920+
asyncSpawn createAndSendPayloadAttestation(
921+
node, fork, genesis_validators_root, validator, vidx, slot,
922+
target.blck.root )
923+
841924
proc handleProposal(node: BeaconNode, head: BlockRef, slot: Slot):
842925
Future[BlockRef] {.async: (raises: [CancelledError]).} =
843926
## Perform the proposal for the given slot, iff we have a validator attached
@@ -1263,6 +1346,15 @@ proc handleValidatorDuties*(node: BeaconNode, lastSlot, slot: Slot) {.async: (ra
12631346
sendAttestations(node, head, slot)
12641347
sendSyncCommitteeMessages(node, head, slot)
12651348

1349+
let payloadAttestationCutOff = node.beaconClock.fromNow(
1350+
slot.payload_attestation_deadline(node.dag.timeParams))
1351+
if payloadAttestationCutOff.inFuture:
1352+
debug "Waiting to send payload attestations",
1353+
payloadAttestationCutOff = shortLog(payloadAttestationCutOff.offset)
1354+
await sleepAsync(payloadAttestationCutOff.offset)
1355+
1356+
sendPayloadAttestations(node, head, slot)
1357+
12661358
updateValidatorMetrics(node) # the important stuff is done, update the vanity numbers
12671359

12681360
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-beta.2/specs/phase0/validator.md#broadcast-aggregate

0 commit comments

Comments
 (0)