Skip to content

Commit

Permalink
feat: startup logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jakim929 committed Jun 28, 2024
1 parent 6f5f3d3 commit 0f49ef6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
17 changes: 8 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: %v", err)
return fmt.Errorf("error creating temporary genesis file: %v", err)
}
defer os.Remove(tempFile.Name())

_, err = tempFile.Write(a.cfg.Genesis)
if err != nil {
return fmt.Errorf("Error writing to genesis file: %v", err)
return fmt.Errorf("error writing to genesis file: %v", 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,7 @@ 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)
}
21 changes: 19 additions & 2 deletions supersim.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package supersim
import (
_ "embed"
"fmt"
"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 +44,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 +58,26 @@ 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()

print("Anvils are ready")

return nil
}

Expand Down
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 @@ -27,7 +29,14 @@ func TestGenesisState(t *testing.T) {
defer supersim.Stop(context.Background())

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
34 changes: 23 additions & 11 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,22 +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:
_, 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
}

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 0f49ef6

Please sign in to comment.