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

Refactored solution #6008

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 2 additions & 23 deletions cmd/node/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,8 @@ func applyCompatibleConfigs(log logger.Logger, configs *config.Configs) error {

isInHistoricalBalancesMode := operationmodes.SliceContainsElement(operationModes, operationmodes.OperationModeHistoricalBalances)
if isInHistoricalBalancesMode {
processHistoricalBalancesMode(log, configs)
// TODO move all operation modes settings in the common/operationmodes package and add tests
operationmodes.ProcessHistoricalBalancesMode(log, configs)
}

isInDbLookupExtensionMode := operationmodes.SliceContainsElement(operationModes, operationmodes.OperationModeDbLookupExtension)
Expand All @@ -648,28 +649,6 @@ func applyCompatibleConfigs(log logger.Logger, configs *config.Configs) error {
return nil
}

func processHistoricalBalancesMode(log logger.Logger, configs *config.Configs) {
configs.GeneralConfig.StoragePruning.Enabled = true
configs.GeneralConfig.StoragePruning.ValidatorCleanOldEpochsData = false
configs.GeneralConfig.StoragePruning.ObserverCleanOldEpochsData = false
configs.GeneralConfig.GeneralSettings.StartInEpochEnabled = false
configs.GeneralConfig.StoragePruning.AccountsTrieCleanOldEpochsData = false
configs.GeneralConfig.StateTriesConfig.AccountsStatePruningEnabled = false
configs.GeneralConfig.DbLookupExtensions.Enabled = true
configs.PreferencesConfig.Preferences.FullArchive = true

log.Warn("the node is in historical balances mode! Will auto-set some config values",
"StoragePruning.Enabled", configs.GeneralConfig.StoragePruning.Enabled,
"StoragePruning.ValidatorCleanOldEpochsData", configs.GeneralConfig.StoragePruning.ValidatorCleanOldEpochsData,
"StoragePruning.ObserverCleanOldEpochsData", configs.GeneralConfig.StoragePruning.ObserverCleanOldEpochsData,
"StoragePruning.AccountsTrieCleanOldEpochsData", configs.GeneralConfig.StoragePruning.AccountsTrieCleanOldEpochsData,
"GeneralSettings.StartInEpochEnabled", configs.GeneralConfig.GeneralSettings.StartInEpochEnabled,
"StateTriesConfig.AccountsStatePruningEnabled", configs.GeneralConfig.StateTriesConfig.AccountsStatePruningEnabled,
"DbLookupExtensions.Enabled", configs.GeneralConfig.DbLookupExtensions.Enabled,
"Preferences.FullArchive", configs.PreferencesConfig.Preferences.FullArchive,
)
}

func processDbLookupExtensionMode(log logger.Logger, configs *config.Configs) {
configs.GeneralConfig.DbLookupExtensions.Enabled = true
configs.GeneralConfig.StoragePruning.Enabled = true
Expand Down
41 changes: 41 additions & 0 deletions common/operationmodes/historicalBalances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package operationmodes

import (
"github.com/multiversx/mx-chain-go/config"
logger "github.com/multiversx/mx-chain-logger-go"
)

// ProcessHistoricalBalancesMode will process the provided flags for the historical balances
func ProcessHistoricalBalancesMode(log logger.Logger, configs *config.Configs) {
configs.GeneralConfig.StoragePruning.Enabled = true
configs.GeneralConfig.StoragePruning.ValidatorCleanOldEpochsData = false
configs.GeneralConfig.StoragePruning.ObserverCleanOldEpochsData = false
configs.GeneralConfig.GeneralSettings.StartInEpochEnabled = false
configs.GeneralConfig.StoragePruning.AccountsTrieCleanOldEpochsData = false
configs.GeneralConfig.StateTriesConfig.AccountsStatePruningEnabled = false
configs.GeneralConfig.DbLookupExtensions.Enabled = true
configs.PreferencesConfig.Preferences.FullArchive = true

log.Warn("the node is in historical balances mode! Will auto-set some config values",
"StoragePruning.Enabled", configs.GeneralConfig.StoragePruning.Enabled,
"StoragePruning.ValidatorCleanOldEpochsData", configs.GeneralConfig.StoragePruning.ValidatorCleanOldEpochsData,
"StoragePruning.ObserverCleanOldEpochsData", configs.GeneralConfig.StoragePruning.ObserverCleanOldEpochsData,
"StoragePruning.AccountsTrieCleanOldEpochsData", configs.GeneralConfig.StoragePruning.AccountsTrieCleanOldEpochsData,
"GeneralSettings.StartInEpochEnabled", configs.GeneralConfig.GeneralSettings.StartInEpochEnabled,
"StateTriesConfig.AccountsStatePruningEnabled", configs.GeneralConfig.StateTriesConfig.AccountsStatePruningEnabled,
"DbLookupExtensions.Enabled", configs.GeneralConfig.DbLookupExtensions.Enabled,
"Preferences.FullArchive", configs.PreferencesConfig.Preferences.FullArchive,
)
}

// IsInHistoricalBalancesMode returns true if the configuration provided denotes a historical balances mode
func IsInHistoricalBalancesMode(configs *config.Configs) bool {
return configs.GeneralConfig.StoragePruning.Enabled &&
!configs.GeneralConfig.StoragePruning.ValidatorCleanOldEpochsData &&
!configs.GeneralConfig.StoragePruning.ObserverCleanOldEpochsData &&
!configs.GeneralConfig.GeneralSettings.StartInEpochEnabled &&
!configs.GeneralConfig.StoragePruning.AccountsTrieCleanOldEpochsData &&
!configs.GeneralConfig.StateTriesConfig.AccountsStatePruningEnabled &&
configs.GeneralConfig.DbLookupExtensions.Enabled &&
configs.PreferencesConfig.Preferences.FullArchive
}
141 changes: 141 additions & 0 deletions common/operationmodes/historicalBalances_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package operationmodes

import (
"testing"

"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/testscommon"
"github.com/stretchr/testify/assert"
)

func TestProcessHistoricalBalancesMode(t *testing.T) {
t.Parallel()

cfg := &config.Configs{
GeneralConfig: &config.Config{},
PreferencesConfig: &config.Preferences{},
}
ProcessHistoricalBalancesMode(&testscommon.LoggerStub{}, cfg)

assert.True(t, cfg.GeneralConfig.StoragePruning.Enabled)
assert.False(t, cfg.GeneralConfig.StoragePruning.ValidatorCleanOldEpochsData)
assert.False(t, cfg.GeneralConfig.StoragePruning.ObserverCleanOldEpochsData)
assert.False(t, cfg.GeneralConfig.GeneralSettings.StartInEpochEnabled)
assert.False(t, cfg.GeneralConfig.StoragePruning.AccountsTrieCleanOldEpochsData)
assert.False(t, cfg.GeneralConfig.StateTriesConfig.AccountsStatePruningEnabled)
assert.True(t, cfg.GeneralConfig.DbLookupExtensions.Enabled)
assert.True(t, cfg.PreferencesConfig.Preferences.FullArchive)
}

func TestIsInHistoricalBalancesMode(t *testing.T) {
t.Parallel()

t.Run("empty configs should return false", func(t *testing.T) {
t.Parallel()

cfg := &config.Configs{
GeneralConfig: &config.Config{},
PreferencesConfig: &config.Preferences{},
}
assert.False(t, IsInHistoricalBalancesMode(cfg))
})
t.Run("storage pruning disabled should return false", func(t *testing.T) {
t.Parallel()

cfg := &config.Configs{
GeneralConfig: &config.Config{},
PreferencesConfig: &config.Preferences{},
}
ProcessHistoricalBalancesMode(&testscommon.LoggerStub{}, cfg)
cfg.GeneralConfig.StoragePruning.Enabled = false
assert.False(t, IsInHistoricalBalancesMode(cfg))
})
t.Run("validator clean old epoch data enabled should return false", func(t *testing.T) {
t.Parallel()

cfg := &config.Configs{
GeneralConfig: &config.Config{},
PreferencesConfig: &config.Preferences{},
}
ProcessHistoricalBalancesMode(&testscommon.LoggerStub{}, cfg)
cfg.GeneralConfig.StoragePruning.ValidatorCleanOldEpochsData = true
assert.False(t, IsInHistoricalBalancesMode(cfg))
})
t.Run("observer clean old epoch data enabled should return false", func(t *testing.T) {
t.Parallel()

cfg := &config.Configs{
GeneralConfig: &config.Config{},
PreferencesConfig: &config.Preferences{},
}
ProcessHistoricalBalancesMode(&testscommon.LoggerStub{}, cfg)
cfg.GeneralConfig.StoragePruning.ObserverCleanOldEpochsData = true
assert.False(t, IsInHistoricalBalancesMode(cfg))
})
t.Run("start in epoch enabled should return false", func(t *testing.T) {
t.Parallel()

cfg := &config.Configs{
GeneralConfig: &config.Config{},
PreferencesConfig: &config.Preferences{},
}
ProcessHistoricalBalancesMode(&testscommon.LoggerStub{}, cfg)
cfg.GeneralConfig.GeneralSettings.StartInEpochEnabled = true
assert.False(t, IsInHistoricalBalancesMode(cfg))
})
t.Run("accounts trie clean old epoch data enabled should return false", func(t *testing.T) {
t.Parallel()

cfg := &config.Configs{
GeneralConfig: &config.Config{},
PreferencesConfig: &config.Preferences{},
}
ProcessHistoricalBalancesMode(&testscommon.LoggerStub{}, cfg)
cfg.GeneralConfig.StoragePruning.AccountsTrieCleanOldEpochsData = true
assert.False(t, IsInHistoricalBalancesMode(cfg))
})
t.Run("accounts state pruning enabled should return false", func(t *testing.T) {
t.Parallel()

cfg := &config.Configs{
GeneralConfig: &config.Config{},
PreferencesConfig: &config.Preferences{},
}
ProcessHistoricalBalancesMode(&testscommon.LoggerStub{}, cfg)
cfg.GeneralConfig.StateTriesConfig.AccountsStatePruningEnabled = true
assert.False(t, IsInHistoricalBalancesMode(cfg))
})
t.Run("db lookup extension disabled should return false", func(t *testing.T) {
t.Parallel()

cfg := &config.Configs{
GeneralConfig: &config.Config{},
PreferencesConfig: &config.Preferences{},
}
ProcessHistoricalBalancesMode(&testscommon.LoggerStub{}, cfg)
cfg.GeneralConfig.DbLookupExtensions.Enabled = false
assert.False(t, IsInHistoricalBalancesMode(cfg))
})
t.Run("not a full archive node should return false", func(t *testing.T) {
t.Parallel()

cfg := &config.Configs{
GeneralConfig: &config.Config{},
PreferencesConfig: &config.Preferences{},
}
ProcessHistoricalBalancesMode(&testscommon.LoggerStub{}, cfg)
cfg.PreferencesConfig.Preferences.FullArchive = false
assert.False(t, IsInHistoricalBalancesMode(cfg))
})
t.Run("with historical balances config should return true", func(t *testing.T) {
t.Parallel()

cfg := &config.Configs{
GeneralConfig: &config.Config{},
PreferencesConfig: &config.Preferences{},
}
ProcessHistoricalBalancesMode(&testscommon.LoggerStub{}, cfg)
assert.True(t, IsInHistoricalBalancesMode(cfg))
})

}
1 change: 1 addition & 0 deletions common/operationmodes/operationmodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
)

// constants that define the operation mode of the node
const (
OperationModeFullArchive = "full-archive"
OperationModeDbLookupExtension = "db-lookup-extension"
Expand Down
Loading
Loading