Skip to content

Commit d4be9bf

Browse files
committed
Address reviewers feedback
1 parent 6da446b commit d4be9bf

File tree

8 files changed

+72
-47
lines changed

8 files changed

+72
-47
lines changed

cmd/sonicd/app/run_test.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package app
22

33
import (
4-
"flag"
54
"fmt"
6-
"gopkg.in/urfave/cli.v1"
5+
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
76
"os"
87
"strings"
98
"testing"
@@ -13,8 +12,6 @@ import (
1312
"github.com/Fantom-foundation/go-opera/integration/makefakegenesis"
1413
futils "github.com/Fantom-foundation/go-opera/utils"
1514
"github.com/Fantom-foundation/lachesis-base/inter/idx"
16-
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
17-
1815
"github.com/docker/docker/pkg/reexec"
1916
"github.com/ethereum/go-ethereum/common"
2017

@@ -28,13 +25,14 @@ func tmpdir(t *testing.T) string {
2825
func initFakenetDatadir(dataDir string, validatorsNum idx.Validator) {
2926
genesisStore := makefakegenesis.FakeGenesisStore(validatorsNum, futils.ToFtm(1000000000), futils.ToFtm(5000000))
3027
defer genesisStore.Close()
31-
if err := genesis.ImportGenesisStore(
32-
cli.NewContext(cli.NewApp(), new(flag.FlagSet), nil),
33-
genesisStore,
34-
dataDir,
35-
false,
36-
cachescale.Identity,
37-
); err != nil {
28+
29+
if err := genesis.ImportGenesisStore(genesis.ImportParams{
30+
GenesisStore: genesisStore,
31+
DataDir: dataDir,
32+
CacheRatio: cachescale.Identity,
33+
LiveDbCache: 1, // Set lowest cache
34+
ArchiveCache: 1, // Set lowest cache
35+
}); err != nil {
3836
panic(err)
3937
}
4038
}

cmd/sonictool/app/chain.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package app
33
import (
44
"compress/gzip"
55
"fmt"
6+
"github.com/Fantom-foundation/go-opera/cmd/sonictool/db"
67
"io"
78
"os"
89
"strconv"
@@ -57,8 +58,14 @@ func exportEvents(ctx *cli.Context) error {
5758
to = idx.Epoch(n)
5859
}
5960

61+
gdbParams := db.GossipDbParameters{
62+
DataDir: dataDir,
63+
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
64+
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
65+
}
66+
6067
log.Info("Exporting events to file", "file", fn)
61-
err = chain.ExportEvents(ctx, writer, dataDir, from, to)
68+
err = chain.ExportEvents(gdbParams, writer, dataDir, from, to)
6269
if err != nil {
6370
return fmt.Errorf("export error: %w", err)
6471
}

cmd/sonictool/app/genesis.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ func gfileGenesisImport(ctx *cli.Context) error {
6666
return fmt.Errorf("genesis file check failed: %w", err)
6767
}
6868
}
69-
return genesis.ImportGenesisStore(ctx, genesisStore, dataDir, validatorMode, cacheRatio)
69+
return genesis.ImportGenesisStore(genesis.ImportParams{
70+
GenesisStore: genesisStore,
71+
DataDir: dataDir,
72+
ValidatorMode: validatorMode,
73+
CacheRatio: cacheRatio,
74+
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
75+
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
76+
})
7077
}
7178

7279
func jsonGenesisImport(ctx *cli.Context) error {
@@ -98,7 +105,14 @@ func jsonGenesisImport(ctx *cli.Context) error {
98105
return fmt.Errorf("failed to prepare JSON genesis: %w", err)
99106
}
100107
defer genesisStore.Close()
101-
return genesis.ImportGenesisStore(ctx, genesisStore, dataDir, validatorMode, cacheRatio)
108+
return genesis.ImportGenesisStore(genesis.ImportParams{
109+
GenesisStore: genesisStore,
110+
DataDir: dataDir,
111+
ValidatorMode: validatorMode,
112+
CacheRatio: cacheRatio,
113+
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
114+
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
115+
})
102116
}
103117

104118
func fakeGenesisImport(ctx *cli.Context) error {
@@ -127,7 +141,14 @@ func fakeGenesisImport(ctx *cli.Context) error {
127141

128142
genesisStore := makefakegenesis.FakeGenesisStore(idx.Validator(validatorsNumber), futils.ToFtm(1000000000), futils.ToFtm(5000000))
129143
defer genesisStore.Close()
130-
return genesis.ImportGenesisStore(ctx, genesisStore, dataDir, validatorMode, cacheRatio)
144+
return genesis.ImportGenesisStore(genesis.ImportParams{
145+
GenesisStore: genesisStore,
146+
DataDir: dataDir,
147+
ValidatorMode: validatorMode,
148+
CacheRatio: cacheRatio,
149+
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
150+
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
151+
})
131152
}
132153

133154
func isValidatorModeSet(ctx *cli.Context) (bool, error) {

cmd/sonictool/app/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ func Run() error {
1818
app.Flags = []cli.Flag{
1919
flags.DataDirFlag,
2020
flags.CacheFlag,
21+
flags.LiveDbCacheFlag,
22+
flags.ArchiveCacheFlag,
2123
}
2224
app.Commands = []cli.Command{
2325
{
@@ -60,8 +62,6 @@ Initialize the database using data from the experimental genesis file.
6062
ArgsUsage: "<validators>",
6163
Action: fakeGenesisImport,
6264
Flags: []cli.Flag{
63-
flags.LiveDbCacheFlag,
64-
flags.ArchiveCacheFlag,
6565
ModeFlag,
6666
},
6767
Description: `

cmd/sonictool/chain/export_events.go

+6-11
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package chain
22

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

29-
func ExportEvents(ctx *cli.Context, w io.Writer, dataDir string, from, to idx.Epoch) (err error) {
27+
func ExportEvents(gdbParams db.GossipDbParameters, w io.Writer, dataDir string, from, to idx.Epoch) (err error) {
3028
chaindataDir := filepath.Join(dataDir, "chaindata")
3129
dbs, err := db.MakeDbProducer(chaindataDir, cachescale.Identity)
3230
if err != nil {
3331
return err
3432
}
3533
defer dbs.Close()
3634

37-
gdb, err := db.MakeGossipDb(db.GossipDbParameters{
38-
Dbs: dbs,
39-
DataDir: dataDir,
40-
ValidatorMode: false,
41-
CacheRatio: cachescale.Identity,
42-
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
43-
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
44-
})
35+
// Fill the rest of the params
36+
gdbParams.Dbs = dbs
37+
gdbParams.CacheRatio = cachescale.Identity
38+
39+
gdb, err := db.MakeGossipDb(gdbParams)
4540
if err != nil {
4641
return err
4742
}

cmd/sonictool/db/dbutils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type GossipDbParameters struct {
6767
DataDir string
6868
ValidatorMode bool
6969
CacheRatio cachescale.Func
70-
LiveDbCache, ArchiveCache int64
70+
LiveDbCache, ArchiveCache int64 // in bytes
7171
}
7272

7373
func MakeGossipDb(params GossipDbParameters) (*gossip.Store, error) {

cmd/sonictool/genesis/import.go

+21-14
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,35 @@ package genesis
33
import (
44
"fmt"
55
"github.com/Fantom-foundation/go-opera/cmd/sonictool/db"
6-
"github.com/Fantom-foundation/go-opera/config/flags"
76
"github.com/Fantom-foundation/go-opera/opera/genesis"
87
"github.com/Fantom-foundation/go-opera/opera/genesisstore"
98
"github.com/Fantom-foundation/lachesis-base/abft"
109
"github.com/Fantom-foundation/lachesis-base/inter/idx"
1110
"github.com/Fantom-foundation/lachesis-base/kvdb"
1211
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
1312
"github.com/ethereum/go-ethereum/log"
14-
"gopkg.in/urfave/cli.v1"
1513
"path/filepath"
1614
)
1715

18-
func ImportGenesisStore(ctx *cli.Context, genesisStore *genesisstore.Store, dataDir string, validatorMode bool, cacheRatio cachescale.Func) error {
19-
if err := db.AssertDatabaseNotInitialized(dataDir); err != nil {
16+
// ImportParams are parameters for ImportGenesisStore func.
17+
type ImportParams struct {
18+
GenesisStore *genesisstore.Store
19+
DataDir string
20+
ValidatorMode bool
21+
CacheRatio cachescale.Func
22+
LiveDbCache, ArchiveCache int64 // in bytes
23+
}
24+
25+
func ImportGenesisStore(params ImportParams) error {
26+
if err := db.AssertDatabaseNotInitialized(params.DataDir); err != nil {
2027
return fmt.Errorf("database in datadir is already initialized: %w", err)
2128
}
22-
if err := db.RemoveDatabase(dataDir); err != nil {
29+
if err := db.RemoveDatabase(params.DataDir); err != nil {
2330
return fmt.Errorf("failed to remove existing data from the datadir: %w", err)
2431
}
2532

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

3441
gdb, err := db.MakeGossipDb(db.GossipDbParameters{
3542
Dbs: dbs,
36-
DataDir: dataDir,
37-
ValidatorMode: validatorMode,
38-
CacheRatio: cacheRatio,
39-
LiveDbCache: ctx.Int64(flags.LiveDbCacheFlag.Name),
40-
ArchiveCache: ctx.Int64(flags.ArchiveCacheFlag.Name),
43+
DataDir: params.DataDir,
44+
ValidatorMode: params.ValidatorMode,
45+
CacheRatio: params.CacheRatio,
46+
LiveDbCache: params.LiveDbCache,
47+
ArchiveCache: params.ArchiveCache,
4148
})
4249
if err != nil {
4350
return err
4451
}
4552
defer gdb.Close()
4653

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

6976
err = cdb.ApplyGenesis(&abft.Genesis{

tests/integration_test_net.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,11 @@ func startIntegrationTestNet(directory string, args []string) (*IntegrationTestN
164164
// initialize the data directory for the single node on the test network
165165
// equivalent to running `sonictool --datadir <dataDir> genesis fake 1`
166166
originalArgs := os.Args
167-
os.Args = append([]string{
167+
os.Args = append([]string{
168168
"sonictool",
169169
"--datadir", result.stateDir(),
170-
"genesis",
171-
"fake",
172170
"--statedb.livecache", "1",
173171
"--statedb.archivecache", "1",
174-
"1",
175172
}, args...)
176173
if err := sonictool.Run(); err != nil {
177174
os.Args = originalArgs

0 commit comments

Comments
 (0)