Skip to content

Commit

Permalink
feat: startup logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jakim929 committed Jul 9, 2024
1 parent a209230 commit dad31ee
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 24 deletions.
21 changes: 12 additions & 9 deletions anvil/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"os"
"os/exec"
"sync/atomic"
"time"

"github.com/ethereum-optimism/supersim/utils"
"github.com/ethereum/go-ethereum/log"
)

Expand Down Expand Up @@ -58,13 +56,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 +106,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())

if err := a.cmd.Wait(); err != nil {
anvilLog.Error("anvil terminated with an error", "error", err)
} else {
Expand Down Expand Up @@ -141,3 +136,11 @@ 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) ChainId() uint64 {
return a.cfg.ChainId
}
38 changes: 36 additions & 2 deletions supersim.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package supersim
import (
_ "embed"
"fmt"
"strings"
"sync"
"time"

"context"

"github.com/ethereum-optimism/supersim/anvil"
"github.com/ethereum-optimism/supersim/utils"

"github.com/ethereum/go-ethereum/log"
)
Expand Down Expand Up @@ -41,8 +45,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 @@ -55,12 +59,27 @@ func (s *Supersim) Start(ctx context.Context) error {
return fmt.Errorf("l1 chain failed to start: %w", err)
}

var wg sync.WaitGroup
waitForAnvil := func(anvil *anvil.Anvil) {
defer wg.Done()
wg.Add(1)
utils.WaitForAnvilEndpointToBeReady(anvil.Endpoint(), 10*time.Second)
}

go waitForAnvil(s.l1Chain)

for _, l2Chain := range s.l2Chains {
if err := l2Chain.Start(ctx); err != nil {
return fmt.Errorf("l2 chain failed to start: %w", err)
}
go waitForAnvil(l2Chain)
}

wg.Wait()

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

return nil
}

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

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()
}
11 changes: 10 additions & 1 deletion supersim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

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

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

const (
Expand All @@ -33,7 +35,14 @@ func TestGenesisState(t *testing.T) {
}()

for _, l2ChainConfig := range DefaultConfig.l2Chains {
client, err := utils.WaitForAnvilClientToBeReady(fmt.Sprintf("http://127.0.0.1:%d", l2ChainConfig.Port), anvilClientTimeout)
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)
}

err := utils.WaitForAnvilClientToBeReady(client, anvilClientTimeout)
if err != nil {
t.Fatalf("Failed to connect to RPC server: %v", err)
}
Expand Down
35 changes: 23 additions & 12 deletions utils/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ package utils
import (
"context"
"fmt"
"net/http"
"strings"
"time"

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

func WaitForAnvilClientToBeReady(rpcUrl string, timeout time.Duration) (*rpc.Client, error) {
func WaitForAnvilEndpointToBeReady(endpoint string, timeout time.Duration) error {
client, clientCreateErr := rpc.Dial(endpoint)
if clientCreateErr != nil {
return fmt.Errorf("failed to create client: %v", clientCreateErr)
}

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

return nil
}

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

Expand All @@ -19,23 +33,20 @@ func WaitForAnvilClientToBeReady(rpcUrl string, timeout time.Duration) (*rpc.Cli
for {
select {
case <-ctx.Done():
return nil, fmt.Errorf("timed out waiting for response from %s", rpcUrl)
return fmt.Errorf("timed out waiting for response from client")
case <-ticker.C:
res, err := http.Get(rpcUrl)
var result string
callErr := client.Call(&result, "web3_clientVersion")

if err != nil {
fmt.Printf("Error making request: %v\n", err)
if callErr != nil {
continue
}
defer res.Body.Close()

client, err := rpc.Dial(rpcUrl)
if err != nil {
fmt.Printf("Error creating rpc client: %v\n", err)
continue
if strings.HasPrefix(result, "anvil") {
return nil
}

return client, nil
return fmt.Errorf("unexpected client version: %s", result)
}
}
}

0 comments on commit dad31ee

Please sign in to comment.