Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1282977
wip: introduce pebbledb, remove chaindb/badger
EclesioMeloJunior Jul 24, 2023
8ed732d
feat: replace chaindb with pebbledb
EclesioMeloJunior Jul 31, 2023
426fd88
chore: introduce pebble + remove chaindb
EclesioMeloJunior Aug 10, 2023
c0da568
chore: remove unneeded deltas
EclesioMeloJunior Aug 10, 2023
fc01e3f
chore: use `database.ErrNotFound` to wrap `pebble.ErrNotFound`
EclesioMeloJunior Aug 10, 2023
c3c6d6d
chore: add license
EclesioMeloJunior Aug 10, 2023
77fa81f
chore: remove errors wrappers over database iteractions
EclesioMeloJunior Aug 10, 2023
4d1f09d
replace `Key not found` with `pebble: not found`
EclesioMeloJunior Aug 10, 2023
0221d8b
chore: avoiding database twice
EclesioMeloJunior Aug 10, 2023
ec82fe5
chore: adjust `isNodeInitialised` to return `bool` instead of `error`
EclesioMeloJunior Aug 10, 2023
9098ffa
chore: closing db when finish tests
EclesioMeloJunior Aug 10, 2023
b041eec
chore: fix `testDelGetter` test
EclesioMeloJunior Aug 11, 2023
de692ac
chore: introduces `ClearDatabase` and add tests
EclesioMeloJunior Aug 11, 2023
31eaf13
chore: fix tests
EclesioMeloJunior Aug 11, 2023
535a25b
chore: remove `Close` method from `Table` interface
EclesioMeloJunior Aug 11, 2023
2e89be8
chore: `OfflinePruner.Prune` uses `database.Database` instead of pebb…
EclesioMeloJunior Aug 11, 2023
98258c5
chore: address comments
EclesioMeloJunior Aug 11, 2023
ec6d8a7
Merge branch 'development' into eclesio/introduce-pebbledb
EclesioMeloJunior Aug 11, 2023
8d94fbb
chore: resolve conflicts
EclesioMeloJunior Aug 11, 2023
8a4b747
chore: ignore `&pebble.Options` and move `SetupDatabase/ClearDatabase…
EclesioMeloJunior Aug 14, 2023
107a24b
chore: address README suggestion
EclesioMeloJunior Aug 14, 2023
d0717c4
chore: add license
EclesioMeloJunior Aug 14, 2023
755929b
chore: introduce `sync.WaitGroup` in babe
EclesioMeloJunior Aug 14, 2023
04f51e8
Merge branch 'development' into eclesio/introduce-pebbledb
EclesioMeloJunior Aug 14, 2023
7dd69fd
Merge branch 'development' into eclesio/introduce-pebbledb
EclesioMeloJunior Aug 15, 2023
806c9ef
Merge branch 'development' into eclesio/introduce-pebbledb
EclesioMeloJunior Aug 15, 2023
217bb7a
Merge branch 'development' into eclesio/introduce-pebbledb
EclesioMeloJunior Aug 15, 2023
85a3c59
chore: solve deepsource warns
EclesioMeloJunior Aug 15, 2023
50b4597
Merge branch 'eclesio/introduce-pebbledb' of github.com:ChainSafe/gos…
EclesioMeloJunior Aug 15, 2023
6494307
chore: address deepsource warns
EclesioMeloJunior Aug 15, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/gossamer/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ func execInit(cmd *cobra.Command) error {
return fmt.Errorf("failed to get --force: %s", err)
}

if dot.IsNodeInitialised(config.BasePath) {
isInitialised, err := dot.IsNodeInitialised(config.BasePath)
if err != nil {
return fmt.Errorf("checking if node is initialised: %w", err)
}

if isInitialised {
// prompt user to confirm reinitialization
if force || confirmMessage("Are you sure you want to reinitialise the node? [Y/n]") {
logger.Info("reinitialising node at base path " + config.BasePath + "...")
Expand Down
7 changes: 6 additions & 1 deletion cmd/gossamer/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,13 @@ func execRoot(cmd *cobra.Command) error {
return fmt.Errorf("failed to ensure root: %s", err)
}

isInitialised, err := dot.IsNodeInitialised(config.BasePath)
if err != nil {
return fmt.Errorf("failed to check is not is initialised: %w", err)
}

// if the node is not initialised, initialise it
if !dot.IsNodeInitialised(config.BasePath) {
if !isInitialised {
if err := dot.InitNode(config); err != nil {
return fmt.Errorf("failed to initialise node: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion dot/build_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestBuildFromDB(t *testing.T) {
},
}}},
{name: "invalid_db_path", path: t.TempDir(),
err: errors.New("cannot start state service: failed to create block state: cannot get block 0: Key not found")},
err: errors.New("cannot start state service: failed to create block state: cannot get block 0: pebble: not found")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions dot/mock_node_builder_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions dot/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ func (s *Service) publishNetworkTelemetry(done <-chan struct{}) {

func (s *Service) sentBlockIntervalTelemetry() {
for {
select {
case <-s.ctx.Done():
return
default:
}

best, err := s.blockState.BestBlockHeader()
if err != nil {
continue
Expand Down
42 changes: 29 additions & 13 deletions dot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Node struct {
}

type nodeBuilderIface interface {
isNodeInitialised(basepath string) error
isNodeInitialised(basepath string) (bool, error)
initNode(config *cfg.Config) error
createStateService(config *cfg.Config) (*state.Service, error)
createNetworkService(config *cfg.Config, stateSrvc *state.Service, telemetryMailer Telemetry) (*network.Service,
Expand Down Expand Up @@ -78,26 +78,37 @@ type nodeBuilder struct{}

// IsNodeInitialised returns true if, within the configured data directory for the
// node, the state database has been created and the genesis data can been loaded
func IsNodeInitialised(basepath string) bool {
func IsNodeInitialised(basepath string) (bool, error) {
nodeInstance := nodeBuilder{}
err := nodeInstance.isNodeInitialised(basepath)
return err == nil
return nodeInstance.isNodeInitialised(basepath)
}

// isNodeInitialised returns nil if the node is successfully initialised
// and an error otherwise.
func (*nodeBuilder) isNodeInitialised(basepath string) error {
func (*nodeBuilder) isNodeInitialised(basepath string) (bool, error) {
// check if key registry exists
registry := filepath.Join(basepath, utils.DefaultDatabaseDir, "KEYREGISTRY")
nodeDatabaseDir := filepath.Join(basepath, utils.DefaultDatabaseDir)

_, err := os.Stat(registry)
if os.IsNotExist(err) {
return fmt.Errorf("cannot find key registry in database directory: %w", err)
_, err := os.Stat(nodeDatabaseDir)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

entries, err := os.ReadDir(nodeDatabaseDir)
if err != nil {
return false, fmt.Errorf("failed to read dir %s: %w", nodeDatabaseDir, err)
}

if len(entries) == 0 {
return false, nil
}

db, err := utils.SetupDatabase(basepath, false)
if err != nil {
return fmt.Errorf("cannot setup database: %w", err)
return false, fmt.Errorf("cannot setup database: %w", err)
}

defer func() {
Expand All @@ -109,10 +120,10 @@ func (*nodeBuilder) isNodeInitialised(basepath string) error {

_, err = state.NewBaseState(db).LoadGenesisData()
if err != nil {
return fmt.Errorf("cannot load genesis data in base state: %w", err)
return false, fmt.Errorf("cannot load genesis data in base state: %w", err)
}

return nil
return true, nil
}

// InitNode initialise the node with the given Config
Expand Down Expand Up @@ -242,7 +253,12 @@ func newNode(config *cfg.Config,
debug.SetGCPercent(prev)
}

if builder.isNodeInitialised(config.BasePath) != nil {
isInitialised, err := builder.isNodeInitialised(config.BasePath)
if err != nil {
return nil, fmt.Errorf("checking if node is initialised: %w", err)
}

if isInitialised {
err := builder.initNode(config)
if err != nil {
return nil, fmt.Errorf("cannot initialise node: %w", err)
Expand Down
15 changes: 11 additions & 4 deletions dot/node_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestNewNode(t *testing.T) {
mockServiceRegistry.EXPECT().RegisterService(gomock.Any()).Times(8)

m := NewMocknodeBuilderIface(ctrl)
m.EXPECT().isNodeInitialised(initConfig.BasePath).Return(nil)
m.EXPECT().isNodeInitialised(initConfig.BasePath).Return(false, nil)
m.EXPECT().createStateService(initConfig).DoAndReturn(func(config *cfg.Config) (*state.Service, error) {
stateSrvc := state.NewService(stateConfig)
// create genesis from configuration file
Expand Down Expand Up @@ -218,6 +218,8 @@ func TestInitNode_Integration(t *testing.T) {
db, err := utils.SetupDatabase(config.BasePath, false)
require.NoError(t, err)
require.NotNil(t, db)
err = db.Close()
require.NoError(t, err)
}

func TestInitNode_GenesisSpec(t *testing.T) {
Expand All @@ -233,6 +235,9 @@ func TestInitNode_GenesisSpec(t *testing.T) {
db, err := utils.SetupDatabase(config.BasePath, false)
require.NoError(t, err)
require.NotNil(t, db)

err = db.Close()
require.NoError(t, err)
}

func TestNodeInitializedIntegration(t *testing.T) {
Expand All @@ -242,13 +247,15 @@ func TestNodeInitializedIntegration(t *testing.T) {

config.ChainSpec = genFile

result := IsNodeInitialised(config.BasePath)
result, err := IsNodeInitialised(config.BasePath)
require.NoError(t, err)
require.False(t, result)

err := InitNode(config)
err = InitNode(config)
require.NoError(t, err)

result = IsNodeInitialised(config.BasePath)
result, err = IsNodeInitialised(config.BasePath)
require.NoError(t, err)
require.True(t, result)
}

Expand Down
13 changes: 9 additions & 4 deletions dot/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ func TestInitNode(t *testing.T) {
err := InitNode(tt.config)
assert.ErrorIs(t, err, tt.err)
// confirm InitNode has created database dir
registry := filepath.Join(tt.config.BasePath, utils.DefaultDatabaseDir, "KEYREGISTRY")
_, err = os.Stat(registry)
nodeDatabaseDir := filepath.Join(tt.config.BasePath, utils.DefaultDatabaseDir)
_, err = os.Stat(nodeDatabaseDir)
require.NoError(t, err)

entries, err := os.ReadDir(nodeDatabaseDir)
require.NoError(t, err)
require.Greater(t, len(entries), 0)
})
}
}
Expand Down Expand Up @@ -86,7 +90,7 @@ func TestLoadGlobalNodeName(t *testing.T) {
{
name: "wrong basepath test",
basepath: t.TempDir(),
err: errors.New("Key not found"),
err: errors.New("pebble: not found"),
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -161,7 +165,8 @@ func TestNodeInitialized(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsNodeInitialised(tt.basepath)
got, err := IsNodeInitialised(tt.basepath)
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
Expand Down
6 changes: 3 additions & 3 deletions dot/rpc/modules/chain_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/database"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/runtime"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/golang/mock/gomock"

database "github.com/ChainSafe/chaindb"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestChainGetHeader_NotFound(t *testing.T) {
req := &ChainHashRequest{Bhash: &bhash}

err = svc.GetHeader(nil, req, res)
require.EqualError(t, err, database.ErrKeyNotFound.Error())
require.EqualError(t, err, database.ErrNotFound.Error())
}

func TestChainGetBlock_Genesis(t *testing.T) {
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestChainGetBlock_NoFound(t *testing.T) {
req := &ChainHashRequest{Bhash: &bhash}

err = svc.GetBlock(nil, req, res)
require.EqualError(t, err, database.ErrKeyNotFound.Error())
require.EqualError(t, err, database.ErrNotFound.Error())
}

func TestChainGetBlockHash_Latest(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions dot/rpc/modules/childstate_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"fmt"
"testing"

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/database"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestChildStateGetStorageSize(t *testing.T) {
keyChild: []byte(":not_exist"),
},
{
err: chaindb.ErrKeyNotFound,
err: database.ErrNotFound,
hash: &invalidHash,
},
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestGetStorageHash(t *testing.T) {
keyChild: []byte(":not_exist"),
},
{
err: chaindb.ErrKeyNotFound,
err: database.ErrNotFound,
hash: &invalidBlockHash,
},
}
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestGetChildStorage(t *testing.T) {
{params: []string{":child_storage_key", ""}, expected: nil},
{params: []string{":child_storage_key", ":child_first"}, expected: []byte(":child_first_value")},
{params: []string{":child_storage_key", ":child_first", blockHash.String()}, expected: []byte(":child_first_value")},
{params: []string{":child_storage_key", ":child_first", randomHash.String()}, errMsg: "Key not found"},
{params: []string{":child_storage_key", ":child_first", randomHash.String()}, errMsg: "pebble: not found"},
}

for _, test := range testCases {
Expand Down
10 changes: 5 additions & 5 deletions dot/rpc/modules/state_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

const (
RandomHash = "0x580d77a9136035a0bc3c3cd86286172f7f81291164c5914266073a30466fba21"
ErrKeyNotFound = "Key not found"
ErrKeyNotFound = "pebble: not found"
)

func TestStateModule_GetRuntimeVersion(t *testing.T) {
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestStateModule_GetPairs(t *testing.T) {
[]string{":key1", "value1"},
[]string{":key2", "value2"}}},
{params: []string{":key1", hash.String()}, expected: []interface{}{[]string{":key1", "value1"}}},
{params: []string{"", randomHash.String()}, errMsg: "Key not found"},
{params: []string{"", randomHash.String()}, errMsg: "pebble: not found"},
}

for _, test := range testCases {
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestStateModule_GetStorage(t *testing.T) {
{params: []string{""}, expected: nil},
{params: []string{":key1"}, expected: []byte("value1")},
{params: []string{":key1", hash.String()}, expected: []byte("value1")},
{params: []string{"", randomHash.String()}, errMsg: "Key not found"},
{params: []string{"", randomHash.String()}, errMsg: "pebble: not found"},
}

for _, test := range testCases {
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestStateModule_GetStorageHash(t *testing.T) {
{params: []string{""}, expected: hashOfNil},
{params: []string{":key1"}, expected: hash1},
{params: []string{":key1", hash.String()}, expected: hash1},
{params: []string{"0x", randomHash.String()}, errMsg: "Key not found"},
{params: []string{"0x", randomHash.String()}, errMsg: "pebble: not found"},
}

for _, test := range testCases {
Expand Down Expand Up @@ -280,7 +280,7 @@ func TestStateModule_GetStorageSize(t *testing.T) {
{params: []string{""}},
{params: []string{":key1"}, expected: 6},
{params: []string{":key1", hash.String()}, expected: 6},
{params: []string{"0x", randomHash.String()}, errMsg: "Key not found"},
{params: []string{"0x", randomHash.String()}, errMsg: "pebble: not found"},
}

for _, test := range testCases {
Expand Down
6 changes: 3 additions & 3 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

cfg "github.com/ChainSafe/gossamer/config"

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/dot/core"
"github.com/ChainSafe/gossamer/dot/digest"
"github.com/ChainSafe/gossamer/dot/network"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/ChainSafe/gossamer/dot/sync"
"github.com/ChainSafe/gossamer/dot/system"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/database"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/internal/metrics"
"github.com/ChainSafe/gossamer/internal/pprof"
Expand Down Expand Up @@ -56,7 +56,7 @@ type rpcServiceSettings struct {
syncer *sync.Service
}

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

Expand Down Expand Up @@ -110,7 +110,7 @@ func (nodeBuilder) createRuntimeStorage(st *state.Service) (*runtime.NodeStorage

return &runtime.NodeStorage{
LocalStorage: localStorage,
PersistentStorage: chaindb.NewTable(st.DB(), "offlinestorage"),
PersistentStorage: database.NewTable(st.DB(), "offlinestorage"),
BaseDB: st.Base,
}, nil
}
Expand Down
Loading