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 15, 2022
1 parent 4aec489 commit d08d1da
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 153 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 @@ -53,7 +53,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 @@ -72,7 +72,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 @@ -104,7 +104,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
2 changes: 1 addition & 1 deletion dot/state/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ func TestRange(t *testing.T) {
db := NewInMemoryDB(t)
blockState, err := NewBlockStateFromGenesis(db, newTriesEmpty(), genesisHeader, telemetryMock)

mockedDb := NewMockDatabase(ctrl)
mockedDb := NewMockBlockStateDatabase(ctrl)
// cannot assert the exact hash type since the block header
// hash is generate by the running test case
mockedDb.EXPECT().Get(gomock.AssignableToTypeOf([]byte{})).
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
51 changes: 49 additions & 2 deletions dot/state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,55 @@ import (
"github.com/ChainSafe/gossamer/lib/transaction"
)

type Database interface {
chaindb.Database
// 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.
Expand Down
2 changes: 1 addition & 1 deletion dot/state/mocks_generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

package state

//go:generate mockgen -destination=mocks_test.go -package $GOPACKAGE . Telemetry,Database
//go:generate mockgen -destination=mocks_test.go -package $GOPACKAGE . Telemetry,BlockStateDatabase
Loading

0 comments on commit d08d1da

Please sign in to comment.