Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[code health] refactor slot details checks in handleSubmitNewBlock #494

Merged
merged 4 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions services/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,32 @@ func (api *RelayAPI) checkSubmissionPayloadAttrs(w http.ResponseWriter, log *log
return true
}

func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Request) { //nolint:gocognit,maintidx
func (api *RelayAPI) checkSubmissionSlotDetails(w http.ResponseWriter, log *logrus.Entry, headSlot uint64, payload *common.BuilderSubmitBlockRequest) bool {
// TODO: add deneb support.
if payload.Capella == nil {
log.Info("rejecting submission - non-capella payload for capella fork")
api.RespondError(w, http.StatusBadRequest, "non-capella payload")
return false
}

if payload.Slot() <= headSlot {
log.Info("submitNewBlock failed: submission for past slot")
api.RespondError(w, http.StatusBadRequest, "submission for past slot")
return false
}

// Timestamp check
expectedTimestamp := api.genesisInfo.Data.GenesisTime + (payload.Slot() * common.SecondsPerSlot)
if payload.Timestamp() != expectedTimestamp {
log.Warnf("incorrect timestamp. got %d, expected %d", payload.Timestamp(), expectedTimestamp)
api.RespondError(w, http.StatusBadRequest, fmt.Sprintf("incorrect timestamp. got %d, expected %d", payload.Timestamp(), expectedTimestamp))
return false
}

return true
}

func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Request) {
var pf common.Profile
var prevTime, nextTime time.Time

Expand Down Expand Up @@ -1646,16 +1671,8 @@ func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Reque
return
}

// TODO: add deneb support.
if payload.Capella == nil {
log.Info("rejecting submission - non capella payload for capella fork")
api.RespondError(w, http.StatusBadRequest, "not capella payload")
return
}

if payload.Slot() <= headSlot {
log.Info("submitNewBlock failed: submission for past slot")
api.RespondError(w, http.StatusBadRequest, "submission for past slot")
cont := api.checkSubmissionSlotDetails(w, log, headSlot, payload)
if !cont {
return
}

Expand All @@ -1674,14 +1691,6 @@ func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Reque
}
log = log.WithField("builderIsHighPrio", builderEntry.status.IsHighPrio)

// Timestamp check
expectedTimestamp := api.genesisInfo.Data.GenesisTime + (payload.Slot() * common.SecondsPerSlot)
if payload.Timestamp() != expectedTimestamp {
log.Warnf("incorrect timestamp. got %d, expected %d", payload.Timestamp(), expectedTimestamp)
api.RespondError(w, http.StatusBadRequest, fmt.Sprintf("incorrect timestamp. got %d, expected %d", payload.Timestamp(), expectedTimestamp))
return
}

if builderEntry.status.IsBlacklisted {
log.Info("builder is blacklisted")
time.Sleep(200 * time.Millisecond)
Expand Down
67 changes: 67 additions & 0 deletions services/api/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,73 @@ func TestCheckSubmissionPayloadAttrs(t *testing.T) {
}
}

func TestCheckSubmissionSlotDetails(t *testing.T) {
cases := []struct {
description string
payload *common.BuilderSubmitBlockRequest
expectCont bool
}{
{
description: "success",
payload: &common.BuilderSubmitBlockRequest{
Capella: &builderCapella.SubmitBlockRequest{
ExecutionPayload: &capella.ExecutionPayload{
Timestamp: testSlot * common.SecondsPerSlot,
},
Message: &v1.BidTrace{
Slot: testSlot,
},
},
},
expectCont: true,
},
{
description: "failure_nil_capella",
payload: &common.BuilderSubmitBlockRequest{
Capella: nil, // nil to cause error
},
expectCont: false,
},
{
description: "failure_past_slot",
payload: &common.BuilderSubmitBlockRequest{
Capella: &builderCapella.SubmitBlockRequest{
Message: &v1.BidTrace{
Slot: testSlot - 1, // use old slot to cause error
},
},
},
expectCont: false,
},
{
description: "failure_wrong_timestamp",
payload: &common.BuilderSubmitBlockRequest{
Capella: &builderCapella.SubmitBlockRequest{
ExecutionPayload: &capella.ExecutionPayload{
Timestamp: testSlot*common.SecondsPerSlot - 1, // use wrong timestamp to cause error
},
Message: &v1.BidTrace{
Slot: testSlot,
},
},
},
expectCont: false,
},
}
for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
_, _, backend := startTestBackend(t)

headSlot := testSlot - 1
w := httptest.NewRecorder()
logger := logrus.New()
log := logrus.NewEntry(logger)
cont := backend.relay.checkSubmissionSlotDetails(w, log, headSlot, tc.payload)
require.Equal(t, tc.expectCont, cont)
})
}
}

func gzipBytes(t *testing.T, b []byte) []byte {
t.Helper()
var buf bytes.Buffer
Expand Down
Loading