diff --git a/blockchain/config.go b/blockchain/config.go index a0731c796d..8993e92adc 100644 --- a/blockchain/config.go +++ b/blockchain/config.go @@ -52,10 +52,6 @@ type ( EnableStateDBCaching bool `yaml:"enableStateDBCaching"` // EnableArchiveMode is only meaningful when EnableTrielessStateDB is false EnableArchiveMode bool `yaml:"enableArchiveMode"` - // VersionedNamespaces specifies the versioned namespaces for versioned state DB - VersionedNamespaces []string `yaml:"versionedNamespaces"` - // VersionedMetadata specifies the metadata namespace for versioned state DB - VersionedMetadata string `yaml:"versionedMetadata"` // EnableAsyncIndexWrite enables writing the block actions' and receipts' index asynchronously EnableAsyncIndexWrite bool `yaml:"enableAsyncIndexWrite"` // deprecated @@ -111,7 +107,6 @@ var ( GravityChainAPIs: []string{}, }, EnableTrielessStateDB: true, - VersionedNamespaces: []string{}, EnableStateDBCaching: false, EnableArchiveMode: false, EnableAsyncIndexWrite: true, diff --git a/chainservice/builder.go b/chainservice/builder.go index 0b4f51448d..83fedbd24a 100644 --- a/chainservice/builder.go +++ b/chainservice/builder.go @@ -174,8 +174,7 @@ func (builder *Builder) createFactory(forTest bool) (factory.Factory, error) { factory.DefaultPatchOption(), } if builder.cfg.Chain.EnableArchiveMode { - dao, err = db.CreateKVStoreVersioned(factoryDBCfg, builder.cfg.Chain.TrieDBPath, builder.cfg.Chain.VersionedNamespaces) - opts = append(opts, factory.MetadataNamespaceOption(builder.cfg.Chain.VersionedMetadata)) + dao, err = db.CreateKVStoreVersioned(factoryDBCfg, builder.cfg.Chain.TrieDBPath, factory.VersionedNamespaces) } else if builder.cfg.Chain.EnableStateDBCaching { dao, err = db.CreateKVStoreWithCache(factoryDBCfg, builder.cfg.Chain.TrieDBPath, builder.cfg.Chain.StateDBCacheSize) } else { diff --git a/config/config.go b/config/config.go index 7a50fd5095..95b5419ee8 100644 --- a/config/config.go +++ b/config/config.go @@ -92,7 +92,6 @@ var ( // Validates is the collection config validation functions Validates = []Validate{ ValidateRollDPoS, - ValidateArchiveMode, ValidateDispatcher, ValidateAPI, ValidateActPool, @@ -254,15 +253,6 @@ func ValidateRollDPoS(cfg Config) error { return nil } -// ValidateArchiveMode validates the state factory setting -func ValidateArchiveMode(cfg Config) error { - if !cfg.Chain.EnableArchiveMode || !cfg.Chain.EnableTrielessStateDB { - return nil - } - - return errors.Wrap(ErrInvalidCfg, "Archive mode is incompatible with trieless state DB") -} - // ValidateAPI validates the api configs func ValidateAPI(cfg Config) error { if cfg.API.TpsWindow <= 0 { diff --git a/config/config_test.go b/config/config_test.go index 485da09d00..73b3840ed9 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -253,23 +253,6 @@ func TestValidateRollDPoS(t *testing.T) { ) } -func TestValidateArchiveMode(t *testing.T) { - cfg := Default - cfg.Chain.EnableArchiveMode = true - cfg.Chain.EnableTrielessStateDB = true - require.Error(t, ErrInvalidCfg, errors.Cause(ValidateArchiveMode(cfg))) - require.EqualError(t, ValidateArchiveMode(cfg), "Archive mode is incompatible with trieless state DB: invalid config value") - cfg.Chain.EnableArchiveMode = false - cfg.Chain.EnableTrielessStateDB = true - require.NoError(t, errors.Cause(ValidateArchiveMode(cfg))) - cfg.Chain.EnableArchiveMode = true - cfg.Chain.EnableTrielessStateDB = false - require.NoError(t, errors.Cause(ValidateArchiveMode(cfg))) - cfg.Chain.EnableArchiveMode = false - cfg.Chain.EnableTrielessStateDB = false - require.NoError(t, errors.Cause(ValidateArchiveMode(cfg))) -} - func TestValidateActPool(t *testing.T) { cfg := Default cfg.ActPool.MaxNumActsPerAcct = 0 diff --git a/db/builder.go b/db/builder.go index 310d14e647..7a445190f2 100644 --- a/db/builder.go +++ b/db/builder.go @@ -5,6 +5,8 @@ import "github.com/pkg/errors" var ( // ErrEmptyDBPath is the error when db path is empty ErrEmptyDBPath = errors.New("empty db path") + // ErrEmptyVersionedNamespace is the error of empty versioned namespace + ErrEmptyVersionedNamespace = errors.New("cannot create versioned KVStore with empty versioned namespace") ) // CreateKVStore creates db from config and db path @@ -38,6 +40,14 @@ func CreateKVStoreVersioned(cfg Config, dbPath string, vns []string) (KVStore, e if len(dbPath) == 0 { return nil, ErrEmptyDBPath } + if len(vns) == 0 { + return nil, ErrEmptyVersionedNamespace + } + for i := range vns { + if len(vns[i]) == 0 { + return nil, ErrEmptyVersionedNamespace + } + } cfg.DbPath = dbPath return NewKVStoreWithVersion(cfg, VersionedNamespaceOption(vns...)), nil } diff --git a/state/factory/statedb.go b/state/factory/statedb.go index 517ecb338f..64abcf952b 100644 --- a/state/factory/statedb.go +++ b/state/factory/statedb.go @@ -20,6 +20,7 @@ import ( "github.com/iotexproject/iotex-core/v2/action" "github.com/iotexproject/iotex-core/v2/action/protocol" + "github.com/iotexproject/iotex-core/v2/action/protocol/execution/evm" "github.com/iotexproject/iotex-core/v2/actpool" "github.com/iotexproject/iotex-core/v2/blockchain/block" "github.com/iotexproject/iotex-core/v2/blockchain/genesis" @@ -31,6 +32,13 @@ import ( "github.com/iotexproject/iotex-core/v2/state" ) +var ( + // VersionedMetadata is the metadata namespace for versioned stateDB + VersionedMetadata = "Meta" + // VersionedNamespaces are the versioned namespaces for versioned stateDB + VersionedNamespaces = []string{AccountKVNamespace, evm.ContractKVNameSpace} +) + type ( // daoRetrofitter represents the DAO-related methods to accommodate archive-mode daoRetrofitter interface { @@ -53,7 +61,6 @@ type ( protocolView protocol.View skipBlockValidationOnPut bool ps *patchStore - metaNS string // metadata namespace, only meaningful when archive-mode enabled } ) @@ -92,14 +99,6 @@ func DisableWorkingSetCacheOption() StateDBOption { } } -// MetadataNamespaceOption specifies the metadat namespace for versioned DB -func MetadataNamespaceOption(ns string) StateDBOption { - return func(sdb *stateDB, cfg *Config) error { - sdb.metaNS = ns - return nil - } -} - // NewStateDB creates a new state db func NewStateDB(cfg Config, dao db.KVStore, opts ...StateDBOption) (Factory, error) { sdb := stateDB{ @@ -120,7 +119,7 @@ func NewStateDB(cfg Config, dao db.KVStore, opts ...StateDBOption) (Factory, err if !ok { return nil, errors.Wrap(ErrNotSupported, "cannot enable archive mode StateDB with non-versioned DB") } - sdb.dao = newDaoRetrofitterArchive(daoVersioned, sdb.metaNS) + sdb.dao = newDaoRetrofitterArchive(daoVersioned, VersionedMetadata) } else { sdb.dao = newDaoRetrofitter(dao) }