Skip to content

Commit

Permalink
fix: remove temp file on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jakim929 committed Sep 19, 2024
1 parent 7414d10 commit 087a64f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
39 changes: 35 additions & 4 deletions anvil/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math/big"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -93,6 +94,8 @@ func (a *Anvil) Start(ctx context.Context) error {

if len(a.cfg.GenesisJSON) > 0 && a.cfg.ForkConfig == nil {
tempFile, err := os.CreateTemp("", "genesis-*.json")
defer a.removeFile(tempFile)

if err != nil {
return fmt.Errorf("error creating temporary genesis file: %w", err)
}
Expand Down Expand Up @@ -120,10 +123,32 @@ func (a *Anvil) Start(ctx context.Context) error {
// and see what port anvil eventually binds to when started
anvilPortCh := make(chan uint64)

// Handle stdout/stderr
logFile, err := os.CreateTemp("", fmt.Sprintf("anvil-chain-%d-", a.cfg.ChainID))
if err != nil {
return fmt.Errorf("failed to create temp log file: %w", err)
var logFile *os.File

// Empty LogsDirectory defaults to temp file
if a.cfg.LogsDirectory == "" {
tempLogFile, err := os.CreateTemp("", fmt.Sprintf("anvil-chain-%d-", a.cfg.ChainID))
if err != nil {
return fmt.Errorf("failed to create temp log file: %w", err)
}

logFile = tempLogFile
// Clean up the temp log file
defer a.removeFile(logFile)
} else {
// Expand the path to the log file
absFilePath, err := filepath.Abs(fmt.Sprintf("%s/anvil-%d.log", a.cfg.LogsDirectory, a.cfg.ChainID))
if err != nil {
return fmt.Errorf("failed to expand path: %w", err)
}

// Handle logs in the specified directory
specifiedLogFile, err := os.Create(absFilePath)
if err != nil {
return fmt.Errorf("failed to create log file: %w", err)
}
logFile = specifiedLogFile
// Don't delete the log file if directory is specified
}

a.logFilePath = logFile.Name()
Expand Down Expand Up @@ -301,3 +326,9 @@ func (a *Anvil) SimulatedLogs(ctx context.Context, tx *types.Transaction) ([]typ

return logs, err
}

func (a *Anvil) removeFile(file *os.File) {
if err := os.Remove(file.Name()); err != nil {
a.log.Warn("failed to remove temp genesis file", "file.path", file.Name(), "err", err)
}
}
8 changes: 7 additions & 1 deletion config/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type ChainConfig struct {

// Optional
StartingTimestamp uint64

// Optional
LogsDirectory string
}

type NetworkConfig struct {
Expand Down Expand Up @@ -90,7 +93,7 @@ type Chain interface {
Stop(ctx context.Context) error
}

func GetDefaultNetworkConfig(startingTimestamp uint64) NetworkConfig {
func GetDefaultNetworkConfig(startingTimestamp uint64, logsDirectory string) NetworkConfig {
return NetworkConfig{
// Enabled by default as it is included in genesis
InteropEnabled: true,
Expand All @@ -101,6 +104,7 @@ func GetDefaultNetworkConfig(startingTimestamp uint64) NetworkConfig {
SecretsConfig: DefaultSecretsConfig,
GenesisJSON: genesis.GeneratedGenesisDeployment.L1.GenesisJSON,
StartingTimestamp: startingTimestamp,
LogsDirectory: logsDirectory,
},
L2Configs: []ChainConfig{
{
Expand All @@ -114,6 +118,7 @@ func GetDefaultNetworkConfig(startingTimestamp uint64) NetworkConfig {
DependencySet: []uint64{genesis.GeneratedGenesisDeployment.L2s[1].ChainID},
},
StartingTimestamp: startingTimestamp,
LogsDirectory: logsDirectory,
},
{
Name: "OPChainB",
Expand All @@ -126,6 +131,7 @@ func GetDefaultNetworkConfig(startingTimestamp uint64) NetworkConfig {
DependencySet: []uint64{genesis.GeneratedGenesisDeployment.L2s[0].ChainID},
},
StartingTimestamp: startingTimestamp,
LogsDirectory: logsDirectory,
},
},
}
Expand Down
14 changes: 13 additions & 1 deletion config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
NetworkFlagName = "network"
L2StartingPortFlagName = "l2.starting.port"

LogsDirectoryFlagName = "logs.directory"

InteropEnabledFlagName = "interop.enabled"
InteropAutoRelayFlagName = "interop.autorelay"
)
Expand All @@ -43,7 +45,13 @@ func BaseCLIFlags(envPrefix string) []cli.Flag {
Name: InteropAutoRelayFlagName,
Value: false,
Usage: "Automatically relay messages sent to the L2ToL2CrossDomainMessenger using account 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720",
EnvVars: opservice.PrefixEnvVar(envPrefix, "AUTORELAY"),
EnvVars: opservice.PrefixEnvVar(envPrefix, "INTEROP_AUTORELAY"),
},
&cli.StringFlag{
Name: LogsDirectoryFlagName,
Usage: "Directory to store logs",
Value: "",
EnvVars: opservice.PrefixEnvVar(envPrefix, "LOGS_DIRECTORY"),
},
}
}
Expand Down Expand Up @@ -93,6 +101,8 @@ type CLIConfig struct {

InteropAutoRelay bool

LogsDirectory string

ForkConfig *ForkCLIConfig
}

Expand All @@ -102,6 +112,8 @@ func ReadCLIConfig(ctx *cli.Context) (*CLIConfig, error) {
L2StartingPort: ctx.Uint64(L2StartingPortFlagName),

InteropAutoRelay: ctx.Bool(InteropAutoRelayFlagName),

LogsDirectory: ctx.String(LogsDirectoryFlagName),
}

if ctx.Command.Name == ForkCommandName {
Expand Down
2 changes: 1 addition & 1 deletion orchestrator/orchestrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type TestSuite struct {
}

func createTestSuite(t *testing.T) *TestSuite {
networkConfig := config.GetDefaultNetworkConfig(uint64(time.Now().Unix()))
networkConfig := config.GetDefaultNetworkConfig(uint64(time.Now().Unix()), "")
testlog := testlog.Logger(t, log.LevelInfo)

ctx, closeApp := context.WithCancelCause(context.Background())
Expand Down
2 changes: 1 addition & 1 deletion supersim.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Supersim struct {
}

func NewSupersim(log log.Logger, envPrefix string, closeApp context.CancelCauseFunc, cliConfig *config.CLIConfig) (*Supersim, error) {
networkConfig := config.GetDefaultNetworkConfig(uint64(time.Now().Unix()))
networkConfig := config.GetDefaultNetworkConfig(uint64(time.Now().Unix()), cliConfig.LogsDirectory)

// If Forking, override the network config with the generated fork config
if cliConfig.ForkConfig != nil {
Expand Down

0 comments on commit 087a64f

Please sign in to comment.