Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dot/state): Implement online pruning of state tries. #1596

Merged
merged 28 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
18b8798
Implement state trie online pruning.
arijitAD May 17, 2021
4b49d26
Test state trie diff
arijitAD May 19, 2021
2325f98
Implement state trie online pruning
arijitAD May 22, 2021
238222d
Self review.
arijitAD May 27, 2021
7efc326
Merge branch 'development' into state-trie-online-pruning
arijitAD May 27, 2021
52705d5
Minor fix.
arijitAD May 27, 2021
00f54e6
Fix pruning for non canoncial chain.
arijitAD May 31, 2021
2b725f1
Move online pruner to storage state
arijitAD Jun 1, 2021
97ea1a1
Address comments.
arijitAD Jun 1, 2021
724dfe4
Merge branch 'development' into state-trie-online-pruning
arijitAD Jun 1, 2021
224dd60
Remove prune check from offline pruning.
arijitAD Jun 1, 2021
d0f765b
Merge branch 'development' into state-trie-online-pruning
arijitAD Jun 1, 2021
ff357b0
Add test for online pruning.
arijitAD Jun 3, 2021
862d706
Address comments.
arijitAD Jun 3, 2021
3790939
Merge branch 'development' into state-trie-online-pruning
arijitAD Jun 3, 2021
6a3e5af
Merge branch 'development' into state-trie-online-pruning
arijitAD Jun 4, 2021
402a642
Address comments.
arijitAD Jun 4, 2021
c219412
Merge branch 'development' into state-trie-online-pruning
arijitAD Jun 4, 2021
e2844a3
Temp
arijitAD Jun 8, 2021
abbf298
Merge remote-tracking branch 'origin/development' into state-trie-onl…
arijitAD Jun 8, 2021
a7cb2f1
Address comments.
arijitAD Jun 14, 2021
82b11d6
Minor change.
arijitAD Jun 14, 2021
b07af75
Minor fix.
arijitAD Jun 14, 2021
83f33c7
Merge remote-tracking branch 'origin/development' into state-trie-onl…
arijitAD Jun 14, 2021
3a8300b
Merge branch 'development' into state-trie-online-pruning
noot Jun 14, 2021
8faf10c
Fix tests.
arijitAD Jun 14, 2021
30289d2
Fix failing test.
arijitAD Jun 16, 2021
626f3b4
Merge remote-tracking branch 'origin/development' into state-trie-onl…
arijitAD Jun 17, 2021
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
5 changes: 5 additions & 0 deletions chain/dev/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ var (
// DefaultLvl is the default log level
DefaultLvl = log.LvlInfo

// DefaultPruningMode is the default pruning mode
DefaultPruningMode = "archive"
// DefaultRetainBlocks is the default retained blocks
DefaultRetainBlocks = int64(512)

// InitConfig

// DefaultGenesis is the default genesis configuration path
Expand Down
5 changes: 5 additions & 0 deletions chain/gssmr/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ var (
// DefaultLvl is the default log level
DefaultLvl = log.LvlInfo

// DefaultPruningMode is the default pruning mode
DefaultPruningMode = "archive"
// DefaultRetainBlocks is the default retained blocks
DefaultRetainBlocks = int64(512)

// InitConfig

// DefaultGenesis is the default genesis configuration path
Expand Down
5 changes: 5 additions & 0 deletions chain/kusama/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ var (
// DefaultLvl is the default log level
DefaultLvl = log.LvlInfo

// DefaultPruningMode is the default pruning mode
DefaultPruningMode = "archive"
// DefaultRetainBlocks is the default retained blocks
DefaultRetainBlocks = int64(512)

// InitConfig

// DefaultGenesis is the default genesis configuration path
Expand Down
5 changes: 5 additions & 0 deletions chain/polkadot/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ var (
// DefaultLvl is the default log level
DefaultLvl = log.LvlInfo

// DefaultPruningMode is the default pruning mode
DefaultPruningMode = "archive"
// DefaultRetainBlocks is the default pruning mode
DefaultRetainBlocks = int64(512)

// InitConfig

// DefaultGenesis is the default genesis configuration path
Expand Down
15 changes: 15 additions & 0 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
"strconv"
"strings"

"github.com/ChainSafe/gossamer/chain/dev"
"github.com/ChainSafe/gossamer/chain/gssmr"
"github.com/ChainSafe/gossamer/dot"
ctoml "github.com/ChainSafe/gossamer/dot/config/toml"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/state/pruner"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/genesis"
Expand Down Expand Up @@ -174,6 +176,14 @@ func createInitConfig(ctx *cli.Context) (*dot.Config, error) {
return nil, err
}

if !cfg.Global.Pruning.IsValid() {
return nil, fmt.Errorf("--%s must be either %s or %s", PruningFlag.Name, pruner.Full, pruner.Archive)
}

if cfg.Global.RetainBlocks < dev.DefaultRetainBlocks {
return nil, fmt.Errorf("--%s cannot be less than %d", RetainBlockNumberFlag.Name, dev.DefaultRetainBlocks)
}

// set log config
err = setLogConfig(ctx, tomlCfg, &cfg.Global, &cfg.Log)
if err != nil {
Expand Down Expand Up @@ -443,6 +453,9 @@ func setDotGlobalConfigFromToml(tomlCfg *ctoml.Config, cfg *dot.GlobalConfig) {
}

cfg.MetricsPort = tomlCfg.Global.MetricsPort

cfg.RetainBlocks = tomlCfg.Global.RetainBlocks
cfg.Pruning = pruner.Mode(tomlCfg.Global.Pruning)
}
}

Expand Down Expand Up @@ -472,6 +485,8 @@ func setDotGlobalConfigFromFlags(ctx *cli.Context, cfg *dot.GlobalConfig) {
cfg.MetricsPort = uint32(metricsPort)
}

cfg.RetainBlocks = ctx.Int64(RetainBlockNumberFlag.Name)
cfg.Pruning = pruner.Mode(ctx.String(PruningFlag.Name))
cfg.NoTelemetry = ctx.Bool("no-telemetry")
}

Expand Down
21 changes: 11 additions & 10 deletions cmd/gossamer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/ioutil"
"testing"

"github.com/ChainSafe/gossamer/chain/dev"
"github.com/ChainSafe/gossamer/chain/gssmr"
"github.com/ChainSafe/gossamer/dot"
"github.com/ChainSafe/gossamer/dot/state"
Expand All @@ -45,26 +46,26 @@ func TestConfigFromChainFlag(t *testing.T) {
}{
{
"Test gossamer --chain gssmr",
[]string{"chain", "name"},
[]interface{}{"gssmr", dot.GssmrConfig().Global.Name},
[]string{"chain", "name", "pruning", "retain-blocks"},
[]interface{}{"gssmr", dot.GssmrConfig().Global.Name, gssmr.DefaultPruningMode, gssmr.DefaultRetainBlocks},
dot.GssmrConfig(),
},
{
"Test gossamer --chain kusama",
[]string{"chain", "name"},
[]interface{}{"kusama", dot.KusamaConfig().Global.Name},
[]string{"chain", "name", "pruning", "retain-blocks"},
[]interface{}{"kusama", dot.KusamaConfig().Global.Name, gssmr.DefaultPruningMode, gssmr.DefaultRetainBlocks},
dot.KusamaConfig(),
},
{
"Test gossamer --chain polkadot",
[]string{"chain", "name"},
[]interface{}{"polkadot", dot.PolkadotConfig().Global.Name},
[]string{"chain", "name", "pruning", "retain-blocks"},
[]interface{}{"polkadot", dot.PolkadotConfig().Global.Name, gssmr.DefaultPruningMode, gssmr.DefaultRetainBlocks},
dot.PolkadotConfig(),
},
{
"Test gossamer --chain dev",
[]string{"chain", "name"},
[]interface{}{"dev", dot.DevConfig().Global.Name},
[]string{"chain", "name", "pruning", "retain-blocks"},
[]interface{}{"dev", dot.DevConfig().Global.Name, dev.DefaultPruningMode, dev.DefaultRetainBlocks},
dot.DevConfig(),
},
}
Expand Down Expand Up @@ -100,8 +101,8 @@ func TestInitConfigFromFlags(t *testing.T) {
}{
{
"Test gossamer --genesis",
[]string{"config", "genesis"},
[]interface{}{testCfgFile.Name(), "test_genesis"},
[]string{"config", "genesis", "pruning", "retain-blocks"},
[]interface{}{testCfgFile.Name(), "test_genesis", dev.DefaultPruningMode, dev.DefaultRetainBlocks},
dot.InitConfig{
Genesis: "test_genesis",
},
Expand Down
12 changes: 7 additions & 5 deletions cmd/gossamer/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ func dotConfigToToml(dcfg *dot.Config) *ctoml.Config {
cfg := &ctoml.Config{}

cfg.Global = ctoml.GlobalConfig{
Name: dcfg.Global.Name,
ID: dcfg.Global.ID,
BasePath: dcfg.Global.BasePath,
LogLvl: dcfg.Global.LogLvl.String(),
MetricsPort: dcfg.Global.MetricsPort,
Name: dcfg.Global.Name,
ID: dcfg.Global.ID,
BasePath: dcfg.Global.BasePath,
LogLvl: dcfg.Global.LogLvl.String(),
MetricsPort: dcfg.Global.MetricsPort,
RetainBlocks: dcfg.Global.RetainBlocks,
Pruning: string(dcfg.Global.Pruning),
}

cfg.Log = ctoml.LogConfig{
Expand Down
9 changes: 5 additions & 4 deletions cmd/gossamer/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/ioutil"
"testing"

"github.com/ChainSafe/gossamer/chain/gssmr"
"github.com/ChainSafe/gossamer/dot"
"github.com/ChainSafe/gossamer/lib/utils"

Expand Down Expand Up @@ -91,8 +92,8 @@ func TestExportCommand(t *testing.T) {
},
{
"Test gossamer export --config --genesis --bootnodes --log --force",
[]string{"config", "genesis", "bootnodes", "name", "force"},
[]interface{}{testConfig, genFile.Name(), testBootnode, "Gossamer", "true"},
[]string{"config", "genesis", "bootnodes", "name", "force", "pruning", "retain-blocks"},
[]interface{}{testConfig, genFile.Name(), testBootnode, "Gossamer", "true", gssmr.DefaultPruningMode, gssmr.DefaultRetainBlocks},
&dot.Config{
Global: testCfg.Global,
Init: dot.InitConfig{
Expand Down Expand Up @@ -122,8 +123,8 @@ func TestExportCommand(t *testing.T) {
},
{
"Test gossamer export --config --genesis --protocol --log --force",
[]string{"config", "genesis", "protocol", "force", "name"},
[]interface{}{testConfig, genFile.Name(), testProtocol, "true", "Gossamer"},
[]string{"config", "genesis", "protocol", "force", "name", "pruning", "retain-blocks"},
[]interface{}{testConfig, genFile.Name(), testProtocol, "true", "Gossamer", gssmr.DefaultPruningMode, gssmr.DefaultRetainBlocks},
&dot.Config{
Global: testCfg.Global,
Init: dot.InitConfig{
Expand Down
16 changes: 13 additions & 3 deletions cmd/gossamer/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"github.com/ChainSafe/gossamer/chain/dev"
log "github.com/ChainSafe/log15"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -280,10 +281,18 @@ var (
}

// RetainBlockNumberFlag retain number of block from latest block while pruning, valid for the use with prune-state subcommand
RetainBlockNumberFlag = cli.IntFlag{
RetainBlockNumberFlag = cli.Int64Flag{
Name: "retain-blocks",
Usage: "Retain number of block from latest block while pruning",
Value: 256,
Value: dev.DefaultRetainBlocks,
}

// PruningFlag triggers the online pruning of historical state tries. It's either full or archive. To enable pruning the value
// should be set to `full`.
PruningFlag = cli.StringFlag{
Name: "pruning",
Usage: `State trie online pruning ("full", "archive")`,
Value: dev.DefaultPruningMode,
}
)

Expand All @@ -301,7 +310,6 @@ var (
RewindFlag,
DBPathFlag,
BloomFilterSizeFlag,
RetainBlockNumberFlag,
}

// StartupFlags are flags that are valid for use with the root command and the export subcommand
Expand Down Expand Up @@ -346,6 +354,8 @@ var (
InitFlags = append([]cli.Flag{
ForceFlag,
GenesisFlag,
PruningFlag,
RetainBlockNumberFlag,
}, GlobalFlags...)

BuildSpecFlags = append([]cli.Flag{
Expand Down
25 changes: 13 additions & 12 deletions cmd/gossamer/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/ioutil"
"testing"

"github.com/ChainSafe/gossamer/chain/dev"
"github.com/ChainSafe/gossamer/dot"
"github.com/ChainSafe/gossamer/lib/utils"

Expand All @@ -43,24 +44,24 @@ func TestFixFlagOrder(t *testing.T) {
values []interface{}
}{
{
"Test gossamer --config --genesis --log --force",
[]string{"config", "genesis", "log", "force"},
[]interface{}{testConfig.Name(), genFile.Name(), "trace", true},
"Test gossamer --config --genesis --log --force --pruning --retain-blocks",
[]string{"config", "genesis", "log", "force", "pruning", "retain-blocks"},
[]interface{}{testConfig.Name(), genFile.Name(), "trace", true, dev.DefaultPruningMode, dev.DefaultRetainBlocks},
},
{
"Test gossamer --config --genesis --force --log",
[]string{"config", "genesis", "force", "log"},
[]interface{}{testConfig.Name(), genFile.Name(), true, "trace"},
"Test gossamer --config --genesis --force --log --pruning --retain-blocks",
[]string{"config", "genesis", "force", "log", "pruning", "retain-blocks"},
[]interface{}{testConfig.Name(), genFile.Name(), true, "trace", dev.DefaultPruningMode, dev.DefaultRetainBlocks},
},
{
"Test gossamer --config --force --genesis --log",
[]string{"config", "force", "genesis", "log"},
[]interface{}{testConfig.Name(), true, genFile.Name(), "trace"},
"Test gossamer --config --force --genesis --log ---pruning --retain-blocks",
[]string{"config", "force", "genesis", "log", "pruning", "retain-blocks"},
[]interface{}{testConfig.Name(), true, genFile.Name(), "trace", dev.DefaultPruningMode, dev.DefaultRetainBlocks},
},
{
"Test gossamer --force --config --genesis --log",
[]string{"force", "config", "genesis", "log"},
[]interface{}{true, testConfig.Name(), genFile.Name(), "trace"},
"Test gossamer --force --config --genesis --log --pruning --retain-blocks",
[]string{"force", "config", "genesis", "log", "pruning", "retain-blocks"},
[]interface{}{true, testConfig.Name(), genFile.Name(), "trace", dev.DefaultPruningMode, dev.DefaultRetainBlocks},
},
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/gossamer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ func initAction(ctx *cli.Context) error {
// expand data directory and update node configuration (performed separately
// from createDotConfig because dot config should not include expanded path)
cfg.Global.BasePath = utils.ExpandDir(cfg.Global.BasePath)

// check if node has been initialised (expected false - no warning log)
if dot.NodeInitialized(cfg.Global.BasePath, false) {

Expand Down Expand Up @@ -443,12 +442,12 @@ func pruneState(ctx *cli.Context) error {
bloomSize := ctx.GlobalUint64(BloomFilterSizeFlag.Name)
retainBlocks := ctx.GlobalInt64(RetainBlockNumberFlag.Name)

pruner, err := state.NewPruner(inputDBPath, prunedDBPath, bloomSize, retainBlocks)
pruner, err := state.NewOfflinePruner(inputDBPath, prunedDBPath, bloomSize, retainBlocks)
if err != nil {
return err
}

logger.Info("Pruner initialised")
logger.Info("Offline pruner initialised")

err = pruner.SetBloomFilter()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/gossamer/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func runPruneCmd(t *testing.T, configFile, prunedDBPath string) {
ctx, err := newTestContext(
"Test state trie offline pruning --prune-state",
[]string{"config", "pruned-db-path", "bloom-size", "retain-blocks"},
[]interface{}{configFile, prunedDBPath, "256", "5"},
[]interface{}{configFile, prunedDBPath, "256", int64(5)},
)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/gossamer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func newTestConfig(t *testing.T) *dot.Config {
LogLvl: log.LvlInfo,
PublishMetrics: dot.GssmrConfig().Global.PublishMetrics,
MetricsPort: dot.GssmrConfig().Global.MetricsPort,
RetainBlocks: dot.GssmrConfig().Global.RetainBlocks,
Pruning: dot.GssmrConfig().Global.Pruning,
},
Log: dot.LogConfig{
CoreLvl: log.LvlInfo,
Expand Down
7 changes: 7 additions & 0 deletions cmd/gossamer/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func newTestContext(description string, flags []string, values []interface{}) (*
set.String(flags[i], v, "")
case uint:
set.Uint(flags[i], v, "")
case int64:
set.Int64(flags[i], v, "")
default:
return nil, fmt.Errorf("unexpected cli value type: %T", values[i])
}
Expand Down Expand Up @@ -69,6 +71,11 @@ func newTestContext(description string, flags []string, values []interface{}) (*
if err != nil {
return nil, fmt.Errorf("failed to set cli flag: %T", flags[i])
}
case int64:
err := ctx.Set(flags[i], strconv.Itoa(int(values[i].(int64))))
if err != nil {
return nil, fmt.Errorf("failed to set cli flag: %T", flags[i])
}
default:
return nil, fmt.Errorf("unexpected cli value type: %T", values[i])
}
Expand Down
6 changes: 5 additions & 1 deletion dot/build_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ func BuildFromDB(path string) (*BuildSpec, error) {
tmpGen.Genesis.Raw = make(map[string]map[string]string)
tmpGen.Genesis.Runtime = make(map[string]map[string]interface{})

stateSrvc := state.NewService(path, log.LvlCrit)
config := state.Config{
Path: path,
LogLevel: log.LvlInfo,
}
stateSrvc := state.NewService(config)

// start state service (initialise state database)
err := stateSrvc.Start()
Expand Down
Loading