Skip to content

Commit

Permalink
feat: get payload and bidtrace from auction api
Browse files Browse the repository at this point in the history
  • Loading branch information
blombern authored and alextes committed Feb 9, 2024
1 parent b02e411 commit 1d9bb58
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
76 changes: 76 additions & 0 deletions datastore/auction_api.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 3 additions & 3 deletions services/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1d9bb58

Please sign in to comment.