Skip to content

Commit

Permalink
fix(utils): create a specific folder for database (#1598)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
EclesioMeloJunior authored May 21, 2021
1 parent f4c79d3 commit 8c67795
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 75 deletions.
5 changes: 1 addition & 4 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"strconv"
"strings"

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/chain/gssmr"
"github.com/ChainSafe/gossamer/dot"
ctoml "github.com/ChainSafe/gossamer/dot/config/toml"
Expand Down Expand Up @@ -788,9 +787,7 @@ func updateDotConfigFromGenesisJSONRaw(tomlCfg ctoml.Config, cfg *dot.Config) {
// updateDotConfigFromGenesisData updates the configuration from genesis data of an initialised node
func updateDotConfigFromGenesisData(ctx *cli.Context, cfg *dot.Config) error {
// initialise database using data directory
db, err := chaindb.NewBadgerDB(&chaindb.Config{
DataDir: cfg.Global.BasePath,
})
db, err := utils.SetupDatabase(cfg.Global.BasePath, false)
if err != nil {
return fmt.Errorf("failed to create database: %s", err)
}
Expand Down
5 changes: 1 addition & 4 deletions cmd/gossamer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/utils"

"github.com/ChainSafe/chaindb"
log "github.com/ChainSafe/log15"
"github.com/stretchr/testify/require"
"github.com/urfave/cli"
Expand Down Expand Up @@ -816,9 +815,7 @@ func TestUpdateConfigFromGenesisData(t *testing.T) {

cfg.Init.Genesis = genFile.Name()

db, err := chaindb.NewBadgerDB(&chaindb.Config{
DataDir: cfg.Global.BasePath,
})
db, err := utils.SetupDatabase(cfg.Global.BasePath, false)
require.Nil(t, err)

gen, err := genesis.NewGenesisFromJSONRaw(genFile.Name())
Expand Down
12 changes: 5 additions & 7 deletions dot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"syscall"
"time"

"github.com/ChainSafe/chaindb"
gssmrmetrics "github.com/ChainSafe/gossamer/dot/metrics"
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
Expand All @@ -37,6 +36,7 @@ import (
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/services"
"github.com/ChainSafe/gossamer/lib/utils"
log "github.com/ChainSafe/log15"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/metrics/prometheus"
Expand Down Expand Up @@ -121,7 +121,7 @@ func InitNode(cfg *Config) error {
// node, the state database has been created and the genesis data has been loaded
func NodeInitialized(basepath string, expected bool) bool {
// check if key registry exists
registry := path.Join(basepath, "KEYREGISTRY")
registry := path.Join(basepath, utils.DefaultDatabaseDir, "KEYREGISTRY")

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

// initialise database using data directory
db, err := chaindb.NewBadgerDB(&chaindb.Config{
DataDir: basepath,
})
db, err := utils.SetupDatabase(basepath, false)
if err != nil {
logger.Error(
"failed to create database",
Expand Down Expand Up @@ -173,7 +171,7 @@ func NodeInitialized(basepath string, expected bool) bool {
// LoadGlobalNodeName returns the stored global node name from database
func LoadGlobalNodeName(basepath string) (nodename string, err error) {
// initialise database using data directory
db, err := state.SetupDatabase(basepath)
db, err := utils.SetupDatabase(basepath, false)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -390,7 +388,7 @@ func setupMetricsServer(address string) {

// stores the global node name to reuse
func storeGlobalNodeName(name, basepath string) (err error) {
db, err := state.SetupDatabase(basepath)
db, err := utils.SetupDatabase(basepath, false)
if err != nil {
return err
}
Expand Down
6 changes: 2 additions & 4 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ import (
"github.com/ChainSafe/gossamer/lib/runtime/life"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/runtime/wasmtime"
"github.com/ChainSafe/gossamer/lib/utils"
)

func newInMemoryDB(path string) (chaindb.Database, error) {
return chaindb.NewBadgerDB(&chaindb.Config{
DataDir: filepath.Join(path, "local_storage"),
InMemory: true,
})
return utils.SetupDatabase(filepath.Join(path, "local_storage"), true)
}

// State Service
Expand Down
7 changes: 0 additions & 7 deletions dot/state/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ import (
"github.com/ChainSafe/chaindb"
)

// SetupDatabase will return an instance of database based on basepath
func SetupDatabase(basepath string) (chaindb.Database, error) {
return chaindb.NewBadgerDB(&chaindb.Config{
DataDir: basepath,
})
}

// BaseState is a wrapper for the chaindb.Database, without any prefixes
type BaseState struct {
db chaindb.Database
Expand Down
17 changes: 3 additions & 14 deletions dot/state/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,26 @@ import (
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"

"github.com/ChainSafe/chaindb"
)

// Initialise initialises the genesis state of the DB using the given storage trie. The trie should be loaded with the genesis storage state.
// This only needs to be called during genesis initialisation of the node; it is not called during normal startup.
func (s *Service) Initialise(gen *genesis.Genesis, header *types.Header, t *trie.Trie) error {
var db chaindb.Database
cfg := &chaindb.Config{}

// check database type
if s.isMemDB {
cfg.InMemory = true
}

// get data directory from service
basepath, err := filepath.Abs(s.dbPath)
if err != nil {
return fmt.Errorf("failed to read basepath: %s", err)
}

cfg.DataDir = basepath

// initialise database using data directory
db, err = chaindb.NewBadgerDB(cfg)
db, err := utils.SetupDatabase(basepath, s.isMemDB)
if err != nil {
return fmt.Errorf("failed to create database: %s", err)
}

s.db = db

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

// check database type
if s.isMemDB {
// append memory database to state service
s.db = db

// append storage state and block state to state service
s.Storage = storageState
s.Block = blockState
Expand Down
18 changes: 4 additions & 14 deletions dot/state/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/blocktree"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"

"github.com/ChainSafe/chaindb"
log "github.com/ChainSafe/log15"
Expand Down Expand Up @@ -88,18 +89,15 @@ func (s *Service) Start() error {
}

db := s.db

if !s.isMemDB {
basepath, err := filepath.Abs(s.dbPath)
if err != nil {
return err
}

cfg := &chaindb.Config{
DataDir: basepath,
}

// initialise database
db, err = chaindb.NewBadgerDB(cfg)
db, err = utils.SetupDatabase(basepath, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -297,17 +295,9 @@ func (s *Service) Stop() error {
// Import imports the given state corresponding to the given header and sets the head of the chain
// to it. Additionally, it uses the first slot to correctly set the epoch number of the block.
func (s *Service) Import(header *types.Header, t *trie.Trie, firstSlot uint64) error {
cfg := &chaindb.Config{
DataDir: s.dbPath,
}

if s.isMemDB {
cfg.InMemory = true
}

var err error
// initialise database using data directory
s.db, err = chaindb.NewBadgerDB(cfg)
s.db, err = utils.SetupDatabase(s.dbPath, s.isMemDB)
if err != nil {
return fmt.Errorf("failed to create database: %s", err)
}
Expand Down
6 changes: 2 additions & 4 deletions dot/state/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
runtime "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"

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

db, err := chaindb.NewBadgerDB(&chaindb.Config{
DataDir: testDatadirPath,
InMemory: true,
})
db, err := utils.SetupDatabase(testDatadirPath, true)
require.NoError(t, err)
t.Cleanup(func() {
_ = db.Close()
Expand Down
5 changes: 1 addition & 4 deletions lib/blocktree/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ func newInMemoryDB(t *testing.T) chaindb.Database {
testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*")
require.NoError(t, err)

db, err := chaindb.NewBadgerDB(&chaindb.Config{
DataDir: testDatadirPath,
InMemory: true,
})
db, err := utils.SetupDatabase(testDatadirPath, true)
require.NoError(t, err)
t.Cleanup(func() {
db.Close()
Expand Down
9 changes: 2 additions & 7 deletions lib/grandpa/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"

"github.com/ChainSafe/chaindb"
"github.com/stretchr/testify/require"
)

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

cfg := &chaindb.Config{
DataDir: testDatadirPath,
InMemory: true,
}

db, err := chaindb.NewBadgerDB(cfg)
db, err := utils.SetupDatabase(testDatadirPath, true)
require.NoError(t, err)

block, err := state.NewBlockStateFromGenesis(db, testGenesisHeader)
Expand Down
8 changes: 2 additions & 6 deletions lib/trie/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,16 @@ import (
"testing"

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/stretchr/testify/require"
)

func newTestDB(t *testing.T) chaindb.Database {
// TODO: dynamically get os.TMPDIR
testDatadirPath, _ := ioutil.TempDir("/tmp", "test-datadir-*")

cfg := &chaindb.Config{
DataDir: testDatadirPath,
InMemory: true,
}

// TODO: don't initialise new DB but pass it in
db, err := chaindb.NewBadgerDB(cfg)
db, err := utils.SetupDatabase(testDatadirPath, true)
require.NoError(t, err)
return chaindb.NewTable(db, "trie")
}
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ import (
"github.com/dgraph-io/badger/v2"
)

// DefaultDatabaseDir directory inside basepath where database contents are stored
const DefaultDatabaseDir = "db"

// SetupDatabase will return an instance of database based on basepath
func SetupDatabase(basepath string, inMemory bool) (chaindb.Database, error) {
return chaindb.NewBadgerDB(&chaindb.Config{
DataDir: filepath.Join(basepath, DefaultDatabaseDir),
InMemory: inMemory,
})
}

// PathExists returns true if the named file or directory exists, otherwise false
func PathExists(p string) bool {
if _, err := os.Stat(p); err != nil {
Expand Down

0 comments on commit 8c67795

Please sign in to comment.