From ea95969020ca1dc844aed94acf650f2736528162 Mon Sep 17 00:00:00 2001 From: Vitaly Drogan Date: Thu, 24 Oct 2024 16:48:52 +0200 Subject: [PATCH] Builder config hub --- cmd/httpserver/main.go | 9 ++++- proxy/confighub.go | 75 +++++++++++++++++++++++++++++++++++++----- proxy/proxy.go | 8 ++--- 3 files changed, 79 insertions(+), 13 deletions(-) diff --git a/cmd/httpserver/main.go b/cmd/httpserver/main.go index 6eec0ce..53ab1c0 100644 --- a/cmd/httpserver/main.go +++ b/cmd/httpserver/main.go @@ -38,6 +38,11 @@ var flags []cli.Flag = []cli.Flag{ Value: "127.0.0.1:8645", Usage: "address to send local ordeflow to", }, + &cli.StringFlag{ + Name: "builder-confighub-endpoint", + Value: "127.0.0.1:14892", + Usage: "address of the builder config hub enpoint (directly or throught the cvm-proxy)", + }, // certificate config &cli.DurationFlag{ @@ -144,6 +149,8 @@ func main() { builderEndpoint := cCtx.String("builder-endpoint") certDuration := cCtx.Duration("cert-duration") certHosts := cCtx.StringSlice("cert-hosts") + builderConfigHubEndpoint := cCtx.String("builder-confighub-endpoint") + proxyConfig := &proxy.Config{ Log: log, UsersListenAddr: usersListenAddr, @@ -154,7 +161,7 @@ func main() { CertValidDuration: certDuration, CertHosts: certHosts, - BuilderConfigHub: proxy.MockBuilderConfigHub{}, + BuilderConfigHub: proxy.NewBuilderConfigHub(builderConfigHubEndpoint), } proxy, err := proxy.New(*proxyConfig) diff --git a/proxy/confighub.go b/proxy/confighub.go index 930ebb7..b123ce9 100644 --- a/proxy/confighub.go +++ b/proxy/confighub.go @@ -1,24 +1,83 @@ // Package proxy provides the main proxy server. package proxy -import "github.com/ethereum/go-ethereum/common" +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" -type BuilderInfo struct { - Cert []byte - OrderflowSigner common.Address + "github.com/ethereum/go-ethereum/common" +) + +type ConfighubOrderflowProxyCredentials struct { + TLSCert string `json:"tls_cert"` + EcdsaPubkeyAddress common.Address `json:"ecdsa_pubkey_address"` +} + +type ConfighubBuilder struct { + IP string `json:"ip"` + OrderflowProxy ConfighubOrderflowProxyCredentials `json:"orderflow_proxy"` } type BuilderConfigHub interface { - PublishConfig(info BuilderInfo) error - GetBuilders() ([]BuilderInfo, error) + RegisterCredentials(args ConfighubOrderflowProxyCredentials) error + Builders() ([]ConfighubBuilder, error) } type MockBuilderConfigHub struct{} -func (m MockBuilderConfigHub) PublishConfig(info BuilderInfo) error { +func (m MockBuilderConfigHub) RegisterCredentials(info ConfighubOrderflowProxyCredentials) error { return nil } -func (m MockBuilderConfigHub) GetBuilders() ([]BuilderInfo, error) { +func (m MockBuilderConfigHub) Builders() ([]ConfighubBuilder, error) { return nil, nil } + +type builderConfigHubImpl struct { + endpoint string +} + +func NewBuilderConfigHub(endpoint string) BuilderConfigHub { + return &builderConfigHubImpl{ + endpoint: endpoint, + } +} + +func (b *builderConfigHubImpl) RegisterCredentials(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)) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + respBody, _ := io.ReadAll(resp.Body) + return fmt.Errorf("builder config hub returned error, code: %d, body: %s", resp.StatusCode, string(respBody)) + } + return nil +} + +func (b *builderConfigHubImpl) Builders() ([]ConfighubBuilder, error) { + resp, err := http.Get(b.endpoint + "/api/l1-builder/v1/builders") + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + var result []ConfighubBuilder + err = json.Unmarshal(body, &result) + if err != nil { + return nil, err + } + return result, nil +} diff --git a/proxy/proxy.go b/proxy/proxy.go index dee4338..6e95b86 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -97,12 +97,12 @@ func (prx *Proxy) GenerateAndPublish() error { prx.log.Info("Generated ordeflow signer", "address", orderflowSigner) - selfInfo := BuilderInfo{ - Cert: cert, - OrderflowSigner: orderflowSigner, + selfInfo := ConfighubOrderflowProxyCredentials{ + TLSCert: string(cert), + EcdsaPubkeyAddress: orderflowSigner, } - err = prx.Config.BuilderConfigHub.PublishConfig(selfInfo) + err = prx.Config.BuilderConfigHub.RegisterCredentials(selfInfo) if err != nil { return err }