-
Notifications
You must be signed in to change notification settings - Fork 14
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
refactor(x/crosschain): migrate module data #764
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package store | ||
|
||
import ( | ||
storetypes "cosmossdk.io/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
func RemoveStoreKeys(ctx sdk.Context, storeKey storetypes.StoreKey, prefixKeys [][]byte) { | ||
store := ctx.KVStore(storeKey) | ||
|
||
deleteFn := func(prefixKey []byte) { | ||
iterator := storetypes.KVStorePrefixIterator(store, prefixKey) | ||
defer iterator.Close() | ||
deleteCount := 0 | ||
for ; iterator.Valid(); iterator.Next() { | ||
store.Delete(iterator.Key()) | ||
deleteCount++ | ||
} | ||
if deleteCount > 0 { | ||
ctx.Logger().Info("remove store key", "kvStore", storeKey.Name(), | ||
"prefix", prefixKey[0], "deleteKeyCount", deleteCount) | ||
} | ||
} | ||
|
||
for _, key := range prefixKeys { | ||
deleteFn(key) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,7 +106,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} | |
// RegisterServices registers module services. | ||
func (am AppModule) RegisterServices(cfg module.Configurator) { | ||
migrator := crosschainkeeper.NewMigrator(am.keeper) | ||
if err := cfg.RegisterMigration(am.Name(), 1, migrator.Migrate); err != nil { | ||
if err := cfg.RegisterMigration(am.Name(), 2, migrator.Migrate7to8WithArbExternalBlockTime); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
@@ -128,7 +128,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw | |
|
||
// ConsensusVersion implements AppModule/ConsensusVersion. | ||
func (am AppModule) ConsensusVersion() uint64 { | ||
return 2 | ||
return 3 | ||
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. 💡 Codebase verification Migration Registration Mismatch Detected The 🔗 Analysis chainEnsure consistency between migration versions The Run the following script to list all registered migrations for the module: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify registered migrations for the arbitrum module.
# Expected: Should list migrations including from version 2 to 3.
rg 'RegisterMigration\(' -A 2 --multiline --glob 'x/arbitrum/*.go'
Length of output: 254 Script: #!/bin/bash
# Description: Verify if migration from version 2 to 3 is registered for the arbitrum module.
ast-grep --pattern $'cfg.RegisterMigration($_, 2, $_)' --glob 'x/arbitrum/*.go'
Length of output: 322 Script: #!/bin/bash
# Description: Verify if migration from version 2 to 3 is registered for the arbitrum module.
ast-grep --pattern $'cfg.RegisterMigration($_, 2, $_)' --globs 'x/arbitrum/*.go'
Length of output: 213 |
||
} | ||
|
||
// EndBlock implements app module | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,7 +108,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} | |
// RegisterServices registers module services. | ||
func (am AppModule) RegisterServices(cfg module.Configurator) { | ||
migrator := crosschainkeeper.NewMigrator(am.keeper) | ||
if err := cfg.RegisterMigration(am.Name(), 2, migrator.Migrate); err != nil { | ||
if err := cfg.RegisterMigration(am.Name(), 3, migrator.Migrate7to8); err != nil { | ||
panic(err) | ||
} | ||
Comment on lines
+111
to
113
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. 💡 Codebase verification
The 🔗 Analysis chainLGTM. Verify migration function implementation. The update to use Please ensure that the 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify the implementation of Migrate7to8 function
# Test: Search for the Migrate7to8 function definition
rg -A 10 'func \(Migrator\) Migrate7to8'
Length of output: 23819 |
||
} | ||
|
@@ -130,7 +130,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw | |
|
||
// ConsensusVersion implements AppModule/ConsensusVersion. | ||
func (am AppModule) ConsensusVersion() uint64 { | ||
return 3 | ||
return 4 | ||
} | ||
|
||
// EndBlock implements app module | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,7 @@ package keeper | |
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
fxtypes "github.com/functionx/fx-core/v8/types" | ||
"github.com/functionx/fx-core/v8/x/crosschain/types" | ||
"github.com/functionx/fx-core/v8/x/crosschain/migrations/v8" | ||
) | ||
|
||
type Migrator struct { | ||
|
@@ -17,18 +16,15 @@ func NewMigrator(k Keeper) Migrator { | |
} | ||
} | ||
|
||
func (m Migrator) Migrate(ctx sdk.Context) error { | ||
params := m.keeper.GetParams(ctx) | ||
|
||
params.BridgeCallTimeout = types.DefBridgeCallTimeout | ||
params.BridgeCallMaxGasLimit = types.MaxGasLimit | ||
func (m Migrator) Migrate7to8(ctx sdk.Context) error { | ||
return v8.Migrate(ctx, m.keeper.storeKey) | ||
} | ||
|
||
enablePending := false | ||
if ctx.ChainID() == fxtypes.TestnetChainId { | ||
enablePending = true | ||
func (m Migrator) Migrate7to8WithArbExternalBlockTime(ctx sdk.Context) error { | ||
if err := m.Migrate7to8(ctx); err != nil { | ||
return err | ||
} | ||
params.EnableSendToExternalPending = enablePending | ||
params.EnableBridgeCallPending = enablePending | ||
|
||
params := m.keeper.GetParams(ctx) | ||
params.AverageExternalBlockTime = 250 | ||
Comment on lines
+23
to
+28
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. 🛠️ Refactor suggestion Consider making Hardcoding |
||
return m.keeper.SetParams(ctx, ¶ms) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package v8 | ||
|
||
import ( | ||
storetypes "cosmossdk.io/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/functionx/fx-core/v8/app/upgrades/store" | ||
) | ||
|
||
var ( | ||
// Deprecated: DenomToTokenKey prefixes the index of asset denom to external token | ||
DenomToTokenKey = []byte{0x26} | ||
|
||
// Deprecated: TokenToDenomKey prefixes the index of assets external token to denom | ||
TokenToDenomKey = []byte{0x27} | ||
|
||
// Deprecated: remove data in upgrade | ||
BridgeCallFromMsgKey = []byte{0x51} | ||
) | ||
|
||
// Deprecated: GetTokenToDenomKey returns the following key format | ||
func GetTokenToDenomKey(denom string) []byte { | ||
return append(TokenToDenomKey, []byte(denom)...) | ||
} | ||
|
||
func GetRemovedStoreKeys() [][]byte { | ||
return [][]byte{ | ||
DenomToTokenKey, | ||
TokenToDenomKey, | ||
BridgeCallFromMsgKey, | ||
} | ||
} | ||
|
||
func Migrate(ctx sdk.Context, storeKey storetypes.StoreKey) error { | ||
store.RemoveStoreKeys(ctx, storeKey, GetRemovedStoreKeys()) | ||
return nil | ||
} | ||
Comment on lines
+34
to
+37
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. 🛠️ Refactor suggestion Enhance error handling and logging in Migrate function. While the Migrate function correctly calls store.RemoveStoreKeys, there are a few areas for improvement:
Here's a suggested improvement: func Migrate(ctx sdk.Context, storeKey storetypes.StoreKey) error {
removedKeys := GetRemovedStoreKeys()
err := store.RemoveStoreKeys(ctx, storeKey, removedKeys)
if err != nil {
ctx.Logger().Error("Failed to remove deprecated store keys", "error", err)
return err
}
ctx.Logger().Info("Successfully removed deprecated store keys", "count", len(removedKeys))
return nil
} This version provides better error handling and adds logging for both success and failure scenarios. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,7 +108,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} | |
// RegisterServices registers module services. | ||
func (am AppModule) RegisterServices(cfg module.Configurator) { | ||
migrator := crosschainkeeper.NewMigrator(am.keeper) | ||
if err := cfg.RegisterMigration(am.Name(), 2, migrator.Migrate); err != nil { | ||
if err := cfg.RegisterMigration(am.Name(), 3, migrator.Migrate7to8); err != nil { | ||
panic(err) | ||
} | ||
Comment on lines
+111
to
113
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. 💡 Codebase verification Migration Function The 🔗 Analysis chainLGTM. Verify impact and update documentation. The migration version update from 2 to 3 and the change to Please ensure the following:
Run the following script to verify the consistency of migration versions across modules: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check for consistency in migration versions across modules
# Test: Search for RegisterMigration calls in all module files
rg --type go 'RegisterMigration.*Migrate.*to.*'
Length of output: 62 Script: #!/bin/bash
# Description: Search for all RegisterMigration calls and list their migration functions
rg --type go 'RegisterMigration\s*\(\s*[^,]+,\s*\d+,\s*([^)\s]+)\s*\)' --replace '$1' --count-matches
Length of output: 309 Script: #!/bin/bash
# Description: Find the definition of migrator.Migrate7to8
rg --type go 'func\s+Migrate7to8\s*\('
Length of output: 40 |
||
} | ||
|
@@ -130,7 +130,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw | |
|
||
// ConsensusVersion implements AppModule/ConsensusVersion. | ||
func (am AppModule) ConsensusVersion() uint64 { | ||
return 3 | ||
return 4 | ||
} | ||
|
||
// EndBlock implements app module | ||
|
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.
Handle errors without panicking to improve stability
In the
RegisterServices
function, usingpanic
on error can lead to unexpected crashes. It's better to handle the error gracefully to enhance the module's robustness.Apply this diff to return the error instead of panicking: