Skip to content

Commit

Permalink
added new endpoint version(v2) for validator aggregate and proof
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Miranda committed Sep 12, 2024
1 parent f53b621 commit a1d3b07
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
3 changes: 2 additions & 1 deletion beacon_chain/networking/eth2_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2648,7 +2648,8 @@ proc broadcastBlsToExecutionChange*(
node.broadcast(topic, bls_to_execution_change)

proc broadcastAggregateAndProof*(
node: Eth2Node, proof: phase0.SignedAggregateAndProof):
node: Eth2Node,
proof: phase0.SignedAggregateAndProof | electra.SignedAggregateAndProof):
Future[SendResult] {.async: (raises: [CancelledError], raw: true).} =
let topic = getAggregateAndProofsTopic(
node.forkDigestAtEpoch(node.getWallEpoch))
Expand Down
60 changes: 60 additions & 0 deletions beacon_chain/rpc/rest_validator_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,66 @@ proc installValidatorApiHandlers*(router: var RestRouter, node: BeaconNode) =
"Unexpected server failure, while sending aggregate and proof")
RestApiResponse.jsonMsgResponse(AggregateAndProofValidationSuccess)

# https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Validator/publishAggregateAndProofsV2
router.api2(MethodPost, "/eth/v2/validator/aggregate_and_proofs") do (
contentBody: Option[ContentBody]) -> RestApiResponse:

type
MultiForkProof = object
case isElectra: bool
of false:
phase0Proofs: seq[phase0.SignedAggregateAndProof]
of true:
electraProofs: seq[electra.SignedAggregateAndProof]

let
consensusVersion = request.headers.getString("Eth-Consensus-Version")
isElectra = consensusVersion == "electra"

if contentBody.isNone():
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)

let proofs =
block:
if isElectra:
let dres = decodeBody(seq[electra.SignedAggregateAndProof], contentBody.get())
if dres.isErr():
return RestApiResponse.jsonError(Http400,
InvalidAggregateAndProofObjectError,
$dres.error())
MultiForkProof(isElectra: true, electraProofs: dres.get())
else:
let dres = decodeBody(seq[phase0.SignedAggregateAndProof], contentBody.get())
if dres.isErr():
return RestApiResponse.jsonError(Http400,
InvalidAggregateAndProofObjectError,
$dres.error())
MultiForkProof(isElectra: false, phase0Proofs: dres.get())

let pending =
block:
var res: seq[Future[SendResult]]
if proofs.isElectra:
for proof in proofs.electraProofs:
res.add(node.router.routeSignedAggregateAndProof(proof))
else:
for proof in proofs.phase0Proofs:
res.add(node.router.routeSignedAggregateAndProof(proof))

res
await allFutures(pending)
for future in pending:
if future.completed():
let res = future.value()
if res.isErr():
return RestApiResponse.jsonError(Http400,
AggregateAndProofValidationError,
$res.error())
else:
return RestApiResponse.jsonError(Http500,
"Unexpected server failure, while sending aggregate and proof")
RestApiResponse.jsonMsgResponse(AggregateAndProofValidationSuccess)

# https://ethereum.github.io/beacon-APIs/#/Validator/prepareBeaconCommitteeSubnet
router.api2(MethodPost,
"/eth/v1/validator/beacon_committee_subscriptions") do (
Expand Down
2 changes: 2 additions & 0 deletions beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ RestJson.useDefaultSerializationFor(
deneb_mev.ExecutionPayloadAndBlobsBundle,
deneb_mev.SignedBlindedBeaconBlock,
deneb_mev.SignedBuilderBid,
electra.AggregateAndProof,
electra.Attestation,
electra.AttesterSlashing,
electra.BeaconBlock,
Expand All @@ -257,6 +258,7 @@ RestJson.useDefaultSerializationFor(
electra.LightClientHeader,
electra.LightClientOptimisticUpdate,
electra.LightClientUpdate,
electra.SignedAggregateAndProof,
electra.SignedBeaconBlock,
electra.TrustedAttestation,
electra_mev.BlindedBeaconBlock,
Expand Down
7 changes: 7 additions & 0 deletions beacon_chain/spec/eth2_apis/rest_validator_calls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ proc publishAggregateAndProofs*(
meth: MethodPost.}
## https://ethereum.github.io/beacon-APIs/#/Validator/publishAggregateAndProofs

proc publishAggregateAndProofsV2*(
body: seq[phase0.SignedAggregateAndProof | electra.SignedAggregateAndProof]
): RestPlainResponse {.
rest, endpoint: "/eth/v2/validator/aggregate_and_proofs",
meth: MethodPost.}
## https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Validator/publishAggregateAndProofsV2

proc prepareBeaconCommitteeSubnet*(
body: seq[RestCommitteeSubscription]
): RestPlainResponse {.
Expand Down
3 changes: 2 additions & 1 deletion beacon_chain/validators/message_router.nim
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ proc routeAttestation*(
attestation, subnet_id, checkSignature = true, checkValidator = true)

proc routeSignedAggregateAndProof*(
router: ref MessageRouter, proof: phase0.SignedAggregateAndProof,
router: ref MessageRouter,
proof: phase0.SignedAggregateAndProof | electra.SignedAggregateAndProof,
checkSignature = true):
Future[SendResult] {.async: (raises: [CancelledError]).} =
## Validate and broadcast aggregate
Expand Down

0 comments on commit a1d3b07

Please sign in to comment.