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!: Nymlab Prop31 #628

Merged
merged 21 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ go-version:

go.sum: go.mod
@echo "Ensuring app dependencies have not been modified..."
go mod verify
# go mod verify
go mod tidy

verify:
Expand Down
332 changes: 178 additions & 154 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/cheqd/cheqd-node/app/migrations"
appparams "github.com/cheqd/cheqd-node/app/params"
upgradeV1 "github.com/cheqd/cheqd-node/app/upgrades/v1"
upgradeV2 "github.com/cheqd/cheqd-node/app/upgrades/v2"
posthandler "github.com/cheqd/cheqd-node/post"
did "github.com/cheqd/cheqd-node/x/did"
didkeeper "github.com/cheqd/cheqd-node/x/did/keeper"
Expand Down Expand Up @@ -223,6 +225,7 @@ type App struct {
ScopedIBCFeeKeeper capabilitykeeper.ScopedKeeper
ScopedICAControllerKeeper capabilitykeeper.ScopedKeeper
ScopedICAHostKeeper capabilitykeeper.ScopedKeeper
ScopedResourceKeeper capabilitykeeper.ScopedKeeper

didKeeper didkeeper.Keeper
resourceKeeper resourcekeeper.Keeper
Expand Down Expand Up @@ -317,6 +320,7 @@ func New(
scopedTransferKeeper := app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)
scopedICAControllerKeeper := app.CapabilityKeeper.ScopeToModule(icacontrollertypes.SubModuleName)
scopedICAHostKeeper := app.CapabilityKeeper.ScopeToModule(icahosttypes.SubModuleName)
scopedResourceKeeper := app.CapabilityKeeper.ScopeToModule(resourcetypes.ModuleName)

// Applications that wish to enforce statically created ScopedKeepers should call `Seal` after creating
// their scoped modules in `NewApp` with `ScopeToModule`
Expand Down Expand Up @@ -528,6 +532,22 @@ func New(
AddRoute(icacontrollertypes.SubModuleName, icaControllerStack).
AddRoute(icahosttypes.SubModuleName, icaHostStack)

// x/resource
app.resourceKeeper = *resourcekeeper.NewKeeper(
appCodec, keys[resourcetypes.StoreKey],
app.GetSubspace(resourcetypes.ModuleName),
&app.IBCKeeper.PortKeeper,
scopedResourceKeeper,
)

// create the resource IBC stack
var resourceIbcStack porttypes.IBCModule
resourceIbcStack = resource.NewIBCModule(app.resourceKeeper)
resourceIbcStack = ibcfee.NewIBCMiddleware(resourceIbcStack, app.IBCFeeKeeper)

// Add resource stack to IBC Router
ibcRouter.AddRoute(resourcetypes.ModuleName, resourceIbcStack)

// Seal the IBC Router
app.IBCKeeper.SetRouter(ibcRouter)

Expand All @@ -550,11 +570,6 @@ func New(
app.GetSubspace(didtypes.ModuleName),
)

app.resourceKeeper = *resourcekeeper.NewKeeper(
appCodec, keys[resourcetypes.StoreKey],
app.GetSubspace(resourcetypes.ModuleName),
)

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
// we prefer to be more strict in what arguments the modules expect.
skipGenesisInvariants := cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants))
Expand Down Expand Up @@ -720,155 +735,11 @@ func New(
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)

// Upgrade handler for the next release
app.UpgradeKeeper.SetUpgradeHandler(UpgradeName,
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Handler for upgrade plan: " + UpgradeName)

// Fix lack of version map initialization in InitChainer for new chains
if len(fromVM) == 0 {
ctx.Logger().Info("Version map is empty. Initialising to required values.")

// Set these explicitly to avoid calling InitGenesis on modules that don't need it.
// Explicitly skipping x/auth migrations.
fromVM = map[string]uint64{
"auth": 2,
"authz": 1,
"bank": 2,
"capability": 1,
"cheqd": 3,
"crisis": 1,
"distribution": 2,
"evidence": 1,
"feegrant": 1,
"genutil": 1,
"gov": 2,
"ibc": 2,
"interchainaccounts": 1,
"mint": 1,
"params": 1,
"resource": 1,
"slashing": 2,
"staking": 2,
"transfer": 1,
"upgrade": 1,
"vesting": 1,
}
}

ctx.Logger().Info("Version map is populated. Running migrations.")
ctx.Logger().Debug(fmt.Sprintf("Previous version map: %v", fromVM))

// Get current version map
newVM := app.mm.GetVersionMap()
ctx.Logger().Debug(fmt.Sprintf("Target version map: %v", newVM))

// Set cheqd/DID module to ConsensusVersion
fromVM[didtypes.ModuleName] = newVM[didtypes.ModuleName]

// Set resource module to ConsensusVersion
fromVM[resourcetypes.ModuleName] = newVM[resourcetypes.ModuleName]

// Add defaults for DID module subspace
didSubspace := app.GetSubspace(didtypes.ModuleName)
didSubspace.Set(ctx, didtypes.ParamStoreKeyFeeParams, didtypes.DefaultFeeParams())

// Add defaults for resource subspace
resourceSubspace := app.GetSubspace(resourcetypes.ModuleName)
resourceSubspace.Set(ctx, resourcetypes.ParamStoreKeyFeeParams, resourcetypes.DefaultFeeParams())

// create ICS27 Controller submodule params
controllerParams := icacontrollertypes.Params{
ControllerEnabled: true,
}

// create ICS27 Host submodule params
hostParams := icahosttypes.Params{
HostEnabled: true,
AllowMessages: []string{
authzMsgExec,
authzMsgGrant,
authzMsgRevoke,
bankMsgSend,
bankMsgMultiSend,
distrMsgSetWithdrawAddr,
distrMsgWithdrawValidatorCommission,
distrMsgFundCommunityPool,
distrMsgWithdrawDelegatorReward,
feegrantMsgGrantAllowance,
feegrantMsgRevokeAllowance,
govMsgVoteWeighted,
govMsgSubmitProposal,
govMsgDeposit,
govMsgVote,
stakingMsgEditValidator,
stakingMsgDelegate,
stakingMsgUndelegate,
stakingMsgBeginRedelegate,
stakingMsgCreateValidator,
vestingMsgCreateVestingAccount,
ibcMsgTransfer,
// cheqd namespace
didMsgCreateDidDoc,
didMsgUpdateDidDoc,
didMsgDeactivateDidDoc,
resourceMsgCreateResource,
},
}

// initialize ICS27 module
ctx.Logger().Debug("Initialise interchainaccount module...")
icaModule.InitModule(ctx, controllerParams, hostParams)

// cheqd migrations
migrationContext := migrations.NewMigrationContext(
app.appCodec,
keys[didtypes.StoreKey],
app.GetSubspace(didtypes.ModuleName),
keys[resourcetypes.StoreKey],
app.GetSubspace(resourcetypes.ModuleName),
)

ctx.Logger().Debug("Initialise cheqd DID and Resource module migrations...")
cheqdMigrator := migrations.NewMigrator(
migrationContext,
[]migrations.Migration{
// Protobufs
migrations.MigrateDidProtobuf,
migrations.MigrateResourceProtobuf,

// Indy style
migrations.MigrateDidIndyStyle,
migrations.MigrateResourceIndyStyle,

// UUID normalizatiion
migrations.MigrateDidUUID,
migrations.MigrateResourceUUID,

// Did version id
migrations.MigrateDidVersionID,

// Resource checksum
migrations.MigrateResourceChecksum,

// Resource version links
migrations.MigrateResourceVersionLinks,

// Resource default alternative url
migrations.MigrateResourceDefaultAlternativeURL,
})

err = cheqdMigrator.Migrate(ctx)
if err != nil {
panic(err)
}

// Run all default migrations
ctx.Logger().Debug(fmt.Sprintf("Previous version map (for default RunMigrations): %v", fromVM))
toVM, err := app.mm.RunMigrations(ctx, app.configurator, fromVM)
ctx.Logger().Debug(fmt.Sprintf("New version map (after default RunMigrations): %v", toVM))
return toVM, err
})
// Upgrade handler for v1
v1UpgradeHandler := app.upgradeHandlerV1(icaModule, keys[didtypes.StoreKey], keys[resourcetypes.StoreKey])
app.UpgradeKeeper.SetUpgradeHandler(upgradeV1.UpgradeName, v1UpgradeHandler)
// Upgrade handler for v2
app.UpgradeKeeper.SetUpgradeHandler(upgradeV2.UpgradeName, upgradeV2.CreateUpgradeHandler(app.mm, app.configurator))

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
Expand All @@ -880,6 +751,7 @@ func New(
app.ScopedTransferKeeper = scopedTransferKeeper
app.ScopedICAControllerKeeper = scopedICAControllerKeeper
app.ScopedICAHostKeeper = scopedICAHostKeeper
app.ScopedResourceKeeper = scopedResourceKeeper

return app
}
Expand Down Expand Up @@ -1048,3 +920,155 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino

return paramsKeeper
}

func (app *App) upgradeHandlerV1(icaModule ica.AppModule, didStoreKey *storetypes.KVStoreKey, resourceStoreKey *storetypes.KVStoreKey) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Handler for upgrade plan: " + upgradeV1.UpgradeName)

// Fix lack of version map initialization in InitChainer for new chains
if len(fromVM) == 0 {
ctx.Logger().Info("Version map is empty. Initialising to required values.")

// Set these explicitly to avoid calling InitGenesis on modules that don't need it.
// Explicitly skipping x/auth migrations.
fromVM = map[string]uint64{
"auth": 2,
"authz": 1,
"bank": 2,
"capability": 1,
"cheqd": 3,
"crisis": 1,
"distribution": 2,
"evidence": 1,
"feegrant": 1,
"genutil": 1,
"gov": 2,
"ibc": 2,
"interchainaccounts": 1,
"mint": 1,
"params": 1,
"resource": 1,
"slashing": 2,
"staking": 2,
"transfer": 1,
"upgrade": 1,
"vesting": 1,
}
}

ctx.Logger().Info("Version map is populated. Running migrations.")
ctx.Logger().Debug(fmt.Sprintf("Previous version map: %v", fromVM))

// Get current version map
newVM := app.mm.GetVersionMap()
ctx.Logger().Debug(fmt.Sprintf("Target version map: %v", newVM))

// Set cheqd/DID module to ConsensusVersion
fromVM[didtypes.ModuleName] = newVM[didtypes.ModuleName]

// Set resource module to ConsensusVersion
fromVM[resourcetypes.ModuleName] = newVM[resourcetypes.ModuleName]

// Add defaults for DID module subspace
didSubspace := app.GetSubspace(didtypes.ModuleName)
didSubspace.Set(ctx, didtypes.ParamStoreKeyFeeParams, didtypes.DefaultFeeParams())

// Add defaults for resource subspace
resourceSubspace := app.GetSubspace(resourcetypes.ModuleName)
resourceSubspace.Set(ctx, resourcetypes.ParamStoreKeyFeeParams, resourcetypes.DefaultFeeParams())

// create ICS27 Controller submodule params
controllerParams := icacontrollertypes.Params{
ControllerEnabled: true,
}

// create ICS27 Host submodule params
hostParams := icahosttypes.Params{
HostEnabled: true,
AllowMessages: []string{
authzMsgExec,
authzMsgGrant,
authzMsgRevoke,
bankMsgSend,
bankMsgMultiSend,
distrMsgSetWithdrawAddr,
distrMsgWithdrawValidatorCommission,
distrMsgFundCommunityPool,
distrMsgWithdrawDelegatorReward,
feegrantMsgGrantAllowance,
feegrantMsgRevokeAllowance,
govMsgVoteWeighted,
govMsgSubmitProposal,
govMsgDeposit,
govMsgVote,
stakingMsgEditValidator,
stakingMsgDelegate,
stakingMsgUndelegate,
stakingMsgBeginRedelegate,
stakingMsgCreateValidator,
vestingMsgCreateVestingAccount,
ibcMsgTransfer,
// cheqd namespace
didMsgCreateDidDoc,
didMsgUpdateDidDoc,
didMsgDeactivateDidDoc,
resourceMsgCreateResource,
},
}

// initialize ICS27 module
ctx.Logger().Debug("Initialise interchainaccount module...")
icaModule.InitModule(ctx, controllerParams, hostParams)

// cheqd migrations
migrationContext := migrations.NewMigrationContext(
app.appCodec,
didStoreKey,
app.GetSubspace(didtypes.ModuleName),
resourceStoreKey,
app.GetSubspace(resourcetypes.ModuleName),
&app.IBCKeeper.PortKeeper,
app.ScopedResourceKeeper,
)

ctx.Logger().Debug("Initialise cheqd DID and Resource module migrations...")
cheqdMigrator := migrations.NewMigrator(
migrationContext,
[]migrations.Migration{
// Protobufs
migrations.MigrateDidProtobuf,
migrations.MigrateResourceProtobuf,

// Indy style
migrations.MigrateDidIndyStyle,
migrations.MigrateResourceIndyStyle,

// UUID normalizatiion
migrations.MigrateDidUUID,
migrations.MigrateResourceUUID,

// Did version id
migrations.MigrateDidVersionID,

// Resource checksum
migrations.MigrateResourceChecksum,

// Resource version links
migrations.MigrateResourceVersionLinks,

// Resource default alternative url
migrations.MigrateResourceDefaultAlternativeURL,
})

err := cheqdMigrator.Migrate(ctx)
if err != nil {
panic(err)
}

// Run all default migrations
ctx.Logger().Debug(fmt.Sprintf("Previous version map (for default RunMigrations): %v", fromVM))
toVM, err := app.mm.RunMigrations(ctx, app.configurator, fromVM)
ctx.Logger().Debug(fmt.Sprintf("New version map (after default RunMigrations): %v", toVM))
return toVM, err
}
}
Loading