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

feat(store/v2): build the migration manager in the root store factory #22336

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions store/v2/commitment/iavl/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ func (e *Exporter) Close() error {

return nil
}

// EmptyExporter is a Exporter for an empty tree.
type EmptyExporter struct{}

// Next returns ExportDone.
func (e *EmptyExporter) Next() (*snapshotstypes.SnapshotIAVLItem, error) {
return nil, commitment.ErrorExportDone
}

// Close does nothing.
func (e *EmptyExporter) Close() error {
return nil
}
17 changes: 17 additions & 0 deletions store/v2/commitment/iavl/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var (
// IavlTree is a wrapper around iavl.MutableTree.
type IavlTree struct {
tree *iavl.MutableTree
// it is only used for new store key during the migration process.
initialVersion uint64
}

// NewIavlTree creates a new IavlTree instance.
Expand Down Expand Up @@ -58,6 +60,17 @@ func (t *IavlTree) WorkingHash() []byte {

// LoadVersion loads the state at the given version.
func (t *IavlTree) LoadVersion(version uint64) error {
if t.initialVersion > 0 {
// If the initial version is set and the tree is empty,
// we don't need to load the version.
latestVersion, err := t.tree.GetLatestVersion()
if err != nil {
return err
}
if latestVersion == 0 {
return nil
}
}
return t.tree.LoadVersionForOverwriting(int64(version))
}

Expand Down Expand Up @@ -106,6 +119,7 @@ func (t *IavlTree) GetLatestVersion() (uint64, error) {
// SetInitialVersion sets the initial version of the database.
func (t *IavlTree) SetInitialVersion(version uint64) error {
t.tree.SetInitialVersion(version)
t.initialVersion = version
return nil
}

Expand All @@ -125,6 +139,9 @@ func (t *IavlTree) PausePruning(pause bool) {

// Export exports the tree exporter at the given version.
func (t *IavlTree) Export(version uint64) (commitment.Exporter, error) {
if version < t.initialVersion {
return &EmptyExporter{}, nil
}
tree, err := t.tree.GetImmutable(int64(version))
if err != nil {
return nil, err
Expand Down
11 changes: 7 additions & 4 deletions store/v2/commitment/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

const (
commitInfoKeyFmt = "c/%d" // c/<version>
latestVersionKey = "c/latest"
removedStoreKeyPrefix = "c/removed/" // c/removed/<version>/<store-name>
commitInfoKeyFmt = "s/%d" // c/<version>
latestVersionKey = "s/latest"
removedStoreKeyPrefix = "s/removed/" // c/removed/<version>/<store-name>
)

// MetadataStore is a store for metadata related to the commitment store.
Expand Down Expand Up @@ -69,7 +69,10 @@ func (m *MetadataStore) GetCommitInfo(version uint64) (*proof.CommitInfo, error)

cInfo := &proof.CommitInfo{}
if err := cInfo.Unmarshal(value); err != nil {
return nil, err
cInfo, err = proof.ConvertV1CommitInfo(value)
if err != nil {
return nil, err
}
}

return cInfo, nil
Expand Down
2 changes: 1 addition & 1 deletion store/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d
go.uber.org/mock v0.5.0
golang.org/x/sync v0.8.0
google.golang.org/protobuf v1.35.1
)

require (
Expand Down Expand Up @@ -61,6 +62,5 @@ require (
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
28 changes: 28 additions & 0 deletions store/v2/proof/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

errors "cosmossdk.io/errors/v2"
storeerrors "cosmossdk.io/store/v2/errors"
v1types "cosmossdk.io/store/v2/proof/v1/types"
)

// Proof operation types
Expand Down Expand Up @@ -228,3 +229,30 @@ func InnerHash(left, right []byte) []byte {
h := sha256.Sum256(data)
return h[:]
}

func ConvertV1CommitInfo(value []byte) (*CommitInfo, error) {
v1CommitInfo := &v1types.CommitInfo{}
if err := v1CommitInfo.Unmarshal(value); err != nil {
return nil, err
}
cInfo := &CommitInfo{
Version: uint64(v1CommitInfo.Version),
StoreInfos: make([]StoreInfo, len(v1CommitInfo.StoreInfos)),
Timestamp: v1CommitInfo.Timestamp,
}
for i, v1StoreInfo := range v1CommitInfo.StoreInfos {
cInfo.StoreInfos[i] = StoreInfo{
Name: []byte(v1StoreInfo.Name),
CommitID: CommitID{
Version: uint64(v1StoreInfo.CommitId.Version),
Hash: v1StoreInfo.CommitId.Hash,
},
Structure: "iavl",
}
Comment on lines +244 to +251
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid hardcoding store structure type

Hardcoding "iavl" as the Structure value is risky as not all stores might use IAVL.

Consider:

  1. Getting the structure type from v1StoreInfo
  2. Adding validation for supported store types
  3. Adding a mapping of v1 to v2 store types
-			Structure: "iavl",
+			Structure: determineStoreStructure(v1StoreInfo),

Committable suggestion was skipped due to low confidence.

}

var err error
cInfo.CommitHash, _, err = cInfo.GetStoreProof([]byte{})

return cInfo, err
}
Loading
Loading