generated from flashbots/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a binary for proxying from central. infra
- Loading branch information
Showing
12 changed files
with
582 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.