Skip to content

Commit

Permalink
admin port cfg (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdiallam authored Oct 27, 2024
1 parent 9e6fdfa commit 5c7d64f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 54 deletions.
33 changes: 24 additions & 9 deletions admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package admin

import (
"context"
"fmt"
"net"
"net/http"
"sync"

Expand All @@ -11,39 +13,47 @@ import (
)

type AdminServer struct {
log log.Logger

srv *http.Server
cancel context.CancelFunc
wg sync.WaitGroup

log log.Logger
port uint64
}

func NewAdminServer(log log.Logger) *AdminServer {
return &AdminServer{log: log}
func NewAdminServer(log log.Logger, port uint64) *AdminServer {
return &AdminServer{log: log, port: port}
}

func (s *AdminServer) Start(ctx context.Context) error {
router := setupRouter()
s.srv = &http.Server{Handler: router}

s.srv = &http.Server{
Addr: ":8420",
Handler: router,
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", s.port))
if err != nil {
panic(err)
}

addr := listener.Addr().(*net.TCPAddr)

s.port = uint64(addr.Port)
s.log.Debug("admin server listening", "port", s.port)

ctx, s.cancel = context.WithCancel(ctx)

s.wg.Add(1)
go func() {
defer s.wg.Done()
if err := s.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
s.log.Error("ListenAndServe error", "error", err)
if err := s.srv.Serve(listener); err != nil && err != http.ErrServerClosed {
s.log.Error("failed to serve", "error", err)
}
}()

go func() {
<-ctx.Done()
if err := s.srv.Shutdown(context.Background()); err != nil {
s.log.Error("Server shutdown error", "error", err)
s.log.Error("failed to shutdown", "error", err)
}
}()

Expand All @@ -54,10 +64,15 @@ func (s *AdminServer) Stop(ctx context.Context) error {
if s.cancel != nil {
s.cancel()
}

s.wg.Wait()
return nil
}

func (s *AdminServer) Endpoint() string {
return fmt.Sprintf("http://127.0.0.1:%d", s.port)
}

func setupRouter() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
router := gin.New()
Expand Down
30 changes: 4 additions & 26 deletions admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package admin

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

"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum/go-ethereum/log"
Expand All @@ -16,14 +16,12 @@ func TestAdminServerBasicFunctionality(t *testing.T) {
testlog := testlog.Logger(t, log.LevelInfo)

ctx, cancel := context.WithCancel(context.Background())
adminServer := NewAdminServer(testlog)
adminServer := NewAdminServer(testlog, 0)
t.Cleanup(func() { cancel() })

require.NoError(t, adminServer.Start(ctx))

require.Equal(t, ":8420", adminServer.srv.Addr)

resp, err := http.Get("http://localhost:8420/ready")
resp, err := http.Get(fmt.Sprintf("%s/ready", adminServer.Endpoint()))
require.NoError(t, err)
defer resp.Body.Close()

Expand All @@ -35,27 +33,7 @@ func TestAdminServerBasicFunctionality(t *testing.T) {

require.NoError(t, adminServer.Stop(context.Background()))

resp, err = http.Get("http://localhost:8420/ready")
if err == nil {
resp.Body.Close()
}
require.Error(t, err)
}

func TestAdminServerContextCancellation(t *testing.T) {
testlog := testlog.Logger(t, log.LevelInfo)

ctx, cancel := context.WithCancel(context.Background())
adminServer := NewAdminServer(testlog)

require.NoError(t, adminServer.Start(ctx))

// Cancel the context
cancel()

time.Sleep(100 * time.Millisecond)

resp, err := http.Get("http://localhost:8420/ready")
resp, err = http.Get(fmt.Sprintf("%s/ready", adminServer.Endpoint()))
if err == nil {
resp.Body.Close()
}
Expand Down
12 changes: 12 additions & 0 deletions config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
const (
ForkCommandName = "fork"

AdminPortFlagName = "admin.port"

L1ForkHeightFlagName = "l1.fork.height"
L1PortFlagName = "l1.port"

Expand All @@ -29,6 +31,12 @@ const (

func BaseCLIFlags(envPrefix string) []cli.Flag {
return []cli.Flag{
&cli.Uint64Flag{
Name: AdminPortFlagName,
Usage: "Listening port for the admin server",
Value: 8420,
EnvVars: opservice.PrefixEnvVar(envPrefix, "ADMIN_PORT"),
},
&cli.Uint64Flag{
Name: L1PortFlagName,
Usage: "Listening port for the L1 instance. `0` binds to any available port",
Expand Down Expand Up @@ -96,6 +104,8 @@ type ForkCLIConfig struct {
}

type CLIConfig struct {
AdminPort uint64

L1Port uint64
L2StartingPort uint64

Expand All @@ -108,6 +118,8 @@ type CLIConfig struct {

func ReadCLIConfig(ctx *cli.Context) (*CLIConfig, error) {
cfg := &CLIConfig{
AdminPort: ctx.Uint64(AdminPortFlagName),

L1Port: ctx.Uint64(L1PortFlagName),
L2StartingPort: ctx.Uint64(L2StartingPortFlagName),

Expand Down
17 changes: 1 addition & 16 deletions orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"sync"

"github.com/ethereum-optimism/supersim/admin"
"github.com/ethereum-optimism/supersim/anvil"
"github.com/ethereum-optimism/supersim/config"
"github.com/ethereum-optimism/supersim/interop"
Expand All @@ -30,8 +29,6 @@ type Orchestrator struct {

l2ToL2MsgIndexer *interop.L2ToL2MessageIndexer
l2ToL2MsgRelayer *interop.L2ToL2MessageRelayer

adminServer *admin.AdminServer
}

func NewOrchestrator(log log.Logger, closeApp context.CancelCauseFunc, networkConfig *config.NetworkConfig) (*Orchestrator, error) {
Expand Down Expand Up @@ -70,9 +67,6 @@ func NewOrchestrator(log log.Logger, closeApp context.CancelCauseFunc, networkCo
}
}

// Admin API server
o.adminServer = admin.NewAdminServer(log)

return &o, nil
}

Expand Down Expand Up @@ -143,22 +137,13 @@ func (o *Orchestrator) Start(ctx context.Context) error {
}
}

if err := o.adminServer.Start(ctx); err != nil {
return fmt.Errorf("admin server failed to start: %w", err)
}

o.log.Debug("orchestrator is ready")
return nil
}

func (o *Orchestrator) Stop(ctx context.Context) error {
var errs []error
o.log.Debug("stopping orchestrator")

if err := o.adminServer.Stop(ctx); err != nil {
errs = append(errs, fmt.Errorf("admin server failed to stop: %w", err))
}

if o.config.InteropEnabled {
if o.l2ToL2MsgRelayer != nil {
o.log.Info("stopping L2ToL2CrossDomainMessenger autorelayer")
Expand Down Expand Up @@ -238,7 +223,7 @@ func (o *Orchestrator) ConfigAsString() string {
l1Cfg := o.l1Chain.Config()
fmt.Fprintf(&b, "L1: Name: %s ChainID: %d RPC: %s LogPath: %s\n", l1Cfg.Name, l1Cfg.ChainID, o.l1Chain.Endpoint(), o.l1Chain.LogPath())

fmt.Fprintf(&b, "\nL2s: Predeploy Contracts Spec ( %s )\n", "https://specs.optimism.io/protocol/predeploys.html")
fmt.Fprintf(&b, "\nL2: Predeploy Contracts Spec ( %s )\n", "https://specs.optimism.io/protocol/predeploys.html")
opSims := make([]*opsimulator.OpSimulator, 0, len(o.l2OpSims))
for _, chain := range o.l2OpSims {
opSims = append(opSims, chain)
Expand Down
23 changes: 20 additions & 3 deletions supersim.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package supersim

import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/ethereum-optimism/optimism/op-service/predeploys"
registry "github.com/ethereum-optimism/superchain-registry/superchain"
"github.com/ethereum-optimism/supersim/admin"
"github.com/ethereum-optimism/supersim/config"
"github.com/ethereum-optimism/supersim/orchestrator"

Expand All @@ -21,6 +23,8 @@ type Supersim struct {
NetworkConfig *config.NetworkConfig

Orchestrator *orchestrator.Orchestrator

adminServer *admin.AdminServer
}

func NewSupersim(log log.Logger, envPrefix string, closeApp context.CancelCauseFunc, cliConfig *config.CLIConfig) (*Supersim, error) {
Expand Down Expand Up @@ -61,28 +65,36 @@ func NewSupersim(log log.Logger, envPrefix string, closeApp context.CancelCauseF
return nil, fmt.Errorf("failed to create orchestrator")
}

return &Supersim{log, cliConfig, &networkConfig, o}, nil
adminServer := admin.NewAdminServer(log, cliConfig.AdminPort)
return &Supersim{log, cliConfig, &networkConfig, o, adminServer}, nil
}

func (s *Supersim) Start(ctx context.Context) error {
s.log.Info("starting supersim")
if err := s.Orchestrator.Start(ctx); err != nil {
return fmt.Errorf("orchestrator failed to start: %w", err)
}
if err := s.adminServer.Start(ctx); err != nil {
return fmt.Errorf("admin server failed to start: %w", err)
}

s.log.Info("supersim is ready")
s.log.Info(s.ConfigAsString())
return nil
}

func (s *Supersim) Stop(ctx context.Context) error {
var errs []error
s.log.Info("stopping supersim")
if err := s.Orchestrator.Stop(ctx); err != nil {
return fmt.Errorf("orchestrator failed to stop: %w", err)
errs = append(errs, fmt.Errorf("orchestrator failed to stop: %w", err))
}
if err := s.adminServer.Stop(ctx); err != nil {
errs = append(errs, fmt.Errorf("admin server failed to stop: %w", err))
}

s.log.Info("stopped supersim")
return nil
return errors.Join(errs...)
}

// no-op dead code in the cliapp lifecycle
Expand All @@ -93,6 +105,11 @@ func (s *Supersim) Stopped() bool {
func (s *Supersim) ConfigAsString() string {
var b strings.Builder
fmt.Fprintln(&b, config.DefaultSecretsConfigAsString())

fmt.Fprintln(&b, "Supersim Config")
fmt.Fprintln(&b, "-----------------------")
fmt.Fprintf(&b, "Admin Server: %s\n\n", s.adminServer.Endpoint())

fmt.Fprintln(&b, "Chain Configuration")
fmt.Fprintln(&b, "-----------------------")
fmt.Fprintln(&b, s.Orchestrator.ConfigAsString())
Expand Down

0 comments on commit 5c7d64f

Please sign in to comment.