Skip to content

Commit

Permalink
Improve sync service config and creation
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro committed Dec 19, 2024
1 parent 0edbcc3 commit 956d49d
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 125 deletions.
2 changes: 1 addition & 1 deletion chain/kusama/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func DefaultConfig() *cfg.Config {
config.Core.GrandpaAuthority = false
config.Core.Role = 1
config.Network.NoMDNS = false
config.Core.Sync = "full"
config.Core.SyncMode = cfg.FullSync

return config
}
2 changes: 1 addition & 1 deletion chain/paseo/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func DefaultConfig() *cfg.Config {
config.Core.GrandpaAuthority = false
config.Core.Role = 1
config.Network.NoMDNS = false
config.Core.Sync = "full"
config.Core.SyncMode = cfg.FullSync

return config
}
2 changes: 1 addition & 1 deletion chain/polkadot/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func DefaultConfig() *cfg.Config {
config.Core.GrandpaAuthority = false
config.Core.Role = 1
config.Network.NoMDNS = false
config.Core.Sync = "full"
config.Core.SyncMode = cfg.FullSync

return config
}
2 changes: 1 addition & 1 deletion chain/westend-dev/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func DefaultConfig() *cfg.Config {
config.RPC.UnsafeRPC = true
config.RPC.WSExternal = true
config.RPC.UnsafeWSExternal = true
config.Core.Sync = "full"
config.Core.SyncMode = cfg.FullSync

return config
}
2 changes: 1 addition & 1 deletion chain/westend-local/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func DefaultConfig() *cfg.Config {
config.RPC.UnsafeRPC = true
config.RPC.WSExternal = true
config.RPC.UnsafeWSExternal = true
config.Core.Sync = "full"
config.Core.SyncMode = cfg.FullSync

return config
}
Expand Down
2 changes: 1 addition & 1 deletion chain/westend/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func DefaultConfig() *cfg.Config {
config.Core.GrandpaAuthority = false
config.Core.Role = 1
config.Network.NoMDNS = false
config.Core.Sync = "full"
config.Core.SyncMode = cfg.FullSync

return config
}
15 changes: 9 additions & 6 deletions cmd/gossamer/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ var (
role string
// validator when set, the node will be an authority
validator bool
// Sync mode [warp | full]
syncMode string

// Account Config
// key to use for the node
Expand Down Expand Up @@ -102,6 +104,10 @@ Usage:
return fmt.Errorf("failed to parse role: %s", err)
}

if err := parseSyncMode(); err != nil {
return fmt.Errorf("failed to parse sync mode: %s", err)
}

if err := parseTelemetryURL(); err != nil {
return fmt.Errorf("failed to parse telemetry-url: %s", err.Error())
}
Expand Down Expand Up @@ -529,13 +535,10 @@ func addCoreFlags(cmd *cobra.Command) error {
return fmt.Errorf("failed to add --grandpa-interval flag: %s", err)
}

if err := addStringFlagBindViper(cmd,
cmd.Flags().StringVar(&syncMode,
"sync",
config.Core.Sync,
"sync mode [warp | full]",
"core.sync"); err != nil {
return fmt.Errorf("failed to add --sync flag: %s", err)
}
cfg.FullSync.String(),
"Sync mode. One of 'full' or 'warp'.")

return nil
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/gossamer/commands/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,23 @@ func parseRole() error {
return nil
}

// parseSyncMode parses the sync mode from the command line flags
func parseSyncMode() error {
var selectedSyncMode cfg.SyncMode
switch syncMode {
case cfg.FullSync.String():
selectedSyncMode = cfg.FullSync
case cfg.WarpSync.String():
selectedSyncMode = cfg.WarpSync
default:
return fmt.Errorf("invalid sync mode: %s", role)
}

config.Core.SyncMode = selectedSyncMode
viper.Set("core.syncMode", config.Core.SyncMode)
return nil
}

// parseTelemetryURL parses the telemetry-url from the command line flag
func parseTelemetryURL() error {
if telemetryURLs == "" {
Expand Down
23 changes: 18 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const (
DefaultSystemVersion = "0.0.0"

// DefaultSyncMode is the default block sync mode
DefaultSyncMode = "full"
DefaultSyncMode = FullSync
)

// DefaultRPCModules the default RPC modules
Expand Down Expand Up @@ -191,7 +191,7 @@ type CoreConfig struct {
GrandpaAuthority bool `mapstructure:"grandpa-authority"`
WasmInterpreter string `mapstructure:"wasm-interpreter,omitempty"`
GrandpaInterval time.Duration `mapstructure:"grandpa-interval,omitempty"`
Sync string `mapstructure:"sync,omitempty"`
SyncMode SyncMode `mapstructure:"sync,omitempty"`
}

// StateConfig contains the configuration for the state.
Expand Down Expand Up @@ -367,7 +367,7 @@ func DefaultConfig() *Config {
GrandpaAuthority: true,
WasmInterpreter: DefaultWasmInterpreter,
GrandpaInterval: DefaultDiscoveryInterval,
Sync: DefaultSyncMode,
SyncMode: DefaultSyncMode,
},
Network: &NetworkConfig{
Port: DefaultNetworkPort,
Expand Down Expand Up @@ -449,7 +449,7 @@ func DefaultConfigFromSpec(nodeSpec *genesis.Genesis) *Config {
GrandpaAuthority: true,
WasmInterpreter: DefaultWasmInterpreter,
GrandpaInterval: DefaultDiscoveryInterval,
Sync: DefaultSyncMode,
SyncMode: DefaultSyncMode,
},
Network: &NetworkConfig{
Port: DefaultNetworkPort,
Expand Down Expand Up @@ -531,7 +531,7 @@ func Copy(c *Config) Config {
GrandpaAuthority: c.Core.GrandpaAuthority,
WasmInterpreter: c.Core.WasmInterpreter,
GrandpaInterval: c.Core.GrandpaInterval,
Sync: c.Core.Sync,
SyncMode: c.Core.SyncMode,
},
Network: &NetworkConfig{
Port: c.Network.Port,
Expand Down Expand Up @@ -611,6 +611,19 @@ func (c Chain) String() string {
return string(c)
}

// SyncMode is a string representing a sync mode
type SyncMode string

const (
FullSync SyncMode = "full"
WarpSync SyncMode = "warp"
StateSync SyncMode = "state"
)

func (n SyncMode) String() string {
return string(n)
}

// NetworkRole is a string representing a network role
type NetworkRole string

Expand Down
65 changes: 8 additions & 57 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"strings"
"time"

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

Expand Down Expand Up @@ -38,8 +37,6 @@ import (
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
)

const blockRequestTimeout = 20 * time.Second

// BlockProducer to produce blocks
type BlockProducer interface {
Pause() error
Expand Down Expand Up @@ -524,65 +521,19 @@ func (nodeBuilder) newSyncService(config *cfg.Config, st *state.Service, fg sync
return nil, fmt.Errorf("failed to parse sync log level: %w", err)
}

// Should be shared between all sync strategies
peersView := sync.NewPeerViewSet()

var warpSyncStrategy sync.Strategy
var stateSyncStrategy sync.Strategy

if config.Core.Sync == "warp" {
warpSyncProvider := warpsync.NewWarpSyncProofProvider(st.Block, st.Grandpa)

warpSyncCfg := &sync.WarpSyncConfig{
Telemetry: telemetryMailer,
BadBlocks: genesisData.BadBlocks,
WarpSyncProvider: warpSyncProvider,
WarpSyncRequestMaker: net.GetRequestResponseProtocol(network.WarpSyncID,
blockRequestTimeout, network.MaxBlockResponseSize),
SyncRequestMaker: net.GetRequestResponseProtocol(network.SyncID,
blockRequestTimeout, network.MaxBlockResponseSize),
BlockState: st.Block,
Peers: peersView,
}

warpSyncStrategy = sync.NewWarpSyncStrategy(warpSyncCfg)

stateSyncCfg := &sync.StateSyncStrategyConfig{
Telemetry: telemetryMailer,
BadBlocks: genesisData.BadBlocks,
BlockState: st.Block,
Peers: peersView,
ReqMaker: net.GetRequestResponseProtocol(network.StateSyncID,
blockRequestTimeout, network.MaxBlockResponseSize),
StateStorage: st.Storage,
}

stateSyncStrategy = sync.NewStateSyncStrategy(stateSyncCfg)
}

syncCfg := &sync.FullSyncConfig{
BlockState: st.Block,
StorageState: st.Storage,
TransactionState: st.Transaction,
FinalityGadget: fg,
BabeVerifier: verifier,
BlockImportHandler: cs,
Telemetry: telemetryMailer,
BadBlocks: genesisData.BadBlocks,
RequestMaker: net.GetRequestResponseProtocol(network.SyncID,
blockRequestTimeout, network.MaxBlockResponseSize),
Peers: peersView,
}
fullSync := sync.NewFullSyncStrategy(syncCfg)

return sync.NewSyncService(
syncLogLevel,
sync.WithNetwork(net),
sync.WithBlockState(st.Block),
sync.WithGrandpaState(st.Grandpa),
sync.WithStorageState(st.Storage),
sync.WithFinalityGadget(fg),
sync.WithBabeVerifier(verifier),
sync.WithBlockImportHandler(cs),
sync.WithTelemetry(telemetryMailer),
sync.WithBadBlocks(genesisData.BadBlocks),
sync.WithSyncMethod(config.Core.SyncMode),
sync.WithSlotDuration(slotDuration),
sync.WithWarpSyncStrategy(warpSyncStrategy),
sync.WithFullSyncStrategy(fullSync),
sync.WithStateSyncStrategy(stateSyncStrategy),
sync.WithMinPeers(config.Network.MinPeers),
), nil
}
Expand Down
1 change: 1 addition & 0 deletions dot/sync/block_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type (

// StorageState is the interface for the storage state
StorageState interface {
StoreTrie(ts *rtstorage.TrieState, header *types.Header) error
TrieState(root *common.Hash) (*rtstorage.TrieState, error)
sync.Locker
}
Expand Down
64 changes: 52 additions & 12 deletions dot/sync/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,78 @@

package sync

import "time"
import (
"time"

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

type ServiceConfig func(svc *SyncService)

func WithWarpSyncStrategy(warpSyncStrategy Strategy) ServiceConfig {
func WithBlockState(bs BlockState) ServiceConfig {
return func(svc *SyncService) {
svc.warpSyncStrategy = warpSyncStrategy
svc.blockState = bs
}
}

func WithFullSyncStrategy(fullSyncStrategy Strategy) ServiceConfig {
func WithGrandpaState(gs GrandpaState) ServiceConfig {
return func(svc *SyncService) {
svc.fullSyncStrategy = fullSyncStrategy
svc.grandpaState = gs
}
}

func WithStateSyncStrategy(stateSyncStrategy Strategy) ServiceConfig {
func WithStorageState(ss StorageState) ServiceConfig {
return func(svc *SyncService) {
svc.stateSyncStrategy = stateSyncStrategy
svc.storageState = ss
}
}

func WithNetwork(net Network) ServiceConfig {
func WithFinalityGadget(fg FinalityGadget) ServiceConfig {
return func(svc *SyncService) {
svc.network = net
svc.workerPool = newSyncWorkerPool(net)
svc.finalityGadget = fg
}
}

func WithBlockState(bs BlockState) ServiceConfig {
func WithBabeVerifier(bv BabeVerifier) ServiceConfig {
return func(svc *SyncService) {
svc.blockState = bs
svc.babeVerifier = bv
}
}

func WithBlockImportHandler(bih BlockImportHandler) ServiceConfig {
return func(svc *SyncService) {
svc.blockImportHandler = bih
}
}

func WithTelemetry(t Telemetry) ServiceConfig {
return func(svc *SyncService) {
svc.telemetry = t
}
}

func WithBadBlocks(badBlocks []string) ServiceConfig {
return func(svc *SyncService) {
svc.badBlocks = badBlocks
}
}

func WithSyncMethod(method config.SyncMode) ServiceConfig {
return func(svc *SyncService) {
svc.syncStrategy = method
}
}

func WithTransactionState(ts TransactionState) ServiceConfig {
return func(svc *SyncService) {
svc.transactionState = ts
}
}

func WithNetwork(net Network) ServiceConfig {
return func(svc *SyncService) {
svc.network = net
svc.workerPool = newSyncWorkerPool(net)
}
}

Expand Down
Loading

0 comments on commit 956d49d

Please sign in to comment.