Skip to content

Commit

Permalink
remove builder name, make registration cancellable
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Oct 31, 2024
1 parent fc08d73 commit 148e247
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 24 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--local-listen-address value address to listen on for orderflow proxy API for external users and local operator (default: "127.0.0.1:443")
--local-listen-addr value address to listen on for orderflow proxy API for external users and local operator (default: "127.0.0.1:443")
--public-listen-addr value address to listen on for orderflow proxy API for other network participants (default: "127.0.0.1:5544")
--cert-listen-addr value address to listen on for orderflow proxy serving its SSL certificate on /cert (default: "127.0.0.1:14727")
--builder-endpoint value address to send local ordeflow to (default: "http://127.0.0.1:8645")
--rpc-endpoint value address of the node RPC that supports eth_blockNumber (default: "http://127.0.0.1:8545")
--builder-confighub-endpoint value address of the builder config hub enpoint (directly or throught the cvm-proxy) (default: "http://127.0.0.1:14892")
--orderflow-archive-endpoint value address of the ordreflow archive endpoint (block-processor) (default: "http://127.0.0.1:14893")
--builder-name value name of this builder (same as in confighub) (default: "test-builder")
--flashbots-orderflow-signer-address value ordreflow from Flashbots will be signed with this address (default: "0x5015Fa72E34f75A9eC64f44a4Fcf0837919D1bB7")
--max-request-body-size-bytes value Maximum size of the request body, if 0 default will be used (default: 0)
--cert-duration value generated certificate duration (default: 8760h0m0s)
Expand Down
25 changes: 15 additions & 10 deletions cmd/receiver-proxy/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"log"
"net/http"
"net/http/pprof"
Expand All @@ -20,7 +21,7 @@ import (
var flags []cli.Flag = []cli.Flag{
// input and output
&cli.StringFlag{
Name: "local-listen-address",
Name: "local-listen-addr",
Value: "127.0.0.1:443",
Usage: "address to listen on for orderflow proxy API for external users and local operator",
},
Expand Down Expand Up @@ -54,11 +55,6 @@ var flags []cli.Flag = []cli.Flag{
Value: "http://127.0.0.1:14893",
Usage: "address of the ordreflow archive endpoint (block-processor)",
},
&cli.StringFlag{
Name: "builder-name",
Value: "test-builder",
Usage: "name of this builder (same as in confighub)",
},
&cli.StringFlag{
Name: "flashbots-orderflow-signer-address",
Value: "0x5015Fa72E34f75A9eC64f44a4Fcf0837919D1bB7",
Expand Down Expand Up @@ -175,13 +171,12 @@ func main() {
certHosts := cCtx.StringSlice("cert-hosts")
builderConfigHubEndpoint := cCtx.String("builder-confighub-endpoint")
archiveEndpoint := cCtx.String("orderflow-archive-endpoint")
name := cCtx.String("builder-name")
flashbotsSignerStr := cCtx.String("flashbots-orderflow-signer-address")
flashbotsSignerAddress := eth.HexToAddress(flashbotsSignerStr)
maxRequestBodySizeBytes := cCtx.Int64("max-request-body-size-bytes")

proxyConfig := &proxy.ReceiverProxyConfig{
ReceiverProxyConstantConfig: proxy.ReceiverProxyConstantConfig{Log: log, Name: name, FlashbotsSignerAddress: flashbotsSignerAddress},
ReceiverProxyConstantConfig: proxy.ReceiverProxyConstantConfig{Log: log, FlashbotsSignerAddress: flashbotsSignerAddress},
CertValidDuration: certDuration,
CertHosts: certHosts,
BuilderConfigHubEndpoint: builderConfigHubEndpoint,
Expand All @@ -196,13 +191,23 @@ func main() {
log.Error("Failed to create proxy server", "err", err)
return err
}
err = instance.RegisterSecrets()

registerContext, registerCancel := context.WithCancel(context.Background())
go func() {
select {
case <-exit:
registerCancel()
case <-registerContext.Done():
}
}()
err = instance.RegisterSecrets(registerContext)
registerCancel()
if err != nil {
log.Error("Failed to generate and publish secrets", "err", err)
return err
}

localListenAddr := cCtx.String("local-listen-address")
localListenAddr := cCtx.String("local-listen-addr")
publicListenAddr := cCtx.String("public-listen-addr")
certListenAddr := cCtx.String("cert-listen-addr")

Expand Down
11 changes: 9 additions & 2 deletions proxy/confighub.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package proxy

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -35,12 +36,18 @@ func NewBuilderConfigHub(log *slog.Logger, endpoint string) *BuilderConfigHub {
}
}

func (b *BuilderConfigHub) RegisterCredentials(info ConfighubOrderflowProxyCredentials) error {
func (b *BuilderConfigHub) RegisterCredentials(ctx context.Context, info ConfighubOrderflowProxyCredentials) error {
body, err := json.Marshal(info)
if err != nil {
return err
}
resp, err := http.Post(b.endpoint+"/api/l1-builder/v1/register_credentials/orderflow-proxy", "application/json", bytes.NewReader(body))
req, err := http.NewRequest(http.MethodPost, b.endpoint+"/api/l1-builder/v1/register_credentials/orderflow-proxy", bytes.NewReader(body))
if err != nil {
return err
}
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
Expand Down
17 changes: 13 additions & 4 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package proxy

import (
"context"
"crypto/tls"
"log/slog"
"net/http"
Expand Down Expand Up @@ -51,7 +52,8 @@ type ReceiverProxy struct {
}

type ReceiverProxyConstantConfig struct {
Log *slog.Logger
Log *slog.Logger
// Name is optional field and it used to distringuish multiple proxies when running in the same process in tests
Name string
FlashbotsSignerAddress common.Address
}
Expand Down Expand Up @@ -187,13 +189,16 @@ func (prx *ReceiverProxy) TLSConfig() *tls.Config {
}
}

func (prx *ReceiverProxy) RegisterSecrets() error {
func (prx *ReceiverProxy) RegisterSecrets(ctx context.Context) error {
const maxRetries = 10
const timeBetweenRetries = time.Second * 10

retry := 0
for {
err := prx.ConfigHub.RegisterCredentials(ConfighubOrderflowProxyCredentials{
if ctx.Err() != nil {
return ctx.Err()
}
err := prx.ConfigHub.RegisterCredentials(ctx, ConfighubOrderflowProxyCredentials{
TLSCert: string(prx.PublicCertPEM),
EcdsaPubkeyAddress: prx.OrderflowSigner.Address(),
})
Expand All @@ -207,7 +212,11 @@ func (prx *ReceiverProxy) RegisterSecrets() error {
return err
}
prx.Log.Error("Fail to register credentials", slog.Any("error", err))
time.Sleep(timeBetweenRetries)
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(timeBetweenRetries):
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func createProxy(localBuilder, name string) *ReceiverProxy {
}

func TestPublishSecrets(t *testing.T) {
err := proxies[0].proxy.RegisterSecrets()
err := proxies[0].proxy.RegisterSecrets(context.Background())
require.NoError(t, err)
}

Expand Down Expand Up @@ -262,7 +262,7 @@ func TestProxyBundleRequestWithPeerUpdate(t *testing.T) {

// we start with no peers
builderHubPeers = nil
err = proxies[0].proxy.RegisterSecrets()
err = proxies[0].proxy.RegisterSecrets(context.Background())
require.NoError(t, err)
proxiesUpdatePeers(t)

Expand All @@ -280,7 +280,7 @@ func TestProxyBundleRequestWithPeerUpdate(t *testing.T) {
slog.Info("Adding first peer")

// add one more peer
err = proxies[1].proxy.RegisterSecrets()
err = proxies[1].proxy.RegisterSecrets(context.Background())
require.NoError(t, err)
proxiesUpdatePeers(t)

Expand All @@ -299,7 +299,7 @@ func TestProxyBundleRequestWithPeerUpdate(t *testing.T) {
// add another peer
slog.Info("Adding second peer")

err = proxies[2].proxy.RegisterSecrets()
err = proxies[2].proxy.RegisterSecrets(context.Background())
require.NoError(t, err)
proxiesUpdatePeers(t)

Expand All @@ -325,7 +325,7 @@ func TestProxySendToArchive(t *testing.T) {

// we start with no peers
builderHubPeers = nil
err = proxies[0].proxy.RegisterSecrets()
err = proxies[0].proxy.RegisterSecrets(context.Background())
require.NoError(t, err)
proxiesUpdatePeers(t)

Expand Down
2 changes: 1 addition & 1 deletion proxy/sharing.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (sq *ShareQueue) Run() {
peers = nil
for _, info := range newPeers {
// don't send to yourself
if info.Name == sq.name {
if info.OrderflowProxy.EcdsaPubkeyAddress == sq.signer.Address() {
continue
}
client, err := RPCClientWithCertAndSigner(OrderflowProxyURLFromIP(info.IP), []byte(info.OrderflowProxy.TLSCert), sq.signer)
Expand Down

0 comments on commit 148e247

Please sign in to comment.