-
Notifications
You must be signed in to change notification settings - Fork 328
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
[factory] implement versioned stateDB to support archive mode #4520
Open
dustinxie
wants to merge
2
commits into
master
Choose a base branch
from
verstatedb
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−67
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) 2024 IoTeX Foundation | ||
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability | ||
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. | ||
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file. | ||
|
||
package factory | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/iotexproject/iotex-core/v2/action/protocol/execution/evm" | ||
"github.com/iotexproject/iotex-core/v2/action/protocol/staking" | ||
"github.com/iotexproject/iotex-core/v2/db" | ||
"github.com/iotexproject/iotex-core/v2/db/batch" | ||
"github.com/iotexproject/iotex-core/v2/pkg/util/byteutil" | ||
) | ||
|
||
type daoRTFArchive struct { | ||
daoVersioned db.KvVersioned | ||
metaNS string // namespace for metadata | ||
} | ||
|
||
func newDaoRetrofitterArchive(dao db.KvVersioned, mns string) *daoRTFArchive { | ||
return &daoRTFArchive{ | ||
daoVersioned: dao, | ||
metaNS: mns, | ||
} | ||
} | ||
|
||
func (rtf *daoRTFArchive) Start(ctx context.Context) error { | ||
return rtf.daoVersioned.Start(ctx) | ||
} | ||
|
||
func (rtf *daoRTFArchive) Stop(ctx context.Context) error { | ||
return rtf.daoVersioned.Stop(ctx) | ||
} | ||
|
||
func (rtf *daoRTFArchive) atHeight(h uint64) db.KVStore { | ||
return rtf.daoVersioned.SetVersion(h) | ||
} | ||
|
||
func (rtf *daoRTFArchive) getHeight() (uint64, error) { | ||
height, err := rtf.daoVersioned.Base().Get(rtf.metaNS, []byte(CurrentHeightKey)) | ||
if err != nil { | ||
return 0, errors.Wrap(err, "failed to get factory's height from underlying DB") | ||
} | ||
return byteutil.BytesToUint64(height), nil | ||
} | ||
|
||
func (rtf *daoRTFArchive) putHeight(h uint64) error { | ||
return rtf.daoVersioned.Base().Put(rtf.metaNS, []byte(CurrentHeightKey), byteutil.Uint64ToBytes(h)) | ||
} | ||
|
||
func (rtf *daoRTFArchive) metadataNS() string { | ||
return rtf.metaNS | ||
} | ||
|
||
func (rtf *daoRTFArchive) flusherOptions(preEaster bool) []db.KVStoreFlusherOption { | ||
opts := []db.KVStoreFlusherOption{ | ||
db.SerializeOption(func(wi *batch.WriteInfo) []byte { | ||
// current height is moved to the metadata namespace | ||
// transform it back for the purpose of calculating digest | ||
wi = rtf.transCurrentHeight(wi) | ||
if preEaster { | ||
return wi.SerializeWithoutWriteType() | ||
} | ||
return wi.Serialize() | ||
}), | ||
} | ||
if !preEaster { | ||
return opts | ||
} | ||
return append( | ||
opts, | ||
db.SerializeFilterOption(func(wi *batch.WriteInfo) bool { | ||
return wi.Namespace() == evm.CodeKVNameSpace || wi.Namespace() == staking.CandsMapNS | ||
}), | ||
) | ||
} | ||
|
||
func (rtf *daoRTFArchive) transCurrentHeight(wi *batch.WriteInfo) *batch.WriteInfo { | ||
if wi.Namespace() == rtf.metaNS && string(wi.Key()) == CurrentHeightKey { | ||
return batch.NewWriteInfo(wi.WriteType(), AccountKVNamespace, wi.Key(), wi.Value(), wi.Error()) | ||
} | ||
return wi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,6 @@ 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/action/protocol/staking" | ||
"github.com/iotexproject/iotex-core/v2/actpool" | ||
"github.com/iotexproject/iotex-core/v2/blockchain/block" | ||
"github.com/iotexproject/iotex-core/v2/blockchain/genesis" | ||
|
@@ -33,13 +32,22 @@ 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 { | ||
lifecycle.StartStopper | ||
atHeight(uint64) db.KVStore | ||
getHeight() (uint64, error) | ||
putHeight(uint64) error | ||
metadataNS() string | ||
flusherOptions(bool) []db.KVStoreFlusherOption | ||
} | ||
// stateDB implements StateFactory interface, tracks changes to account/contract and batch-commits to DB | ||
stateDB struct { | ||
|
@@ -106,7 +114,15 @@ func NewStateDB(cfg Config, dao db.KVStore, opts ...StateDBOption) (Factory, err | |
return nil, err | ||
} | ||
} | ||
sdb.dao = newDaoRetrofitter(dao) | ||
if cfg.Chain.EnableArchiveMode { | ||
daoVersioned, ok := dao.(db.KvVersioned) | ||
if !ok { | ||
return nil, errors.Wrap(ErrNotSupported, "cannot enable archive mode StateDB with non-versioned DB") | ||
} | ||
sdb.dao = newDaoRetrofitterArchive(daoVersioned, VersionedMetadata) | ||
} else { | ||
sdb.dao = newDaoRetrofitter(dao) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or we can add a new struct/file to implement statedb for archive-mode, like
but after checking, that would entail duplicating almost all the codes, and in the future, any changes would need to go to 2 files. Let me know if you have good suggestion. |
||
timerFactory, err := prometheustimer.New( | ||
"iotex_statefactory_perf", | ||
"Performance of state factory module", | ||
|
@@ -181,7 +197,7 @@ func (sdb *stateDB) newWorkingSet(ctx context.Context, height uint64) (*workingS | |
flusher, err := db.NewKVStoreFlusher( | ||
sdb.dao.atHeight(height), | ||
batch.NewCachedBatch(), | ||
sdb.flusherOptions(!g.IsEaster(height))..., | ||
sdb.dao.flusherOptions(!g.IsEaster(height))..., | ||
) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -193,11 +209,10 @@ func (sdb *stateDB) newWorkingSet(ctx context.Context, height uint64) (*workingS | |
flusher.KVStoreWithBuffer().MustPut(p.Namespace, p.Key, p.Value) | ||
} | ||
} | ||
store := newStateDBWorkingSetStore(sdb.protocolView, flusher, g.IsNewfoundland(height)) | ||
store := newStateDBWorkingSetStore(sdb.protocolView, flusher, g.IsNewfoundland(height), sdb.dao.metadataNS()) | ||
if err := store.Start(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
return newWorkingSet(height, store), nil | ||
} | ||
|
||
|
@@ -271,7 +286,6 @@ func (sdb *stateDB) WorkingSet(ctx context.Context) (protocol.StateManager, erro | |
} | ||
|
||
func (sdb *stateDB) WorkingSetAtHeight(ctx context.Context, height uint64) (protocol.StateManager, error) { | ||
// TODO: implement archive mode | ||
return sdb.newWorkingSet(ctx, height) | ||
} | ||
|
||
|
@@ -368,29 +382,9 @@ func (sdb *stateDB) ReadView(name string) (interface{}, error) { | |
} | ||
|
||
//====================================== | ||
// private trie constructor functions | ||
// private statedb functions | ||
//====================================== | ||
|
||
func (sdb *stateDB) flusherOptions(preEaster bool) []db.KVStoreFlusherOption { | ||
opts := []db.KVStoreFlusherOption{ | ||
db.SerializeOption(func(wi *batch.WriteInfo) []byte { | ||
if preEaster { | ||
return wi.SerializeWithoutWriteType() | ||
} | ||
return wi.Serialize() | ||
}), | ||
} | ||
if !preEaster { | ||
return opts | ||
} | ||
return append( | ||
opts, | ||
db.SerializeFilterOption(func(wi *batch.WriteInfo) bool { | ||
return wi.Namespace() == evm.CodeKVNameSpace || wi.Namespace() == staking.CandsMapNS | ||
}), | ||
) | ||
} | ||
|
||
func (sdb *stateDB) state(h uint64, ns string, addr []byte, s interface{}) error { | ||
data, err := sdb.dao.atHeight(h).Get(ns, addr) | ||
if err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix ci