Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
avalonche committed Oct 3, 2023
1 parent 7b2871c commit 0014259
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 21 deletions.
1 change: 1 addition & 0 deletions beaconclient/prod_beacon_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type PayloadAttributes struct {
PrevRandao string `json:"prev_randao"`
SuggestedFeeRecipient string `json:"suggested_fee_recipient"`
Withdrawals []*capella.Withdrawal `json:"withdrawals"`
ParentBeaconBlockRoot string `json:"parent_beacon_block_root"`
}

func (c *ProdBeaconInstance) SubscribeToHeadEvents(slotC chan HeadEventData) {
Expand Down
2 changes: 1 addition & 1 deletion common/types_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (r *BuilderBlockValidationRequest) MarshalJSON() ([]byte, error) {
}

attrs, err := json.Marshal(&struct {
RegisteredGasLimit uint64 `json:"registered_gas_limit,string"`
RegisteredGasLimit uint64 `json:"registered_gas_limit,string"`
ParentBeaconBlockRoot *phase0.Root `json:"parent_beacon_block_root,omitempty"`
}{
RegisteredGasLimit: r.RegisteredGasLimit,
Expand Down
6 changes: 3 additions & 3 deletions datastore/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (r *RedisCache) GetBestBid(slot uint64, parentHash, proposerPubkey string)
func (r *RedisCache) GetPayloadContents(slot uint64, proposerPubkey, blockHash string) (*builderApi.VersionedSubmitBlindedBlockResponse, error) {
resp, err := r.GetPayloadContentsDeneb(slot, proposerPubkey, blockHash)
if errors.Is(err, redis.Nil) {
// can't find builderApiDeneb payload, try find capella payload
// can't find deneb payload, try find capella payload
return r.GetExecutionPayloadCapella(slot, proposerPubkey, blockHash)
}
return resp, err
Expand Down Expand Up @@ -549,12 +549,12 @@ func (r *RedisCache) SaveBidAndUpdateTopBid(ctx context.Context, pipeliner redis
// 1. Save the execution payload
switch payload.Version {
case spec.DataVersionCapella:
err = r.SaveExecutionPayloadCapella(ctx, pipeliner, submission.Slot, submission.ParentHash.String(), submission.Proposer.String(), getPayloadResponse.Capella)
err = r.SaveExecutionPayloadCapella(ctx, pipeliner, submission.Slot, submission.Proposer.String(), submission.BlockHash.String(), getPayloadResponse.Capella)
if err != nil {
return state, err
}
case spec.DataVersionDeneb:
err = r.SavePayloadContentsDeneb(ctx, pipeliner, submission.Slot, submission.ParentHash.String(), submission.Proposer.String(), getPayloadResponse.Deneb)
err = r.SavePayloadContentsDeneb(ctx, pipeliner, submission.Slot, submission.Proposer.String(), submission.BlockHash.String(), getPayloadResponse.Deneb)
if err != nil {
return state, err
}
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,6 @@ retract (
v1.0.0-alpha1
)

replace github.com/attestantio/go-eth2-client => github.com/avalonche/go-eth2-client v0.0.0-20230810012152-c934f6ee3bee
replace github.com/attestantio/go-eth2-client => github.com/avalonche/go-eth2-client v0.0.0-20231003132440-649530ab1e0e

replace github.com/attestantio/go-builder-client => github.com/avalonche/go-builder-client v0.0.0-20231003081344-5371b036e940
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ github.com/alicebob/miniredis/v2 v2.30.5/go.mod h1:b25qWj4fCEsBeAAR2mlb0ufImGC6u
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8=
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/attestantio/go-builder-client v0.3.2-0.20230809093013-1fa02af241e1 h1:c6Jmkd6mryaFuWv2/Pa4aLqi+2QOv3gEN+Krj0Nmiq4=
github.com/attestantio/go-builder-client v0.3.2-0.20230809093013-1fa02af241e1/go.mod h1:8NZx9L/rC7nLhSMbbVR6PXr37tC28wTE4u65n+Pa3BQ=
github.com/avalonche/go-eth2-client v0.0.0-20230810012152-c934f6ee3bee h1:+Z2E/TGJW7k+MSPWPLvwsOm4SLcRgMeO02WUjDwC8OI=
github.com/avalonche/go-eth2-client v0.0.0-20230810012152-c934f6ee3bee/go.mod h1:KSVlZSW1A3jUg5H8O89DLtqxgJprRfTtI7k89fLdhu0=
github.com/avalonche/go-builder-client v0.0.0-20231003081344-5371b036e940 h1:shNNoTQ1YB4V+6wwvZEKYM32N8yMDdI719iVuRj1TiE=
github.com/avalonche/go-builder-client v0.0.0-20231003081344-5371b036e940/go.mod h1:qLWtbyzPMFmlKbZKIdj/UJ4aCJCZU5jKJUYs3zHfdVM=
github.com/avalonche/go-eth2-client v0.0.0-20231003132440-649530ab1e0e h1:W3OTQC1JU++5TVqcPDHYFqhh1PIYCIgAubmd4z9jyE0=
github.com/avalonche/go-eth2-client v0.0.0-20231003132440-649530ab1e0e/go.mod h1:KSVlZSW1A3jUg5H8O89DLtqxgJprRfTtI7k89fLdhu0=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
11 changes: 8 additions & 3 deletions services/api/blocksim_ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
ErrSimulationFailed = errors.New("simulation failed")
ErrJSONDecodeFailed = errors.New("json error")
ErrNoCapellaPayload = errors.New("capella payload is nil")
ErrNoDenebPayload = errors.New("deneb payload is nil")

maxConcurrentBlocks = int64(cli.GetEnvInt("BLOCKSIM_MAX_CONCURRENT", 4)) // 0 for no maximum
simRequestTimeout = time.Duration(cli.GetEnvInt("BLOCKSIM_TIMEOUT_MS", 10000)) * time.Millisecond
Expand Down Expand Up @@ -71,10 +72,14 @@ func (b *BlockSimulationRateLimiter) Send(context context.Context, payload *comm
}

var simReq *jsonrpc.JSONRPCRequest
if payload.Capella == nil {
if payload.Version == spec.DataVersionCapella && payload.Capella == nil {
return ErrNoCapellaPayload, nil
}

if payload.Version == spec.DataVersionDeneb && payload.Deneb == nil {
return ErrNoDenebPayload, nil
}

submission, err := common.GetBlockSubmissionInfo(payload.VersionedSubmitBlockRequest)
if err != nil {
return err, nil
Expand All @@ -92,9 +97,9 @@ func (b *BlockSimulationRateLimiter) Send(context context.Context, payload *comm

// Create and fire off JSON-RPC request
if payload.Version == spec.DataVersionDeneb {
simReq = jsonrpc.NewJSONRPCRequest("1", "flashbots_validateBuilderSubmissionV2", payload)
} else {
simReq = jsonrpc.NewJSONRPCRequest("1", "flashbots_validateBuilderSubmissionV3", payload)
} else {
simReq = jsonrpc.NewJSONRPCRequest("1", "flashbots_validateBuilderSubmissionV2", payload)
}
_, requestErr, validationErr = SendJSONRPCRequest(&b.client, *simReq, b.blockSimURL, headers)
return requestErr, validationErr
Expand Down
35 changes: 27 additions & 8 deletions services/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ type payloadAttributesHelper struct {
slot uint64
parentHash string
withdrawalsRoot phase0.Root
parentBeaconRoot *phase0.Root
payloadAttributes beaconclient.PayloadAttributes
}

Expand Down Expand Up @@ -691,6 +692,22 @@ func (api *RelayAPI) processPayloadAttributes(payloadAttributes beaconclient.Pay
}
}

var parentBeaconRoot *phase0.Root
if hasReachedFork(payloadAttrSlot, api.denebEpoch) {
if payloadAttributes.Data.PayloadAttributes.ParentBeaconBlockRoot == "" {
log.Error("parent beacon block root in payload attributes is empty")
return
}
// TODO: (deneb) HexToRoot util function
hash, err := utils.HexToHash(payloadAttributes.Data.PayloadAttributes.ParentBeaconBlockRoot)
if err != nil {
log.WithError(err).Error("error parsing parent beacon block root from payload attributes")
return
}
root := phase0.Root(hash)
parentBeaconRoot = &root
}

api.payloadAttributesLock.Lock()
defer api.payloadAttributesLock.Unlock()

Expand All @@ -706,6 +723,7 @@ func (api *RelayAPI) processPayloadAttributes(payloadAttributes beaconclient.Pay
slot: payloadAttrSlot,
parentHash: payloadAttributes.Data.ParentBlockHash,
withdrawalsRoot: withdrawalsRoot,
parentBeaconRoot: parentBeaconRoot,
payloadAttributes: payloadAttributes.Data.PayloadAttributes,
}

Expand Down Expand Up @@ -1314,7 +1332,7 @@ func (api *RelayAPI) handleGetPayload(w http.ResponseWriter, req *http.Request)
if slotDuty == nil {
log.Warn("could not find slot duty")
} else {
log = log.WithField("feeRecipient", slotDuty.Entry.Message.FeeRecipient)
log = log.WithField("feeRecipient", slotDuty.Entry.Message.FeeRecipient.String())
if slotDuty.ValidatorIndex != uint64(proposerIndex) {
log.WithField("expectedProposerIndex", slotDuty.ValidatorIndex).Warn("not the expected proposer index")
api.RespondError(w, http.StatusBadRequest, "not the expected proposer index")
Expand Down Expand Up @@ -1603,7 +1621,7 @@ func (api *RelayAPI) checkSubmissionFeeRecipient(w http.ResponseWriter, log *log
return slotDuty.Entry.Message.GasLimit, true
}

func (api *RelayAPI) checkSubmissionPayloadAttrs(w http.ResponseWriter, log *logrus.Entry, submission *common.BlockSubmissionInfo) bool {
func (api *RelayAPI) checkSubmissionPayloadAttrs(w http.ResponseWriter, log *logrus.Entry, submission *common.BlockSubmissionInfo) (payloadAttributesHelper, bool) {
api.payloadAttributesLock.RLock()
attrs, ok := api.payloadAttributes[submission.ParentHash.String()]
api.payloadAttributesLock.RUnlock()
Expand All @@ -1612,33 +1630,33 @@ func (api *RelayAPI) checkSubmissionPayloadAttrs(w http.ResponseWriter, log *log
log.Info("payload", submission.Slot, "attrs", attrs.slot)
log.Warn("payload attributes not (yet) known")
api.RespondError(w, http.StatusBadRequest, "payload attributes not (yet) known")
return false
return attrs, false
}

if submission.PrevRandao.String() != attrs.payloadAttributes.PrevRandao {
msg := fmt.Sprintf("incorrect prev_randao - got: %s, expected: %s", submission.PrevRandao.String(), attrs.payloadAttributes.PrevRandao)
log.Info(msg)
api.RespondError(w, http.StatusBadRequest, msg)
return false
return attrs, false
}

if hasReachedFork(submission.Slot, api.capellaEpoch) { // Capella requires correct withdrawals
withdrawalsRoot, err := ComputeWithdrawalsRoot(submission.Withdrawals)
if err != nil {
log.WithError(err).Warn("could not compute withdrawals root from payload")
api.RespondError(w, http.StatusBadRequest, "could not compute withdrawals root")
return false
return attrs, false
}

if withdrawalsRoot != attrs.withdrawalsRoot {
msg := fmt.Sprintf("incorrect withdrawals root - got: %s, expected: %s", withdrawalsRoot.String(), attrs.withdrawalsRoot.String())
log.Info(msg)
api.RespondError(w, http.StatusBadRequest, msg)
return false
return attrs, false
}
}

return true
return attrs, true
}

func (api *RelayAPI) checkSubmissionSlotDetails(w http.ResponseWriter, log *logrus.Entry, headSlot uint64, payload *common.VersionedSubmitBlockRequest, submission *common.BlockSubmissionInfo) bool {
Expand Down Expand Up @@ -1954,7 +1972,7 @@ func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Reque

log = log.WithField("timestampBeforeAttributesCheck", time.Now().UTC().UnixMilli())

ok = api.checkSubmissionPayloadAttrs(w, log, submission)
attrs, ok := api.checkSubmissionPayloadAttrs(w, log, submission)
if !ok {
return
}
Expand Down Expand Up @@ -2063,6 +2081,7 @@ func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Reque
req: &common.BuilderBlockValidationRequest{
VersionedSubmitBlockRequest: payload,
RegisteredGasLimit: gasLimit,
ParentBeaconBlockRoot: attrs.parentBeaconRoot,
},
}
// With sufficient collateral, process the block optimistically.
Expand Down
2 changes: 1 addition & 1 deletion services/api/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ func TestCheckSubmissionPayloadAttrs(t *testing.T) {
log := logrus.NewEntry(logger)
submission, err := common.GetBlockSubmissionInfo(tc.payload)
require.NoError(t, err)
ok := backend.relay.checkSubmissionPayloadAttrs(w, log, submission)
_, ok := backend.relay.checkSubmissionPayloadAttrs(w, log, submission)
require.Equal(t, tc.expectOk, ok)
})
}
Expand Down

0 comments on commit 0014259

Please sign in to comment.