Skip to content

Commit

Permalink
validator register
Browse files Browse the repository at this point in the history
  • Loading branch information
domiwei committed May 28, 2024
1 parent 7ca564f commit 267a91d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cl/beacon/builder/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewBlockBuilderClient(baseUrl string) *BlockBuilderClient {
}
}

func (b *BlockBuilderClient) RegisterValidator(ctx context.Context, registers []*ValidatorRegistration) error {
func (b *BlockBuilderClient) RegisterValidator(ctx context.Context, registers []*cltypes.ValidatorRegistration) error {
// https://ethereum.github.io/builder-specs/#/Builder/registerValidator
url := b.baseUrl + "/eth/v1/builder/validators"
payload, err := json.Marshal(registers)
Expand Down
10 changes: 0 additions & 10 deletions cl/beacon/builder/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ type ExecutionPayloadHeader struct {
} `json:"data"`
}

type ValidatorRegistration struct {
Message struct {
FeeRecipient string `json:"fee_recipient"`
GasLimit string `json:"gas_limit"`
Timestamp string `json:"timestamp"`
PubKey string `json:"pubkey"`
} `json:"message"`
Sginature string `json:"signature"`
}

type BlindedBlockResponse struct {
Version string `json:"version"`
Data cltypes.Eth1Block `json:"data"`
Expand Down
19 changes: 19 additions & 0 deletions cl/beacon/handler/builder.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package handler

import (
"encoding/json"
"fmt"
"net/http"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/cl/beacon/beaconhttp"
"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/persistence/beacon_indicies"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
)
Expand Down Expand Up @@ -70,3 +72,20 @@ func (a *ApiHandler) GetEth1V1BuilderStatesExpectedWithdrawals(w http.ResponseWr

return nil, beaconhttp.NewEndpointError(http.StatusNotFound, fmt.Errorf("state not found"))
}

func (a *ApiHandler) PostEthV1BuilderRegisterValidator(w http.ResponseWriter, r *http.Request) (*beaconhttp.BeaconResponse, error) {
registerReq := []*cltypes.ValidatorRegistration{}
if err := json.NewDecoder(r.Body).Decode(&registerReq); err != nil {
return nil, beaconhttp.NewEndpointError(http.StatusBadRequest, err)
}
if len(registerReq) == 0 {
return nil, beaconhttp.NewEndpointError(http.StatusBadRequest, fmt.Errorf("empty request"))
}
if err := a.builderClient.RegisterValidator(r.Context(), registerReq); err != nil {
return nil, beaconhttp.NewEndpointError(http.StatusInternalServerError, err)
}
for _, v := range registerReq {
a.logger.Debug("[Caplin] Registred new validator", "fee_recipient", v.Message.FeeRecipient)
}
return newBeaconResponse(nil), nil
}
5 changes: 5 additions & 0 deletions cl/beacon/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ledgerwatch/erigon/cl/beacon/beacon_router_configuration"
"github.com/ledgerwatch/erigon/cl/beacon/beaconevents"
"github.com/ledgerwatch/erigon/cl/beacon/beaconhttp"
"github.com/ledgerwatch/erigon/cl/beacon/builder"
"github.com/ledgerwatch/erigon/cl/beacon/synced_data"
"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
Expand Down Expand Up @@ -86,6 +87,7 @@ type ApiHandler struct {
voluntaryExitService services.VoluntaryExitService
blsToExecutionChangeService services.BLSToExecutionChangeService
proposerSlashingService services.ProposerSlashingService
builderClient *builder.BlockBuilderClient
}

func NewApiHandler(
Expand Down Expand Up @@ -118,6 +120,7 @@ func NewApiHandler(
voluntaryExitService services.VoluntaryExitService,
blsToExecutionChangeService services.BLSToExecutionChangeService,
proposerSlashingService services.ProposerSlashingService,
builderClient *builder.BlockBuilderClient,
) *ApiHandler {
blobBundles, err := lru.New[common.Bytes48, BlobBundle]("blobs", maxBlobBundleCacheSize)
if err != nil {
Expand Down Expand Up @@ -158,6 +161,7 @@ func NewApiHandler(
voluntaryExitService: voluntaryExitService,
blsToExecutionChangeService: blsToExecutionChangeService,
proposerSlashingService: proposerSlashingService,
builderClient: builderClient,
}
}

Expand All @@ -182,6 +186,7 @@ func (a *ApiHandler) init() {
r.Route("/v1", func(r chi.Router) {
if a.routerCfg.Builder {
r.Get("/builder/states/{state_id}/expected_withdrawals", beaconhttp.HandleEndpointFunc(a.GetEth1V1BuilderStatesExpectedWithdrawals))
r.Post("/validator/register_validator", beaconhttp.HandleEndpointFunc(a.PostEthV1BuilderRegisterValidator))
}
if a.routerCfg.Events {
r.Get("/events", a.EventSourceGetV1Events)
Expand Down
13 changes: 13 additions & 0 deletions cl/cltypes/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cltypes

import libcommon "github.com/ledgerwatch/erigon-lib/common"

type ValidatorRegistration struct {
Message struct {
FeeRecipient libcommon.Address `json:"fee_recipient"`
GasLimit string `json:"gas_limit"`
Timestamp string `json:"timestamp"`
PubKey libcommon.Bytes48 `json:"pubkey"`
} `json:"message"`
Signature libcommon.Bytes96 `json:"signature"`
}
1 change: 1 addition & 0 deletions cmd/caplin/caplin1/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ func RunCaplinPhase1(ctx context.Context, engine execution_client.ExecutionEngin
voluntaryExitService,
blsToExecutionChangeService,
proposerSlashingService,
option.builderClient,
)
go beacon.ListenAndServe(&beacon.LayeredBeaconHandler{
ArchiveApi: apiHandler,
Expand Down

0 comments on commit 267a91d

Please sign in to comment.