Skip to content

Commit

Permalink
address comment: versioned namespace should not be an option
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Dec 19, 2024
1 parent 2969aaa commit b49bdc5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 44 deletions.
5 changes: 0 additions & 5 deletions blockchain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -111,7 +107,6 @@ var (
GravityChainAPIs: []string{},
},
EnableTrielessStateDB: true,
VersionedNamespaces: []string{},
EnableStateDBCaching: false,
EnableArchiveMode: false,
EnableAsyncIndexWrite: true,
Expand Down
3 changes: 1 addition & 2 deletions chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 0 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ var (
// Validates is the collection config validation functions
Validates = []Validate{
ValidateRollDPoS,
ValidateArchiveMode,
ValidateDispatcher,
ValidateAPI,
ValidateActPool,
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 0 additions & 17 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions db/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
19 changes: 9 additions & 10 deletions state/factory/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -53,7 +61,6 @@ type (
protocolView protocol.View
skipBlockValidationOnPut bool
ps *patchStore
metaNS string // metadata namespace, only meaningful when archive-mode enabled
}
)

Expand Down Expand Up @@ -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{
Expand All @@ -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)
}
Expand Down

0 comments on commit b49bdc5

Please sign in to comment.