Skip to content

Commit

Permalink
feat: enable logging after anvil is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
jakim929 committed Jul 9, 2024
1 parent a30767b commit 55c0f5e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"editor.defaultFormatter": "golang.go"
"editor.defaultFormatter": "golang.go",
"go.lintTool": "golangci-lint",
"go.lintFlags": [
"--fast"
]
}
37 changes: 18 additions & 19 deletions anvil/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Config struct {
}

type Anvil struct {
RpcClient *rpc.Client

log log.Logger

cfg *Config
Expand Down Expand Up @@ -70,6 +72,7 @@ func (a *Anvil) Start(ctx context.Context) error {

// Prep args
args := []string{
"--silent",
"--host", host,
"--chain-id", fmt.Sprintf("%d", a.cfg.ChainId),
"--port", fmt.Sprintf("%d", a.cfg.Port),
Expand Down Expand Up @@ -110,14 +113,22 @@ func (a *Anvil) Start(ctx context.Context) error {
return fmt.Errorf("failed to start anvil: %w", err)
}

rpcClient, err := rpc.Dial(a.Endpoint())
if err != nil {
return fmt.Errorf("failed to create RPC client: %w", err)
}
a.RpcClient = rpcClient

go func() {
defer os.Remove(tempFile.Name())
defer a.RpcClient.Close()

if err := a.cmd.Wait(); err != nil {
anvilLog.Error("anvil terminated with an error", "error", err)
} else {
anvilLog.Info("anvil terminated")
}

a.stoppedCh <- struct{}{}
}()

Expand Down Expand Up @@ -145,31 +156,19 @@ 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)
func (a *Anvil) EnableLogging() {
var result string
if err := a.RpcClient.Call(&result, "anvil_setLoggingEnabled", true); err != nil {
a.log.Error("failed to enable logging", "error", err)
}

return nil
}

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

ticker := time.NewTicker(100 * time.Millisecond)
Expand All @@ -183,7 +182,7 @@ func waitForAnvilClientToBeReady(ctx context.Context, client *rpc.Client, timeou
return fmt.Errorf("timed out waiting for response from client")
case <-ticker.C:
var result string
callErr := client.Call(&result, "web3_clientVersion")
callErr := a.RpcClient.Call(&result, "web3_clientVersion")

if callErr != nil {
continue
Expand Down
25 changes: 19 additions & 6 deletions supersim.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func (s *Supersim) Start(ctx context.Context) error {
return fmt.Errorf("supersim failed to get ready: %w", err)
}

s.EnableLogging()

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

Expand Down Expand Up @@ -165,19 +167,30 @@ func (s *Supersim) WaitUntilReady() error {
handleErr(anvil.WaitUntilReady(ctx))
}

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

for _, l2Anvil := range s.l2Anvils {
s.IterateChains(func(chain *anvil.Anvil) {
wg.Add(1)
go waitForAnvil(l2Anvil)
}
go waitForAnvil(chain)
})

wg.Wait()

return err
}

func (s *Supersim) EnableLogging() {
s.IterateChains(func(chain *anvil.Anvil) {
chain.EnableLogging()
})
}

func (s *Supersim) IterateChains(fn func(anvil *anvil.Anvil)) {
fn(s.l1Anvil)

for _, l2Anvil := range s.l2Anvils {
fn(l2Anvil)
}
}

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

Expand Down

0 comments on commit 55c0f5e

Please sign in to comment.