diff --git a/datastore/auction_api.go b/datastore/auction_api.go new file mode 100644 index 000000000..446a27102 --- /dev/null +++ b/datastore/auction_api.go @@ -0,0 +1,76 @@ +package datastore + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + + builderApi "github.com/attestantio/go-builder-client/api" + "github.com/flashbots/mev-boost-relay/common" +) + +const API_ROOT = "http://turbo-auction-api" + +func GetPayloadContents(slot uint64, proposerPubkey, blockHash string) (*builderApi.VersionedSubmitBlindedBlockResponse, error) { + queryParams := url.Values{} + queryParams.Add("slot", fmt.Sprintf("%d", slot)) + queryParams.Add("proposer_pubkey", proposerPubkey) + queryParams.Add("block_hash", blockHash) + + fullUrl := fmt.Sprintf("%s/internal/payload_contents?%s", API_ROOT, queryParams.Encode()) + + var err error + + resp, err := http.Get(fullUrl) + if err != nil { + return nil, err + } + + if resp.StatusCode == 404 { + return nil, ErrExecutionPayloadNotFound + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + payload := new(builderApi.VersionedSubmitBlindedBlockResponse) + err = json.Unmarshal(body, &payload) + if err != nil { + return nil, err + } + + return payload, nil +} + +func GetBidTrace(slot uint64, proposerPubkey, blockHash string) (*common.BidTraceV2, error) { + queryParams := url.Values{} + queryParams.Add("slot", fmt.Sprintf("%d", slot)) + queryParams.Add("proposer_pubkey", proposerPubkey) + queryParams.Add("block_hash", blockHash) + + fullUrl := fmt.Sprintf("%s/internal/bid_trace?%s", API_ROOT, queryParams.Encode()) + + var err error + + resp, err := http.Get(fullUrl) + if err != nil { + return nil, err + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + bidtrace := new(common.BidTraceV2) + err = json.Unmarshal(body, &bidtrace) + if err != nil { + return nil, err + } + + return bidtrace, nil +} diff --git a/services/api/service.go b/services/api/service.go index 155e3f1d2..959716662 100644 --- a/services/api/service.go +++ b/services/api/service.go @@ -1393,7 +1393,7 @@ func (api *RelayAPI) handleGetPayload(w http.ResponseWriter, req *http.Request) // Save information about delivered payload defer func() { - bidTrace, err := api.redis.GetBidTrace(uint64(slot), proposerPubkey.String(), blockHash.String()) + bidTrace, err := datastore.GetBidTrace(uint64(slot), proposerPubkey.String(), blockHash.String()) if err != nil { log.WithError(err).Info("failed to get bidTrace for delivered payload from redis") return @@ -1538,13 +1538,13 @@ func (api *RelayAPI) handleGetPayload(w http.ResponseWriter, req *http.Request) // Get the response - from Redis, Memcache or DB // note that recent mev-boost versions only send getPayload to relays that provided the bid, older versions send getPayload to all relays. // Additionally, proposers may feel it's safer to ask for a bid from all relays and fork. - getPayloadResp, err = api.datastore.GetGetPayloadResponse(log, uint64(slot), proposerPubkey.String(), blockHash.String()) + getPayloadResp, err = datastore.GetPayloadContents(uint64(slot), proposerPubkey.String(), blockHash.String()) if err != nil || getPayloadResp == nil { log.WithError(err).Warn("failed first attempt to get execution payload") // Wait, then try again. time.Sleep(time.Duration(timeoutGetPayloadRetryMs) * time.Millisecond) - getPayloadResp, err = api.datastore.GetGetPayloadResponse(log, uint64(slot), proposerPubkey.String(), blockHash.String()) + getPayloadResp, err = datastore.GetPayloadContents(uint64(slot), proposerPubkey.String(), blockHash.String()) if err != nil || getPayloadResp == nil { // Still not found! Error out now.