From 9987afea4c5fe476f50872ff37a1a0f4eecb1c96 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 8 Jul 2022 19:42:06 +0300 Subject: [PATCH] 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. --- cli/server/server_test.go | 4 +- pkg/config/application_config.go | 42 +++++++++---------- pkg/core/bench_test.go | 5 ++- pkg/core/blockchain_neotest_test.go | 3 +- .../native/native_test/management_test.go | 5 ++- pkg/core/storage/boltdb_store.go | 8 +--- pkg/core/storage/boltdb_store_test.go | 3 +- pkg/core/storage/dbconfig/store_config.go | 21 ++++++++++ pkg/core/storage/leveldb_store.go | 8 +--- pkg/core/storage/leveldb_store_test.go | 5 ++- pkg/core/storage/store.go | 3 +- pkg/core/storage/store_config.go | 10 ----- 12 files changed, 63 insertions(+), 54 deletions(-) create mode 100644 pkg/core/storage/dbconfig/store_config.go delete mode 100644 pkg/core/storage/store_config.go diff --git a/cli/server/server_test.go b/cli/server/server_test.go index 741a0ced6..b55d5b600 100644 --- a/cli/server/server_test.go +++ b/cli/server/server_test.go @@ -9,7 +9,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/config" "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/stretchr/testify/require" "github.com/urfave/cli" @@ -349,7 +349,7 @@ func TestInitBlockChain(t *testing.T) { t.Run("empty logger", func(t *testing.T) { _, err := initBlockChain(config.Config{ ApplicationConfiguration: config.ApplicationConfiguration{ - DBConfiguration: storage.DBConfiguration{ + DBConfiguration: dbconfig.DBConfiguration{ Type: "inmemory", }, }, diff --git a/pkg/config/application_config.go b/pkg/config/application_config.go index 40322a15a..0a8cb6435 100644 --- a/pkg/config/application_config.go +++ b/pkg/config/application_config.go @@ -1,32 +1,32 @@ package config 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" ) // ApplicationConfiguration config specific to the node. type ApplicationConfiguration struct { - Address string `yaml:"Address"` - AnnouncedNodePort uint16 `yaml:"AnnouncedPort"` - AttemptConnPeers int `yaml:"AttemptConnPeers"` - DBConfiguration storage.DBConfiguration `yaml:"DBConfiguration"` - DialTimeout int64 `yaml:"DialTimeout"` - LogPath string `yaml:"LogPath"` - MaxPeers int `yaml:"MaxPeers"` - MinPeers int `yaml:"MinPeers"` - NodePort uint16 `yaml:"NodePort"` - PingInterval int64 `yaml:"PingInterval"` - PingTimeout int64 `yaml:"PingTimeout"` - Pprof BasicService `yaml:"Pprof"` - Prometheus BasicService `yaml:"Prometheus"` - ProtoTickInterval int64 `yaml:"ProtoTickInterval"` - Relay bool `yaml:"Relay"` - RPC rpc.Config `yaml:"RPC"` - UnlockWallet Wallet `yaml:"UnlockWallet"` - Oracle OracleConfiguration `yaml:"Oracle"` - P2PNotary P2PNotary `yaml:"P2PNotary"` - StateRoot StateRoot `yaml:"StateRoot"` + Address string `yaml:"Address"` + AnnouncedNodePort uint16 `yaml:"AnnouncedPort"` + AttemptConnPeers int `yaml:"AttemptConnPeers"` + DBConfiguration dbconfig.DBConfiguration `yaml:"DBConfiguration"` + DialTimeout int64 `yaml:"DialTimeout"` + LogPath string `yaml:"LogPath"` + MaxPeers int `yaml:"MaxPeers"` + MinPeers int `yaml:"MinPeers"` + NodePort uint16 `yaml:"NodePort"` + PingInterval int64 `yaml:"PingInterval"` + PingTimeout int64 `yaml:"PingTimeout"` + Pprof BasicService `yaml:"Pprof"` + Prometheus BasicService `yaml:"Prometheus"` + ProtoTickInterval int64 `yaml:"ProtoTickInterval"` + Relay bool `yaml:"Relay"` + RPC rpc.Config `yaml:"RPC"` + UnlockWallet Wallet `yaml:"UnlockWallet"` + Oracle OracleConfiguration `yaml:"Oracle"` + P2PNotary P2PNotary `yaml:"P2PNotary"` + StateRoot StateRoot `yaml:"StateRoot"` // ExtensiblePoolSize is the maximum amount of the extensible payloads from a single sender. ExtensiblePoolSize int `yaml:"ExtensiblePoolSize"` } diff --git a/pkg/core/bench_test.go b/pkg/core/bench_test.go index 8af373b80..2e6415a10 100644 --- a/pkg/core/bench_test.go +++ b/pkg/core/bench_test.go @@ -11,6 +11,7 @@ import ( "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/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/crypto/keys" "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 { dbPath := t.TempDir() - dbOptions := storage.LevelDBOptions{ + dbOptions := dbconfig.LevelDBOptions{ DataDirectoryPath: dbPath, } newLevelStore, err := storage.NewLevelDBStore(dbOptions) @@ -147,7 +148,7 @@ func newLevelDBForTesting(t testing.TB) storage.Store { func newBoltStoreForTesting(t testing.TB) storage.Store { d := t.TempDir() 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) return boltDBStore } diff --git a/pkg/core/blockchain_neotest_test.go b/pkg/core/blockchain_neotest_test.go index e53a12d94..15e8a56d3 100644 --- a/pkg/core/blockchain_neotest_test.go +++ b/pkg/core/blockchain_neotest_test.go @@ -28,6 +28,7 @@ import ( "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/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/crypto/hash" "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 == "" { dbPath = t.TempDir() } - dbOptions := storage.LevelDBOptions{ + dbOptions := dbconfig.LevelDBOptions{ DataDirectoryPath: dbPath, } newLevelStore, err := storage.NewLevelDBStore(dbOptions) diff --git a/pkg/core/native/native_test/management_test.go b/pkg/core/native/native_test/management_test.go index 50d1b894d..adadde1fd 100644 --- a/pkg/core/native/native_test/management_test.go +++ b/pkg/core/native/native_test/management_test.go @@ -11,6 +11,7 @@ import ( "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/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/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/io" @@ -281,9 +282,9 @@ func TestManagement_ContractDeploy(t *testing.T) { func TestManagement_StartFromHeight(t *testing.T) { // Create database to be able to start another chain from the same height later. ldbDir := t.TempDir() - dbConfig := storage.DBConfiguration{ + dbConfig := dbconfig.DBConfiguration{ Type: "leveldb", - LevelDBOptions: storage.LevelDBOptions{ + LevelDBOptions: dbconfig.LevelDBOptions{ DataDirectoryPath: ldbDir, }, } diff --git a/pkg/core/storage/boltdb_store.go b/pkg/core/storage/boltdb_store.go index 4045998b9..7464a9fcb 100644 --- a/pkg/core/storage/boltdb_store.go +++ b/pkg/core/storage/boltdb_store.go @@ -5,16 +5,12 @@ import ( "fmt" "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/util/slice" "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. var Bucket = []byte("DB") @@ -25,7 +21,7 @@ type BoltDBStore struct { } // 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 fileMode := os.FileMode(0600) // should be exposed via BoltDBOptions if anything needed fileName := cfg.FilePath diff --git a/pkg/core/storage/boltdb_store_test.go b/pkg/core/storage/boltdb_store_test.go index dac682318..e46c5b3d4 100644 --- a/pkg/core/storage/boltdb_store_test.go +++ b/pkg/core/storage/boltdb_store_test.go @@ -4,13 +4,14 @@ import ( "path/filepath" "testing" + "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" "github.com/stretchr/testify/require" ) func newBoltStoreForTesting(t testing.TB) Store { d := t.TempDir() testFileName := filepath.Join(d, "test_bolt_db") - boltDBStore, err := NewBoltDBStore(BoltDBOptions{FilePath: testFileName}) + boltDBStore, err := NewBoltDBStore(dbconfig.BoltDBOptions{FilePath: testFileName}) require.NoError(t, err) return boltDBStore } diff --git a/pkg/core/storage/dbconfig/store_config.go b/pkg/core/storage/dbconfig/store_config.go new file mode 100644 index 000000000..c2dde12b7 --- /dev/null +++ b/pkg/core/storage/dbconfig/store_config.go @@ -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"` + } +) diff --git a/pkg/core/storage/leveldb_store.go b/pkg/core/storage/leveldb_store.go index 24aa74891..c656f58bb 100644 --- a/pkg/core/storage/leveldb_store.go +++ b/pkg/core/storage/leveldb_store.go @@ -1,17 +1,13 @@ package storage import ( + "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/filter" "github.com/syndtr/goleveldb/leveldb/iterator" "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 // blockchain data. type LevelDBStore struct { @@ -21,7 +17,7 @@ type LevelDBStore struct { // NewLevelDBStore returns a new LevelDBStore object that will // 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 opts.Filter = filter.NewBloomFilter(10) diff --git a/pkg/core/storage/leveldb_store_test.go b/pkg/core/storage/leveldb_store_test.go index 24f21e76b..5bdb31828 100644 --- a/pkg/core/storage/leveldb_store_test.go +++ b/pkg/core/storage/leveldb_store_test.go @@ -3,14 +3,15 @@ package storage import ( "testing" + "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" "github.com/stretchr/testify/require" ) func newLevelDBForTesting(t testing.TB) Store { ldbDir := t.TempDir() - dbConfig := DBConfiguration{ + dbConfig := dbconfig.DBConfiguration{ Type: "leveldb", - LevelDBOptions: LevelDBOptions{ + LevelDBOptions: dbconfig.LevelDBOptions{ DataDirectoryPath: ldbDir, }, } diff --git a/pkg/core/storage/store.go b/pkg/core/storage/store.go index 4d2b1a550..3e2ef4c89 100644 --- a/pkg/core/storage/store.go +++ b/pkg/core/storage/store.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" + "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" "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. -func NewStore(cfg DBConfiguration) (Store, error) { +func NewStore(cfg dbconfig.DBConfiguration) (Store, error) { var store Store var err error switch cfg.Type { diff --git a/pkg/core/storage/store_config.go b/pkg/core/storage/store_config.go deleted file mode 100644 index 3521e32a1..000000000 --- a/pkg/core/storage/store_config.go +++ /dev/null @@ -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"` - } -)