From 55c0f5ebb17f23a567a6284b14233d5e16ca8d22 Mon Sep 17 00:00:00 2001 From: James Kim Date: Tue, 9 Jul 2024 13:56:59 -0400 Subject: [PATCH] feat: enable logging after anvil is ready --- .vscode/settings.json | 6 +++++- anvil/anvil.go | 37 ++++++++++++++++++------------------- supersim.go | 25 +++++++++++++++++++------ 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index ca1751b9..4d8284fe 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,7 @@ { - "editor.defaultFormatter": "golang.go" + "editor.defaultFormatter": "golang.go", + "go.lintTool": "golangci-lint", + "go.lintFlags": [ + "--fast" + ] } \ No newline at end of file diff --git a/anvil/anvil.go b/anvil/anvil.go index 46f2bab7..a26da624 100644 --- a/anvil/anvil.go +++ b/anvil/anvil.go @@ -23,6 +23,8 @@ type Config struct { } type Anvil struct { + RpcClient *rpc.Client + log log.Logger cfg *Config @@ -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), @@ -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{}{} }() @@ -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) @@ -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 diff --git a/supersim.go b/supersim.go index 81f1b692..483cc038 100644 --- a/supersim.go +++ b/supersim.go @@ -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()) @@ -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