Commit a370ecf3 authored by Adrian Sutton's avatar Adrian Sutton

op-node: Close file before renaming.

Improve commenting/organisation.
parent 4352684f
...@@ -54,6 +54,8 @@ var ( ...@@ -54,6 +54,8 @@ var (
Usage: "RPC listening port", Usage: "RPC listening port",
EnvVars: prefixEnvVars("RPC_PORT"), EnvVars: prefixEnvVars("RPC_PORT"),
} }
/* Optional Flags */
RPCEnableAdmin = &cli.BoolFlag{ RPCEnableAdmin = &cli.BoolFlag{
Name: "rpc.enable-admin", Name: "rpc.enable-admin",
Usage: "Enable the admin API (experimental)", Usage: "Enable the admin API (experimental)",
...@@ -64,8 +66,6 @@ var ( ...@@ -64,8 +66,6 @@ var (
Usage: "File path used to persist state changes made via the admin API so they persist across restarts", Usage: "File path used to persist state changes made via the admin API so they persist across restarts",
EnvVars: prefixEnvVars("RPC_ADMIN_STATE"), EnvVars: prefixEnvVars("RPC_ADMIN_STATE"),
} }
/* Optional Flags */
L1TrustRPC = &cli.BoolFlag{ L1TrustRPC = &cli.BoolFlag{
Name: "l1.trustrpc", Name: "l1.trustrpc",
Usage: "Trust the L1 RPC, sync faster at risk of malicious/buggy RPC providing bad or inconsistent L1 data", Usage: "Trust the L1 RPC, sync faster at risk of malicious/buggy RPC providing bad or inconsistent L1 data",
......
...@@ -69,7 +69,7 @@ func (p *ActiveConfigPersistence) persist(sequencerStarted bool) error { ...@@ -69,7 +69,7 @@ func (p *ActiveConfigPersistence) persist(sequencerStarted bool) error {
if err != nil { if err != nil {
return fmt.Errorf("open file (%v) for writing: %w", tmpFile, err) return fmt.Errorf("open file (%v) for writing: %w", tmpFile, err)
} }
defer file.Close() defer file.Close() // Ensure file is closed even if write or sync fails
_, err = file.Write(data) _, err = file.Write(data)
if err != nil { if err != nil {
return fmt.Errorf("write new config to temp file (%v): %w", tmpFile, err) return fmt.Errorf("write new config to temp file (%v): %w", tmpFile, err)
...@@ -77,7 +77,9 @@ func (p *ActiveConfigPersistence) persist(sequencerStarted bool) error { ...@@ -77,7 +77,9 @@ func (p *ActiveConfigPersistence) persist(sequencerStarted bool) error {
if err := file.Sync(); err != nil { if err := file.Sync(); err != nil {
return fmt.Errorf("sync new config temp file (%v): %w", tmpFile, err) return fmt.Errorf("sync new config temp file (%v): %w", tmpFile, err)
} }
if err := file.Close(); err != nil {
return fmt.Errorf("close new config temp file (%v): %w", tmpFile, err)
}
// Rename to replace the previous file // Rename to replace the previous file
if err := os.Rename(tmpFile, p.file); err != nil { if err := os.Rename(tmpFile, p.file); err != nil {
return fmt.Errorf("rename temp config file to final destination: %w", err) return fmt.Errorf("rename temp config file to final destination: %w", err)
...@@ -105,6 +107,7 @@ func (p *ActiveConfigPersistence) read() (persistedState, error) { ...@@ -105,6 +107,7 @@ func (p *ActiveConfigPersistence) read() (persistedState, error) {
defer p.lock.Unlock() defer p.lock.Unlock()
data, err := os.ReadFile(p.file) data, err := os.ReadFile(p.file)
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
// persistedState.SequencerStarted == nil: SequencerState() will return StateUnset if no state is found
return persistedState{}, nil return persistedState{}, nil
} else if err != nil { } else if err != nil {
return persistedState{}, fmt.Errorf("read config file (%v): %w", p.file, err) return persistedState{}, fmt.Errorf("read config file (%v): %w", p.file, err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment