Skip to content

Commit

Permalink
Local interfaces instead of chaindb.Database
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Dec 14, 2022
1 parent 59bfaf6 commit c6e9924
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 36 deletions.
2 changes: 1 addition & 1 deletion dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type rpcServiceSettings struct {
syncer *sync.Service
}

func newInMemoryDB() (chaindb.Database, error) {
func newInMemoryDB() (*chaindb.BadgerDB, error) {
return utils.SetupDatabase("", true)
}

Expand Down
7 changes: 3 additions & 4 deletions dot/state/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ import (
"encoding/json"
"fmt"

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/dot/state/pruner"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/genesis"
)

// BaseState is a wrapper for the chaindb.Database, without any prefixes
// BaseState is a wrapper for a database, without any prefixes
type BaseState struct {
db chaindb.Database
db GetPutDeleter
}

// NewBaseState returns a new BaseState
func NewBaseState(db chaindb.Database) *BaseState {
func NewBaseState(db GetPutDeleter) *BaseState {
return &BaseState{
db: db,
}
Expand Down
6 changes: 3 additions & 3 deletions dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type BlockState struct {
bt *blocktree.BlockTree
baseState *BaseState
dbPath string
db chaindb.Database
db BlockStateDatabase
sync.RWMutex
genesisHash common.Hash
lastFinalised common.Hash
Expand All @@ -71,7 +71,7 @@ type BlockState struct {
}

// NewBlockState will create a new BlockState backed by the database located at basePath
func NewBlockState(db chaindb.Database, trs *Tries, telemetry Telemetry) (*BlockState, error) {
func NewBlockState(db *chaindb.BadgerDB, trs *Tries, telemetry Telemetry) (*BlockState, error) {
bs := &BlockState{
dbPath: db.Path(),
baseState: NewBaseState(db),
Expand Down Expand Up @@ -103,7 +103,7 @@ func NewBlockState(db chaindb.Database, trs *Tries, telemetry Telemetry) (*Block

// NewBlockStateFromGenesis initialises a BlockState from a genesis header,
// saving it to the database located at basePath
func NewBlockStateFromGenesis(db chaindb.Database, trs *Tries, header *types.Header,
func NewBlockStateFromGenesis(db *chaindb.BadgerDB, trs *Tries, header *types.Header,
telemetryMailer Telemetry) (*BlockState, error) {
bs := &BlockState{
bt: blocktree.NewBlockTreeFromRoot(header),
Expand Down
2 changes: 1 addition & 1 deletion dot/state/block_race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestConcurrencySetHeader(t *testing.T) {
telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes()

threads := runtime.NumCPU()
dbs := make([]chaindb.Database, threads)
dbs := make([]*chaindb.BadgerDB, threads)
for i := 0; i < threads; i++ {
dbs[i] = NewInMemoryDB(t)
}
Expand Down
6 changes: 3 additions & 3 deletions dot/state/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func configDataKey(epoch uint64) []byte {

// EpochState tracks information related to each epoch
type EpochState struct {
db chaindb.Database
db GetPutter
baseState *BaseState
blockState *BlockState
epochLength uint64 // measured in slots
Expand All @@ -68,7 +68,7 @@ type EpochState struct {
}

// NewEpochStateFromGenesis returns a new EpochState given information for the first epoch, fetched from the runtime
func NewEpochStateFromGenesis(db chaindb.Database, blockState *BlockState,
func NewEpochStateFromGenesis(db *chaindb.BadgerDB, blockState *BlockState,
genesisConfig *types.BabeConfiguration) (*EpochState, error) {
baseState := NewBaseState(db)

Expand Down Expand Up @@ -134,7 +134,7 @@ func NewEpochStateFromGenesis(db chaindb.Database, blockState *BlockState,
}

// NewEpochState returns a new EpochState
func NewEpochState(db chaindb.Database, blockState *BlockState) (*EpochState, error) {
func NewEpochState(db *chaindb.BadgerDB, blockState *BlockState) (*EpochState, error) {
baseState := NewBaseState(db)

epochLength, err := baseState.loadEpochLength()
Expand Down
6 changes: 3 additions & 3 deletions dot/state/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ var (

// GrandpaState tracks information related to grandpa
type GrandpaState struct {
db chaindb.Database
db GetPutDeleter
blockState *BlockState

forcedChanges *orderedPendingChanges
scheduledChangeRoots *changeTree
}

// NewGrandpaStateFromGenesis returns a new GrandpaState given the grandpa genesis authorities
func NewGrandpaStateFromGenesis(db chaindb.Database, bs *BlockState,
func NewGrandpaStateFromGenesis(db *chaindb.BadgerDB, bs *BlockState,
genesisAuthorities []types.GrandpaVoter) (*GrandpaState, error) {
grandpaDB := chaindb.NewTable(db, grandpaPrefix)
s := &GrandpaState{
Expand Down Expand Up @@ -73,7 +73,7 @@ func NewGrandpaStateFromGenesis(db chaindb.Database, bs *BlockState,
}

// NewGrandpaState returns a new GrandpaState
func NewGrandpaState(db chaindb.Database, bs *BlockState) *GrandpaState {
func NewGrandpaState(db *chaindb.BadgerDB, bs *BlockState) *GrandpaState {
return &GrandpaState{
db: chaindb.NewTable(db, grandpaPrefix),
blockState: bs,
Expand Down
2 changes: 1 addition & 1 deletion dot/state/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestGrandpaState_LatestRound(t *testing.T) {
require.Equal(t, uint64(99), r)
}

func testBlockState(t *testing.T, db chaindb.Database) *BlockState {
func testBlockState(t *testing.T, db *chaindb.BadgerDB) *BlockState {
ctrl := gomock.NewController(t)
telemetryMock := NewMockTelemetry(ctrl)
telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes()
Expand Down
52 changes: 52 additions & 0 deletions dot/state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,65 @@ package state
import (
"encoding/json"

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/transaction"
)

// GetPutDeleter has methods to get, put and delete key values.
type GetPutDeleter interface {
GetPutter
Deleter
}

// BlockStateDatabase is the database interface for the block state.
type BlockStateDatabase interface {
GetPutDeleter
Haser
NewBatcher
}

// GetPutter has methods to get and put key values.
type GetPutter interface {
Getter
Putter
}

// GetNewBatcher has methods to get values and create a
// new batch.
type GetNewBatcher interface {
Getter
NewBatcher
}

// Getter gets a value corresponding to the given key.
type Getter interface {
Get(key []byte) (value []byte, err error)
}

// Putter puts a value at the given key and returns an error.
type Putter interface {
Put(key []byte, value []byte) error
}

// Deleter deletes a value at the given key and returns an error.
type Deleter interface {
Del(key []byte) error
}

// Haser checks if a value exists at the given key and returns an error.
type Haser interface {
Has(key []byte) (has bool, err error)
}

// NewBatcher creates a new database batch.
type NewBatcher interface {
NewBatch() chaindb.Batch
}

// Runtime interface.
type Runtime interface {
UpdateRuntimeCode([]byte) error
Expand Down
4 changes: 2 additions & 2 deletions dot/state/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var logger = log.NewFromGlobal(
type Service struct {
dbPath string
logLvl log.Level
db chaindb.Database
db *chaindb.BadgerDB
isMemDB bool // set to true if using an in-memory database; only used for testing.
Base *BaseState
Storage *StorageState
Expand Down Expand Up @@ -78,7 +78,7 @@ func (s *Service) UseMemDB() {
}

// DB returns the Service's database
func (s *Service) DB() chaindb.Database {
func (s *Service) DB() *chaindb.BadgerDB {
return s.db
}

Expand Down
8 changes: 2 additions & 6 deletions dot/state/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type StorageState struct {
blockState *BlockState
tries *Tries

db chaindb.Database
db GetNewBatcher
sync.RWMutex

// change notifiers
Expand All @@ -44,12 +44,8 @@ type StorageState struct {

// NewStorageState creates a new StorageState backed by the given block state
// and database located at basePath.
func NewStorageState(db chaindb.Database, blockState *BlockState,
func NewStorageState(db *chaindb.BadgerDB, blockState *BlockState,
tries *Tries, onlinePruner pruner.Config) (*StorageState, error) {
if db == nil {
return nil, fmt.Errorf("cannot have nil database")
}

storageTable := chaindb.NewTable(db, storagePrefix)

var p pruner.Pruner
Expand Down
2 changes: 1 addition & 1 deletion dot/state/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
var inc, _ = time.ParseDuration("1s")

// NewInMemoryDB creates a new in-memory database
func NewInMemoryDB(t *testing.T) chaindb.Database {
func NewInMemoryDB(t *testing.T) *chaindb.BadgerDB {
testDatadirPath := t.TempDir()

db, err := utils.SetupDatabase(testDatadirPath, true)
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

// NewInMemoryDB creates a new in-memory database
func NewInMemoryDB(t *testing.T) chaindb.Database {
func NewInMemoryDB(t *testing.T) *chaindb.BadgerDB {
testDatadirPath := t.TempDir()

db, err := chaindb.NewBadgerDB(&chaindb.Config{
Expand Down
27 changes: 18 additions & 9 deletions lib/trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@ import (
"github.com/ChainSafe/chaindb"
)

// Database is an interface to get values from a
// key value database.
type Database interface {
// Getter gets a value corresponding to the given key.
type Getter interface {
Get(key []byte) (value []byte, err error)
}

// Putter puts a value at the given key and returns an error.
type Putter interface {
Put(key []byte, value []byte) error
}

// NewBatcher creates a new database batch.
type NewBatcher interface {
NewBatch() chaindb.Batch
}

// Load reconstructs the trie from the database from the given root hash.
// It is used when restarting the node to load the current state trie.
func (t *Trie) Load(db Database, rootHash common.Hash) error {
func (t *Trie) Load(db Getter, rootHash common.Hash) error {
if rootHash == EmptyHash {
t.root = nil
return nil
Expand All @@ -46,7 +55,7 @@ func (t *Trie) Load(db Database, rootHash common.Hash) error {
return t.loadNode(db, t.root)
}

func (t *Trie) loadNode(db Database, n *Node) error {
func (t *Trie) loadNode(db Getter, n *Node) error {
if n.Kind() != node.Branch {
return nil
}
Expand Down Expand Up @@ -155,7 +164,7 @@ func PopulateNodeHashes(n *Node, nodeHashes map[string]struct{}) {
// It recursively descends into the trie using the database starting
// from the root node until it reaches the node with the given key.
// It then reads the value from the database.
func GetFromDB(db chaindb.Database, rootHash common.Hash, key []byte) (
func GetFromDB(db Getter, rootHash common.Hash, key []byte) (
value []byte, err error) {
if rootHash == EmptyHash {
return nil, nil
Expand All @@ -181,7 +190,7 @@ func GetFromDB(db chaindb.Database, rootHash common.Hash, key []byte) (
// for the value corresponding to a key.
// Note it does not copy the value so modifying the value bytes
// slice will modify the value of the node in the trie.
func getFromDBAtNode(db chaindb.Database, n *Node, key []byte) (
func getFromDBAtNode(db Getter, n *Node, key []byte) (
value []byte, err error) {
if n.Kind() == node.Leaf {
if bytes.Equal(n.PartialKey, key) {
Expand Down Expand Up @@ -237,7 +246,7 @@ func getFromDBAtNode(db chaindb.Database, n *Node, key []byte) (
}

// WriteDirty writes all dirty nodes to the database and sets them to clean
func (t *Trie) WriteDirty(db chaindb.Database) error {
func (t *Trie) WriteDirty(db NewBatcher) error {
batch := db.NewBatch()
err := t.writeDirtyNode(batch, t.root)
if err != nil {
Expand All @@ -248,7 +257,7 @@ func (t *Trie) WriteDirty(db chaindb.Database) error {
return batch.Flush()
}

func (t *Trie) writeDirtyNode(db chaindb.Batch, n *Node) (err error) {
func (t *Trie) writeDirtyNode(db Putter, n *Node) (err error) {
if n == nil || !n.Dirty {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
const DefaultDatabaseDir = "db"

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

0 comments on commit c6e9924

Please sign in to comment.