-
Notifications
You must be signed in to change notification settings - Fork 4
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: add v2.0 upgrade handler #75
Changes from 2 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 | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,10 @@ | ||||||||||||||||||||||||||||||
package upgrades | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||||||||
agentkeeper "github.com/Lorenzo-Protocol/lorenzo/x/agent/keeper" | ||||||||||||||||||||||||||||||
btcstakingkeeper "github.com/Lorenzo-Protocol/lorenzo/x/btcstaking/keeper" | ||||||||||||||||||||||||||||||
plankeeper "github.com/Lorenzo-Protocol/lorenzo/x/plan/keeper" | ||||||||||||||||||||||||||||||
tokenkeeper "github.com/Lorenzo-Protocol/lorenzo/x/token/keeper" | ||||||||||||||||||||||||||||||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||||||||||||||||||||||||||||||
"github.com/cosmos/cosmos-sdk/codec" | ||||||||||||||||||||||||||||||
store "github.com/cosmos/cosmos-sdk/store/types" | ||||||||||||||||||||||||||||||
|
@@ -10,6 +14,7 @@ import ( | |||||||||||||||||||||||||||||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" | ||||||||||||||||||||||||||||||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" | ||||||||||||||||||||||||||||||
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||||||||||||||||||||||||||||||
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper" | ||||||||||||||||||||||||||||||
evmkeeper "github.com/evmos/ethermint/x/evm/keeper" | ||||||||||||||||||||||||||||||
feemarketkeeper "github.com/evmos/ethermint/x/feemarket/keeper" | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
|
@@ -35,13 +40,42 @@ type ConsensusParamsReaderWriter interface { | |||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
type AppKeepers struct { | ||||||||||||||||||||||||||||||
AppCodec codec.Codec | ||||||||||||||||||||||||||||||
AccountKeeper authkeeper.AccountKeeper | ||||||||||||||||||||||||||||||
BankKeeper bankkeeper.Keeper | ||||||||||||||||||||||||||||||
GetKey func(moduleName string) *storetypes.KVStoreKey | ||||||||||||||||||||||||||||||
ModuleManager *module.Manager | ||||||||||||||||||||||||||||||
EvmKeeper *evmkeeper.Keeper | ||||||||||||||||||||||||||||||
FeeMarketKeeper feemarketkeeper.Keeper | ||||||||||||||||||||||||||||||
AppCodec codec.Codec | ||||||||||||||||||||||||||||||
AccountKeeper authkeeper.AccountKeeper | ||||||||||||||||||||||||||||||
BankKeeper bankkeeper.Keeper | ||||||||||||||||||||||||||||||
GetKey func(moduleName string) *storetypes.KVStoreKey | ||||||||||||||||||||||||||||||
ModuleManager *module.Manager | ||||||||||||||||||||||||||||||
IBCKeeper *ibckeeper.Keeper | ||||||||||||||||||||||||||||||
EvmKeeper *evmkeeper.Keeper | ||||||||||||||||||||||||||||||
FeeMarketKeeper feemarketkeeper.Keeper | ||||||||||||||||||||||||||||||
AgentKeeper agentkeeper.Keeper | ||||||||||||||||||||||||||||||
PlanKeeper *plankeeper.Keeper | ||||||||||||||||||||||||||||||
BTCStakingKeeper btcstakingkeeper.Keeper | ||||||||||||||||||||||||||||||
TokenKeeper *tokenkeeper.Keeper | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
ReaderWriter ConsensusParamsReaderWriter | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
type UpgradeRouter struct { | ||||||||||||||||||||||||||||||
mu map[string]Upgrade | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
func NewUpgradeRouter() *UpgradeRouter { | ||||||||||||||||||||||||||||||
return &UpgradeRouter{make(map[string]Upgrade)} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
func (r *UpgradeRouter) Register(u Upgrade) *UpgradeRouter { | ||||||||||||||||||||||||||||||
if _, has := r.mu[u.UpgradeName]; has { | ||||||||||||||||||||||||||||||
panic(u.UpgradeName + " already registered") | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
r.mu[u.UpgradeName] = u | ||||||||||||||||||||||||||||||
return r | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
Comment on lines
+67
to
+73
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. Handle duplicate registration appropriately. The Register function panics if an upgrade is already registered. Consider handling this case more gracefully, such as logging an error or returning an error. - panic(u.UpgradeName + " already registered")
+ return fmt.Errorf("%s already registered", u.UpgradeName) Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
func (r *UpgradeRouter) Routers() map[string]Upgrade { | ||||||||||||||||||||||||||||||
return r.mu | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
func (r *UpgradeRouter) UpgradeInfo(planName string) Upgrade { | ||||||||||||||||||||||||||||||
return r.mu[planName] | ||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,29 @@ | ||||||||||||||||||||||||||||||||||
package v200 | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||||||||||||
"github.com/Lorenzo-Protocol/lorenzo/app/upgrades" | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
sdk "github.com/cosmos/cosmos-sdk/types" | ||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
func migrateAgentFromBTCStakingToAgent( | ||||||||||||||||||||||||||||||||||
ctx sdk.Context, | ||||||||||||||||||||||||||||||||||
app upgrades.AppKeepers, | ||||||||||||||||||||||||||||||||||
) error { | ||||||||||||||||||||||||||||||||||
btcStakingParams := app.BTCStakingKeeper.GetParams(ctx) | ||||||||||||||||||||||||||||||||||
for _, receiver := range btcStakingParams.Receivers { | ||||||||||||||||||||||||||||||||||
app.AgentKeeper.AddAgent( | ||||||||||||||||||||||||||||||||||
ctx, | ||||||||||||||||||||||||||||||||||
receiver.Name, | ||||||||||||||||||||||||||||||||||
receiver.Addr, receiver.EthAddr, | ||||||||||||||||||||||||||||||||||
"", | ||||||||||||||||||||||||||||||||||
"", | ||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||
Comment on lines
+15
to
+21
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. Handle errors from The function does not handle potential errors from - app.AgentKeeper.AddAgent(
+ if err := app.AgentKeeper.AddAgent(
ctx,
receiver.Name,
receiver.Addr, receiver.EthAddr,
"",
"",
); err != nil {
return err
} Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
// TODO: Is params.Receiver of btcstaking module removed? | ||||||||||||||||||||||||||||||||||
btcStakingParams.Receivers = nil | ||||||||||||||||||||||||||||||||||
if err := app.BTCStakingKeeper.SetParams(ctx, btcStakingParams); err != nil { | ||||||||||||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package v200 | ||
|
||
import ( | ||
"github.com/Lorenzo-Protocol/lorenzo/app/upgrades" | ||
"github.com/Lorenzo-Protocol/lorenzo/x/agent" | ||
agenttypes "github.com/Lorenzo-Protocol/lorenzo/x/agent/types" | ||
"github.com/Lorenzo-Protocol/lorenzo/x/plan" | ||
plantypes "github.com/Lorenzo-Protocol/lorenzo/x/plan/types" | ||
"github.com/Lorenzo-Protocol/lorenzo/x/token" | ||
tokentypes "github.com/Lorenzo-Protocol/lorenzo/x/token/types" | ||
|
||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
) | ||
|
||
// Upgrade defines a struct containing necessary fields that a SoftwareUpgradeProposal | ||
var Upgrade = upgrades.Upgrade{ | ||
UpgradeName: "v2.0", | ||
UpgradeHandlerConstructor: upgradeHandlerConstructor, | ||
StoreUpgrades: &storetypes.StoreUpgrades{ | ||
Added: []string{ | ||
agenttypes.StoreKey, | ||
plantypes.StoreKey, | ||
tokentypes.StoreKey, | ||
}, | ||
}, | ||
} | ||
|
||
func upgradeHandlerConstructor( | ||
m *module.Manager, | ||
c module.Configurator, | ||
app upgrades.AppKeepers, | ||
) upgradetypes.UpgradeHandler { | ||
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { | ||
fromVM[agenttypes.ModuleName] = agent.AppModule{}.ConsensusVersion() | ||
fromVM[plantypes.ModuleName] = plan.AppModule{}.ConsensusVersion() | ||
fromVM[tokentypes.ModuleName] = token.AppModule{}.ConsensusVersion() | ||
|
||
// agent module init | ||
// 1. set admin | ||
admin, err := sdk.AccAddressFromBech32("") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := app.AgentKeeper.SetAdmin(ctx, admin); err != nil { | ||
return nil, err | ||
} | ||
orisalden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 2. set agents | ||
if err := migrateAgentFromBTCStakingToAgent(ctx, app); err != nil { | ||
return nil, err | ||
} | ||
|
||
// plan module init | ||
planParams := plantypes.Params{ | ||
AllowList: []string{"*"}, | ||
} | ||
|
||
if err := app.PlanKeeper.SetParams(ctx, planParams); err != nil { | ||
return nil, err | ||
} | ||
|
||
// 3. set token params | ||
tokenParams := tokentypes.DefaultParams() | ||
app.TokenKeeper.SetParams(ctx, tokenParams) | ||
|
||
return app.ModuleManager.RunMigrations(ctx, c, fromVM) | ||
} | ||
} |
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.
Consider using a less aggressive error handling strategy.
Using
panic
for error handling might be too aggressive. Consider using error logging or returning an error.Committable suggestion