Skip to content

Commit

Permalink
feat(payloads): log first publish time
Browse files Browse the repository at this point in the history
  • Loading branch information
alextes committed Dec 14, 2023
1 parent 392b77f commit 552583d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
31 changes: 20 additions & 11 deletions beaconclient/multi_beacon_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"
"sync"
"time"

"github.com/flashbots/mev-boost-relay/common"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -42,7 +43,7 @@ type IMultiBeaconClient interface {
// GetStateValidators returns all active and pending validators from the beacon node
GetStateValidators(stateID string) (*GetStateValidatorsResponse, error)
GetProposerDuties(epoch uint64) (*ProposerDutiesResponse, error)
PublishBlock(block *common.SignedBeaconBlock) (code int, err error)
PublishBlock(block *common.SignedBeaconBlock) (code int, firstPublishTime int64, err error)
GetGenesis() (*GetGenesisResponse, error)
GetSpec() (spec *GetSpecResponse, err error)
GetForkSchedule() (spec *GetForkScheduleResponse, err error)
Expand Down Expand Up @@ -249,21 +250,22 @@ func (c *MultiBeaconClient) beaconInstancesByLeastUsed() []IBeaconInstance {
}

type publishResp struct {
index int
code int
err error
index int
code int
err error
completedAt int64
}

// PublishBlock publishes the signed beacon block via https://ethereum.github.io/beacon-APIs/#/ValidatorRequiredApi/publishBlock
func (c *MultiBeaconClient) PublishBlock(block *common.SignedBeaconBlock) (code int, err error) {
func (c *MultiBeaconClient) PublishBlock(block *common.SignedBeaconBlock) (code int, firstPublishTime int64, err error) {
log := c.log.WithFields(logrus.Fields{
"slot": block.Slot(),
"blockHash": block.BlockHash(),
})

clients := c.beaconInstancesByLastResponse()

// The chan will be cleaner up automatically once the function exists even if it was still being written to
// The chan will be cleaned up automatically once the function exits even if it was still being written to
resChans := make(chan publishResp, len(clients))

for i, client := range clients {
Expand All @@ -272,9 +274,10 @@ func (c *MultiBeaconClient) PublishBlock(block *common.SignedBeaconBlock) (code
go func(index int, client IBeaconInstance) {
code, err := client.PublishBlock(block, c.broadcastMode)
resChans <- publishResp{
index: index,
code: code,
err: err,
index: index,
code: code,
err: err,
completedAt: time.Now().UTC().UnixMilli(),
}
}(i, client)
}
Expand All @@ -295,14 +298,20 @@ func (c *MultiBeaconClient) PublishBlock(block *common.SignedBeaconBlock) (code
continue
}

if firstPublishTime == 0 {
firstPublishTime = res.completedAt
} else if res.completedAt < firstPublishTime {
firstPublishTime = res.completedAt
}

c.bestBeaconIndex.Store(int64(res.index))

log.WithField("statusCode", res.code).Info("published block")
return res.code, nil
return res.code, firstPublishTime, nil
}

log.Error("failed to publish block on any CL node")
return lastErrPublishResp.code, fmt.Errorf("last error: %w", lastErrPublishResp.err)
return lastErrPublishResp.code, 0, fmt.Errorf("last error: %w", lastErrPublishResp.err)
}

// GetGenesis returns the genesis info - https://ethereum.github.io/beacon-APIs/#/Beacon/getGenesis
Expand Down
6 changes: 3 additions & 3 deletions services/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1569,15 +1569,15 @@ func (api *RelayAPI) handleGetPayload(w http.ResponseWriter, req *http.Request)
timeBeforePublish = time.Now().UTC().UnixMilli()
log = log.WithField("timestampBeforePublishing", timeBeforePublish)
signedBeaconBlock := common.SignedBlindedBeaconBlockToBeaconBlock(payload, getPayloadResp)
code, err := api.beaconClient.PublishBlock(signedBeaconBlock) // errors are logged inside
code, firstPublishTime, err := api.beaconClient.PublishBlock(signedBeaconBlock) // errors are logged inside
if err != nil || code != http.StatusOK {
log.WithError(err).WithField("code", code).Error("failed to publish block")
api.RespondError(w, http.StatusBadRequest, "failed to publish block")
return
}
timeAfterPublish = time.Now().UTC().UnixMilli()
msNeededForPublishing = uint64(timeAfterPublish - timeBeforePublish)
log = log.WithField("timestampAfterPublishing", timeAfterPublish)
msNeededForPublishing = uint64(firstPublishTime - timeBeforePublish)
log = log.WithField("timestampAfterPublishing", firstPublishTime)
log.WithField("msNeededForPublishing", msNeededForPublishing).Info("block published through beacon node")

// give the beacon network some time to propagate the block
Expand Down

0 comments on commit 552583d

Please sign in to comment.