Skip to content

Commit

Permalink
Create a binary for proxying from central. infra
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Oct 30, 2024
1 parent 3c59df5 commit edfcd29
Show file tree
Hide file tree
Showing 12 changed files with 582 additions and 84 deletions.
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ clean: ## Clean the build directory
.PHONY: build
build: ## Build the HTTP server
@mkdir -p ./build
go build -trimpath -ldflags "-X github.com/flashbots/tdx-orderflow-proxy/common.Version=${VERSION}" -v -o ./build/orderflow-proxy cmd/httpserver/main.go
go build -trimpath -ldflags "-X github.com/flashbots/tdx-orderflow-proxy/common.Version=${VERSION}" -v -o ./build/sender-proxy cmd/sender-proxy/main.go
go build -trimpath -ldflags "-X github.com/flashbots/tdx-orderflow-proxy/common.Version=${VERSION}" -v -o ./build/receiver-proxy cmd/receiver-proxy/main.go

##@ Test & Development

Expand Down Expand Up @@ -75,6 +76,13 @@ docker: ## Build the HTTP server Docker image
DOCKER_BUILDKIT=1 docker build \
--platform linux/amd64 \
--build-arg VERSION=${VERSION} \
--file httpserver.dockerfile \
--tag your-project \
--file sender.dockerfile \
--tag tdx-orderflow-proxy-sender-proxy \
.

DOCKER_BUILDKIT=1 docker build \
--platform linux/amd64 \
--build-arg VERSION=${VERSION} \
--file receiver.dockerfile \
--tag tdx-orderflow-proxy-receiver-proxy \
.
73 changes: 66 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
make build
```

## Run
There are two separate programs in this repo:
* receiver proxy that should be part of tdx image
* sender proxy that is part of infra that sends orderflow to all peers

Orderflow proxy will:
## Run receiver proxy

Receiver proxy will:

* generate SSL certificate
* generate orderflow signer
Expand All @@ -24,15 +28,15 @@ Orderflow proxy will:
* proxy local request to other builders in the network
* archive local requests by sending them to archive endpoint

Flags for the orderflow proxy
Flags for the receiver proxy

```
./build/orderflow-proxy -h
./build/receiver-proxy -h
NAME:
orderflow-proxy - Serve API, and metrics
receiver-proxy - Serve API, and metrics
USAGE:
orderflow-proxy [global options] command [command options]
receiver-proxy [global options] command [command options]
COMMANDS:
help, h Shows a list of commands or help for one command
Expand All @@ -54,7 +58,62 @@ GLOBAL OPTIONS:
--log-json log in JSON format (default: false)
--log-debug log debug messages (default: false)
--log-uid generate a uuid and add to all log messages (default: false)
--log-service value add 'service' tag to logs (default: "your-project")
--log-service value add 'service' tag to logs (default: "tdx-orderflow-proxy-receiver")
--pprof enable pprof debug endpoint (pprof is served on $metrics-addr/debug/pprof/*) (default: false)
--help, -h show help
```


## Run sender proxy

Sender proxy will:
* listen for http requests
* sign request with `orderflow-signer-key`
* poxy them to the peers received form builder config hub

```
./build/sender-proxy -h
NAME:
sender-proxy - Serve API, and metrics
USAGE:
sender-proxy [global options] command [command options]
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--listen-address value address to listen on for requests (default: "127.0.0.1:8080")
--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-signer-key value ordreflow will be signed with this address (default: "0xfb5ad18432422a84514f71d63b45edf51165d33bef9c2bd60957a48d4c4cb68e")
--max-request-body-size-bytes value Maximum size of the request body, if 0 default will be used (default: 0)
--metrics-addr value address to listen on for Prometheus metrics (metrics are served on $metrics-addr/metrics) (default: "127.0.0.1:8090")
--log-json log in JSON format (default: false)
--log-debug log debug messages (default: false)
--log-uid generate a uuid and add to all log messages (default: false)
--log-service value add 'service' tag to logs (default: "tdx-orderflow-proxy-sender")
--pprof enable pprof debug endpoint (pprof is served on $metrics-addr/debug/pprof/*) (default: false)
--help, -h show help
dvush@ripper> ./build/sender-proxy -h ~/flashbots/orderflow-proxy
NAME:
sender-proxy - Serve API, and metrics
USAGE:
sender-proxy [global options] command [command options]
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--listen-address value address to listen on for requests (default: "127.0.0.1:8080")
--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-signer-key value ordreflow will be signed with this address (default: "0xfb5ad18432422a84514f71d63b45edf51165d33bef9c2bd60957a48d4c4cb68e")
--max-request-body-size-bytes value Maximum size of the request body, if 0 default will be used (default: 0)
--metrics-addr value address to listen on for Prometheus metrics (metrics are served on $metrics-addr/metrics) (default: "127.0.0.1:8090")
--log-json log in JSON format (default: false)
--log-debug log debug messages (default: false)
--log-uid generate a uuid and add to all log messages (default: false)
--log-service value add 'service' tag to logs (default: "tdx-orderflow-proxy-sender")
--pprof enable pprof debug endpoint (pprof is served on $metrics-addr/debug/pprof/*) (default: false)
--help, -h show help
```
34 changes: 18 additions & 16 deletions cmd/httpserver/main.go → cmd/receiver-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ var flags []cli.Flag = []cli.Flag{
},
&cli.StringFlag{
Name: "log-service",
Value: "your-project",
Value: "tdx-orderflow-proxy-receiver",
Usage: "add 'service' tag to logs",
},
&cli.BoolFlag{
Expand All @@ -117,7 +117,7 @@ var flags []cli.Flag = []cli.Flag{

func main() {
app := &cli.App{
Name: "orderflow-proxy",
Name: "receiver-proxy",
Usage: "Serve API, and metrics",
Flags: flags,
Action: func(cCtx *cli.Context) error {
Expand Down Expand Up @@ -180,38 +180,40 @@ func main() {
flashbotsSignerAddress := eth.HexToAddress(flashbotsSignerStr)
maxRequestBodySizeBytes := cCtx.Int64("max-request-body-size-bytes")

proxyConfig := &proxy.NewProxyConfig{
NewProxyConstantConfig: proxy.NewProxyConstantConfig{Log: log, Name: name, FlashbotsSignerAddress: flashbotsSignerAddress},
CertValidDuration: certDuration,
CertHosts: certHosts,
BuilderConfigHubEndpoint: builderConfigHubEndpoint,
ArchiveEndpoint: archiveEndpoint,
LocalBuilderEndpoint: builderEndpoint,
EthRPC: rpcEndpoint,
MaxRequestBodySizeBytes: maxRequestBodySizeBytes,
proxyConfig := &proxy.ReceiverProxyConfig{
ReceiverProxyConstantConfig: proxy.ReceiverProxyConstantConfig{Log: log, Name: name, FlashbotsSignerAddress: flashbotsSignerAddress},
CertValidDuration: certDuration,
CertHosts: certHosts,
BuilderConfigHubEndpoint: builderConfigHubEndpoint,
ArchiveEndpoint: archiveEndpoint,
LocalBuilderEndpoint: builderEndpoint,
EthRPC: rpcEndpoint,
MaxRequestBodySizeBytes: maxRequestBodySizeBytes,
}

instance, err := proxy.NewNewProxy(*proxyConfig)
instance, err := proxy.NewReceiverProxy(*proxyConfig)
if err != nil {
log.Error("failed to create proxy server", "err", err)
log.Error("Failed to create proxy server", "err", err)
return err
}
err = instance.RegisterSecrets()
if err != nil {
log.Error("failed to generate and publish secrets", "err", err)
log.Error("Failed to generate and publish secrets", "err", err)
return err
}

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

servers, err := proxy.StartServers(instance, publicListenAddr, localListenAddr, certListenAddr)
servers, err := proxy.StartReceiverServers(instance, publicListenAddr, localListenAddr, certListenAddr)
if err != nil {
log.Error("failed to start proxy server", "err", err)
log.Error("Failed to start proxy server", "err", err)
return err
}

log.Info("Started receiver proxy", "publicListenAddress", publicListenAddr, "localListenAddress", localListenAddr, "certListenAddress", certListenAddr)

<-exit
servers.Stop()
return nil
Expand Down
172 changes: 172 additions & 0 deletions cmd/sender-proxy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package main

import (
"log"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"syscall"
"time"

"github.com/VictoriaMetrics/metrics"
"github.com/flashbots/go-utils/signature"
"github.com/flashbots/tdx-orderflow-proxy/common"
"github.com/flashbots/tdx-orderflow-proxy/proxy"
"github.com/google/uuid"
"github.com/urfave/cli/v2" // imports as package "cli"
)

var flags []cli.Flag = []cli.Flag{
// input and output
&cli.StringFlag{
Name: "listen-address",
Value: "127.0.0.1:8080",
Usage: "address to listen on for requests",
},
&cli.StringFlag{
Name: "builder-confighub-endpoint",
Value: "http://127.0.0.1:14892",
Usage: "address of the builder config hub enpoint (directly or throught the cvm-proxy)",
},
&cli.StringFlag{
Name: "orderflow-signer-key",
Value: "0xfb5ad18432422a84514f71d63b45edf51165d33bef9c2bd60957a48d4c4cb68e",
Usage: "ordreflow will be signed with this address",
},
&cli.Int64Flag{
Name: "max-request-body-size-bytes",
Value: 0,
Usage: "Maximum size of the request body, if 0 default will be used",
},

// logging, metrics and debug
&cli.StringFlag{
Name: "metrics-addr",
Value: "127.0.0.1:8090",
Usage: "address to listen on for Prometheus metrics (metrics are served on $metrics-addr/metrics)",
},
&cli.BoolFlag{
Name: "log-json",
Value: false,
Usage: "log in JSON format",
},
&cli.BoolFlag{
Name: "log-debug",
Value: false,
Usage: "log debug messages",
},
&cli.BoolFlag{
Name: "log-uid",
Value: false,
Usage: "generate a uuid and add to all log messages",
},
&cli.StringFlag{
Name: "log-service",
Value: "tdx-orderflow-proxy-sender",
Usage: "add 'service' tag to logs",
},
&cli.BoolFlag{
Name: "pprof",
Value: false,
Usage: "enable pprof debug endpoint (pprof is served on $metrics-addr/debug/pprof/*)",
},
}

func main() {
app := &cli.App{
Name: "sender-proxy",
Usage: "Serve API, and metrics",
Flags: flags,
Action: func(cCtx *cli.Context) error {
logJSON := cCtx.Bool("log-json")
logDebug := cCtx.Bool("log-debug")
logUID := cCtx.Bool("log-uid")
logService := cCtx.String("log-service")

log := common.SetupLogger(&common.LoggingOpts{
Debug: logDebug,
JSON: logJSON,
Service: logService,
Version: common.Version,
})

if logUID {
id := uuid.Must(uuid.NewRandom())
log = log.With("uid", id.String())
}

exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)

// metrics server
go func() {
metricsAddr := cCtx.String("metrics-addr")
usePprof := cCtx.Bool("pprof")
metricsMux := http.NewServeMux()
metricsMux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
metrics.WritePrometheus(w, true)
})
if usePprof {
metricsMux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
metricsMux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
metricsMux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
metricsMux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
metricsMux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
}

metricsServer := &http.Server{
Addr: metricsAddr,
ReadHeaderTimeout: 5 * time.Second,
Handler: metricsMux,
}

err := metricsServer.ListenAndServe()
if err != nil {
log.Error("Failed to start metrics server", "err", err)
}
}()

builderConfigHubEndpoint := cCtx.String("builder-confighub-endpoint")
orderflowSignerKeyStr := cCtx.String("orderflow-signer-key")
orderflowSigner, err := signature.NewSignerFromHexPrivateKey(orderflowSignerKeyStr)
if err != nil {
log.Error("Failed to get signer from private key", "error", err)
}
log.Info("Ordeflow signing address", "address", orderflowSigner.Address())
maxRequestBodySizeBytes := cCtx.Int64("max-request-body-size-bytes")

proxyConfig := &proxy.SenderProxyConfig{
SenderProxyConstantConfig: proxy.SenderProxyConstantConfig{
Log: log,
OrderflowSigner: orderflowSigner,
},
BuilderConfigHubEndpoint: builderConfigHubEndpoint,
MaxRequestBodySizeBytes: maxRequestBodySizeBytes,
}

instance, err := proxy.NewSenderProxy(*proxyConfig)
if err != nil {
log.Error("Failed to create proxy server", "err", err)
return err
}

listenAddr := cCtx.String("listen-address")
servers, err := proxy.StartSenderServers(instance, listenAddr)
if err != nil {
log.Error("Failed to start proxy server", "err", err)
return err
}

log.Info("Started sender proxy", "listenAddres", listenAddr)

<-exit
servers.Stop()
return nil
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
Loading

0 comments on commit edfcd29

Please sign in to comment.