forked from cosmos/ibc-go
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration for checksums to collections.KeySet (cosmos#5324)
* wip, we're migrating * add first draft of tests. * Add Logger, log in migrations, use more specific name, handle store errors. * Update modules/light-clients/08-wasm/keeper/migrations.go Co-authored-by: Damian Nolan <[email protected]> * Mark key, Checksums msg as deprecated. --------- Co-authored-by: Damian Nolan <[email protected]>
- Loading branch information
1 parent
b04b843
commit 2a39b26
Showing
7 changed files
with
367 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/ibcwasm" | ||
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" | ||
) | ||
|
||
// Migrator is a struct for handling in-place store migrations. | ||
type Migrator struct { | ||
keeper Keeper | ||
} | ||
|
||
// NewMigrator returns a new Migrator. | ||
func NewMigrator(keeper Keeper) Migrator { | ||
return Migrator{ | ||
keeper: keeper, | ||
} | ||
} | ||
|
||
// MigrateChecksums migrates the wasm store from using a single key to | ||
// store a list of checksums to using a collections.KeySet to store the checksums. | ||
// | ||
// It grabs the checksums stored previously under the old key and stores | ||
// them in the global KeySet collection. It then deletes the old key and | ||
// the checksums stored under it. | ||
func (m Migrator) MigrateChecksums(ctx sdk.Context) error { | ||
checksums, err := m.getStoredChecksums(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, hash := range checksums { | ||
if err := ibcwasm.Checksums.Set(ctx, hash); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// delete the previously stored checksums | ||
if err := m.deleteChecksums(ctx); err != nil { | ||
return err | ||
} | ||
|
||
m.keeper.Logger(ctx).Info("successfully migrated Checksums to collections") | ||
return nil | ||
} | ||
|
||
// getStoredChecksums returns the checksums stored under the KeyChecksums key. | ||
func (m Migrator) getStoredChecksums(ctx sdk.Context) ([][]byte, error) { | ||
store := m.keeper.storeService.OpenKVStore(ctx) | ||
|
||
bz, err := store.Get([]byte(types.KeyChecksums)) | ||
if err != nil { | ||
return [][]byte{}, err | ||
} | ||
|
||
var hashes types.Checksums | ||
err = m.keeper.cdc.Unmarshal(bz, &hashes) | ||
if err != nil { | ||
return [][]byte{}, err | ||
} | ||
|
||
return hashes.Checksums, nil | ||
} | ||
|
||
// deleteChecksums deletes the checksums stored under the KeyChecksums key. | ||
func (m Migrator) deleteChecksums(ctx sdk.Context) error { | ||
store := m.keeper.storeService.OpenKVStore(ctx) | ||
err := store.Delete([]byte(types.KeyChecksums)) | ||
|
||
return err | ||
} |
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,61 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/internal/ibcwasm" | ||
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/keeper" | ||
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestMigrateWasmStore() { | ||
testCases := []struct { | ||
name string | ||
checksums [][]byte | ||
}{ | ||
{ | ||
"success: empty checksums", | ||
[][]byte{}, | ||
}, | ||
{ | ||
"success: multiple checksums", | ||
[][]byte{[]byte("hash1"), []byte("hash2"), []byte("hash3")}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
suite.Run(tc.name, func() { | ||
suite.SetupTest() | ||
|
||
suite.storeChecksums(tc.checksums) | ||
|
||
// run the migration | ||
wasmKeeper := GetSimApp(suite.chainA).WasmClientKeeper | ||
m := keeper.NewMigrator(wasmKeeper) | ||
|
||
err := m.MigrateChecksums(suite.chainA.GetContext()) | ||
suite.Require().NoError(err) | ||
|
||
// check that they were stored in KeySet | ||
for _, hash := range tc.checksums { | ||
suite.Require().True(ibcwasm.Checksums.Has(suite.chainA.GetContext(), hash)) | ||
} | ||
|
||
// check that the data under the old key was deleted | ||
store := suite.chainA.GetContext().KVStore(GetSimApp(suite.chainA).GetKey(types.StoreKey)) | ||
suite.Require().Nil(store.Get([]byte(types.KeyChecksums))) | ||
}) | ||
} | ||
} | ||
|
||
// storeChecksums stores the given checksums under the KeyChecksums key, it runs | ||
// each time on an empty store so we don't need to read the previous checksums. | ||
func (suite *KeeperTestSuite) storeChecksums(checksums [][]byte) { | ||
ctx := suite.chainA.GetContext() | ||
|
||
store := ctx.KVStore(GetSimApp(suite.chainA).GetKey(types.StoreKey)) | ||
checksum := types.Checksums{Checksums: checksums} | ||
bz, err := GetSimApp(suite.chainA).AppCodec().Marshal(&checksum) | ||
suite.Require().NoError(err) | ||
|
||
store.Set([]byte(types.KeyChecksums), bz) | ||
} |
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
Oops, something went wrong.