diff --git a/cmd/gossamer/config.go b/cmd/gossamer/config.go index 0784c515da..f055a1db44 100644 --- a/cmd/gossamer/config.go +++ b/cmd/gossamer/config.go @@ -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" @@ -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) } diff --git a/cmd/gossamer/config_test.go b/cmd/gossamer/config_test.go index 43726b84b8..2f2a3368de 100644 --- a/cmd/gossamer/config_test.go +++ b/cmd/gossamer/config_test.go @@ -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" @@ -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()) diff --git a/dot/node.go b/dot/node.go index 53e11a5c9a..59444d47e5 100644 --- a/dot/node.go +++ b/dot/node.go @@ -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" @@ -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" @@ -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) { @@ -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", @@ -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 } @@ -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 } diff --git a/dot/services.go b/dot/services.go index e74e073e81..8f8092720c 100644 --- a/dot/services.go +++ b/dot/services.go @@ -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 diff --git a/dot/state/base.go b/dot/state/base.go index 0d0ff47e81..f66d462096 100644 --- a/dot/state/base.go +++ b/dot/state/base.go @@ -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 diff --git a/dot/state/initialize.go b/dot/state/initialize.go index c1e73cedc1..11185a7862 100644 --- a/dot/state/initialize.go +++ b/dot/state/initialize.go @@ -28,6 +28,7 @@ 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" ) @@ -35,27 +36,18 @@ import ( // 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 { @@ -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 diff --git a/dot/state/service.go b/dot/state/service.go index 1c378c879e..dc3788d9bd 100644 --- a/dot/state/service.go +++ b/dot/state/service.go @@ -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" @@ -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 } @@ -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) } diff --git a/dot/state/test_helpers.go b/dot/state/test_helpers.go index cbb2e5c4af..f75faa3e0a 100644 --- a/dot/state/test_helpers.go +++ b/dot/state/test_helpers.go @@ -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" ) @@ -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() diff --git a/lib/blocktree/database_test.go b/lib/blocktree/database_test.go index efeee2ae9f..0f7804e14f 100644 --- a/lib/blocktree/database_test.go +++ b/lib/blocktree/database_test.go @@ -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() diff --git a/lib/grandpa/grandpa_test.go b/lib/grandpa/grandpa_test.go index 6d79c53ce8..511515d687 100644 --- a/lib/grandpa/grandpa_test.go +++ b/lib/grandpa/grandpa_test.go @@ -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" ) @@ -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) diff --git a/lib/trie/database_test.go b/lib/trie/database_test.go index 4e8fc12aed..ecda191550 100644 --- a/lib/trie/database_test.go +++ b/lib/trie/database_test.go @@ -23,6 +23,7 @@ import ( "testing" "github.com/ChainSafe/chaindb" + "github.com/ChainSafe/gossamer/lib/utils" "github.com/stretchr/testify/require" ) @@ -30,13 +31,8 @@ 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") } diff --git a/lib/utils/utils.go b/lib/utils/utils.go index f2e22e9f06..93127e64e4 100644 --- a/lib/utils/utils.go +++ b/lib/utils/utils.go @@ -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 {