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: enable logging after anvil is ready #35

Merged
merged 1 commit into from
Jul 10, 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
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
Loading