storage: move DB configuration into a package on its own

Lightweight thing to import anywhere, pkg/config should not be dependent on
Level/Bolt/anything else.
This commit is contained in:
Roman Khimov 2022-07-08 19:42:06 +03:00
parent dc59dc991b
commit 9987afea4c
12 changed files with 63 additions and 54 deletions

View file

@ -9,7 +9,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/nspcc-dev/neo-go/pkg/rpc" "github.com/nspcc-dev/neo-go/pkg/rpc"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -349,7 +349,7 @@ func TestInitBlockChain(t *testing.T) {
t.Run("empty logger", func(t *testing.T) { t.Run("empty logger", func(t *testing.T) {
_, err := initBlockChain(config.Config{ _, err := initBlockChain(config.Config{
ApplicationConfiguration: config.ApplicationConfiguration{ ApplicationConfiguration: config.ApplicationConfiguration{
DBConfiguration: storage.DBConfiguration{ DBConfiguration: dbconfig.DBConfiguration{
Type: "inmemory", Type: "inmemory",
}, },
}, },

View file

@ -1,7 +1,7 @@
package config package config
import ( import (
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/nspcc-dev/neo-go/pkg/rpc" "github.com/nspcc-dev/neo-go/pkg/rpc"
) )
@ -10,7 +10,7 @@ type ApplicationConfiguration struct {
Address string `yaml:"Address"` Address string `yaml:"Address"`
AnnouncedNodePort uint16 `yaml:"AnnouncedPort"` AnnouncedNodePort uint16 `yaml:"AnnouncedPort"`
AttemptConnPeers int `yaml:"AttemptConnPeers"` AttemptConnPeers int `yaml:"AttemptConnPeers"`
DBConfiguration storage.DBConfiguration `yaml:"DBConfiguration"` DBConfiguration dbconfig.DBConfiguration `yaml:"DBConfiguration"`
DialTimeout int64 `yaml:"DialTimeout"` DialTimeout int64 `yaml:"DialTimeout"`
LogPath string `yaml:"LogPath"` LogPath string `yaml:"LogPath"`
MaxPeers int `yaml:"MaxPeers"` MaxPeers int `yaml:"MaxPeers"`

View file

@ -11,6 +11,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
@ -136,7 +137,7 @@ func benchmarkForEachNEP17Transfer(t *testing.B, ps storage.Store, startFromBloc
func newLevelDBForTesting(t testing.TB) storage.Store { func newLevelDBForTesting(t testing.TB) storage.Store {
dbPath := t.TempDir() dbPath := t.TempDir()
dbOptions := storage.LevelDBOptions{ dbOptions := dbconfig.LevelDBOptions{
DataDirectoryPath: dbPath, DataDirectoryPath: dbPath,
} }
newLevelStore, err := storage.NewLevelDBStore(dbOptions) newLevelStore, err := storage.NewLevelDBStore(dbOptions)
@ -147,7 +148,7 @@ func newLevelDBForTesting(t testing.TB) storage.Store {
func newBoltStoreForTesting(t testing.TB) storage.Store { func newBoltStoreForTesting(t testing.TB) storage.Store {
d := t.TempDir() d := t.TempDir()
dbPath := filepath.Join(d, "test_bolt_db") dbPath := filepath.Join(d, "test_bolt_db")
boltDBStore, err := storage.NewBoltDBStore(storage.BoltDBOptions{FilePath: dbPath}) boltDBStore, err := storage.NewBoltDBStore(dbconfig.BoltDBOptions{FilePath: dbPath})
require.NoError(t, err) require.NoError(t, err)
return boltDBStore return boltDBStore
} }

View file

@ -28,6 +28,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/native/noderoles" "github.com/nspcc-dev/neo-go/pkg/core/native/noderoles"
"github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
@ -52,7 +53,7 @@ func newLevelDBForTestingWithPath(t testing.TB, dbPath string) (storage.Store, s
if dbPath == "" { if dbPath == "" {
dbPath = t.TempDir() dbPath = t.TempDir()
} }
dbOptions := storage.LevelDBOptions{ dbOptions := dbconfig.LevelDBOptions{
DataDirectoryPath: dbPath, DataDirectoryPath: dbPath,
} }
newLevelStore, err := storage.NewLevelDBStore(dbOptions) newLevelStore, err := storage.NewLevelDBStore(dbOptions)

View file

@ -11,6 +11,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
@ -281,9 +282,9 @@ func TestManagement_ContractDeploy(t *testing.T) {
func TestManagement_StartFromHeight(t *testing.T) { func TestManagement_StartFromHeight(t *testing.T) {
// Create database to be able to start another chain from the same height later. // Create database to be able to start another chain from the same height later.
ldbDir := t.TempDir() ldbDir := t.TempDir()
dbConfig := storage.DBConfiguration{ dbConfig := dbconfig.DBConfiguration{
Type: "leveldb", Type: "leveldb",
LevelDBOptions: storage.LevelDBOptions{ LevelDBOptions: dbconfig.LevelDBOptions{
DataDirectoryPath: ldbDir, DataDirectoryPath: ldbDir,
}, },
} }

View file

@ -5,16 +5,12 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/util/slice"
"go.etcd.io/bbolt" "go.etcd.io/bbolt"
) )
// BoltDBOptions configuration for boltdb.
type BoltDBOptions struct {
FilePath string `yaml:"FilePath"`
}
// Bucket represents bucket used in boltdb to store all the data. // Bucket represents bucket used in boltdb to store all the data.
var Bucket = []byte("DB") var Bucket = []byte("DB")
@ -25,7 +21,7 @@ type BoltDBStore struct {
} }
// NewBoltDBStore returns a new ready to use BoltDB storage with created bucket. // NewBoltDBStore returns a new ready to use BoltDB storage with created bucket.
func NewBoltDBStore(cfg BoltDBOptions) (*BoltDBStore, error) { func NewBoltDBStore(cfg dbconfig.BoltDBOptions) (*BoltDBStore, error) {
var opts *bbolt.Options // should be exposed via BoltDBOptions if anything needed var opts *bbolt.Options // should be exposed via BoltDBOptions if anything needed
fileMode := os.FileMode(0600) // should be exposed via BoltDBOptions if anything needed fileMode := os.FileMode(0600) // should be exposed via BoltDBOptions if anything needed
fileName := cfg.FilePath fileName := cfg.FilePath

View file

@ -4,13 +4,14 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func newBoltStoreForTesting(t testing.TB) Store { func newBoltStoreForTesting(t testing.TB) Store {
d := t.TempDir() d := t.TempDir()
testFileName := filepath.Join(d, "test_bolt_db") testFileName := filepath.Join(d, "test_bolt_db")
boltDBStore, err := NewBoltDBStore(BoltDBOptions{FilePath: testFileName}) boltDBStore, err := NewBoltDBStore(dbconfig.BoltDBOptions{FilePath: testFileName})
require.NoError(t, err) require.NoError(t, err)
return boltDBStore return boltDBStore
} }

View file

@ -0,0 +1,21 @@
/*
Package dbconfig is a micropackage that contains storage DB configuration options.
*/
package dbconfig
type (
// DBConfiguration describes configuration for DB. Supported: 'levelDB', 'boltDB'.
DBConfiguration struct {
Type string `yaml:"Type"`
LevelDBOptions LevelDBOptions `yaml:"LevelDBOptions"`
BoltDBOptions BoltDBOptions `yaml:"BoltDBOptions"`
}
// LevelDBOptions configuration for LevelDB.
LevelDBOptions struct {
DataDirectoryPath string `yaml:"DataDirectoryPath"`
}
// BoltDBOptions configuration for BoltDB.
BoltDBOptions struct {
FilePath string `yaml:"FilePath"`
}
)

View file

@ -1,17 +1,13 @@
package storage package storage
import ( import (
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/filter" "github.com/syndtr/goleveldb/leveldb/filter"
"github.com/syndtr/goleveldb/leveldb/iterator" "github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/opt" "github.com/syndtr/goleveldb/leveldb/opt"
) )
// LevelDBOptions configuration for LevelDB.
type LevelDBOptions struct {
DataDirectoryPath string `yaml:"DataDirectoryPath"`
}
// LevelDBStore is the official storage implementation for storing and retrieving // LevelDBStore is the official storage implementation for storing and retrieving
// blockchain data. // blockchain data.
type LevelDBStore struct { type LevelDBStore struct {
@ -21,7 +17,7 @@ type LevelDBStore struct {
// NewLevelDBStore returns a new LevelDBStore object that will // NewLevelDBStore returns a new LevelDBStore object that will
// initialize the database found at the given path. // initialize the database found at the given path.
func NewLevelDBStore(cfg LevelDBOptions) (*LevelDBStore, error) { func NewLevelDBStore(cfg dbconfig.LevelDBOptions) (*LevelDBStore, error) {
var opts = new(opt.Options) // should be exposed via LevelDBOptions if anything needed var opts = new(opt.Options) // should be exposed via LevelDBOptions if anything needed
opts.Filter = filter.NewBloomFilter(10) opts.Filter = filter.NewBloomFilter(10)

View file

@ -3,14 +3,15 @@ package storage
import ( import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func newLevelDBForTesting(t testing.TB) Store { func newLevelDBForTesting(t testing.TB) Store {
ldbDir := t.TempDir() ldbDir := t.TempDir()
dbConfig := DBConfiguration{ dbConfig := dbconfig.DBConfiguration{
Type: "leveldb", Type: "leveldb",
LevelDBOptions: LevelDBOptions{ LevelDBOptions: dbconfig.LevelDBOptions{
DataDirectoryPath: ldbDir, DataDirectoryPath: ldbDir,
}, },
} }

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig"
"github.com/syndtr/goleveldb/leveldb/util" "github.com/syndtr/goleveldb/leveldb/util"
) )
@ -125,7 +126,7 @@ func seekRangeToPrefixes(sr SeekRange) *util.Range {
} }
// NewStore creates storage with preselected in configuration database type. // NewStore creates storage with preselected in configuration database type.
func NewStore(cfg DBConfiguration) (Store, error) { func NewStore(cfg dbconfig.DBConfiguration) (Store, error) {
var store Store var store Store
var err error var err error
switch cfg.Type { switch cfg.Type {

View file

@ -1,10 +0,0 @@
package storage
type (
// DBConfiguration describes configuration for DB. Supported: 'levelDB', 'boltDB'.
DBConfiguration struct {
Type string `yaml:"Type"`
LevelDBOptions LevelDBOptions `yaml:"LevelDBOptions"`
BoltDBOptions BoltDBOptions `yaml:"BoltDBOptions"`
}
)