Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils): create a specific folder for database #1598

Merged
merged 8 commits into from
May 21, 2021
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