Skip to content

Commit

Permalink
feat(babe, epoch): retrieve first slot number correctly (#3841)
Browse files Browse the repository at this point in the history
Co-authored-by: Axay Sagathiya <[email protected]>
  • Loading branch information
EclesioMeloJunior and axaysagathiya authored Apr 15, 2024
1 parent a01d36a commit b9168a2
Show file tree
Hide file tree
Showing 43 changed files with 1,246 additions and 1,071 deletions.
3 changes: 2 additions & 1 deletion cmd/gossamer/commands/import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ func execImportState(cmd *cobra.Command) error {

basePath = utils.ExpandDir(basePath)

return dot.ImportState(basePath, stateFile, headerFile, stateTrieVersion, firstSlot)
return dot.ImportState(basePath, stateFile,
headerFile, stateTrieVersion, nil, firstSlot)
}
6 changes: 5 additions & 1 deletion dot/build_spec.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/telemetry"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/genesis"
Expand Down Expand Up @@ -98,10 +99,13 @@ func BuildFromDB(path string) (*BuildSpec, error) {
Path: path,
LogLevel: log.Info,
Telemetry: telemetry.NewNoopMailer(),
GenesisBABEConfig: &types.BabeConfiguration{
EpochLength: 10,
SlotDuration: 6,
},
}

stateSrvc := state.NewService(config)

err := stateSrvc.SetupBase()
if err != nil {
return nil, fmt.Errorf("cannot setup state database: %w", err)
Expand Down
15 changes: 9 additions & 6 deletions dot/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/ChainSafe/gossamer/pkg/trie"
"github.com/ChainSafe/gossamer/tests/utils/config"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -76,9 +77,10 @@ func createTestService(t *testing.T, genesisFilePath string,
telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes()

stateConfig := state.Config{
Path: testDatadirPath,
LogLevel: log.Critical,
Telemetry: telemetryMock,
Path: testDatadirPath,
LogLevel: log.Critical,
Telemetry: telemetryMock,
GenesisBABEConfig: config.BABEConfigurationTestDefault,
}

stateSrvc = state.NewService(stateConfig)
Expand Down Expand Up @@ -181,9 +183,10 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes()

config := state.Config{
Path: testDatadirPath,
LogLevel: log.Info,
Telemetry: telemetryMock,
Path: testDatadirPath,
LogLevel: log.Info,
Telemetry: telemetryMock,
GenesisBABEConfig: config.BABEConfigurationTestDefault,
}

stateSrvc = state.NewService(config)
Expand Down
6 changes: 4 additions & 2 deletions dot/digest/digest_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/ChainSafe/gossamer/tests/utils/config"
"go.uber.org/mock/gomock"

"github.com/stretchr/testify/require"
Expand All @@ -31,8 +32,9 @@ func newTestHandler(t *testing.T) (*Handler, *BlockImportHandler, *state.Service
telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes()

config := state.Config{
Path: testDatadirPath,
Telemetry: telemetryMock,
Path: testDatadirPath,
Telemetry: telemetryMock,
GenesisBABEConfig: config.BABEConfigurationTestDefault,
}
stateSrvc := state.NewService(config)
stateSrvc.UseMemDB()
Expand Down
8 changes: 5 additions & 3 deletions dot/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
)

// ImportState imports the state in the given files to the database with the given path.
func ImportState(basepath, stateFP, headerFP string, stateTrieVersion trie.TrieLayout, firstSlot uint64) error {
func ImportState(basepath, stateFP, headerFP string, stateTrieVersion trie.TrieLayout,
genesisBABEConfig *types.BabeConfiguration, firstSlot uint64) error {
tr, err := newTrieFromPairs(stateFP, trie.V0)
if err != nil {
return err
Expand All @@ -35,8 +36,9 @@ func ImportState(basepath, stateFP, headerFP string, stateTrieVersion trie.TrieL
logger.Infof("ImportState with header: %v", header)

config := state.Config{
Path: basepath,
LogLevel: log.Info,
Path: basepath,
LogLevel: log.Info,
GenesisBABEConfig: genesisBABEConfig,
}
srv := state.NewService(config)
return srv.Import(header, tr, stateTrieVersion, firstSlot)
Expand Down
31 changes: 17 additions & 14 deletions dot/import_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/ChainSafe/gossamer/pkg/trie"
"github.com/ChainSafe/gossamer/tests/utils/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -90,24 +91,25 @@ func TestNewHeaderFromFile(t *testing.T) {
}

func TestImportState_Integration(t *testing.T) {
config := DefaultTestWestendDevConfig(t)
defaultWestendDevConfig := DefaultTestWestendDevConfig(t)

genFile := NewTestGenesisRawFile(t, config)

config.ChainSpec = genFile
err := InitNode(config)
genFile := NewTestGenesisRawFile(t, defaultWestendDevConfig)
defaultWestendDevConfig.ChainSpec = genFile
err := InitNode(defaultWestendDevConfig)
require.NoError(t, err)

stateFP := setupStateFile(t)
headerFP := setupHeaderFile(t)

const firstSlot = uint64(262493679)
err = ImportState(config.BasePath, stateFP, headerFP, trie.V0, firstSlot)
firstSlot := uint64(1)
err = ImportState(defaultWestendDevConfig.BasePath, stateFP, headerFP,
trie.V0, config.BABEConfigurationTestDefault, firstSlot)
require.NoError(t, err)
// confirm data is imported into db
stateConfig := state.Config{
Path: config.BasePath,
LogLevel: log.Info,
Path: defaultWestendDevConfig.BasePath,
LogLevel: log.Info,
GenesisBABEConfig: config.BABEConfigurationTestDefault,
}
srv := state.NewService(stateConfig)
srv.SetupBase()
Expand All @@ -122,11 +124,11 @@ func TestImportState_Integration(t *testing.T) {
func TestImportState(t *testing.T) {
t.Parallel()

config := DefaultTestWestendDevConfig(t)
defaultWestendDevConfig := DefaultTestWestendDevConfig(t)

config.ChainSpec = NewTestGenesisRawFile(t, config)
defaultWestendDevConfig.ChainSpec = NewTestGenesisRawFile(t, defaultWestendDevConfig)
nodeInstance := nodeBuilder{}
err := nodeInstance.initNode(config)
err := nodeInstance.initNode(defaultWestendDevConfig)
require.NoError(t, err)

stateFP := setupStateFile(t)
Expand All @@ -151,7 +153,7 @@ func TestImportState(t *testing.T) {
{
name: "working_example",
args: args{
basepath: config.BasePath,
basepath: defaultWestendDevConfig.BasePath,
stateFP: stateFP,
headerFP: headerFP,
stateVersion: trie.V0,
Expand All @@ -164,7 +166,8 @@ func TestImportState(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

err := ImportState(tt.args.basepath, tt.args.stateFP, tt.args.headerFP, tt.args.stateVersion, tt.args.firstSlot)
err := ImportState(tt.args.basepath, tt.args.stateFP,
tt.args.headerFP, tt.args.stateVersion, config.BABEConfigurationTestDefault, tt.args.firstSlot)
if tt.err != nil {
assert.EqualError(t, err, tt.err.Error())
} else {
Expand Down
6 changes: 4 additions & 2 deletions dot/node_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"

"github.com/ChainSafe/gossamer/chain/westend"
"github.com/ChainSafe/gossamer/tests/utils/config"

cfg "github.com/ChainSafe/gossamer/config"
"github.com/ChainSafe/gossamer/dot/core"
Expand Down Expand Up @@ -73,8 +74,9 @@ func TestNewNode(t *testing.T) {
logLevel, err := log.ParseLevel(initConfig.Log.State)
require.NoError(t, err)
stateConfig := state.Config{
Path: initConfig.BasePath,
LogLevel: logLevel,
Path: initConfig.BasePath,
LogLevel: logLevel,
GenesisBABEConfig: config.BABEConfigurationTestDefault,
}

systemInfo := &types.SystemInfo{
Expand Down
8 changes: 5 additions & 3 deletions dot/rpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/tests/utils/config"
"github.com/libp2p/go-libp2p/core/peer"

"github.com/ChainSafe/gossamer/dot/core"
Expand Down Expand Up @@ -308,9 +309,10 @@ func newCoreServiceTest(t *testing.T) *core.Service {
telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes()

config := state.Config{
Path: testDatadirPath,
LogLevel: log.Debug,
Telemetry: telemetryMock,
Path: testDatadirPath,
LogLevel: log.Debug,
Telemetry: telemetryMock,
GenesisBABEConfig: config.BABEConfigurationTestDefault,
}

stateSrvc := state.NewService(config)
Expand Down
15 changes: 9 additions & 6 deletions dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/ChainSafe/gossamer/pkg/trie"
"github.com/ChainSafe/gossamer/tests/utils/config"
cscale "github.com/centrifuge/go-substrate-rpc-client/v4/scale"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
ctypes "github.com/centrifuge/go-substrate-rpc-client/v4/types"
Expand Down Expand Up @@ -661,9 +662,10 @@ func setupStateAndRuntime(t *testing.T, basepath string, useInstance useRuntimeI
)

state2test := state.NewService(state.Config{
LogLevel: log.DoNotChange,
Path: basepath,
Telemetry: telemetryMock,
LogLevel: log.DoNotChange,
Path: basepath,
Telemetry: telemetryMock,
GenesisBABEConfig: config.BABEConfigurationTestDefault,
})
state2test.UseMemDB()

Expand Down Expand Up @@ -721,9 +723,10 @@ func setupStateAndPopulateTrieState(t *testing.T, basepath string,
)

state2test := state.NewService(state.Config{
LogLevel: log.DoNotChange,
Path: basepath,
Telemetry: telemetryMock,
LogLevel: log.DoNotChange,
Path: basepath,
Telemetry: telemetryMock,
GenesisBABEConfig: config.BABEConfigurationTestDefault,
})
state2test.UseMemDB()

Expand Down
8 changes: 5 additions & 3 deletions dot/rpc/modules/chain_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/ChainSafe/gossamer/pkg/trie"
"github.com/ChainSafe/gossamer/tests/utils/config"
"go.uber.org/mock/gomock"

rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
Expand Down Expand Up @@ -344,9 +345,10 @@ func newTestStateService(t *testing.T) *state.Service {
telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes()

config := state.Config{
Path: testDatadirPath,
LogLevel: log.Info,
Telemetry: telemetryMock,
Path: testDatadirPath,
LogLevel: log.Info,
Telemetry: telemetryMock,
GenesisBABEConfig: config.BABEConfigurationTestDefault,
}
stateSrvc := state.NewService(config)
stateSrvc.UseMemDB()
Expand Down
14 changes: 2 additions & 12 deletions dot/rpc/modules/dev_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,18 @@ import (
"testing"

"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/babe"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
inmemory_trie "github.com/ChainSafe/gossamer/pkg/trie/inmemory"
"github.com/ChainSafe/gossamer/tests/utils/config"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

var genesisBABEConfig = &types.BabeConfiguration{
SlotDuration: 1000,
EpochLength: 200,
C1: 1,
C2: 4,
GenesisAuthorities: []types.AuthorityRaw{},
Randomness: [32]byte{},
SecondarySlots: 0,
}

func newState(t *testing.T) (*state.BlockState, *state.EpochState) {
ctrl := gomock.NewController(t)
telemetryMock := NewMockTelemetry(ctrl)
Expand All @@ -44,7 +34,7 @@ func newState(t *testing.T) (*state.BlockState, *state.EpochState) {
tries.SetTrie(genesisTrie)
bs, err := state.NewBlockStateFromGenesis(db, tries, &genesisHeader, telemetryMock)
require.NoError(t, err)
es, err := state.NewEpochStateFromGenesis(db, bs, genesisBABEConfig)
es, err := state.NewEpochStateFromGenesis(db, bs, config.BABEConfigurationTestDefault)
require.NoError(t, err)
return bs, es
}
Expand Down
41 changes: 38 additions & 3 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/grandpa"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
)

Expand Down Expand Up @@ -63,14 +65,47 @@ func newInMemoryDB() (database.Database, error) {
func (nodeBuilder) createStateService(config *cfg.Config) (*state.Service, error) {
logger.Debug("creating state service...")

gen, err := genesis.NewGenesisFromJSONRaw(config.ChainSpec)
if err != nil {
return nil, fmt.Errorf("genesis from json raw: %w", err)
}

if !gen.IsRaw() {
return nil, fmt.Errorf("genesis should be raw")
}

genTrie, err := runtime.NewTrieFromGenesis(*gen)
if err != nil {
return nil, fmt.Errorf("creating trie from genesis: %w", err)
}

// create genesis runtime
rtCfg := wazero_runtime.Config{
LogLvl: log.Critical,
Storage: rtstorage.NewTrieState(genTrie),
}

genesisRuntime, err := wazero_runtime.NewRuntimeFromGenesis(rtCfg)
if err != nil {
return nil, fmt.Errorf("instantiating genesis runtime: %w", err)
}
defer genesisRuntime.Stop()

babeCfg, err := genesisRuntime.BabeConfiguration()
if err != nil {
return nil, fmt.Errorf("getting babe configuration: %w", err)
}

stateLogLevel, err := log.ParseLevel(config.Log.State)
if err != nil {
return nil, err
}

stateConfig := state.Config{
Path: config.BasePath,
LogLevel: stateLogLevel,
Metrics: metrics.NewIntervalConfig(config.PrometheusExternal),
Path: config.BasePath,
LogLevel: stateLogLevel,
Metrics: metrics.NewIntervalConfig(config.PrometheusExternal),
GenesisBABEConfig: babeCfg,
}

stateSrvc := state.NewService(stateConfig)
Expand Down
Loading

0 comments on commit b9168a2

Please sign in to comment.