diff --git a/cmd/sonictool/app/chain.go b/cmd/sonictool/app/chain.go index 9896502ce..e6047e13f 100644 --- a/cmd/sonictool/app/chain.go +++ b/cmd/sonictool/app/chain.go @@ -3,12 +3,14 @@ package app import ( "compress/gzip" "fmt" - "github.com/Fantom-foundation/go-opera/cmd/sonictool/db" "io" "os" "strconv" "strings" + "github.com/Fantom-foundation/go-opera/cmd/sonictool/db" + "github.com/Fantom-foundation/go-opera/utils/caution" + "github.com/Fantom-foundation/go-opera/cmd/sonictool/chain" "github.com/Fantom-foundation/go-opera/config/flags" "github.com/Fantom-foundation/lachesis-base/inter/idx" @@ -16,12 +18,12 @@ import ( "gopkg.in/urfave/cli.v1" ) -func exportEvents(ctx *cli.Context) error { +func exportEvents(ctx *cli.Context) (err error) { if len(ctx.Args()) < 1 { return fmt.Errorf("this command requires an argument - the output file") } - fn := ctx.Args().First() + filename := ctx.Args().First() dataDir := ctx.GlobalString(flags.DataDirFlag.Name) if dataDir == "" { @@ -29,16 +31,18 @@ func exportEvents(ctx *cli.Context) error { } // Open the file handle and potentially wrap with a gzip stream - fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) + fileHandler, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) if err != nil { - return err + return } - defer fh.Close() + defer caution.CloseAndReportError(&err, fileHandler, fmt.Sprintf("failed to close file %v", filename)) - var writer io.Writer = fh - if strings.HasSuffix(fn, ".gz") { + var writer io.Writer = fileHandler + if strings.HasSuffix(filename, ".gz") { writer = gzip.NewWriter(writer) - defer writer.(*gzip.Writer).Close() + defer caution.CloseAndReportError(&err, + writer.(*gzip.Writer), + fmt.Sprintf("failed to close gzip writer for file %v", filename)) } from := idx.Epoch(1) @@ -64,13 +68,13 @@ func exportEvents(ctx *cli.Context) error { ArchiveCache: ctx.GlobalInt64(flags.ArchiveCacheFlag.Name), } - log.Info("Exporting events to file", "file", fn) + log.Info("Exporting events to file", "file", filename) err = chain.ExportEvents(gdbParams, writer, from, to) if err != nil { return fmt.Errorf("export error: %w", err) } - return nil + return } func importEvents(ctx *cli.Context) error { diff --git a/cmd/sonictool/app/cli.go b/cmd/sonictool/app/cli.go index 424d505dc..a22512467 100644 --- a/cmd/sonictool/app/cli.go +++ b/cmd/sonictool/app/cli.go @@ -2,11 +2,13 @@ package app import ( "fmt" + "strings" + "github.com/Fantom-foundation/go-opera/config/flags" + "github.com/Fantom-foundation/go-opera/utils/caution" "github.com/ethereum/go-ethereum/console" "github.com/ethereum/go-ethereum/rpc" "gopkg.in/urfave/cli.v1" - "strings" ) var ( @@ -27,7 +29,7 @@ var ( // remoteConsole will connect to a remote opera instance, attaching a JavaScript // console to it. -func remoteConsole(ctx *cli.Context) error { +func remoteConsole(ctx *cli.Context) (err error) { // Attach to a remotely running opera instance and start the JavaScript console endpoint := ctx.Args().First() if endpoint == "" { @@ -57,18 +59,20 @@ func remoteConsole(ctx *cli.Context) error { if err != nil { return fmt.Errorf("failed to start the JavaScript console: %v", err) } - defer console.Stop(false) + defer caution.ExecuteAndReportError(&err, + func() error { return console.Stop(false) }, + "failed to stop the JavaScript console") if script := ctx.String(ExecFlag.Name); script != "" { console.Evaluate(script) - return nil + return } // Otherwise print the welcome screen and enter interactive mode console.Welcome() console.Interactive() - return nil + return } // makeConsolePreloads retrieves the absolute paths for the console JavaScript diff --git a/cmd/sonictool/app/compact.go b/cmd/sonictool/app/compact.go index df89811f6..2016e4411 100644 --- a/cmd/sonictool/app/compact.go +++ b/cmd/sonictool/app/compact.go @@ -6,6 +6,7 @@ import ( "github.com/Fantom-foundation/go-opera/config/flags" "github.com/Fantom-foundation/go-opera/integration" + "github.com/Fantom-foundation/go-opera/utils/caution" "github.com/Fantom-foundation/go-opera/utils/dbutil" "github.com/Fantom-foundation/go-opera/utils/dbutil/compactdb" "github.com/Fantom-foundation/lachesis-base/kvdb" @@ -38,13 +39,13 @@ func compactDbs(ctx *cli.Context) error { return nil } -func compactDB(name string, producer kvdb.DBProducer) error { +func compactDB(name string, producer kvdb.DBProducer) (err error) { db, err := producer.OpenDB(name) if err != nil { log.Error("Cannot open db or db does not exists", "db", name) return err } - defer db.Close() + defer caution.CloseAndReportError(&err, db, "failed to close db") log.Info("Stats before compaction", "db", name) showDbStats(db) @@ -52,13 +53,13 @@ func compactDB(name string, producer kvdb.DBProducer) error { err = compactdb.Compact(db, name, 64*opt.GiB) if err != nil { log.Error("Database compaction failed", "err", err) - return err + return } log.Info("Stats after compaction", "db", name) showDbStats(db) - return nil + return } func showDbStats(db ethdb.Stater) { diff --git a/cmd/sonictool/app/config.go b/cmd/sonictool/app/config.go index e19ad7d2f..ecb95a2b3 100644 --- a/cmd/sonictool/app/config.go +++ b/cmd/sonictool/app/config.go @@ -2,9 +2,11 @@ package app import ( "fmt" + "os" + "github.com/Fantom-foundation/go-opera/config" + "github.com/Fantom-foundation/go-opera/utils/caution" "gopkg.in/urfave/cli.v1" - "os" ) func checkConfig(ctx *cli.Context) error { @@ -17,28 +19,30 @@ func checkConfig(ctx *cli.Context) error { } // dumpConfig is the dumpconfig command. -func dumpConfig(ctx *cli.Context) error { +func dumpConfig(ctx *cli.Context) (err error) { cfg, err := config.MakeAllConfigs(ctx) if err != nil { - return err + return } comment := "" out, err := config.TomlSettings.Marshal(&cfg) if err != nil { - return err + return } dump := os.Stdout if ctx.NArg() > 0 { dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { - return err + return } - defer dump.Close() + defer caution.CloseAndReportError(&err, dump, "failed to close config file") } - dump.WriteString(comment) - dump.Write(out) - - return nil + _, err = dump.WriteString(comment) + if err != nil { + return + } + _, err = dump.Write(out) + return } diff --git a/cmd/sonictool/app/export_genesis.go b/cmd/sonictool/app/export_genesis.go index 6dbeae51e..0d6a32908 100644 --- a/cmd/sonictool/app/export_genesis.go +++ b/cmd/sonictool/app/export_genesis.go @@ -3,20 +3,22 @@ package app import ( "context" "fmt" + "os" + "os/signal" + "path" + "path/filepath" + "syscall" + "github.com/Fantom-foundation/go-opera/cmd/sonictool/db" "github.com/Fantom-foundation/go-opera/cmd/sonictool/genesis" "github.com/Fantom-foundation/go-opera/config/flags" "github.com/Fantom-foundation/go-opera/integration" + "github.com/Fantom-foundation/go-opera/utils/caution" "github.com/syndtr/goleveldb/leveldb/opt" "gopkg.in/urfave/cli.v1" - "os" - "os/signal" - "path" - "path/filepath" - "syscall" ) -func exportGenesis(ctx *cli.Context) error { +func exportGenesis(ctx *cli.Context) (err error) { dataDir := ctx.GlobalString(flags.DataDirFlag.Name) if dataDir == "" { return fmt.Errorf("--%s need to be set", flags.DataDirFlag.Name) @@ -45,7 +47,7 @@ func exportGenesis(ctx *cli.Context) error { if err != nil { return fmt.Errorf("failed to make DB producer: %v", err) } - defer dbs.Close() + defer caution.CloseAndReportError(&err, dbs, "failed to close DB producer") gdb, err := db.MakeGossipDb(db.GossipDbParameters{ Dbs: dbs, @@ -58,17 +60,18 @@ func exportGenesis(ctx *cli.Context) error { if err != nil { return err } - defer gdb.Close() + defer caution.CloseAndReportError(&err, gdb, "failed to close Gossip DB") - fh, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) + fileHandler, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) if err != nil { return err } - defer fh.Close() + defer caution.CloseAndReportError(&err, fileHandler, fmt.Sprintf("failed to close file %v", fileName)) tmpPath := path.Join(dataDir, "tmp-genesis-export") _ = os.RemoveAll(tmpPath) - defer os.RemoveAll(tmpPath) + defer caution.ExecuteAndReportError(&err, func() error { return os.RemoveAll(tmpPath) }, + "failed to remove tmp genesis export dir") - return genesis.ExportGenesis(cancelCtx, gdb, !forValidatorMode, fh, tmpPath) + return genesis.ExportGenesis(cancelCtx, gdb, !forValidatorMode, fileHandler, tmpPath) }