Skip to content

Commit

Permalink
Use flags when changing the cache
Browse files Browse the repository at this point in the history
  • Loading branch information
cabrador committed Nov 26, 2024
1 parent 21b3f29 commit 7eb6764
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 21 deletions.
11 changes: 8 additions & 3 deletions cmd/sonicd/app/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func initFlags() {
}
performanceFlags = []cli.Flag{
flags.CacheFlag,
flags.LiveDbCacheFlag,
flags.ArchiveCacheFlag,
}
networkingFlags = []cli.Flag{
flags.BootnodesFlag,
Expand Down Expand Up @@ -222,9 +224,12 @@ func lachesisMain(ctx *cli.Context) error {
}

metrics.SetDataDir(cfg.Node.DataDir) // report disk space usage into metrics
if ctx.GlobalIsSet(config.FakeNetFlag.Name) {
cfg.OperaStore.EVM.StateDb.LiveCache = 1
cfg.OperaStore.EVM.StateDb.ArchiveCache = 1
if ctx.IsSet(flags.LiveDbCacheFlag.Name) {
cfg.OperaStore.EVM.StateDb.LiveCache = ctx.Int64(flags.LiveDbCacheFlag.Name)
}

if ctx.IsSet(flags.ArchiveCacheFlag.Name) {
cfg.OperaStore.EVM.StateDb.ArchiveCache = ctx.Int64(flags.ArchiveCacheFlag.Name)
}

node, _, nodeClose, err := config.MakeNode(ctx, cfg)
Expand Down
3 changes: 2 additions & 1 deletion cmd/sonicd/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"fmt"
"gopkg.in/urfave/cli.v1"
"os"
"strings"
"testing"
Expand All @@ -26,7 +27,7 @@ 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(genesisStore, dataDir, false, cachescale.Identity, true); err != nil {
if err := genesis.ImportGenesisStore(&cli.Context{}, genesisStore, dataDir, false, cachescale.Identity); err != nil {
panic(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/sonictool/app/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func exportEvents(ctx *cli.Context) error {
}

log.Info("Exporting events to file", "file", fn)
err = chain.ExportEvents(writer, dataDir, from, to)
err = chain.ExportEvents(ctx, writer, dataDir, from, to)
if err != nil {
return fmt.Errorf("export error: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/sonictool/app/export_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func exportGenesis(ctx *cli.Context) error {
}
defer dbs.Close()

gdb, err := db.MakeGossipDb(dbs, dataDir, false, cachescale.Identity, false)
gdb, err := db.MakeGossipDb(ctx, dbs, dataDir, false, cachescale.Identity)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/sonictool/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func gfileGenesisImport(ctx *cli.Context) error {
return fmt.Errorf("genesis file check failed: %w", err)
}
}
return genesis.ImportGenesisStore(genesisStore, dataDir, validatorMode, cacheRatio, false)
return genesis.ImportGenesisStore(ctx, genesisStore, dataDir, validatorMode, cacheRatio)
}

func jsonGenesisImport(ctx *cli.Context) error {
Expand Down Expand Up @@ -98,7 +98,7 @@ func jsonGenesisImport(ctx *cli.Context) error {
return fmt.Errorf("failed to prepare JSON genesis: %w", err)
}
defer genesisStore.Close()
return genesis.ImportGenesisStore(genesisStore, dataDir, validatorMode, cacheRatio, false)
return genesis.ImportGenesisStore(ctx, genesisStore, dataDir, validatorMode, cacheRatio)
}

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

genesisStore := makefakegenesis.FakeGenesisStore(idx.Validator(validatorsNumber), futils.ToFtm(1000000000), futils.ToFtm(5000000))
defer genesisStore.Close()
return genesis.ImportGenesisStore(genesisStore, dataDir, validatorMode, cacheRatio, true)
return genesis.ImportGenesisStore(ctx, genesisStore, dataDir, validatorMode, cacheRatio)
}

func isValidatorModeSet(ctx *cli.Context) (bool, error) {
Expand Down
2 changes: 2 additions & 0 deletions cmd/sonictool/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ 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
5 changes: 3 additions & 2 deletions cmd/sonictool/chain/export_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chain
import (
"github.com/Fantom-foundation/go-opera/cmd/sonictool/db"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
"gopkg.in/urfave/cli.v1"
"io"
"path/filepath"
"time"
Expand All @@ -24,15 +25,15 @@ var (
// always print out progress. This avoids the user wondering what's going on.
const statsReportLimit = 8 * time.Second

func ExportEvents(w io.Writer, dataDir string, from, to idx.Epoch) (err error) {
func ExportEvents(ctx *cli.Context, 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(dbs, dataDir, false, cachescale.Identity, false)
gdb, err := db.MakeGossipDb(ctx, dbs, dataDir, false, cachescale.Identity)
if err != nil {
return err
}
Expand Down
15 changes: 10 additions & 5 deletions cmd/sonictool/db/dbutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"errors"
"fmt"
carmen "github.com/Fantom-foundation/Carmen/go/state"
"github.com/Fantom-foundation/go-opera/config/flags"
"github.com/Fantom-foundation/go-opera/gossip"
"github.com/Fantom-foundation/go-opera/integration"
"github.com/Fantom-foundation/lachesis-base/kvdb"
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
"github.com/ethereum/go-ethereum/common/fdlimit"
"github.com/syndtr/goleveldb/leveldb/opt"
"gopkg.in/urfave/cli.v1"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -61,19 +63,22 @@ func MakeDbProducer(chaindataDir string, cacheRatio cachescale.Func) (kvdb.FullD
})
}

func MakeGossipDb(dbs kvdb.FullDBProducer, dataDir string, validatorMode bool, cacheRatio cachescale.Func, isFakeNet bool) (*gossip.Store, error) {
func MakeGossipDb(ctx *cli.Context, dbs kvdb.FullDBProducer, dataDir string, validatorMode bool, cacheRatio cachescale.Func) (*gossip.Store, error) {
gdbConfig := gossip.DefaultStoreConfig(cacheRatio)
gdbConfig.EVM.StateDb.Directory = filepath.Join(dataDir, "carmen")
if validatorMode {
gdbConfig.EVM.StateDb.Archive = carmen.NoArchive
gdbConfig.EVM.DisableLogsIndexing = true
gdbConfig.EVM.DisableTxHashesIndexing = true
}

if isFakeNet {
gdbConfig.EVM.StateDb.ArchiveCache = 1
gdbConfig.EVM.StateDb.LiveCache = 1
if ctx.IsSet(flags.LiveDbCacheFlag.Name) {
gdbConfig.EVM.StateDb.LiveCache = ctx.Int64(flags.LiveDbCacheFlag.Name)
}

if ctx.IsSet(flags.ArchiveCacheFlag.Name) {
gdbConfig.EVM.StateDb.ArchiveCache = ctx.Int64(flags.ArchiveCacheFlag.Name)
}
gdbConfig.EVM.StateDb.Directory = filepath.Join(dataDir, "carmen")

gdb, err := gossip.NewStore(dbs, gdbConfig)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/sonictool/genesis/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"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(genesisStore *genesisstore.Store, dataDir string, validatorMode bool, cacheRatio cachescale.Func, isFakeNet bool) error {
func ImportGenesisStore(ctx *cli.Context, genesisStore *genesisstore.Store, dataDir string, validatorMode bool, cacheRatio cachescale.Func) error {
if err := db.AssertDatabaseNotInitialized(dataDir); err != nil {
return fmt.Errorf("database in datadir is already initialized: %w", err)
}
Expand All @@ -29,7 +30,7 @@ func ImportGenesisStore(genesisStore *genesisstore.Store, dataDir string, valida
defer dbs.Close()
setGenesisProcessing(chaindataDir)

gdb, err := db.MakeGossipDb(dbs, dataDir, validatorMode, cacheRatio, isFakeNet)
gdb, err := db.MakeGossipDb(ctx, dbs, dataDir, false, cacheRatio)
if err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions config/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,14 @@ var (
Name: "lachesis.suppress-frame-panic",
Usage: "Suppress frame missmatch error (when testing on historical imported/synced events)",
}

// StateDb
LiveDbCacheFlag = cli.Int64Flag{
Name: "statedb.livecache",
Usage: "Size of live db cache in bytes.",
}
ArchiveCacheFlag = cli.IntFlag{
Name: "statedb.archivecache",
Usage: "Size of archive cache in bytes.",
}
)
21 changes: 18 additions & 3 deletions tests/integration_test_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ func StartIntegrationTestNet(directory string) (*IntegrationTestNet, error) {
// 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 = []string{"sonictool", "--datadir", directory, "genesis", "fake", "1"}
os.Args = []string{
"sonictool",
"--datadir", directory,
"genesis",
"fake",
"--statedb.livecache", "1",
"--statedb.archivecache", "1",
"1",
}
sonictool.Run()
os.Args = originalArgs

Expand Down Expand Up @@ -88,10 +96,17 @@ func (n *IntegrationTestNet) start() error {
"sonicd",
"--datadir", n.directory,
"--fakenet", "1/1",
"--http", "--http.addr", "0.0.0.0", "--http.port", "18545",
"--http",
"--http.addr", "0.0.0.0",
"--http.port", "18545",
"--http.api", "admin,eth,web3,net,txpool,ftm,trace,debug",
"--ws", "--ws.addr", "0.0.0.0", "--ws.port", "18546", "--ws.api", "admin,eth,ftm",
"--ws",
"--ws.addr", "0.0.0.0",
"--ws.port", "18546",
"--ws.api", "admin,eth,ftm",
"--datadir.minfreedisk", "0",
"--statedb.livecache", "1",
"--statedb.archivecache", "1",
}
sonicd.Run()
}()
Expand Down

0 comments on commit 7eb6764

Please sign in to comment.