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 fee recipient check in handleSubmitNewBlock #479

Merged
merged 2 commits into from
Aug 1, 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
37 changes: 22 additions & 15 deletions services/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,25 @@ func (api *RelayAPI) handleBuilderGetValidators(w http.ResponseWriter, req *http
}
}

func (api *RelayAPI) checkSubmissionFeeRecipient(w http.ResponseWriter, log *logrus.Entry, payload *common.BuilderSubmitBlockRequest) (uint64, bool) {
api.proposerDutiesLock.RLock()
slotDuty := api.proposerDutiesMap[payload.Slot()]
api.proposerDutiesLock.RUnlock()
if slotDuty == nil {
log.Warn("could not find slot duty")
api.RespondError(w, http.StatusBadRequest, "could not find slot duty")
return 0, false
} else if !strings.EqualFold(slotDuty.Entry.Message.FeeRecipient.String(), payload.ProposerFeeRecipient()) {
log.WithFields(logrus.Fields{
"expectedFeeRecipient": slotDuty.Entry.Message.FeeRecipient.String(),
"actualFeeRecipient": payload.ProposerFeeRecipient(),
}).Info("fee recipient does not match")
api.RespondError(w, http.StatusBadRequest, "fee recipient does not match")
return 0, false
}
return slotDuty.Entry.Message.GasLimit, true
}

func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Request) { //nolint:gocognit,maintidx
var pf common.Profile
var prevTime, nextTime time.Time
Expand Down Expand Up @@ -1649,20 +1668,8 @@ func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Reque

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

// ensure correct feeRecipient is used
api.proposerDutiesLock.RLock()
slotDuty := api.proposerDutiesMap[payload.Slot()]
api.proposerDutiesLock.RUnlock()
if slotDuty == nil {
log.Warn("could not find slot duty")
api.RespondError(w, http.StatusBadRequest, "could not find slot duty")
return
} else if !strings.EqualFold(slotDuty.Entry.Message.FeeRecipient.String(), payload.ProposerFeeRecipient()) {
log.WithFields(logrus.Fields{
"expectedFeeRecipient": slotDuty.Entry.Message.FeeRecipient.String(),
"actualFeeRecipient": payload.ProposerFeeRecipient(),
}).Info("fee recipient does not match")
api.RespondError(w, http.StatusBadRequest, "fee recipient does not match")
gasLimit, cont := api.checkSubmissionFeeRecipient(w, log, payload)
if !cont {
return
}

Expand Down Expand Up @@ -1843,7 +1850,7 @@ func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Reque
builder: builderEntry,
req: &common.BuilderBlockValidationRequest{
BuilderSubmitBlockRequest: *payload,
RegisteredGasLimit: slotDuty.Entry.Message.GasLimit,
RegisteredGasLimit: gasLimit,
},
}
// With sufficient collateral, process the block optimistically.
Expand Down
94 changes: 93 additions & 1 deletion services/api/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/alicebob/miniredis/v2"
builderCapella "github.com/attestantio/go-builder-client/api/capella"
v1 "github.com/attestantio/go-builder-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/bellatrix"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/flashbots/go-boost-utils/bls"
Expand All @@ -24,10 +25,20 @@ import (
"github.com/flashbots/mev-boost-relay/database"
"github.com/flashbots/mev-boost-relay/datastore"
"github.com/holiman/uint256"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)

var builderSigningDomain = types.Domain([32]byte{0, 0, 0, 1, 245, 165, 253, 66, 209, 106, 32, 48, 39, 152, 239, 110, 211, 9, 151, 155, 67, 0, 61, 35, 32, 217, 240, 232, 234, 152, 49, 169})
const (
testGasLimit = uint64(30000000)
testSlot = uint64(42)
)

var (
builderSigningDomain = types.Domain([32]byte{0, 0, 0, 1, 245, 165, 253, 66, 209, 106, 32, 48, 39, 152, 239, 110, 211, 9, 151, 155, 67, 0, 61, 35, 32, 217, 240, 232, 234, 152, 49, 169})
testAddress = types.Address([20]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19})
testAddress2 = types.Address([20]byte{1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19})
)

type testBackend struct {
t require.TestingT
Expand Down Expand Up @@ -488,6 +499,87 @@ func TestBuilderSubmitBlock(t *testing.T) {
require.Equal(t, http.StatusBadRequest, rr.Code)
}

func TestCheckSubmissionFeeRecipient(t *testing.T) {
cases := []struct {
description string
slotDuty *common.BuilderGetValidatorsResponseEntry
payload *common.BuilderSubmitBlockRequest
expectCont bool
expectGasLimit uint64
}{
{
description: "success",
slotDuty: &common.BuilderGetValidatorsResponseEntry{
Entry: &types.SignedValidatorRegistration{
Message: &types.RegisterValidatorRequestMessage{
FeeRecipient: testAddress,
GasLimit: testGasLimit,
},
},
},
payload: &common.BuilderSubmitBlockRequest{
Capella: &builderCapella.SubmitBlockRequest{
Message: &v1.BidTrace{
Slot: testSlot,
ProposerFeeRecipient: bellatrix.ExecutionAddress(testAddress),
},
},
},
expectCont: true,
expectGasLimit: testGasLimit,
},
{
description: "failure_nil_slot_duty",
slotDuty: nil,
payload: &common.BuilderSubmitBlockRequest{
Capella: &builderCapella.SubmitBlockRequest{
Message: &v1.BidTrace{
Slot: testSlot,
},
},
},
expectCont: false,
expectGasLimit: 0,
},
{
description: "failure_diff_fee_recipient",
slotDuty: &common.BuilderGetValidatorsResponseEntry{
Entry: &types.SignedValidatorRegistration{
Message: &types.RegisterValidatorRequestMessage{
FeeRecipient: testAddress,
GasLimit: testGasLimit,
},
},
},
payload: &common.BuilderSubmitBlockRequest{
Capella: &builderCapella.SubmitBlockRequest{
Message: &v1.BidTrace{
Slot: testSlot,
ProposerFeeRecipient: bellatrix.ExecutionAddress(testAddress2),
},
},
},
expectCont: false,
expectGasLimit: 0,
},
}
for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
_, _, backend := startTestBackend(t)
backend.relay.proposerDutiesLock.RLock()
backend.relay.proposerDutiesMap[tc.payload.Slot()] = tc.slotDuty
backend.relay.proposerDutiesLock.RUnlock()

w := httptest.NewRecorder()
logger := logrus.New()
log := logrus.NewEntry(logger)
gasLimit, cont := backend.relay.checkSubmissionFeeRecipient(w, log, tc.payload)
require.Equal(t, tc.expectGasLimit, gasLimit)
require.Equal(t, tc.expectCont, cont)
})
}
}

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