From e3ae4839a36fac3ec2c1a0d15f4c7bc8356c0078 Mon Sep 17 00:00:00 2001 From: Randy Grok Date: Mon, 30 Dec 2024 20:29:59 +0100 Subject: [PATCH 01/14] add methods to set v2 migration info --- store/v2/commitment/metadata.go | 30 ++++++++++++++++++++- store/v2/commitment/metadata_test.go | 40 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 store/v2/commitment/metadata_test.go diff --git a/store/v2/commitment/metadata.go b/store/v2/commitment/metadata.go index a054acf26e89..6883aea9a038 100644 --- a/store/v2/commitment/metadata.go +++ b/store/v2/commitment/metadata.go @@ -13,7 +13,8 @@ import ( const ( commitInfoKeyFmt = "c/%d" // c/ latestVersionKey = "c/latest" - removedStoreKeyPrefix = "c/removed/" // c/removed// + v2MigrationHeightKey = "c/v2migration" // c/v2migration height where the migration to store v2 happened + removedStoreKeyPrefix = "c/removed/" // c/removed// ) // MetadataStore is a store for metadata related to the commitment store. @@ -56,6 +57,33 @@ func (m *MetadataStore) setLatestVersion(version uint64) error { return m.kv.Set([]byte(latestVersionKey), buf.Bytes()) } +func (m *MetadataStore) GetV2MigrationHeight() (uint64, error) { + value, err := m.kv.Get([]byte(v2MigrationHeightKey)) + if err != nil { + return 0, err + } + if value == nil { + return 0, nil + } + + version, _, err := encoding.DecodeUvarint(value) + if err != nil { + return 0, err + } + + return version, nil +} + +// setV2MigrationHeight sets the v2 migration height. +func (m *MetadataStore) setV2MigrationHeight(height uint64) error { + var buf bytes.Buffer + buf.Grow(encoding.EncodeUvarintSize(height)) + if err := encoding.EncodeUvarint(&buf, height); err != nil { + return err + } + return m.kv.Set([]byte(v2MigrationHeightKey), buf.Bytes()) +} + // GetCommitInfo returns the commit info for the given version. func (m *MetadataStore) GetCommitInfo(version uint64) (*proof.CommitInfo, error) { key := []byte(fmt.Sprintf(commitInfoKeyFmt, version)) diff --git a/store/v2/commitment/metadata_test.go b/store/v2/commitment/metadata_test.go new file mode 100644 index 000000000000..c2d660da9513 --- /dev/null +++ b/store/v2/commitment/metadata_test.go @@ -0,0 +1,40 @@ +package commitment + +import ( + dbm "cosmossdk.io/store/v2/db" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestMetadataStore_GetLatestVersion(t *testing.T) { + db := dbm.NewMemDB() + ms := NewMetadataStore(db) + + version, err := ms.GetLatestVersion() + assert.NoError(t, err) + assert.Equal(t, uint64(0), version) + + // set latest version + err = ms.setLatestVersion(10) + assert.NoError(t, err) + + version, err = ms.GetLatestVersion() + assert.NoError(t, err) + assert.Equal(t, uint64(10), version) +} + +func TestMetadataStore_GetV2MigrationHeight(t *testing.T) { + db := dbm.NewMemDB() + ms := NewMetadataStore(db) + + version, err := ms.GetV2MigrationHeight() + assert.NoError(t, err) + assert.Equal(t, uint64(0), version) + + err = ms.setV2MigrationHeight(10) + assert.NoError(t, err) + + version, err = ms.GetV2MigrationHeight() + assert.NoError(t, err) + assert.Equal(t, uint64(10), version) +} From 9437f972edf005d53ba0ab71943815165c0adbf3 Mon Sep 17 00:00:00 2001 From: Randy Grok Date: Mon, 30 Dec 2024 20:31:51 +0100 Subject: [PATCH 02/14] add refactor --- store/v2/commitment/metadata.go | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/store/v2/commitment/metadata.go b/store/v2/commitment/metadata.go index 6883aea9a038..60c17f24fba5 100644 --- a/store/v2/commitment/metadata.go +++ b/store/v2/commitment/metadata.go @@ -30,9 +30,9 @@ func NewMetadataStore(kv corestore.KVStoreWithBatch) *MetadataStore { } } -// GetLatestVersion returns the latest committed version. -func (m *MetadataStore) GetLatestVersion() (uint64, error) { - value, err := m.kv.Get([]byte(latestVersionKey)) +// getVersion is an internal helper method to retrieve and decode a version from the store. +func (m *MetadataStore) getVersion(key string) (uint64, error) { + value, err := m.kv.Get([]byte(key)) if err != nil { return 0, err } @@ -48,6 +48,11 @@ func (m *MetadataStore) GetLatestVersion() (uint64, error) { return version, nil } +// GetLatestVersion returns the latest committed version. +func (m *MetadataStore) GetLatestVersion() (uint64, error) { + return m.getVersion(latestVersionKey) +} + func (m *MetadataStore) setLatestVersion(version uint64) error { var buf bytes.Buffer buf.Grow(encoding.EncodeUvarintSize(version)) @@ -57,21 +62,9 @@ func (m *MetadataStore) setLatestVersion(version uint64) error { return m.kv.Set([]byte(latestVersionKey), buf.Bytes()) } +// GetV2MigrationHeight retrieves the height at which the migration to store v2 occurred. func (m *MetadataStore) GetV2MigrationHeight() (uint64, error) { - value, err := m.kv.Get([]byte(v2MigrationHeightKey)) - if err != nil { - return 0, err - } - if value == nil { - return 0, nil - } - - version, _, err := encoding.DecodeUvarint(value) - if err != nil { - return 0, err - } - - return version, nil + return m.getVersion(v2MigrationHeightKey) } // setV2MigrationHeight sets the v2 migration height. From 01175a875aea46801430fdf9af813eacabf291b0 Mon Sep 17 00:00:00 2001 From: Randy Grok Date: Tue, 31 Dec 2024 16:14:30 +0100 Subject: [PATCH 03/14] some changes --- store/v2/commitment/metadata_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/store/v2/commitment/metadata_test.go b/store/v2/commitment/metadata_test.go index c2d660da9513..63ba05289a1d 100644 --- a/store/v2/commitment/metadata_test.go +++ b/store/v2/commitment/metadata_test.go @@ -1,9 +1,10 @@ package commitment import ( + "testing" + dbm "cosmossdk.io/store/v2/db" "github.com/stretchr/testify/assert" - "testing" ) func TestMetadataStore_GetLatestVersion(t *testing.T) { From 24fa8b7b49a9a81b8fd6c37e21615792accb0117 Mon Sep 17 00:00:00 2001 From: Randy Grok Date: Wed, 1 Jan 2025 14:51:09 +0100 Subject: [PATCH 04/14] saves the last version on the metadata --- store/v2/commitment/store.go | 15 +++++++++++++++ store/v2/migration/manager.go | 6 ++++++ store/v2/root/migrate_test.go | 20 +++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/store/v2/commitment/store.go b/store/v2/commitment/store.go index aa383b57ae56..440b14012d01 100644 --- a/store/v2/commitment/store.go +++ b/store/v2/commitment/store.go @@ -233,6 +233,21 @@ func (c *CommitStore) SetInitialVersion(version uint64) error { return nil } +// SetV2MigrationHeight sets the migration height for v2 store. +func (c *CommitStore) SetV2MigrationHeight(height uint64) error { + err := c.metadata.setV2MigrationHeight(height) + if err != nil { + return err + } + + return nil +} + +// GetV2MigrationHeight returns the migration height for v2 store. +func (c *CommitStore) GetV2MigrationHeight() (uint64, error) { + return c.metadata.GetV2MigrationHeight() +} + // GetProof returns a proof for the given key and version. func (c *CommitStore) GetProof(storeKey []byte, version uint64, key []byte) ([]proof.CommitmentOp, error) { rawStoreKey := conv.UnsafeBytesToStr(storeKey) diff --git a/store/v2/migration/manager.go b/store/v2/migration/manager.go index 5365e8eb6a11..addb50b362ab 100644 --- a/store/v2/migration/manager.go +++ b/store/v2/migration/manager.go @@ -107,6 +107,12 @@ func (m *Manager) Migrate(height uint64) error { m.migratedVersion.Store(height) + // Updates migration height to support historical queries + err := m.stateCommitment.SetV2MigrationHeight(height) + if err != nil { + return err + } + return nil } diff --git a/store/v2/root/migrate_test.go b/store/v2/root/migrate_test.go index 3b431bdb24f6..46b4a5f24fd0 100644 --- a/store/v2/root/migrate_test.go +++ b/store/v2/root/migrate_test.go @@ -78,7 +78,7 @@ func (s *MigrateStoreTestSuite) SetupTest() { } func (s *MigrateStoreTestSuite) TestMigrateState() { - err := s.rootStore.LoadLatestVersion() + err := s.rootStore.LoadLatestVersion() // here it starts the migration s.Require().NoError(err) originalLatestVersion, err := s.rootStore.GetLatestVersion() s.Require().NoError(err) @@ -153,4 +153,22 @@ func (s *MigrateStoreTestSuite) TestMigrateState() { version, err = s.rootStore.GetLatestVersion() s.Require().NoError(err) s.Require().Equal(latestVersion+10, version) + + commitStore, ok := s.rootStore.GetStateCommitment().(*commitment.CommitStore) + s.Require().True(ok) + height, err := commitStore.GetV2MigrationHeight() + s.Require().NoError(err) + fmt.Printf("commit store height %d\n", height) + s.Require().Equal(height, originalLatestVersion) + + // check if the Query fallback to the original SC after the migration + for version := uint64(1); version <= originalLatestVersion; version++ { + for _, storeKey := range storeKeys { + for i := 0; i < 10; i++ { + res, err := s.rootStore.Query([]byte(storeKey), version, []byte(fmt.Sprintf("key-%d-%d", version, i)), true) + s.Require().NoError(err) + s.Require().Equal([]byte(fmt.Sprintf("value-%d-%d", version, i)), res.Value) + } + } + } } From 34c37f769b6bdd70fefa84e3bbf4e4b59395a32c Mon Sep 17 00:00:00 2001 From: Randy Grok Date: Wed, 1 Jan 2025 15:20:45 +0100 Subject: [PATCH 05/14] test pass without proofs --- store/v2/root/migrate_test.go | 2 +- store/v2/root/store.go | 43 +++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/store/v2/root/migrate_test.go b/store/v2/root/migrate_test.go index 46b4a5f24fd0..bc09d4f6b253 100644 --- a/store/v2/root/migrate_test.go +++ b/store/v2/root/migrate_test.go @@ -165,7 +165,7 @@ func (s *MigrateStoreTestSuite) TestMigrateState() { for version := uint64(1); version <= originalLatestVersion; version++ { for _, storeKey := range storeKeys { for i := 0; i < 10; i++ { - res, err := s.rootStore.Query([]byte(storeKey), version, []byte(fmt.Sprintf("key-%d-%d", version, i)), true) + res, err := s.rootStore.Query([]byte(storeKey), version, []byte(fmt.Sprintf("key-%d-%d", version, i)), false) s.Require().NoError(err) s.Require().Equal([]byte(fmt.Sprintf("value-%d-%d", version, i)), res.Value) } diff --git a/store/v2/root/store.go b/store/v2/root/store.go index ab22d3f90117..632c2335b9f0 100644 --- a/store/v2/root/store.go +++ b/store/v2/root/store.go @@ -1,6 +1,7 @@ package root import ( + "cosmossdk.io/store/v2/commitment" "crypto/sha256" "errors" "fmt" @@ -35,6 +36,9 @@ type Store struct { // stateCommitment reflects the state commitment (SC) backend stateCommitment store.Committer + // v1StateCommitment reflects the v1 state commitment prior to migration + v1StateCommitment store.Committer + // lastCommitInfo reflects the last version/hash that has been committed lastCommitInfo *proof.CommitInfo @@ -173,9 +177,33 @@ func (s *Store) Query(storeKey []byte, version uint64, key []byte, prove bool) ( defer s.telemetry.MeasureSince(now, "root_store", "query") } - val, err := s.stateCommitment.Get(storeKey, version, key) - if err != nil { - return store.QueryResult{}, fmt.Errorf("failed to query SC store: %w", err) + var err error + var v2upgradeHeight uint64 + var val []byte + v2StateCommitment, stateCommitmentIsV2 := s.stateCommitment.(*commitment.CommitStore) + + if stateCommitmentIsV2 { + v2upgradeHeight, err = v2StateCommitment.GetV2MigrationHeight() + if err != nil { + return store.QueryResult{}, fmt.Errorf("failed to get v2 migration height: %w", err) + } + + if version <= v2upgradeHeight { + val, err = s.v1StateCommitment.Get(storeKey, version, key) + if err != nil { + return store.QueryResult{}, fmt.Errorf("failed to query SC store: %w", err) + } + } else { + val, err = s.stateCommitment.Get(storeKey, version, key) + if err != nil { + return store.QueryResult{}, fmt.Errorf("failed to query SC store: %w", err) + } + } + } else { + val, err = s.stateCommitment.Get(storeKey, version, key) + if err != nil { + return store.QueryResult{}, fmt.Errorf("failed to query SC store: %w", err) + } } result := store.QueryResult{ @@ -363,10 +391,15 @@ func (s *Store) handleMigration(cs *corestore.Changeset) error { close(s.chDone) close(s.chChangeset) s.isMigrating = false - // close the old state commitment and replace it with the new one + + /*// close the old state commitment and replace it with the new one if err := s.stateCommitment.Close(); err != nil { return fmt.Errorf("failed to close the old SC store: %w", err) - } + } */ + + // set old state commitment as v1StateCommitment + s.v1StateCommitment = s.stateCommitment + newStateCommitment := s.migrationManager.GetStateCommitment() if newStateCommitment != nil { s.stateCommitment = newStateCommitment From 41706aac0da492e4d8f32fad23aa4dfd0845588c Mon Sep 17 00:00:00 2001 From: Randy Grok <@faulttolerance.net> Date: Thu, 2 Jan 2025 14:29:47 +0100 Subject: [PATCH 06/14] support proofs --- store/v2/root/migrate_test.go | 4 +-- store/v2/root/store.go | 48 +++++++++++++++++------------------ 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/store/v2/root/migrate_test.go b/store/v2/root/migrate_test.go index bc09d4f6b253..d4a4b2c1cc50 100644 --- a/store/v2/root/migrate_test.go +++ b/store/v2/root/migrate_test.go @@ -161,11 +161,11 @@ func (s *MigrateStoreTestSuite) TestMigrateState() { fmt.Printf("commit store height %d\n", height) s.Require().Equal(height, originalLatestVersion) - // check if the Query fallback to the original SC after the migration + // check if the Query fallback to the original SC after the migration without proofs for version := uint64(1); version <= originalLatestVersion; version++ { for _, storeKey := range storeKeys { for i := 0; i < 10; i++ { - res, err := s.rootStore.Query([]byte(storeKey), version, []byte(fmt.Sprintf("key-%d-%d", version, i)), false) + res, err := s.rootStore.Query([]byte(storeKey), version, []byte(fmt.Sprintf("key-%d-%d", version, i)), true) s.Require().NoError(err) s.Require().Equal([]byte(fmt.Sprintf("value-%d-%d", version, i)), res.Value) } diff --git a/store/v2/root/store.go b/store/v2/root/store.go index 632c2335b9f0..5d99fe34e117 100644 --- a/store/v2/root/store.go +++ b/store/v2/root/store.go @@ -177,49 +177,47 @@ func (s *Store) Query(storeKey []byte, version uint64, key []byte, prove bool) ( defer s.telemetry.MeasureSince(now, "root_store", "query") } - var err error - var v2upgradeHeight uint64 - var val []byte - v2StateCommitment, stateCommitmentIsV2 := s.stateCommitment.(*commitment.CommitStore) + var ( + val []byte + proofOps []proof.CommitmentOp + err error + ) + + var cs store.Committer - if stateCommitmentIsV2 { - v2upgradeHeight, err = v2StateCommitment.GetV2MigrationHeight() + if v2Commitment, isV2 := s.stateCommitment.(*commitment.CommitStore); isV2 { + v2UpgradeHeight, err := v2Commitment.GetV2MigrationHeight() if err != nil { return store.QueryResult{}, fmt.Errorf("failed to get v2 migration height: %w", err) } - if version <= v2upgradeHeight { - val, err = s.v1StateCommitment.Get(storeKey, version, key) - if err != nil { - return store.QueryResult{}, fmt.Errorf("failed to query SC store: %w", err) - } + if version <= v2UpgradeHeight { + cs = s.v1StateCommitment } else { - val, err = s.stateCommitment.Get(storeKey, version, key) - if err != nil { - return store.QueryResult{}, fmt.Errorf("failed to query SC store: %w", err) - } + cs = s.stateCommitment } } else { - val, err = s.stateCommitment.Get(storeKey, version, key) - if err != nil { - return store.QueryResult{}, fmt.Errorf("failed to query SC store: %w", err) - } + cs = s.stateCommitment } - result := store.QueryResult{ - Key: key, - Value: val, - Version: version, + val, err = cs.Get(storeKey, version, key) + if err != nil { + return store.QueryResult{}, fmt.Errorf("failed to query SC store: %w", err) } if prove { - result.ProofOps, err = s.stateCommitment.GetProof(storeKey, version, key) + proofOps, err = cs.GetProof(storeKey, version, key) if err != nil { return store.QueryResult{}, fmt.Errorf("failed to get SC store proof: %w", err) } } - return result, nil + return store.QueryResult{ + Key: key, + Value: val, + Version: version, + ProofOps: proofOps, + }, nil } func (s *Store) LoadLatestVersion() error { From 6547c787d54dd7eaf7dbd3c8fe4031f1dc40e53f Mon Sep 17 00:00:00 2001 From: Randy Grok <@faulttolerance.net> Date: Thu, 2 Jan 2025 14:31:47 +0100 Subject: [PATCH 07/14] add comments related to migration --- store/v2/root/store.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/store/v2/root/store.go b/store/v2/root/store.go index 5d99fe34e117..83fcdfc427a9 100644 --- a/store/v2/root/store.go +++ b/store/v2/root/store.go @@ -185,18 +185,21 @@ func (s *Store) Query(storeKey []byte, version uint64, key []byte, prove bool) ( var cs store.Committer + // if is V2 means that the store is a v2 store, it can be a migrated v1 store or a v2 store if v2Commitment, isV2 := s.stateCommitment.(*commitment.CommitStore); isV2 { + // if the store is a v2 store, we need to check if the version is less than or equal to the v2 migration height v2UpgradeHeight, err := v2Commitment.GetV2MigrationHeight() if err != nil { return store.QueryResult{}, fmt.Errorf("failed to get v2 migration height: %w", err) } + // if the version is less than or equal to the v2 migration height, we need to use the v1 state commitment if version <= v2UpgradeHeight { cs = s.v1StateCommitment - } else { + } else { // if the version is greater than the v2 migration height, we need to use the v2 state commitment cs = s.stateCommitment } - } else { + } else { // if is V1 means that the store is a v1 store cs = s.stateCommitment } From 5ec75f01006588f8b3534bf2b090cb31bebfe134 Mon Sep 17 00:00:00 2001 From: Randy Grok <@faulttolerance.net> Date: Thu, 2 Jan 2025 14:35:18 +0100 Subject: [PATCH 08/14] clean the code --- store/v2/root/store.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/store/v2/root/store.go b/store/v2/root/store.go index 83fcdfc427a9..4e82c42cddda 100644 --- a/store/v2/root/store.go +++ b/store/v2/root/store.go @@ -177,14 +177,7 @@ func (s *Store) Query(storeKey []byte, version uint64, key []byte, prove bool) ( defer s.telemetry.MeasureSince(now, "root_store", "query") } - var ( - val []byte - proofOps []proof.CommitmentOp - err error - ) - var cs store.Committer - // if is V2 means that the store is a v2 store, it can be a migrated v1 store or a v2 store if v2Commitment, isV2 := s.stateCommitment.(*commitment.CommitStore); isV2 { // if the store is a v2 store, we need to check if the version is less than or equal to the v2 migration height @@ -203,11 +196,13 @@ func (s *Store) Query(storeKey []byte, version uint64, key []byte, prove bool) ( cs = s.stateCommitment } - val, err = cs.Get(storeKey, version, key) + val, err := cs.Get(storeKey, version, key) if err != nil { return store.QueryResult{}, fmt.Errorf("failed to query SC store: %w", err) } + var proofOps []proof.CommitmentOp + if prove { proofOps, err = cs.GetProof(storeKey, version, key) if err != nil { From a42d9ee41c60d01f53b6e91448acfd6cac8d887c Mon Sep 17 00:00:00 2001 From: Randy Grok <@faulttolerance.net> Date: Thu, 2 Jan 2025 14:44:38 +0100 Subject: [PATCH 09/14] gci --- store/v2/commitment/metadata_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/store/v2/commitment/metadata_test.go b/store/v2/commitment/metadata_test.go index 63ba05289a1d..a86ae13182d2 100644 --- a/store/v2/commitment/metadata_test.go +++ b/store/v2/commitment/metadata_test.go @@ -3,8 +3,9 @@ package commitment import ( "testing" - dbm "cosmossdk.io/store/v2/db" "github.com/stretchr/testify/assert" + + dbm "cosmossdk.io/store/v2/db" ) func TestMetadataStore_GetLatestVersion(t *testing.T) { From 5bc8fd2d347ee9e7cd58fb2615bfa167686c5729 Mon Sep 17 00:00:00 2001 From: Randy Grok <@faulttolerance.net> Date: Thu, 2 Jan 2025 15:25:11 +0100 Subject: [PATCH 10/14] gci --- store/v2/root/migrate_test.go | 43 ++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/store/v2/root/migrate_test.go b/store/v2/root/migrate_test.go index d4a4b2c1cc50..123ee9164e2c 100644 --- a/store/v2/root/migrate_test.go +++ b/store/v2/root/migrate_test.go @@ -68,7 +68,13 @@ func (s *MigrateStoreTestSuite) SetupTest() { snapshotsStore, err := snapshots.NewStore(s.T().TempDir()) s.Require().NoError(err) - snapshotManager := snapshots.NewManager(snapshotsStore, snapshots.NewSnapshotOptions(1500, 2), orgSC, nil, testLog) + snapshotManager := snapshots.NewManager( + snapshotsStore, + snapshots.NewSnapshotOptions(1500, 2), + orgSC, + nil, + testLog, + ) migrationManager := migration.NewManager(dbm.NewMemDB(), snapshotManager, sc, testLog) pm := pruning.NewManager(sc, nil) @@ -87,7 +93,12 @@ func (s *MigrateStoreTestSuite) TestMigrateState() { for version := uint64(1); version <= originalLatestVersion; version++ { for _, storeKey := range storeKeys { for i := 0; i < 10; i++ { - res, err := s.rootStore.Query([]byte(storeKey), version, []byte(fmt.Sprintf("key-%d-%d", version, i)), true) + res, err := s.rootStore.Query( + []byte(storeKey), + version, + []byte(fmt.Sprintf("key-%d-%d", version, i)), + true, + ) s.Require().NoError(err) s.Require().Equal([]byte(fmt.Sprintf("value-%d-%d", version, i)), res.Value) } @@ -101,7 +112,12 @@ func (s *MigrateStoreTestSuite) TestMigrateState() { cs := corestore.NewChangeset(latestVersion) for _, storeKey := range storeKeys { for i := 0; i < keyCount; i++ { - cs.Add([]byte(storeKey), []byte(fmt.Sprintf("key-%d-%d", latestVersion, i)), []byte(fmt.Sprintf("value-%d-%d", latestVersion, i)), false) + cs.Add( + []byte(storeKey), + []byte(fmt.Sprintf("key-%d-%d", latestVersion, i)), + []byte(fmt.Sprintf("value-%d-%d", latestVersion, i)), + false, + ) } } _, err = s.rootStore.Commit(cs) @@ -131,7 +147,12 @@ func (s *MigrateStoreTestSuite) TestMigrateState() { if version < originalLatestVersion { targetVersion = originalLatestVersion } - res, err := s.rootStore.Query([]byte(storeKey), targetVersion, []byte(fmt.Sprintf("key-%d-%d", version, i)), true) + res, err := s.rootStore.Query( + []byte(storeKey), + targetVersion, + []byte(fmt.Sprintf("key-%d-%d", version, i)), + true, + ) s.Require().NoError(err) s.Require().Equal([]byte(fmt.Sprintf("value-%d-%d", version, i)), res.Value) } @@ -143,7 +164,12 @@ func (s *MigrateStoreTestSuite) TestMigrateState() { cs := corestore.NewChangeset(version) for _, storeKey := range storeKeys { for i := 0; i < keyCount; i++ { - cs.Add([]byte(storeKey), []byte(fmt.Sprintf("key-%d-%d", version, i)), []byte(fmt.Sprintf("value-%d-%d", version, i)), false) + cs.Add( + []byte(storeKey), + []byte(fmt.Sprintf("key-%d-%d", version, i)), + []byte(fmt.Sprintf("value-%d-%d", version, i)), + false, + ) } } _, err = s.rootStore.Commit(cs) @@ -165,7 +191,12 @@ func (s *MigrateStoreTestSuite) TestMigrateState() { for version := uint64(1); version <= originalLatestVersion; version++ { for _, storeKey := range storeKeys { for i := 0; i < 10; i++ { - res, err := s.rootStore.Query([]byte(storeKey), version, []byte(fmt.Sprintf("key-%d-%d", version, i)), true) + res, err := s.rootStore.Query( + []byte(storeKey), + version, + []byte(fmt.Sprintf("key-%d-%d", version, i)), + true, + ) s.Require().NoError(err) s.Require().Equal([]byte(fmt.Sprintf("value-%d-%d", version, i)), res.Value) } From a62cf08b6d71086ebbefde9615074493e3c3be80 Mon Sep 17 00:00:00 2001 From: Randy Grok <@faulttolerance.net> Date: Thu, 2 Jan 2025 15:30:36 +0100 Subject: [PATCH 11/14] gci --- store/v2/root/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/v2/root/store.go b/store/v2/root/store.go index 4e82c42cddda..c4430f619860 100644 --- a/store/v2/root/store.go +++ b/store/v2/root/store.go @@ -1,7 +1,6 @@ package root import ( - "cosmossdk.io/store/v2/commitment" "crypto/sha256" "errors" "fmt" @@ -12,6 +11,7 @@ import ( corelog "cosmossdk.io/core/log" corestore "cosmossdk.io/core/store" "cosmossdk.io/store/v2" + "cosmossdk.io/store/v2/commitment" "cosmossdk.io/store/v2/metrics" "cosmossdk.io/store/v2/migration" "cosmossdk.io/store/v2/proof" From ff4ef84091b95ad21c62789c7139b04da6fe7adb Mon Sep 17 00:00:00 2001 From: Randy Grok <98407738+randygrok@users.noreply.github.com> Date: Tue, 7 Jan 2025 14:39:02 +0100 Subject: [PATCH 12/14] Update store/v2/root/store.go Co-authored-by: Marko --- store/v2/root/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/v2/root/store.go b/store/v2/root/store.go index c4430f619860..70bad7162043 100644 --- a/store/v2/root/store.go +++ b/store/v2/root/store.go @@ -178,7 +178,7 @@ func (s *Store) Query(storeKey []byte, version uint64, key []byte, prove bool) ( } var cs store.Committer - // if is V2 means that the store is a v2 store, it can be a migrated v1 store or a v2 store + // if is V2 means that the store is using iavlv2, it has been migrated from iavlv1 to iavlv2 if v2Commitment, isV2 := s.stateCommitment.(*commitment.CommitStore); isV2 { // if the store is a v2 store, we need to check if the version is less than or equal to the v2 migration height v2UpgradeHeight, err := v2Commitment.GetV2MigrationHeight() From e6e4ee745c5dcca9c669d69c2117f82bbdac2b55 Mon Sep 17 00:00:00 2001 From: Randy Grok <98407738+randygrok@users.noreply.github.com> Date: Tue, 7 Jan 2025 14:39:21 +0100 Subject: [PATCH 13/14] Update store/v2/root/store.go Co-authored-by: Marko --- store/v2/root/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/v2/root/store.go b/store/v2/root/store.go index 70bad7162043..703937b3f806 100644 --- a/store/v2/root/store.go +++ b/store/v2/root/store.go @@ -180,7 +180,7 @@ func (s *Store) Query(storeKey []byte, version uint64, key []byte, prove bool) ( var cs store.Committer // if is V2 means that the store is using iavlv2, it has been migrated from iavlv1 to iavlv2 if v2Commitment, isV2 := s.stateCommitment.(*commitment.CommitStore); isV2 { - // if the store is a v2 store, we need to check if the version is less than or equal to the v2 migration height + // if the commitment structure is iavlv2 store, we need to check if the version is less than or equal to the v2 migration height v2UpgradeHeight, err := v2Commitment.GetV2MigrationHeight() if err != nil { return store.QueryResult{}, fmt.Errorf("failed to get v2 migration height: %w", err) From 7aaa559cdc0f0deb5d21f34538ea11b93977281a Mon Sep 17 00:00:00 2001 From: Randy Grok <@faulttolerance.net> Date: Tue, 7 Jan 2025 14:58:41 +0100 Subject: [PATCH 14/14] add check to validate stateCommitment is not nil --- store/v2/root/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/v2/root/store.go b/store/v2/root/store.go index c4430f619860..47545363e7d7 100644 --- a/store/v2/root/store.go +++ b/store/v2/root/store.go @@ -187,7 +187,7 @@ func (s *Store) Query(storeKey []byte, version uint64, key []byte, prove bool) ( } // if the version is less than or equal to the v2 migration height, we need to use the v1 state commitment - if version <= v2UpgradeHeight { + if version <= v2UpgradeHeight && s.v1StateCommitment != nil { cs = s.v1StateCommitment } else { // if the version is greater than the v2 migration height, we need to use the v2 state commitment cs = s.stateCommitment