Skip to content

Commit

Permalink
Address reviewers feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
cabrador committed Nov 28, 2024
1 parent 40b7bbc commit e65e7cc
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 47 deletions.
20 changes: 9 additions & 11 deletions cmd/sonicd/app/run_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package app

import (
"flag"
"fmt"
"gopkg.in/urfave/cli.v1"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
"os"
"strings"
"testing"
Expand All @@ -13,8 +12,6 @@ import (
"github.com/Fantom-foundation/go-opera/integration/makefakegenesis"
futils "github.com/Fantom-foundation/go-opera/utils"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"

"github.com/docker/docker/pkg/reexec"
"github.com/ethereum/go-ethereum/common"

Expand All @@ -28,13 +25,14 @@ func tmpdir(t *testing.T) string {
func initFakenetDatadir(dataDir string, validatorsNum idx.Validator) {
genesisStore := makefakegenesis.FakeGenesisStore(validatorsNum, futils.ToFtm(1000000000), futils.ToFtm(5000000))
defer genesisStore.Close()
if err := genesis.ImportGenesisStore(
cli.NewContext(cli.NewApp(), new(flag.FlagSet), nil),
genesisStore,
dataDir,
false,
cachescale.Identity,
); err != nil {

if err := genesis.ImportGenesisStore(genesis.ImportParams{
GenesisStore: genesisStore,
DataDir: dataDir,
CacheRatio: cachescale.Identity,
LiveDbCache: 1, // Set lowest cache
ArchiveCache: 1, // Set lowest cache
}); err != nil {
panic(err)
}
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/sonictool/app/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"compress/gzip"
"fmt"
"github.com/Fantom-foundation/go-opera/cmd/sonictool/db"
"io"
"os"
"strconv"
Expand Down Expand Up @@ -57,8 +58,14 @@ func exportEvents(ctx *cli.Context) error {
to = idx.Epoch(n)
}

gdbParams := db.GossipDbParameters{
DataDir: dataDir,
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
}

log.Info("Exporting events to file", "file", fn)
err = chain.ExportEvents(ctx, writer, dataDir, from, to)
err = chain.ExportEvents(gdbParams, writer, dataDir, from, to)
if err != nil {
return fmt.Errorf("export error: %w", err)
}
Expand Down
27 changes: 24 additions & 3 deletions cmd/sonictool/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ func gfileGenesisImport(ctx *cli.Context) error {
return fmt.Errorf("genesis file check failed: %w", err)
}
}
return genesis.ImportGenesisStore(ctx, genesisStore, dataDir, validatorMode, cacheRatio)
return genesis.ImportGenesisStore(genesis.ImportParams{
GenesisStore: genesisStore,
DataDir: dataDir,
ValidatorMode: validatorMode,
CacheRatio: cacheRatio,
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
})
}

func jsonGenesisImport(ctx *cli.Context) error {
Expand Down Expand Up @@ -98,7 +105,14 @@ func jsonGenesisImport(ctx *cli.Context) error {
return fmt.Errorf("failed to prepare JSON genesis: %w", err)
}
defer genesisStore.Close()
return genesis.ImportGenesisStore(ctx, genesisStore, dataDir, validatorMode, cacheRatio)
return genesis.ImportGenesisStore(genesis.ImportParams{
GenesisStore: genesisStore,
DataDir: dataDir,
ValidatorMode: validatorMode,
CacheRatio: cacheRatio,
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
})
}

func fakeGenesisImport(ctx *cli.Context) error {
Expand Down Expand Up @@ -127,7 +141,14 @@ func fakeGenesisImport(ctx *cli.Context) error {

genesisStore := makefakegenesis.FakeGenesisStore(idx.Validator(validatorsNumber), futils.ToFtm(1000000000), futils.ToFtm(5000000))
defer genesisStore.Close()
return genesis.ImportGenesisStore(ctx, genesisStore, dataDir, validatorMode, cacheRatio)
return genesis.ImportGenesisStore(genesis.ImportParams{
GenesisStore: genesisStore,
DataDir: dataDir,
ValidatorMode: validatorMode,
CacheRatio: cacheRatio,
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
})
}

func isValidatorModeSet(ctx *cli.Context) (bool, error) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/sonictool/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func Run() error {
app.Flags = []cli.Flag{
flags.DataDirFlag,
flags.CacheFlag,
flags.LiveDbCacheFlag,
flags.ArchiveCacheFlag,
}
app.Commands = []cli.Command{
{
Expand Down Expand Up @@ -60,8 +62,6 @@ Initialize the database using data from the experimental genesis file.
ArgsUsage: "<validators>",
Action: fakeGenesisImport,
Flags: []cli.Flag{
flags.LiveDbCacheFlag,
flags.ArchiveCacheFlag,
ModeFlag,
},
Description: `
Expand Down
17 changes: 6 additions & 11 deletions cmd/sonictool/chain/export_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package chain

import (
"github.com/Fantom-foundation/go-opera/cmd/sonictool/db"
"github.com/Fantom-foundation/go-opera/config/flags"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
"gopkg.in/urfave/cli.v1"
"io"
"path/filepath"
"time"
Expand All @@ -26,22 +24,19 @@ var (
// always print out progress. This avoids the user wondering what's going on.
const statsReportLimit = 8 * time.Second

func ExportEvents(ctx *cli.Context, w io.Writer, dataDir string, from, to idx.Epoch) (err error) {
func ExportEvents(gdbParams db.GossipDbParameters, w io.Writer, dataDir string, from, to idx.Epoch) (err error) {
chaindataDir := filepath.Join(dataDir, "chaindata")
dbs, err := db.MakeDbProducer(chaindataDir, cachescale.Identity)
if err != nil {
return err
}
defer dbs.Close()

gdb, err := db.MakeGossipDb(db.GossipDbParameters{
Dbs: dbs,
DataDir: dataDir,
ValidatorMode: false,
CacheRatio: cachescale.Identity,
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
})
// Fill the rest of the params
gdbParams.Dbs = dbs
gdbParams.CacheRatio = cachescale.Identity

gdb, err := db.MakeGossipDb(gdbParams)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/sonictool/db/dbutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type GossipDbParameters struct {
DataDir string
ValidatorMode bool
CacheRatio cachescale.Func
LiveDbCache, ArchiveCache int64
LiveDbCache, ArchiveCache int64 // in bytes
}

func MakeGossipDb(params GossipDbParameters) (*gossip.Store, error) {
Expand Down
35 changes: 21 additions & 14 deletions cmd/sonictool/genesis/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,35 @@ package genesis
import (
"fmt"
"github.com/Fantom-foundation/go-opera/cmd/sonictool/db"
"github.com/Fantom-foundation/go-opera/config/flags"
"github.com/Fantom-foundation/go-opera/opera/genesis"
"github.com/Fantom-foundation/go-opera/opera/genesisstore"
"github.com/Fantom-foundation/lachesis-base/abft"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/Fantom-foundation/lachesis-base/kvdb"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
"github.com/ethereum/go-ethereum/log"
"gopkg.in/urfave/cli.v1"
"path/filepath"
)

func ImportGenesisStore(ctx *cli.Context, genesisStore *genesisstore.Store, dataDir string, validatorMode bool, cacheRatio cachescale.Func) error {
if err := db.AssertDatabaseNotInitialized(dataDir); err != nil {
// ImportParams are parameters for ImportGenesisStore func.
type ImportParams struct {
GenesisStore *genesisstore.Store
DataDir string
ValidatorMode bool
CacheRatio cachescale.Func
LiveDbCache, ArchiveCache int64 // in bytes
}

func ImportGenesisStore(params ImportParams) error {
if err := db.AssertDatabaseNotInitialized(params.DataDir); err != nil {
return fmt.Errorf("database in datadir is already initialized: %w", err)
}
if err := db.RemoveDatabase(dataDir); err != nil {
if err := db.RemoveDatabase(params.DataDir); err != nil {
return fmt.Errorf("failed to remove existing data from the datadir: %w", err)
}

chaindataDir := filepath.Join(dataDir, "chaindata")
dbs, err := db.MakeDbProducer(chaindataDir, cacheRatio)
chaindataDir := filepath.Join(params.DataDir, "chaindata")
dbs, err := db.MakeDbProducer(chaindataDir, params.CacheRatio)
if err != nil {
return err
}
Expand All @@ -33,18 +40,18 @@ func ImportGenesisStore(ctx *cli.Context, genesisStore *genesisstore.Store, data

gdb, err := db.MakeGossipDb(db.GossipDbParameters{
Dbs: dbs,
DataDir: dataDir,
ValidatorMode: validatorMode,
CacheRatio: cacheRatio,
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
DataDir: params.DataDir,
ValidatorMode: params.ValidatorMode,
CacheRatio: params.CacheRatio,
LiveDbCache: params.LiveDbCache,
ArchiveCache: params.ArchiveCache,
})
if err != nil {
return err
}
defer gdb.Close()

err = gdb.ApplyGenesis(genesisStore.Genesis())
err = gdb.ApplyGenesis(params.GenesisStore.Genesis())
if err != nil {
return fmt.Errorf("failed to write Gossip genesis state: %v", err)
}
Expand All @@ -63,7 +70,7 @@ func ImportGenesisStore(ctx *cli.Context, genesisStore *genesisstore.Store, data
abftCrit := func(err error) {
panic(fmt.Errorf("lachesis store error: %w", err))
}
cdb := abft.NewStore(cMainDb, cGetEpochDB, abftCrit, abft.DefaultStoreConfig(cacheRatio))
cdb := abft.NewStore(cMainDb, cGetEpochDB, abftCrit, abft.DefaultStoreConfig(params.CacheRatio))
defer cdb.Close()

err = cdb.ApplyGenesis(&abft.Genesis{
Expand Down
5 changes: 1 addition & 4 deletions tests/integration_test_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,11 @@ func startIntegrationTestNet(directory string, args []string) (*IntegrationTestN
// initialize the data directory for the single node on the test network
// equivalent to running `sonictool --datadir <dataDir> genesis fake 1`
originalArgs := os.Args
os.Args = append([]string{
os.Args = append([]string{
"sonictool",
"--datadir", result.stateDir(),
"genesis",
"fake",
"--statedb.livecache", "1",
"--statedb.archivecache", "1",
"1",
}, args...)
if err := sonictool.Run(); err != nil {
os.Args = originalArgs
Expand Down

0 comments on commit e65e7cc

Please sign in to comment.