Skip to content

Commit 8c67795

Browse files
fix(utils): create a specific folder for database (#1598)
* feat: create a specific folder for database * chore: remove unused const * fix: add default database dir on NodeInitialized function * fix: change default database dir to db and fix typo
1 parent f4c79d3 commit 8c67795

File tree

12 files changed

+34
-75
lines changed

12 files changed

+34
-75
lines changed

cmd/gossamer/config.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"strconv"
2222
"strings"
2323

24-
"github.com/ChainSafe/chaindb"
2524
"github.com/ChainSafe/gossamer/chain/gssmr"
2625
"github.com/ChainSafe/gossamer/dot"
2726
ctoml "github.com/ChainSafe/gossamer/dot/config/toml"
@@ -788,9 +787,7 @@ func updateDotConfigFromGenesisJSONRaw(tomlCfg ctoml.Config, cfg *dot.Config) {
788787
// updateDotConfigFromGenesisData updates the configuration from genesis data of an initialised node
789788
func updateDotConfigFromGenesisData(ctx *cli.Context, cfg *dot.Config) error {
790789
// initialise database using data directory
791-
db, err := chaindb.NewBadgerDB(&chaindb.Config{
792-
DataDir: cfg.Global.BasePath,
793-
})
790+
db, err := utils.SetupDatabase(cfg.Global.BasePath, false)
794791
if err != nil {
795792
return fmt.Errorf("failed to create database: %s", err)
796793
}

cmd/gossamer/config_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/ChainSafe/gossamer/lib/genesis"
2828
"github.com/ChainSafe/gossamer/lib/utils"
2929

30-
"github.com/ChainSafe/chaindb"
3130
log "github.com/ChainSafe/log15"
3231
"github.com/stretchr/testify/require"
3332
"github.com/urfave/cli"
@@ -816,9 +815,7 @@ func TestUpdateConfigFromGenesisData(t *testing.T) {
816815

817816
cfg.Init.Genesis = genFile.Name()
818817

819-
db, err := chaindb.NewBadgerDB(&chaindb.Config{
820-
DataDir: cfg.Global.BasePath,
821-
})
818+
db, err := utils.SetupDatabase(cfg.Global.BasePath, false)
822819
require.Nil(t, err)
823820

824821
gen, err := genesis.NewGenesisFromJSONRaw(genFile.Name())

dot/node.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"syscall"
2929
"time"
3030

31-
"github.com/ChainSafe/chaindb"
3231
gssmrmetrics "github.com/ChainSafe/gossamer/dot/metrics"
3332
"github.com/ChainSafe/gossamer/dot/network"
3433
"github.com/ChainSafe/gossamer/dot/state"
@@ -37,6 +36,7 @@ import (
3736
"github.com/ChainSafe/gossamer/lib/genesis"
3837
"github.com/ChainSafe/gossamer/lib/keystore"
3938
"github.com/ChainSafe/gossamer/lib/services"
39+
"github.com/ChainSafe/gossamer/lib/utils"
4040
log "github.com/ChainSafe/log15"
4141
"github.com/ethereum/go-ethereum/metrics"
4242
"github.com/ethereum/go-ethereum/metrics/prometheus"
@@ -121,7 +121,7 @@ func InitNode(cfg *Config) error {
121121
// node, the state database has been created and the genesis data has been loaded
122122
func NodeInitialized(basepath string, expected bool) bool {
123123
// check if key registry exists
124-
registry := path.Join(basepath, "KEYREGISTRY")
124+
registry := path.Join(basepath, utils.DefaultDatabaseDir, "KEYREGISTRY")
125125

126126
_, err := os.Stat(registry)
127127
if os.IsNotExist(err) {
@@ -136,9 +136,7 @@ func NodeInitialized(basepath string, expected bool) bool {
136136
}
137137

138138
// initialise database using data directory
139-
db, err := chaindb.NewBadgerDB(&chaindb.Config{
140-
DataDir: basepath,
141-
})
139+
db, err := utils.SetupDatabase(basepath, false)
142140
if err != nil {
143141
logger.Error(
144142
"failed to create database",
@@ -173,7 +171,7 @@ func NodeInitialized(basepath string, expected bool) bool {
173171
// LoadGlobalNodeName returns the stored global node name from database
174172
func LoadGlobalNodeName(basepath string) (nodename string, err error) {
175173
// initialise database using data directory
176-
db, err := state.SetupDatabase(basepath)
174+
db, err := utils.SetupDatabase(basepath, false)
177175
if err != nil {
178176
return "", err
179177
}
@@ -390,7 +388,7 @@ func setupMetricsServer(address string) {
390388

391389
// stores the global node name to reuse
392390
func storeGlobalNodeName(name, basepath string) (err error) {
393-
db, err := state.SetupDatabase(basepath)
391+
db, err := utils.SetupDatabase(basepath, false)
394392
if err != nil {
395393
return err
396394
}

dot/services.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ import (
4141
"github.com/ChainSafe/gossamer/lib/runtime/life"
4242
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
4343
"github.com/ChainSafe/gossamer/lib/runtime/wasmtime"
44+
"github.com/ChainSafe/gossamer/lib/utils"
4445
)
4546

4647
func newInMemoryDB(path string) (chaindb.Database, error) {
47-
return chaindb.NewBadgerDB(&chaindb.Config{
48-
DataDir: filepath.Join(path, "local_storage"),
49-
InMemory: true,
50-
})
48+
return utils.SetupDatabase(filepath.Join(path, "local_storage"), true)
5149
}
5250

5351
// State Service

dot/state/base.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ import (
2727
"github.com/ChainSafe/chaindb"
2828
)
2929

30-
// SetupDatabase will return an instance of database based on basepath
31-
func SetupDatabase(basepath string) (chaindb.Database, error) {
32-
return chaindb.NewBadgerDB(&chaindb.Config{
33-
DataDir: basepath,
34-
})
35-
}
36-
3730
// BaseState is a wrapper for the chaindb.Database, without any prefixes
3831
type BaseState struct {
3932
db chaindb.Database

dot/state/initialize.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,26 @@ import (
2828
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
2929
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
3030
"github.com/ChainSafe/gossamer/lib/trie"
31+
"github.com/ChainSafe/gossamer/lib/utils"
3132

3233
"github.com/ChainSafe/chaindb"
3334
)
3435

3536
// Initialise initialises the genesis state of the DB using the given storage trie. The trie should be loaded with the genesis storage state.
3637
// This only needs to be called during genesis initialisation of the node; it is not called during normal startup.
3738
func (s *Service) Initialise(gen *genesis.Genesis, header *types.Header, t *trie.Trie) error {
38-
var db chaindb.Database
39-
cfg := &chaindb.Config{}
40-
41-
// check database type
42-
if s.isMemDB {
43-
cfg.InMemory = true
44-
}
45-
4639
// get data directory from service
4740
basepath, err := filepath.Abs(s.dbPath)
4841
if err != nil {
4942
return fmt.Errorf("failed to read basepath: %s", err)
5043
}
5144

52-
cfg.DataDir = basepath
53-
5445
// initialise database using data directory
55-
db, err = chaindb.NewBadgerDB(cfg)
46+
db, err := utils.SetupDatabase(basepath, s.isMemDB)
5647
if err != nil {
5748
return fmt.Errorf("failed to create database: %s", err)
5849
}
50+
5951
s.db = db
6052

6153
if err = db.ClearAll(); err != nil {
@@ -119,9 +111,6 @@ func (s *Service) Initialise(gen *genesis.Genesis, header *types.Header, t *trie
119111

120112
// check database type
121113
if s.isMemDB {
122-
// append memory database to state service
123-
s.db = db
124-
125114
// append storage state and block state to state service
126115
s.Storage = storageState
127116
s.Block = blockState

dot/state/service.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/ChainSafe/gossamer/dot/types"
2727
"github.com/ChainSafe/gossamer/lib/blocktree"
2828
"github.com/ChainSafe/gossamer/lib/trie"
29+
"github.com/ChainSafe/gossamer/lib/utils"
2930

3031
"github.com/ChainSafe/chaindb"
3132
log "github.com/ChainSafe/log15"
@@ -88,18 +89,15 @@ func (s *Service) Start() error {
8889
}
8990

9091
db := s.db
92+
9193
if !s.isMemDB {
9294
basepath, err := filepath.Abs(s.dbPath)
9395
if err != nil {
9496
return err
9597
}
9698

97-
cfg := &chaindb.Config{
98-
DataDir: basepath,
99-
}
100-
10199
// initialise database
102-
db, err = chaindb.NewBadgerDB(cfg)
100+
db, err = utils.SetupDatabase(basepath, false)
103101
if err != nil {
104102
return err
105103
}
@@ -297,17 +295,9 @@ func (s *Service) Stop() error {
297295
// Import imports the given state corresponding to the given header and sets the head of the chain
298296
// to it. Additionally, it uses the first slot to correctly set the epoch number of the block.
299297
func (s *Service) Import(header *types.Header, t *trie.Trie, firstSlot uint64) error {
300-
cfg := &chaindb.Config{
301-
DataDir: s.dbPath,
302-
}
303-
304-
if s.isMemDB {
305-
cfg.InMemory = true
306-
}
307-
308298
var err error
309299
// initialise database using data directory
310-
s.db, err = chaindb.NewBadgerDB(cfg)
300+
s.db, err = utils.SetupDatabase(s.dbPath, s.isMemDB)
311301
if err != nil {
312302
return fmt.Errorf("failed to create database: %s", err)
313303
}

dot/state/test_helpers.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/ChainSafe/gossamer/lib/common"
3030
runtime "github.com/ChainSafe/gossamer/lib/runtime/storage"
3131
"github.com/ChainSafe/gossamer/lib/trie"
32+
"github.com/ChainSafe/gossamer/lib/utils"
3233

3334
"github.com/stretchr/testify/require"
3435
)
@@ -40,10 +41,7 @@ func NewInMemoryDB(t *testing.T) chaindb.Database {
4041
testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*")
4142
require.NoError(t, err)
4243

43-
db, err := chaindb.NewBadgerDB(&chaindb.Config{
44-
DataDir: testDatadirPath,
45-
InMemory: true,
46-
})
44+
db, err := utils.SetupDatabase(testDatadirPath, true)
4745
require.NoError(t, err)
4846
t.Cleanup(func() {
4947
_ = db.Close()

lib/blocktree/database_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ func newInMemoryDB(t *testing.T) chaindb.Database {
9292
testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*")
9393
require.NoError(t, err)
9494

95-
db, err := chaindb.NewBadgerDB(&chaindb.Config{
96-
DataDir: testDatadirPath,
97-
InMemory: true,
98-
})
95+
db, err := utils.SetupDatabase(testDatadirPath, true)
9996
require.NoError(t, err)
10097
t.Cleanup(func() {
10198
db.Close()

lib/grandpa/grandpa_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import (
3030
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
3131
"github.com/ChainSafe/gossamer/lib/keystore"
3232
"github.com/ChainSafe/gossamer/lib/trie"
33+
"github.com/ChainSafe/gossamer/lib/utils"
3334

34-
"github.com/ChainSafe/chaindb"
3535
"github.com/stretchr/testify/require"
3636
)
3737

@@ -56,12 +56,7 @@ func newTestState(t *testing.T) *state.Service {
5656
testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*")
5757
require.NoError(t, err)
5858

59-
cfg := &chaindb.Config{
60-
DataDir: testDatadirPath,
61-
InMemory: true,
62-
}
63-
64-
db, err := chaindb.NewBadgerDB(cfg)
59+
db, err := utils.SetupDatabase(testDatadirPath, true)
6560
require.NoError(t, err)
6661

6762
block, err := state.NewBlockStateFromGenesis(db, testGenesisHeader)

0 commit comments

Comments
 (0)