Skip to content

Commit be318ff

Browse files
committed
Stop passing context around
1 parent 267ee81 commit be318ff

File tree

5 files changed

+67
-40
lines changed

5 files changed

+67
-40
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/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/genesis/import.go

+20-14
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,34 @@ 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+
type ImportParams struct {
17+
GenesisStore *genesisstore.Store
18+
DataDir string
19+
ValidatorMode bool
20+
CacheRatio cachescale.Func
21+
LiveDbCache, ArchiveCache int64
22+
}
23+
24+
func ImportGenesisStore(params ImportParams) error {
25+
if err := db.AssertDatabaseNotInitialized(params.DataDir); err != nil {
2026
return fmt.Errorf("database in datadir is already initialized: %w", err)
2127
}
22-
if err := db.RemoveDatabase(dataDir); err != nil {
28+
if err := db.RemoveDatabase(params.DataDir); err != nil {
2329
return fmt.Errorf("failed to remove existing data from the datadir: %w", err)
2430
}
2531

26-
chaindataDir := filepath.Join(dataDir, "chaindata")
27-
dbs, err := db.MakeDbProducer(chaindataDir, cacheRatio)
32+
chaindataDir := filepath.Join(params.DataDir, "chaindata")
33+
dbs, err := db.MakeDbProducer(chaindataDir, params.CacheRatio)
2834
if err != nil {
2935
return err
3036
}
@@ -33,18 +39,18 @@ func ImportGenesisStore(ctx *cli.Context, genesisStore *genesisstore.Store, data
3339

3440
gdb, err := db.MakeGossipDb(db.GossipDbParameters{
3541
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),
42+
DataDir: params.DataDir,
43+
ValidatorMode: params.ValidatorMode,
44+
CacheRatio: params.CacheRatio,
45+
LiveDbCache: params.LiveDbCache,
46+
ArchiveCache: params.ArchiveCache,
4147
})
4248
if err != nil {
4349
return err
4450
}
4551
defer gdb.Close()
4652

47-
err = gdb.ApplyGenesis(genesisStore.Genesis())
53+
err = gdb.ApplyGenesis(params.GenesisStore.Genesis())
4854
if err != nil {
4955
return fmt.Errorf("failed to write Gossip genesis state: %v", err)
5056
}
@@ -63,7 +69,7 @@ func ImportGenesisStore(ctx *cli.Context, genesisStore *genesisstore.Store, data
6369
abftCrit := func(err error) {
6470
panic(fmt.Errorf("lachesis store error: %w", err))
6571
}
66-
cdb := abft.NewStore(cMainDb, cGetEpochDB, abftCrit, abft.DefaultStoreConfig(cacheRatio))
72+
cdb := abft.NewStore(cMainDb, cGetEpochDB, abftCrit, abft.DefaultStoreConfig(params.CacheRatio))
6773
defer cdb.Close()
6874

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

0 commit comments

Comments
 (0)