Skip to content

Commit

Permalink
name field && lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TymKh committed Oct 28, 2024
1 parent 2a9d5ef commit ef7fba8
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 190 deletions.
7 changes: 5 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
run:
tests: false
linters:
enable-all: true
disable:
Expand All @@ -11,7 +13,6 @@ linters:
- godot
- godox
- gomnd
- mnd
- lll
- nestif
- nilnil
Expand All @@ -27,7 +28,9 @@ linters:
- wsl
- exhaustruct
- depguard
- err113
- prealloc
- whitespace
- musttag

#
# Disabled because of generics:
Expand Down
8 changes: 5 additions & 3 deletions adapters/database/service.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package database provides a database adapter for postgres
package database

import (
Expand Down Expand Up @@ -36,7 +37,6 @@ func (s *Service) Close() error {
return s.DB.Close()
}

// GetMeasurementByTypeAndHash retrieves a measurement by OID and hash
func (s *Service) GetActiveMeasurementsByType(ctx context.Context, attestationType string) ([]domain.Measurement, error) {
var measurements []Measurement
err := s.DB.SelectContext(ctx, &measurements, `SELECT * FROM measurements_whitelist WHERE is_active=true AND attestation_type=$1`, attestationType)
Expand Down Expand Up @@ -90,13 +90,15 @@ func (s *Service) GetActiveMeasurements(ctx context.Context) ([]domain.Measureme

// RegisterCredentialsForBuilder registers new credentials for a builder, deprecating all previous credentials
// It uses hash and attestation_type to fetch the corresponding measurement_id via a subquery.
func (s *Service) RegisterCredentialsForBuilder(ctx context.Context, builderName, service, tlsCert string, ecdsaPubKey []byte, measurementName string, attestationType string) error {
func (s *Service) RegisterCredentialsForBuilder(ctx context.Context, builderName, service, tlsCert string, ecdsaPubKey []byte, measurementName, attestationType string) error {
// Start a transaction
tx, err := s.DB.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback() // Rollback the transaction if it's not committed
defer func() {
_ = tx.Rollback()
}() // Rollback the transaction if it's not committed

// Deprecate all previous credentials for this builder and service
_, err = tx.Exec(`
Expand Down
35 changes: 0 additions & 35 deletions adapters/database/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,6 @@ func TestGetBuilder(t *testing.T) {
if err != nil {
t.Errorf("NewDatabaseService() = %v; want nil", err)
}
//t.Run("GetBuilder", func(t *testing.T) {
// _, err = db.Exec("create temporary table t (id serial primary key, ip inet not null);")
// if err != nil {
// log.Fatal(err)
// }
//
// var paramIP pgtype.Inet
// err = paramIP.Set("10.0.0.0/16")
// if err != nil {
// log.Fatal(err)
// }
//
// _, err = db.Exec("insert into t (ip) values ($1);", paramIP)
// if err != nil {
// log.Fatal(err)
// }
//
// var resultIP pgtype.Inet
// err = db.QueryRow("select ip from t").Scan(&resultIP)
// if err != nil {
// log.Fatal(err)
// }
//
// fmt.Println(resultIP.IPNet)
//})
//t.Run("GetBuilder", func(t *testing.T) {
// t.Run("should return a builder", func(t *testing.T) {
// builder, err := serv.GetBuilderByIP(net.ParseIP("192.168.1.100"))
// if err != nil {
// t.Errorf("GetBuilder() = %v; want nil", err)
// }
// fmt.Println(builder)
// })
//})
t.Run("GetBuilder2", func(t *testing.T) {
t.Run("should return a builder", func(t *testing.T) {
whitelist, err := serv.GetBuilderByIP(net.ParseIP("192.168.1.1"))
Expand All @@ -69,5 +35,4 @@ func TestGetBuilder(t *testing.T) {
fmt.Println(whitelist)
})
})

}
3 changes: 2 additions & 1 deletion adapters/secrets/service.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package secrets contains logic for adapter to aws secrets manager
package secrets

import (
Expand Down Expand Up @@ -60,7 +61,7 @@ func (s *Service) GetSecretValues(builderName string) (map[string]string, error)
return MergeSecrets(defaultSecrets, builderSecrets), nil
}

func MergeSecrets(defaultSecrets map[string]string, secrets map[string]string) map[string]string {
func MergeSecrets(defaultSecrets, secrets map[string]string) map[string]string {
// merge secrets
res := make(map[string]string)
for k, v := range defaultSecrets {
Expand Down
3 changes: 2 additions & 1 deletion application/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,6 @@ func TestMerge(t *testing.T) {

cfg := ExampleConfig{}
err = json.Unmarshal(newC, &cfg)
require.Equal(t, cfg.OrderflowProxy.FlashbotsOfSigningKey, "test_value_1")
require.NoError(t, err)
require.Equal(t, "test_value_1", cfg.OrderflowProxy.FlashbotsOfSigningKey)
}
3 changes: 2 additions & 1 deletion application/config_utils.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package application contains application logic for the builder-hub
package application

import (
Expand All @@ -9,7 +10,7 @@ import (

func MergeConfigSecrets(config json.RawMessage, secrets map[string]string) (json.RawMessage, error) {
// merge config and secrets
var bts = []byte(config)
bts := []byte(config)
var err error
for k, v := range secrets {
tV := "\"" + v + "\""
Expand Down
6 changes: 3 additions & 3 deletions application/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type BuilderDataAccessor interface {
GetActiveMeasurementsByType(ctx context.Context, attestationType string) ([]domain.Measurement, error)
GetBuilderByIP(ip net.IP) (*domain.Builder, error)
GetActiveConfigForBuilder(ctx context.Context, builderName string) (json.RawMessage, error)
RegisterCredentialsForBuilder(ctx context.Context, builderName, service, tlsCert string, ecdsaPubKey []byte, measurementName string, attestationType string) error
RegisterCredentialsForBuilder(ctx context.Context, builderName, service, tlsCert string, ecdsaPubKey []byte, measurementName, attestationType string) error
}

type SecretAccessor interface {
Expand All @@ -39,7 +39,7 @@ func (b *BuilderHub) GetActiveBuilders(ctx context.Context) ([]domain.BuilderWit
return b.dataAccessor.GetActiveBuildersWithServiceCredentials(ctx)
}

func (b *BuilderHub) RegisterCredentialsForBuilder(ctx context.Context, builderName, service, tlsCert string, ecdsaPubKey []byte, measurementName string, attestationType string) error {
func (b *BuilderHub) RegisterCredentialsForBuilder(ctx context.Context, builderName, service, tlsCert string, ecdsaPubKey []byte, measurementName, attestationType string) error {
return b.dataAccessor.RegisterCredentialsForBuilder(ctx, builderName, service, tlsCert, ecdsaPubKey, measurementName, attestationType)
}

Expand All @@ -59,7 +59,7 @@ func (b *BuilderHub) GetConfigWithSecrets(ctx context.Context, builderName strin
return res, nil
}

func (b *BuilderHub) VerifyIpAndMeasurements(ctx context.Context, ip net.IP, measurement map[string]string, attestationType string) (*domain.Builder, string, error) {
func (b *BuilderHub) VerifyIPAndMeasurements(ctx context.Context, ip net.IP, measurement map[string]string, attestationType string) (*domain.Builder, string, error) {
measurements, err := b.dataAccessor.GetActiveMeasurementsByType(ctx, attestationType)
if err != nil {
return nil, "", fmt.Errorf("failing to fetch corresponding measurement data %s %w", attestationType, err)
Expand Down
1 change: 0 additions & 1 deletion domain/measurements-jq.json

This file was deleted.

1 change: 0 additions & 1 deletion domain/measurements.json

This file was deleted.

12 changes: 5 additions & 7 deletions domain/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Package domain contains domain area types/functions for builder hub
package domain

import (
"fmt"
"errors"
"net"
)

var (
ErrNotFound = fmt.Errorf("not found")
ErrIncorrectBuilder = fmt.Errorf("incorrect builder")
ErrInvalidMeasurement = fmt.Errorf("no such active measurement found")
ErrNotFound = errors.New("not found")
ErrIncorrectBuilder = errors.New("incorrect builder")
ErrInvalidMeasurement = errors.New("no such active measurement found")
)

type Measurement struct {
Expand Down Expand Up @@ -45,6 +46,3 @@ type BuilderServices struct {
ECDSAPubKey []byte
Service string
}

type BuilderConfig struct {
}
60 changes: 0 additions & 60 deletions domain/types_test.go

This file was deleted.

41 changes: 0 additions & 41 deletions httpserver/handler.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package httpserver

import (
"fmt"
"io"
"net/http"
"time"

"github.com/go-chi/chi/v5"
)

func (srv *Server) handleLivenessCheck(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -47,40 +43,3 @@ func (srv *Server) handleTestPanic(w http.ResponseWriter, r *http.Request) {
//
// BuilderConfigHub API: https://www.notion.so/flashbots/BuilderConfigHub-1076b4a0d8768074bcdcd1c06c26ec87?pvs=4#10a6b4a0d87680fd81e0cad9bac3b8c5
//

func (srv *Server) handleGetConfiguration(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, srv.mockGetConfigResponse)
w.WriteHeader(http.StatusOK)
}

func (srv *Server) handleGetBuilders(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, srv.mockGetBuildersResponse)
w.WriteHeader(http.StatusOK)
}

func (srv *Server) handleGetMeasurements(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, srv.mockGetMeasurementsResponse)
w.WriteHeader(http.StatusOK)
}

func (srv *Server) handleRegisterCredentials(w http.ResponseWriter, r *http.Request) {
// get service name from URL
service := chi.URLParam(r, "service")
if !allowedServices[service] {
srv.log.Error("Invalid service name", "service", service)
w.WriteHeader(http.StatusBadRequest)
return
}

// read request body
body, err := io.ReadAll(r.Body)
if err != nil {
srv.log.Error("Failed to read request body", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer r.Body.Close()

srv.log.Info("Register credentials", "service", service, "body", string(body))
w.WriteHeader(http.StatusOK)
}
5 changes: 0 additions & 5 deletions httpserver/vars.go
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
package httpserver

var allowedServices = map[string]bool{
"of-proxy": true,
"rbuilder": true,
}
3 changes: 2 additions & 1 deletion metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"context"
"errors"
"net/http"
"time"

Expand Down Expand Up @@ -32,7 +33,7 @@ func (srv *MetricsServer) Start() error {
ReadHeaderTimeout: 5 * time.Second,
}

if err := srv.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
if err := srv.srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
Expand Down
Loading

0 comments on commit ef7fba8

Please sign in to comment.