-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[factory] implement versioned stateDB to support archive mode
- Loading branch information
Showing
11 changed files
with
286 additions
and
58 deletions.
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
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 | ||
} |
Oops, something went wrong.