Skip to content

Commit 36ed373

Browse files
[code health] refactor redis update to independent function (#514)
* refactor redis update * remove stale comment * rename
1 parent 36d55f7 commit 36ed373

File tree

2 files changed

+128
-27
lines changed

2 files changed

+128
-27
lines changed

services/api/service.go

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,50 @@ func (api *RelayAPI) checkFloorBidValue(opts bidFloorOpts) (*big.Int, *logrus.En
16491649
return floorBidValue, opts.log, true
16501650
}
16511651

1652+
type redisUpdateBidOpts struct {
1653+
w http.ResponseWriter
1654+
tx redis.Pipeliner
1655+
log *logrus.Entry
1656+
cancellationsEnabled bool
1657+
receivedAt time.Time
1658+
floorBidValue *big.Int
1659+
payload *common.BuilderSubmitBlockRequest
1660+
}
1661+
1662+
func (api *RelayAPI) updateRedisBid(opts redisUpdateBidOpts) (*datastore.SaveBidAndUpdateTopBidResponse, *common.GetPayloadResponse, bool) {
1663+
// Prepare the response data
1664+
getHeaderResponse, err := common.BuildGetHeaderResponse(opts.payload, api.blsSk, api.publicKey, api.opts.EthNetDetails.DomainBuilder)
1665+
if err != nil {
1666+
opts.log.WithError(err).Error("could not sign builder bid")
1667+
api.RespondError(opts.w, http.StatusBadRequest, err.Error())
1668+
return nil, nil, false
1669+
}
1670+
1671+
getPayloadResponse, err := common.BuildGetPayloadResponse(opts.payload)
1672+
if err != nil {
1673+
opts.log.WithError(err).Error("could not build getPayload response")
1674+
api.RespondError(opts.w, http.StatusBadRequest, err.Error())
1675+
return nil, nil, false
1676+
}
1677+
1678+
bidTrace := common.BidTraceV2{
1679+
BidTrace: *opts.payload.Message(),
1680+
BlockNumber: opts.payload.BlockNumber(),
1681+
NumTx: uint64(opts.payload.NumTx()),
1682+
}
1683+
1684+
//
1685+
// Save to Redis
1686+
//
1687+
updateBidResult, err := api.redis.SaveBidAndUpdateTopBid(context.Background(), opts.tx, &bidTrace, opts.payload, getPayloadResponse, getHeaderResponse, opts.receivedAt, opts.cancellationsEnabled, opts.floorBidValue)
1688+
if err != nil {
1689+
opts.log.WithError(err).Error("could not save bid and update top bids")
1690+
api.RespondError(opts.w, http.StatusInternalServerError, "failed saving and updating bid")
1691+
return nil, nil, false
1692+
}
1693+
return &updateBidResult, getPayloadResponse, true
1694+
}
1695+
16521696
func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Request) {
16531697
var pf common.Profile
16541698
var prevTime, nextTime time.Time
@@ -1953,34 +1997,17 @@ func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Reque
19531997
}
19541998
}
19551999

1956-
// Prepare the response data
1957-
getHeaderResponse, err := common.BuildGetHeaderResponse(payload, api.blsSk, api.publicKey, api.opts.EthNetDetails.DomainBuilder)
1958-
if err != nil {
1959-
log.WithError(err).Error("could not sign builder bid")
1960-
api.RespondError(w, http.StatusBadRequest, err.Error())
1961-
return
1962-
}
1963-
1964-
getPayloadResponse, err := common.BuildGetPayloadResponse(payload)
1965-
if err != nil {
1966-
log.WithError(err).Error("could not build getPayload response")
1967-
api.RespondError(w, http.StatusBadRequest, err.Error())
1968-
return
1969-
}
1970-
1971-
bidTrace := common.BidTraceV2{
1972-
BidTrace: *payload.Message(),
1973-
BlockNumber: payload.BlockNumber(),
1974-
NumTx: uint64(payload.NumTx()),
2000+
redisOpts := redisUpdateBidOpts{
2001+
w: w,
2002+
tx: tx,
2003+
log: log,
2004+
cancellationsEnabled: isCancellationEnabled,
2005+
receivedAt: receivedAt,
2006+
floorBidValue: floorBidValue,
2007+
payload: payload,
19752008
}
1976-
1977-
//
1978-
// Save to Redis
1979-
//
1980-
updateBidResult, err := api.redis.SaveBidAndUpdateTopBid(context.Background(), tx, &bidTrace, payload, getPayloadResponse, getHeaderResponse, receivedAt, isCancellationEnabled, floorBidValue)
1981-
if err != nil {
1982-
log.WithError(err).Error("could not save bid and update top bids")
1983-
api.RespondError(w, http.StatusInternalServerError, "failed saving and updating bid")
2009+
updateBidResult, getPayloadResponse, ok := api.updateRedisBid(redisOpts)
2010+
if !ok {
19842011
return
19852012
}
19862013

services/api/service_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,80 @@ func TestCheckFloorBidValue(t *testing.T) {
947947
}
948948
}
949949

950+
func TestUpdateRedis(t *testing.T) {
951+
cases := []struct {
952+
description string
953+
cancellationsEnabled bool
954+
floorValue string
955+
payload *common.BuilderSubmitBlockRequest
956+
expectOk bool
957+
}{
958+
{
959+
description: "success",
960+
floorValue: "10",
961+
payload: &common.BuilderSubmitBlockRequest{
962+
Capella: &builderCapella.SubmitBlockRequest{
963+
Message: &v1.BidTrace{
964+
Slot: testSlot,
965+
Value: uint256.NewInt(1),
966+
},
967+
ExecutionPayload: &capella.ExecutionPayload{},
968+
},
969+
},
970+
expectOk: true,
971+
},
972+
{
973+
description: "failure_no_payload",
974+
floorValue: "10",
975+
payload: nil,
976+
expectOk: false,
977+
},
978+
{
979+
description: "failure_encode_failure_too_long_extra_data",
980+
floorValue: "10",
981+
payload: &common.BuilderSubmitBlockRequest{
982+
Capella: &builderCapella.SubmitBlockRequest{
983+
Message: &v1.BidTrace{
984+
Slot: testSlot,
985+
Value: uint256.NewInt(1),
986+
},
987+
ExecutionPayload: &capella.ExecutionPayload{
988+
ExtraData: make([]byte, 33), // Max extra data length is 32.
989+
},
990+
},
991+
},
992+
expectOk: false,
993+
},
994+
}
995+
for _, tc := range cases {
996+
t.Run(tc.description, func(t *testing.T) {
997+
_, _, backend := startTestBackend(t)
998+
w := httptest.NewRecorder()
999+
logger := logrus.New()
1000+
log := logrus.NewEntry(logger)
1001+
tx := backend.redis.NewTxPipeline()
1002+
1003+
floorValue := new(big.Int)
1004+
floorValue, ok := floorValue.SetString(tc.floorValue, 10)
1005+
require.True(t, ok)
1006+
rOpts := redisUpdateBidOpts{
1007+
w: w,
1008+
tx: tx,
1009+
log: log,
1010+
cancellationsEnabled: tc.cancellationsEnabled,
1011+
floorBidValue: floorValue,
1012+
payload: tc.payload,
1013+
}
1014+
updateResp, getPayloadResp, ok := backend.relay.updateRedisBid(rOpts)
1015+
require.Equal(t, tc.expectOk, ok)
1016+
if ok {
1017+
require.NotNil(t, updateResp)
1018+
require.NotNil(t, getPayloadResp)
1019+
}
1020+
})
1021+
}
1022+
}
1023+
9501024
func gzipBytes(t *testing.T, b []byte) []byte {
9511025
t.Helper()
9521026
var buf bytes.Buffer

0 commit comments

Comments
 (0)