Skip to content

Commit

Permalink
first uses of caution
Browse files Browse the repository at this point in the history
  • Loading branch information
facuMH committed Dec 16, 2024
1 parent 4846231 commit 58528a8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 42 deletions.
26 changes: 15 additions & 11 deletions cmd/sonictool/app/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,46 @@ 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"
"github.com/ethereum/go-ethereum/log"
"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 == "" {
return fmt.Errorf("--%s need to be set", flags.DataDirFlag.Name)
}

// 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)
Expand All @@ -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 {
Expand Down
14 changes: 9 additions & 5 deletions cmd/sonictool/app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 == "" {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions cmd/sonictool/app/compact.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -38,27 +39,27 @@ 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)

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) {
Expand Down
24 changes: 14 additions & 10 deletions cmd/sonictool/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
27 changes: 15 additions & 12 deletions cmd/sonictool/app/export_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}

0 comments on commit 58528a8

Please sign in to comment.