Skip to content

Commit

Permalink
Pruner config changes
Browse files Browse the repository at this point in the history
- Do not store and load pruning config from base state
- Move Config struct definition in `dot/state`
- Set pruning state service config from user config in `dot`
  • Loading branch information
qdm12 committed Sep 16, 2022
1 parent e582a4f commit 85c570d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 50 deletions.
6 changes: 5 additions & 1 deletion dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ func (nodeBuilder) createStateService(cfg *Config) (*state.Service, error) {
config := state.Config{
Path: cfg.Global.BasePath,
LogLevel: cfg.Log.StateLvl,
Metrics: metrics.NewIntervalConfig(cfg.Global.PublishMetrics),
PrunerCfg: state.PrunerConfig{
Enabled: cfg.Global.Pruning,
RetainBlocks: uint32(cfg.Global.RetainBlocks),
},
Metrics: metrics.NewIntervalConfig(cfg.Global.PublishMetrics),
}

stateSrvc := state.NewService(config)
Expand Down
26 changes: 0 additions & 26 deletions dot/state/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/internal/pruner"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/genesis"
)
Expand Down Expand Up @@ -156,28 +155,3 @@ func (s *BaseState) loadSlotDuration() (uint64, error) {

return binary.LittleEndian.Uint64(data), nil
}

// storePruningData stores the pruner configuration.
func (s *BaseState) storePruningData(mode pruner.Config) error {
encMode, err := json.Marshal(mode)
if err != nil {
return fmt.Errorf("cannot scale encode pruning mode: %s", err)
}

return s.db.Put(common.PruningKey, encMode)
}

// loadPruningData retrieves pruner configuration from db.
func (s *BaseState) loadPruningData() (config pruner.Config, err error) {
data, err := s.db.Get(common.PruningKey)
if err != nil {
return config, fmt.Errorf("getting database pruning key: %w", err)
}

err = json.Unmarshal(data, &config)
if err != nil {
return config, fmt.Errorf("unmarshaling pruning config: %w", err)
}

return config, nil
}
4 changes: 0 additions & 4 deletions dot/state/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ func (s *Service) storeInitialValues(data *genesis.Data, t *trie.Trie) error {
return fmt.Errorf("failed to write genesis data to database: %s", err)
}

if err := s.Base.storePruningData(s.PrunerCfg); err != nil {
return fmt.Errorf("failed to write pruning data to database: %s", err)
}

return nil
}

Expand Down
18 changes: 9 additions & 9 deletions dot/state/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Service struct {
Grandpa *GrandpaState
closeCh chan interface{}

PrunerCfg pruner.Config
PrunerCfg PrunerConfig
Telemetry telemetry.Client

// Below are for testing only.
Expand All @@ -49,11 +49,16 @@ type Service struct {
type Config struct {
Path string
LogLevel log.Level
PrunerCfg pruner.Config
PrunerCfg PrunerConfig
Telemetry telemetry.Client
Metrics metrics.IntervalConfig
}

type PrunerConfig struct {
Enabled bool
RetainBlocks uint32
}

// NewService create a new instance of Service
func NewService(config Config) *Service {
logger.Patch(log.SetLevel(config.LogLevel))
Expand Down Expand Up @@ -131,17 +136,12 @@ func (s *Service) Start() (err error) {
stateRoot := bestHeader.StateRoot
logger.Debugf("start with latest state root: %s", stateRoot)

prunerConfig, err := s.Base.loadPruningData()
if err != nil {
return fmt.Errorf("loading online pruner config: %w", err)
}

// create storage state
storageTable := chaindb.NewTable(s.db, storagePrefix)
journalTable := chaindb.NewTable(s.db, journalPrefix)
var p Pruner
if prunerConfig.Enabled {
p, err = pruner.NewFullNode(journalTable, storageTable, prunerConfig.RetainedBlocks, logger)
if s.PrunerCfg.Enabled {
p, err = pruner.NewFullNode(journalTable, storageTable, int64(s.PrunerCfg.RetainBlocks), logger)
if err != nil {
return fmt.Errorf("creating full node pruner: %w", err)
}
Expand Down
7 changes: 3 additions & 4 deletions dot/state/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ChainSafe/gossamer/dot/telemetry"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/internal/pruner"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/trie"
Expand Down Expand Up @@ -167,9 +166,9 @@ func TestService_StorageTriePruning(t *testing.T) {
config := Config{
Path: t.TempDir(),
LogLevel: log.Info,
PrunerCfg: pruner.Config{
Enabled: true,
RetainedBlocks: int64(retainBlocks),
PrunerCfg: PrunerConfig{
Enabled: true,
RetainBlocks: uint32(retainBlocks),
},
Telemetry: telemetryMock,
}
Expand Down
6 changes: 0 additions & 6 deletions internal/pruner/pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ const (
pruneInterval = time.Second
)

// Config holds state trie pruning mode and retained blocks
type Config struct {
Enabled bool
RetainedBlocks int64
}

type deathRecord struct {
blockHash common.Hash
deletedKeys map[common.Hash]int64 // Mapping from deleted key hash to block number.
Expand Down

0 comments on commit 85c570d

Please sign in to comment.