Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add startup logging #31

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 64 additions & 8 deletions anvil/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"sync/atomic"
"time"

"github.com/ethereum-optimism/supersim/utils"
"github.com/ethereum/go-ethereum/rpc"

"github.com/ethereum/go-ethereum/log"
)

Expand Down Expand Up @@ -58,13 +60,12 @@ func (a *Anvil) Start(ctx context.Context) error {

tempFile, err := os.CreateTemp("", "genesis-*.json")
if err != nil {
return fmt.Errorf("Error creating temporary genesis file: %w", err)
return fmt.Errorf("error creating temporary genesis file: %w", err)
}
defer os.Remove(tempFile.Name())

_, err = tempFile.Write(a.cfg.Genesis)
if err != nil {
return fmt.Errorf("Error writing to genesis file: %w", err)
return fmt.Errorf("error writing to genesis file: %w", err)
}

// Prep args
Expand Down Expand Up @@ -109,11 +110,9 @@ func (a *Anvil) Start(ctx context.Context) error {
return fmt.Errorf("failed to start anvil: %w", err)
}

if _, err := utils.WaitForAnvilClientToBeReady(fmt.Sprintf("http://%s:%d", host, a.cfg.Port), 5*time.Second); err != nil {
return fmt.Errorf("failed to start anvil: %w", err)
}

go func() {
defer os.Remove(tempFile.Name())
jakim929 marked this conversation as resolved.
Show resolved Hide resolved

if err := a.cmd.Wait(); err != nil {
anvilLog.Error("anvil terminated with an error", "error", err)
} else {
Expand Down Expand Up @@ -141,3 +140,60 @@ func (a *Anvil) Stop() error {
func (a *Anvil) Stopped() bool {
return a.stopped.Load()
}

func (a *Anvil) Endpoint() string {
return fmt.Sprintf("http://%s:%d", host, a.cfg.Port)
}

func (a *Anvil) WaitUntilReady(ctx context.Context) error {
return waitForAnvilEndpointToBeReady(ctx, a.Endpoint(), 10*time.Second)
}

func (a *Anvil) ChainId() uint64 {
return a.cfg.ChainId
}

func waitForAnvilEndpointToBeReady(ctx context.Context, endpoint string, timeout time.Duration) error {
client, err := rpc.Dial(endpoint)
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
}

defer client.Close()

if err := waitForAnvilClientToBeReady(ctx, client, timeout); err != nil {
return fmt.Errorf("failed to connect to RPC server: %w", err)
}

return nil
}

func waitForAnvilClientToBeReady(ctx context.Context, client *rpc.Client, timeout time.Duration) error {
timeoutCtx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return fmt.Errorf("context cancelled")
case <-timeoutCtx.Done():
return fmt.Errorf("timed out waiting for response from client")
case <-ticker.C:
var result string
callErr := client.Call(&result, "web3_clientVersion")

if callErr != nil {
continue
}

if strings.HasPrefix(result, "anvil") {
return nil
}

return fmt.Errorf("unexpected client version: %s", result)
}
}
}
62 changes: 60 additions & 2 deletions supersim.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package supersim
import (
_ "embed"
"fmt"
"strings"
"sync"

"context"

Expand Down Expand Up @@ -41,8 +43,8 @@ func NewSupersim(log log.Logger, config *Config) *Supersim {
l1Chain := anvil.New(log, &config.l1Chain)

l2Chains := make(map[uint64]*anvil.Anvil)
for _, l2Chain := range config.l2Chains {
l2Chains[l2Chain.ChainId] = anvil.New(log, &l2Chain)
for _, l2ChainConfig := range config.l2Chains {
l2Chains[l2ChainConfig.ChainId] = anvil.New(log, &l2ChainConfig)
}

return &Supersim{log, l1Chain, l2Chains}
Expand All @@ -61,6 +63,13 @@ func (s *Supersim) Start(ctx context.Context) error {
}
}

if err := s.WaitUntilReady(); err != nil {
return fmt.Errorf("supersim failed to get ready: %w", err)
}

s.log.Info("supersim is ready")
s.log.Info(s.ConfigAsString())

return nil
}

Expand All @@ -83,3 +92,52 @@ func (s *Supersim) Stop(_ context.Context) error {
func (s *Supersim) Stopped() bool {
return s.l1Chain.Stopped()
}

func (s *Supersim) WaitUntilReady() error {
var once sync.Once
var err error
ctx, cancel := context.WithCancel(context.Background())

handleErr := func(e error) {
if e != nil {
once.Do(func() {
err = e
cancel()
})
}
}

var wg sync.WaitGroup

waitForAnvil := func(anvil *anvil.Anvil) {
defer wg.Done()
handleErr(anvil.WaitUntilReady(ctx))
}

wg.Add(1)
go waitForAnvil(s.l1Chain)

for _, l2Chain := range s.l2Chains {
wg.Add(1)
go waitForAnvil(l2Chain)
}

wg.Wait()

return err
}

func (s *Supersim) ConfigAsString() string {
var b strings.Builder

fmt.Fprintf(&b, "\nSupersim Config:\n")
fmt.Fprintf(&b, "L1:\n")
fmt.Fprintf(&b, " Chain ID: %d RPC: %s\n", s.l1Chain.ChainId(), s.l1Chain.Endpoint())

fmt.Fprintf(&b, "L2:\n")
for _, l2Chain := range s.l2Chains {
fmt.Fprintf(&b, " Chain ID: %d RPC: %s\n", l2Chain.ChainId(), l2Chain.Endpoint())
}

return b.String()
}
20 changes: 14 additions & 6 deletions supersim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"time"

oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/supersim/utils"

"github.com/ethereum/go-ethereum/rpc"
)

const (
Expand All @@ -23,7 +24,7 @@ const (
func TestGenesisState(t *testing.T) {
logger := oplog.NewLogger(os.Stderr, oplog.DefaultCLIConfig())
supersim := NewSupersim(logger, &DefaultConfig)
_ = supersim.Start(context.Background())
err := supersim.Start(context.Background())

defer func() {
err := supersim.Stop(context.Background())
Expand All @@ -32,15 +33,22 @@ func TestGenesisState(t *testing.T) {
}
}()

if err != nil {
t.Fatalf("Failed to start supersim: %v", err)
}

for _, l2ChainConfig := range DefaultConfig.l2Chains {
client, err := utils.WaitForAnvilClientToBeReady(fmt.Sprintf("http://127.0.0.1:%d", l2ChainConfig.Port), anvilClientTimeout)
if err != nil {
t.Fatalf("Failed to connect to RPC server: %v", err)
rpcUrl := fmt.Sprintf("http://127.0.0.1:%d", l2ChainConfig.Port)
client, clientCreateErr := rpc.Dial(rpcUrl)

if clientCreateErr != nil {
t.Fatalf("Failed to create client: %v", clientCreateErr)
}

defer client.Close()

var code string
err = client.CallContext(context.Background(), &code, "eth_getCode", crossL2InboxAddress, "latest")
err := client.CallContext(context.Background(), &code, "eth_getCode", crossL2InboxAddress, "latest")
if err != nil {
log.Fatalf("Failed to get code: %v", err)
}
Expand Down
41 changes: 0 additions & 41 deletions utils/rpc.go

This file was deleted.

Loading