Skip to content

Commit

Permalink
add usage for caution utils and catch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
facuMH committed Dec 16, 2024
1 parent b554d33 commit 9626eab
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 129 deletions.
27 changes: 14 additions & 13 deletions cmd/sonictool/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Fantom-foundation/go-opera/integration/makefakegenesis"
"github.com/Fantom-foundation/go-opera/opera/genesisstore"
futils "github.com/Fantom-foundation/go-opera/utils"
"github.com/Fantom-foundation/go-opera/utils/caution"
"github.com/Fantom-foundation/go-opera/utils/memory"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
Expand All @@ -31,7 +32,7 @@ var (
}
)

func gfileGenesisImport(ctx *cli.Context) error {
func gfileGenesisImport(ctx *cli.Context) (err error) {
if len(ctx.Args()) < 1 {
return fmt.Errorf("this command requires an argument - the genesis file to import")
}
Expand All @@ -41,24 +42,24 @@ func gfileGenesisImport(ctx *cli.Context) error {
}
validatorMode, err := isValidatorModeSet(ctx)
if err != nil {
return err
return
}
cacheRatio, err := cacheScaler(ctx)
if err != nil {
return err
return
}

genesisReader, err := os.Open(ctx.Args().First())
if err != nil {
return fmt.Errorf("failed to open the genesis file: %w", err)
}
defer genesisReader.Close()
// note, genesisStore closes the reader, no need to defer close it here

genesisStore, genesisHashes, err := genesisstore.OpenGenesisStore(genesisReader)
if err != nil {
return fmt.Errorf("failed to read genesis file: %w", err)
}
defer genesisStore.Close()
defer caution.CloseAndReportError(&err, genesisStore, "failed to close the genesis store")
if err := genesis.IsGenesisTrusted(genesisStore, genesisHashes); err != nil {
if ctx.IsSet(ExperimentalFlag.Name) {
log.Warn("Experimental genesis file is used", "err", err)
Expand All @@ -76,7 +77,7 @@ func gfileGenesisImport(ctx *cli.Context) error {
})
}

func jsonGenesisImport(ctx *cli.Context) error {
func jsonGenesisImport(ctx *cli.Context) (err error) {
if len(ctx.Args()) < 1 {
return fmt.Errorf("this command requires an argument - the genesis file to import")
}
Expand All @@ -89,11 +90,11 @@ func jsonGenesisImport(ctx *cli.Context) error {
}
validatorMode, err := isValidatorModeSet(ctx)
if err != nil {
return err
return
}
cacheRatio, err := cacheScaler(ctx)
if err != nil {
return err
return
}

genesisJson, err := makefakegenesis.LoadGenesisJson(ctx.Args().First())
Expand All @@ -105,7 +106,7 @@ func jsonGenesisImport(ctx *cli.Context) error {
if err != nil {
return fmt.Errorf("failed to prepare JSON genesis: %w", err)
}
defer genesisStore.Close()
defer caution.CloseAndReportError(&err, genesisStore, "failed to close the genesis store")
return genesis.ImportGenesisStore(genesis.ImportParams{
GenesisStore: genesisStore,
DataDir: dataDir,
Expand All @@ -116,7 +117,7 @@ func jsonGenesisImport(ctx *cli.Context) error {
})
}

func fakeGenesisImport(ctx *cli.Context) error {
func fakeGenesisImport(ctx *cli.Context) (err error) {
if len(ctx.Args()) < 1 {
return fmt.Errorf("this command requires an argument - the number of validators in the fake network")
}
Expand All @@ -133,19 +134,19 @@ func fakeGenesisImport(ctx *cli.Context) error {
}
validatorMode, err := isValidatorModeSet(ctx)
if err != nil {
return err
return
}
cacheRatio, err := cacheScaler(ctx)
if err != nil {
return err
return
}

genesisStore := makefakegenesis.FakeGenesisStore(
idx.Validator(validatorsNumber),
futils.ToFtm(1000000000),
futils.ToFtm(5000000),
)
defer genesisStore.Close()
defer caution.CloseAndReportError(&err, genesisStore, "failed to close the genesis store")
return genesis.ImportGenesisStore(genesis.ImportParams{
GenesisStore: genesisStore,
DataDir: dataDir,
Expand Down
9 changes: 5 additions & 4 deletions cmd/sonictool/app/heal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/Fantom-foundation/go-opera/cmd/sonictool/db"
"github.com/Fantom-foundation/go-opera/config"
"github.com/Fantom-foundation/go-opera/config/flags"
"github.com/Fantom-foundation/go-opera/utils/caution"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/ethereum/go-ethereum/log"
"gopkg.in/urfave/cli.v1"
Expand Down Expand Up @@ -93,7 +94,7 @@ func heal(ctx *cli.Context) error {
return nil
}

func healLiveFromArchive(ctx context.Context, carmenLiveDir, carmenArchiveDir string, recoveredBlock idx.Block) error {
func healLiveFromArchive(ctx context.Context, carmenLiveDir, carmenArchiveDir string, recoveredBlock idx.Block) (err error) {
if err := os.RemoveAll(carmenLiveDir); err != nil {
return fmt.Errorf("failed to remove broken live state: %w", err)
}
Expand All @@ -102,7 +103,7 @@ func healLiveFromArchive(ctx context.Context, carmenLiveDir, carmenArchiveDir st
}

reader, writer := io.Pipe()
defer reader.Close()
defer caution.CloseAndReportError(&err, reader, "failed to close reader")
bufReader := bufio.NewReaderSize(reader, 100*1024*1024) // 100 MiB
bufWriter := bufio.NewWriterSize(writer, 100*1024*1024) // 100 MiB

Expand All @@ -111,14 +112,14 @@ func healLiveFromArchive(ctx context.Context, carmenLiveDir, carmenArchiveDir st
wg.Add(1)
go func() {
defer wg.Done()
defer writer.Close()
defer caution.CloseAndReportError(&err, writer, "failed to close writer")
exportErr = mptio.ExportBlockFromArchive(ctx, mptio.NewLog(), carmenArchiveDir, bufWriter, uint64(recoveredBlock))
if exportErr == nil {
exportErr = bufWriter.Flush()
}
}()

err := mptio.ImportLiveDb(mptio.NewLog(), carmenLiveDir, bufReader)
err = mptio.ImportLiveDb(mptio.NewLog(), carmenLiveDir, bufReader)

wg.Wait()
return errors.Join(err, exportErr)
Expand Down
8 changes: 5 additions & 3 deletions cmd/sonictool/app/sign_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package app

import (
"fmt"
"os"

"github.com/Fantom-foundation/go-opera/cmd/sonictool/genesis"
ogenesis "github.com/Fantom-foundation/go-opera/opera/genesis"
"github.com/Fantom-foundation/go-opera/opera/genesisstore"
"github.com/Fantom-foundation/go-opera/utils/caution"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
"gopkg.in/urfave/cli.v1"
"os"
)

func signGenesis(ctx *cli.Context) error {
Expand Down Expand Up @@ -59,16 +61,16 @@ func signGenesis(ctx *cli.Context) error {

func getGenesisHeaderHashes(genesisFile string) (ogenesis.Header, ogenesis.Hashes, error) {
genesisReader, err := os.Open(genesisFile)
// note, genesisStore closes the reader, no need to defer close it here
if err != nil {
return ogenesis.Header{}, nil, fmt.Errorf("failed to open the genesis file: %w", err)
}
defer genesisReader.Close()

genesisStore, genesisHashes, err := genesisstore.OpenGenesisStore(genesisReader)
if err != nil {
return ogenesis.Header{}, nil, fmt.Errorf("failed to read genesis file: %w", err)
}
defer genesisStore.Close()
defer caution.CloseAndReportError(&err, genesisStore, "failed to close the genesis store")

return genesisStore.Header(), genesisHashes, nil
}
16 changes: 9 additions & 7 deletions cmd/sonictool/chain/export_events.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package chain

import (
"github.com/Fantom-foundation/go-opera/cmd/sonictool/db"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
"io"
"path/filepath"
"time"

"github.com/Fantom-foundation/go-opera/cmd/sonictool/db"
"github.com/Fantom-foundation/go-opera/utils/caution"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"

"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -28,24 +30,24 @@ func ExportEvents(gdbParams db.GossipDbParameters, w io.Writer, from, to idx.Epo
chaindataDir := filepath.Join(gdbParams.DataDir, "chaindata")
dbs, err := db.MakeDbProducer(chaindataDir, cachescale.Identity)
if err != nil {
return err
return
}
defer dbs.Close()
defer caution.CloseAndReportError(&err, dbs, "failed to close db producer")

// Fill the rest of the params
gdbParams.Dbs = dbs
gdbParams.CacheRatio = cachescale.Identity

gdb, err := db.MakeGossipDb(gdbParams)
if err != nil {
return err
return
}
defer gdb.Close()
defer caution.CloseAndReportError(&err, gdb, "failed to close gossip db")

// Write header and version
_, err = w.Write(append(eventsFileHeader, eventsFileVersion...))
if err != nil {
return err
return
}

start, reported := time.Now(), time.Time{}
Expand Down
21 changes: 11 additions & 10 deletions cmd/sonictool/chain/import_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/Fantom-foundation/go-opera/gossip"
"github.com/Fantom-foundation/go-opera/gossip/emitter"
"github.com/Fantom-foundation/go-opera/inter"
"github.com/Fantom-foundation/go-opera/utils/caution"
"github.com/Fantom-foundation/go-opera/utils/ioread"
"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
Expand Down Expand Up @@ -85,26 +86,26 @@ func checkEventsFileHeader(reader io.Reader) error {
return nil
}

func importEventsFile(srv *gossip.Service, fn string) error {
func importEventsFile(srv *gossip.Service, filename string) (err error) {
// Watch for Ctrl-C while the import is running.
// If a signal is received, the import will stop.
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(interrupt)

// Open the file handle and potentially unwrap the gzip stream
fh, err := os.Open(fn)
fileHandle, err := os.Open(filename)
if err != nil {
return err
}
defer fh.Close()
defer caution.CloseAndReportError(&err, fileHandle, "failed to close file")

var reader io.Reader = fh
if strings.HasSuffix(fn, ".gz") {
var reader io.Reader = fileHandle
if strings.HasSuffix(filename, ".gz") {
if reader, err = gzip.NewReader(reader); err != nil {
return err
}
defer reader.(*gzip.Reader).Close()
defer caution.CloseAndReportError(&err, reader.(*gzip.Reader), "failed to close gzip reader")
}

// Check file version and header
Expand Down Expand Up @@ -153,17 +154,17 @@ func importEventsFile(srv *gossip.Service, fn string) error {
if err == io.EOF {
err = processBatch()
if err != nil {
return err
return
}
break
}
if err != nil {
return err
return
}
if e.Epoch() != epoch || batchSize >= maxBatchSize {
err = processBatch()
if err != nil {
return err
return
}
}
epoch = e.Epoch()
Expand All @@ -173,7 +174,7 @@ func importEventsFile(srv *gossip.Service, fn string) error {
events++
}
srv.WaitBlockEnd()
log.Info("Events import is finished", "file", fn, "last", last.String(), "imported", events, "txs", txs, "elapsed", common.PrettyDuration(time.Since(start)))
log.Info("Events import is finished", "file", filename, "last", last.String(), "imported", events, "txs", txs, "elapsed", common.PrettyDuration(time.Since(start)))

return nil
}
11 changes: 6 additions & 5 deletions cmd/sonictool/check/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Fantom-foundation/Carmen/go/database/mpt"
"github.com/Fantom-foundation/Carmen/go/database/mpt/io"
carmen "github.com/Fantom-foundation/Carmen/go/state"
"github.com/Fantom-foundation/go-opera/utils/caution"
"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
Expand All @@ -33,13 +34,13 @@ func CheckArchiveStateDb(ctx context.Context, dataDir string, cacheRatio cachesc
return nil
}

func checkArchiveBlockRoots(dataDir string, cacheRatio cachescale.Func) error {
func checkArchiveBlockRoots(dataDir string, cacheRatio cachescale.Func) (err error) {
gdb, dbs, err := createGdb(dataDir, cacheRatio, carmen.S5Archive, false)
if err != nil {
return err
return fmt.Errorf("failed to create gdb and db producer: %w", err)
}
defer gdb.Close()
defer dbs.Close()
defer caution.CloseAndReportError(&err, gdb, "failed to close gossip db")
defer caution.CloseAndReportError(&err, dbs, "failed to close db producer")

invalidBlocks := 0
lastBlockIdx := gdb.GetLatestBlockIndex()
Expand All @@ -61,5 +62,5 @@ func checkArchiveBlockRoots(dataDir string, cacheRatio cachescale.Func) error {
return fmt.Errorf("block root verification failed for %d blocks (from %d total blocks)", invalidBlocks, lastBlockIdx)
}
log.Info("Block root verification OK for all blocks", "blocks", lastBlockIdx)
return nil
return
}
11 changes: 6 additions & 5 deletions cmd/sonictool/check/live.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Fantom-foundation/Carmen/go/database/mpt"
"github.com/Fantom-foundation/Carmen/go/database/mpt/io"
carmen "github.com/Fantom-foundation/Carmen/go/state"
"github.com/Fantom-foundation/go-opera/utils/caution"
"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
"github.com/ethereum/go-ethereum/log"
Expand All @@ -32,13 +33,13 @@ func CheckLiveStateDb(ctx context.Context, dataDir string, cacheRatio cachescale
return nil
}

func checkLiveBlockRoot(dataDir string, cacheRatio cachescale.Func) error {
func checkLiveBlockRoot(dataDir string, cacheRatio cachescale.Func) (err error) {
gdb, dbs, err := createGdb(dataDir, cacheRatio, carmen.NoArchive, true)
if err != nil {
return err
return fmt.Errorf("failed to create gdb and db producer: %w", err)
}
defer gdb.Close()
defer dbs.Close()
defer caution.CloseAndReportError(&err, gdb, "failed to close gossip db")
defer caution.CloseAndReportError(&err, dbs, "failed to close db producer")

lastBlockIdx := gdb.GetLatestBlockIndex()
lastBlock := gdb.GetBlock(lastBlockIdx)
Expand All @@ -50,5 +51,5 @@ func checkLiveBlockRoot(dataDir string, cacheRatio cachescale.Func) error {
return fmt.Errorf("checking live state failed: %w", err)
}
log.Info("Live block root verification OK", "block", lastBlockIdx)
return nil
return
}
Loading

0 comments on commit 9626eab

Please sign in to comment.