Skip to content

Commit 256fd3c

Browse files
authored
Use dedicated http client for publishing (#608)
* Use dedicated http client for publishing * Add publish beacon URI
1 parent f1781db commit 256fd3c

File tree

10 files changed

+62
-43
lines changed

10 files changed

+62
-43
lines changed

beaconclient/beacon_client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func newTestBackend(t require.TestingT, numBeaconNodes int) *testBackend {
4848
func TestBeaconInstance(t *testing.T) {
4949
r := mux.NewRouter()
5050
srv := httptest.NewServer(r)
51-
bc := NewProdBeaconInstance(common.TestLog, srv.URL)
51+
bc := NewProdBeaconInstance(common.TestLog, srv.URL, srv.URL)
5252

5353
r.HandleFunc("/eth/v1/beacon/states/1/validators", func(w http.ResponseWriter, _ *http.Request) {
5454
resp := []byte(`{
@@ -206,7 +206,7 @@ func TestFetchValidators(t *testing.T) {
206206
func TestGetForkSchedule(t *testing.T) {
207207
r := mux.NewRouter()
208208
srv := httptest.NewServer(r)
209-
bc := NewProdBeaconInstance(common.TestLog, srv.URL)
209+
bc := NewProdBeaconInstance(common.TestLog, srv.URL, srv.URL)
210210

211211
r.HandleFunc("/eth/v1/config/fork_schedule", func(w http.ResponseWriter, _ *http.Request) {
212212
resp := []byte(`{

beaconclient/mock_beacon_instance.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ func (c *MockBeaconInstance) GetURI() string {
100100
return ""
101101
}
102102

103+
func (c *MockBeaconInstance) GetPublishURI() string {
104+
return ""
105+
}
106+
103107
func (c *MockBeaconInstance) addDelay() {
104108
if c.ResponseDelay > 0 {
105109
time.Sleep(c.ResponseDelay)

beaconclient/multi_beacon_client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type IBeaconInstance interface {
5555
GetStateValidators(stateID string) (*GetStateValidatorsResponse, error)
5656
GetProposerDuties(epoch uint64) (*ProposerDutiesResponse, error)
5757
GetURI() string
58+
GetPublishURI() string
5859
PublishBlock(block *common.VersionedSignedProposal, broadcastMode BroadcastMode) (code int, err error)
5960
GetGenesis() (*GetGenesisResponse, error)
6061
GetSpec() (spec *GetSpecResponse, err error)
@@ -271,7 +272,7 @@ func (c *MultiBeaconClient) PublishBlock(block *common.VersionedSignedProposal)
271272
resChans := make(chan publishResp, len(clients))
272273

273274
for i, client := range clients {
274-
log := log.WithField("uri", client.GetURI())
275+
log := log.WithField("uri", client.GetPublishURI())
275276
log.Debug("publishing block")
276277
go func(index int, client IBeaconInstance) {
277278
code, err := client.PublishBlock(block, c.broadcastMode)
@@ -286,7 +287,7 @@ func (c *MultiBeaconClient) PublishBlock(block *common.VersionedSignedProposal)
286287
var lastErrPublishResp publishResp
287288
for i := 0; i < len(clients); i++ {
288289
res := <-resChans
289-
log = log.WithField("beacon", clients[res.index].GetURI())
290+
log = log.WithField("beacon", clients[res.index].GetPublishURI())
290291
if res.err != nil {
291292
log.WithField("statusCode", res.code).WithError(res.err).Warn("failed to publish block")
292293
lastErrPublishResp = res

beaconclient/prod_beacon_instance.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,26 @@ import (
1515
)
1616

1717
type ProdBeaconInstance struct {
18-
log *logrus.Entry
19-
beaconURI string
18+
log *logrus.Entry
19+
beaconURI string
20+
beaconPublishURI string
2021

2122
// feature flags
2223
ffUseV1PublishBlockEndpoint bool
2324
ffUseSSZEncodingPublishBlock bool
25+
26+
// http clients
27+
publishingClient *http.Client
2428
}
2529

26-
func NewProdBeaconInstance(log *logrus.Entry, beaconURI string) *ProdBeaconInstance {
30+
func NewProdBeaconInstance(log *logrus.Entry, beaconURI, beaconPublishURI string) *ProdBeaconInstance {
2731
_log := log.WithFields(logrus.Fields{
28-
"component": "beaconInstance",
29-
"beaconURI": beaconURI,
32+
"component": "beaconInstance",
33+
"beaconURI": beaconURI,
34+
"beaconPublishURI": beaconPublishURI,
3035
})
3136

32-
client := &ProdBeaconInstance{_log, beaconURI, false, false}
37+
client := &ProdBeaconInstance{_log, beaconURI, beaconPublishURI, false, false, &http.Client{}}
3338

3439
// feature flags
3540
if os.Getenv("USE_V1_PUBLISH_BLOCK_ENDPOINT") != "" {
@@ -179,7 +184,7 @@ func (c *ProdBeaconInstance) SyncStatus() (*SyncStatusPayloadData, error) {
179184
uri := c.beaconURI + "/eth/v1/node/syncing"
180185
timeout := 5 * time.Second
181186
resp := new(SyncStatusPayload)
182-
_, err := fetchBeacon(http.MethodGet, uri, nil, resp, &timeout, http.Header{}, false)
187+
_, err := fetchBeacon(http.MethodGet, uri, nil, resp, &http.Client{Timeout: timeout}, http.Header{}, false)
183188
if err != nil {
184189
return nil, err
185190
}
@@ -248,12 +253,16 @@ func (c *ProdBeaconInstance) GetURI() string {
248253
return c.beaconURI
249254
}
250255

256+
func (c *ProdBeaconInstance) GetPublishURI() string {
257+
return c.beaconPublishURI
258+
}
259+
251260
func (c *ProdBeaconInstance) PublishBlock(block *common.VersionedSignedProposal, broadcastMode BroadcastMode) (code int, err error) {
252261
var uri string
253262
if c.ffUseV1PublishBlockEndpoint {
254-
uri = fmt.Sprintf("%s/eth/v1/beacon/blocks", c.beaconURI)
263+
uri = fmt.Sprintf("%s/eth/v1/beacon/blocks", c.beaconPublishURI)
255264
} else {
256-
uri = fmt.Sprintf("%s/eth/v2/beacon/blocks?broadcast_validation=%s", c.beaconURI, broadcastMode)
265+
uri = fmt.Sprintf("%s/eth/v2/beacon/blocks?broadcast_validation=%s", c.beaconPublishURI, broadcastMode)
257266
}
258267
headers := http.Header{}
259268
headers.Add("Eth-Consensus-Version", strings.ToLower(block.Version.String())) // optional in v1, required in v2
@@ -279,7 +288,7 @@ func (c *ProdBeaconInstance) PublishBlock(block *common.VersionedSignedProposal,
279288
}
280289
publishingStartTime := time.Now().UTC()
281290
encodeDurationMs := publishingStartTime.Sub(encodeStartTime).Milliseconds()
282-
code, err = fetchBeacon(http.MethodPost, uri, payloadBytes, nil, nil, headers, useSSZ)
291+
code, err = fetchBeacon(http.MethodPost, uri, payloadBytes, nil, c.publishingClient, headers, useSSZ)
283292
publishDurationMs := time.Now().UTC().Sub(publishingStartTime).Milliseconds()
284293
log.WithFields(logrus.Fields{
285294
"slot": slot,

beaconclient/util.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io"
99
"net/http"
1010
"strings"
11-
"time"
1211
)
1312

1413
var (
@@ -31,7 +30,7 @@ func parseBroadcastModeString(s string) (BroadcastMode, bool) {
3130
return b, ok
3231
}
3332

34-
func fetchBeacon(method, url string, payload []byte, dst any, timeout *time.Duration, headers http.Header, ssz bool) (code int, err error) {
33+
func fetchBeacon(method, url string, payload []byte, dst any, httpClient *http.Client, headers http.Header, ssz bool) (code int, err error) {
3534
var req *http.Request
3635

3736
if payload == nil {
@@ -55,10 +54,11 @@ func fetchBeacon(method, url string, payload []byte, dst any, timeout *time.Dura
5554
}
5655
req.Header.Set("accept", "application/json")
5756

58-
client := &http.Client{}
59-
if timeout != nil && timeout.Milliseconds() > 0 {
60-
client.Timeout = *timeout
57+
client := http.DefaultClient
58+
if httpClient != nil {
59+
client = httpClient
6160
}
61+
6262
resp, err := client.Do(req)
6363
if err != nil {
6464
return 0, fmt.Errorf("client refused for %s: %w", url, err)

cmd/api.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func init() {
5353

5454
apiCmd.Flags().StringVar(&apiListenAddr, "listen-addr", apiDefaultListenAddr, "listen address for webserver")
5555
apiCmd.Flags().StringSliceVar(&beaconNodeURIs, "beacon-uris", defaultBeaconURIs, "beacon endpoints")
56+
apiCmd.Flags().StringSliceVar(&beaconNodePublishURIs, "beacon-publish-uris", defaultBeaconPublishURIs, "beacon publish endpoints")
5657
apiCmd.Flags().StringVar(&redisURI, "redis-uri", defaultRedisURI, "redis uri")
5758
apiCmd.Flags().StringVar(&redisReadonlyURI, "redis-readonly-uri", defaultRedisReadonlyURI, "redis readonly uri")
5859
apiCmd.Flags().StringVar(&postgresDSN, "db", defaultPostgresDSN, "PostgreSQL DSN")
@@ -100,9 +101,18 @@ var apiCmd = &cobra.Command{
100101
log.Fatalf("no beacon endpoints specified")
101102
}
102103
log.Infof("Using beacon endpoints: %s", strings.Join(beaconNodeURIs, ", "))
104+
if len(beaconNodePublishURIs) == 0 {
105+
// default to same endpoint as the beacon endpoints
106+
beaconNodePublishURIs = beaconNodeURIs
107+
} else if len(beaconNodePublishURIs) != len(beaconNodeURIs) {
108+
log.Fatalf("beacon publish endpoints do not match the number of beacon endpoints")
109+
} else {
110+
log.Infof("Using beacon publish endpoints: %s", strings.Join(beaconNodePublishURIs, ", "))
111+
}
112+
103113
var beaconInstances []beaconclient.IBeaconInstance
104-
for _, uri := range beaconNodeURIs {
105-
beaconInstances = append(beaconInstances, beaconclient.NewProdBeaconInstance(log, uri))
114+
for i, uri := range beaconNodeURIs {
115+
beaconInstances = append(beaconInstances, beaconclient.NewProdBeaconInstance(log, uri, beaconNodePublishURIs[i]))
106116
}
107117
beaconClient := beaconclient.NewMultiBeaconClient(log, beaconInstances)
108118

cmd/housekeeper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var housekeeperCmd = &cobra.Command{
6363
log.Infof("Using beacon endpoints: %s", strings.Join(beaconNodeURIs, ", "))
6464
var beaconInstances []beaconclient.IBeaconInstance
6565
for _, uri := range beaconNodeURIs {
66-
beaconInstances = append(beaconInstances, beaconclient.NewProdBeaconInstance(log, uri))
66+
beaconInstances = append(beaconInstances, beaconclient.NewProdBeaconInstance(log, uri, uri))
6767
}
6868
beaconClient := beaconclient.NewMultiBeaconClient(log, beaconInstances)
6969

cmd/variables.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ import (
77
)
88

99
var (
10-
defaultNetwork = common.GetEnv("NETWORK", "")
11-
defaultBeaconURIs = common.GetSliceEnv("BEACON_URIS", []string{"http://localhost:3500"})
12-
defaultRedisURI = common.GetEnv("REDIS_URI", "localhost:6379")
13-
defaultRedisReadonlyURI = common.GetEnv("REDIS_READONLY_URI", "")
14-
defaultPostgresDSN = common.GetEnv("POSTGRES_DSN", "")
15-
defaultMemcachedURIs = common.GetSliceEnv("MEMCACHED_URIS", nil)
16-
defaultLogJSON = os.Getenv("LOG_JSON") != ""
17-
defaultLogLevel = common.GetEnv("LOG_LEVEL", "info")
10+
defaultNetwork = common.GetEnv("NETWORK", "")
11+
defaultBeaconURIs = common.GetSliceEnv("BEACON_URIS", []string{"http://localhost:3500"})
12+
defaultBeaconPublishURIs = common.GetSliceEnv("BEACON_PUBLISH_URIS", []string{})
13+
defaultRedisURI = common.GetEnv("REDIS_URI", "localhost:6379")
14+
defaultRedisReadonlyURI = common.GetEnv("REDIS_READONLY_URI", "")
15+
defaultPostgresDSN = common.GetEnv("POSTGRES_DSN", "")
16+
defaultMemcachedURIs = common.GetSliceEnv("MEMCACHED_URIS", nil)
17+
defaultLogJSON = os.Getenv("LOG_JSON") != ""
18+
defaultLogLevel = common.GetEnv("LOG_LEVEL", "info")
1819

19-
beaconNodeURIs []string
20-
redisURI string
21-
redisReadonlyURI string
22-
postgresDSN string
23-
memcachedURIs []string
20+
beaconNodeURIs []string
21+
beaconNodePublishURIs []string
22+
redisURI string
23+
redisReadonlyURI string
24+
postgresDSN string
25+
memcachedURIs []string
2426

2527
logJSON bool
2628
logLevel string

scripts/sse-event-logger/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func main() {
2222

2323
log.Infof("Using beacon endpoints: %s", strings.Join(beaconURIs, ", "))
2424
for _, uri := range beaconURIs {
25-
beaconInstance := beaconclient.NewProdBeaconInstance(log, uri)
25+
beaconInstance := beaconclient.NewProdBeaconInstance(log, uri, uri)
2626
go subscribeHead(beaconInstance)
2727
go subscribePayloadAttr(beaconInstance)
2828
}

services/api/service.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,13 +2007,6 @@ func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Reque
20072007
simResultC := make(chan *blockSimResult, 1)
20082008
var eligibleAt time.Time // will be set once the bid is ready
20092009

2010-
submission, err = common.GetBlockSubmissionInfo(payload)
2011-
if err != nil {
2012-
log.WithError(err).Warn("missing fields in submit block request")
2013-
api.RespondError(w, http.StatusBadRequest, err.Error())
2014-
return
2015-
}
2016-
20172010
bfOpts := bidFloorOpts{
20182011
w: w,
20192012
tx: tx,

0 commit comments

Comments
 (0)