Skip to content

Commit

Permalink
Merge pull request #414 from ComposableFi/release/v6.4.x-epoch-staking
Browse files Browse the repository at this point in the history
Release/v6.4.x with a epoch staking feature
  • Loading branch information
rjonczy authored Jan 15, 2024
2 parents bfdbc74 + 2071c3c commit db053a7
Show file tree
Hide file tree
Showing 37 changed files with 4,122 additions and 12 deletions.
17 changes: 12 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank"

"github.com/notional-labs/composable/v6/app/keepers"
v4 "github.com/notional-labs/composable/v6/app/upgrades/v4"
v5 "github.com/notional-labs/composable/v6/app/upgrades/v5"
v6 "github.com/notional-labs/composable/v6/app/upgrades/v6"
v6_4 "github.com/notional-labs/composable/v6/app/upgrades/v6_4"

// bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

Expand Down Expand Up @@ -75,6 +73,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/slashing"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"

stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/cosmos-sdk/x/upgrade"
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
Expand All @@ -88,6 +87,7 @@ import (
ibcclientclient "github.com/cosmos/ibc-go/v7/modules/core/02-client/client"
ibchost "github.com/cosmos/ibc-go/v7/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
customstaking "github.com/notional-labs/composable/v6/custom/staking"
"github.com/spf13/cast"
icq "github.com/strangelove-ventures/async-icq/v7"
icqtypes "github.com/strangelove-ventures/async-icq/v7/types"
Expand All @@ -101,6 +101,7 @@ import (
custombankmodule "github.com/notional-labs/composable/v6/custom/bank"

"github.com/notional-labs/composable/v6/app/ante"
"github.com/notional-labs/composable/v6/x/stakingmiddleware"
transfermiddleware "github.com/notional-labs/composable/v6/x/transfermiddleware"
transfermiddlewaretypes "github.com/notional-labs/composable/v6/x/transfermiddleware/types"

Expand All @@ -125,6 +126,7 @@ import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

upgrades "github.com/notional-labs/composable/v6/app/upgrades"
stakingmiddlewaretypes "github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
)

const (
Expand All @@ -142,7 +144,7 @@ var (
// https://github.com/CosmWasm/wasmd/blob/02a54d33ff2c064f3539ae12d75d027d9c665f05/x/wasm/internal/types/proposal.go#L28-L34
EnableSpecificProposals = ""

Upgrades = []upgrades.Upgrade{v4.Upgrade, v5.Upgrade, v6.Upgrade}
Upgrades = []upgrades.Upgrade{v6_4.Upgrade}
Forks = []upgrades.Fork{}
)

Expand Down Expand Up @@ -223,6 +225,7 @@ var (
ratelimitmodule.AppModuleBasic{},
consensus.AppModuleBasic{},
alliancemodule.AppModuleBasic{},
stakingmiddleware.AppModuleBasic{},
// this line is used by starport scaffolding # stargate/app/moduleBasic
)

Expand Down Expand Up @@ -364,7 +367,8 @@ func NewComposableApp(
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, nil),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName)),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(distrtypes.ModuleName)),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
customstaking.NewAppModule(appCodec, *app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
stakingmiddleware.NewAppModule(appCodec, app.StakingMiddlewareKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
upgrade.NewAppModule(app.UpgradeKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
Expand Down Expand Up @@ -420,6 +424,7 @@ func NewComposableApp(
icatypes.ModuleName,
wasm.ModuleName,
alliancemoduletypes.ModuleName,
stakingmiddlewaretypes.ModuleName,
// this line is used by starport scaffolding # stargate/app/beginBlockers
)

Expand Down Expand Up @@ -454,6 +459,7 @@ func NewComposableApp(
icatypes.ModuleName,
wasm.ModuleName,
alliancemoduletypes.ModuleName,
stakingmiddlewaretypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -492,6 +498,7 @@ func NewComposableApp(
icatypes.ModuleName,
wasm.ModuleName,
alliancemoduletypes.ModuleName,
stakingmiddlewaretypes.ModuleName,
// this line is used by starport scaffolding # stargate/app/initGenesis
)

Expand Down
2 changes: 1 addition & 1 deletion app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (app *ComposableApp) ExportAppStateAndValidators(
return servertypes.ExportedApp{}, err
}

validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
validators, err := staking.WriteValidators(ctx, &app.StakingKeeper.Keeper)
if err != nil {
return servertypes.ExportedApp{}, err
}
Expand Down
14 changes: 10 additions & 4 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ import (
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"

stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
customstaking "github.com/notional-labs/composable/v6/custom/staking/keeper"

"github.com/cosmos/cosmos-sdk/x/upgrade"
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
Expand Down Expand Up @@ -103,6 +103,8 @@ import (
ibc_hooks "github.com/notional-labs/composable/v6/x/ibc-hooks"
ibchookskeeper "github.com/notional-labs/composable/v6/x/ibc-hooks/keeper"
ibchookstypes "github.com/notional-labs/composable/v6/x/ibc-hooks/types"
stakingmiddleware "github.com/notional-labs/composable/v6/x/stakingmiddleware/keeper"
stakingmiddlewaretypes "github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
)

const (
Expand All @@ -120,7 +122,7 @@ type AppKeepers struct {
AccountKeeper authkeeper.AccountKeeper
BankKeeper custombankkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
StakingKeeper *customstaking.Keeper
SlashingKeeper slashingkeeper.Keeper
MintKeeper mintkeeper.Keeper
DistrKeeper distrkeeper.Keeper
Expand Down Expand Up @@ -154,6 +156,7 @@ type AppKeepers struct {
RouterKeeper *routerkeeper.Keeper
RatelimitKeeper ratelimitmodulekeeper.Keeper
AllianceKeeper alliancemodulekeeper.Keeper
StakingMiddlewareKeeper stakingmiddleware.Keeper
}

// InitNormalKeepers initializes all 'normal' keepers.
Expand Down Expand Up @@ -185,8 +188,10 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.AccountKeeper,
)

appKeepers.StakingKeeper = stakingkeeper.NewKeeper(
appCodec, appKeepers.keys[stakingtypes.StoreKey], appKeepers.AccountKeeper, appKeepers.BankKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
appKeepers.StakingMiddlewareKeeper = stakingmiddleware.NewKeeper(appCodec, appKeepers.keys[stakingmiddlewaretypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())

appKeepers.StakingKeeper = customstaking.NewKeeper(
appCodec, appKeepers.keys[stakingtypes.StoreKey], appKeepers.AccountKeeper, appKeepers.BankKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(), &appKeepers.StakingMiddlewareKeeper,
)

appKeepers.MintKeeper = mintkeeper.NewKeeper(
Expand Down Expand Up @@ -477,6 +482,7 @@ func (appKeepers *AppKeepers) initParamsKeeper(appCodec codec.BinaryCodec, legac
paramsKeeper.Subspace(alliancemoduletypes.ModuleName)
paramsKeeper.Subspace(wasm.ModuleName)
paramsKeeper.Subspace(transfermiddlewaretypes.ModuleName)
paramsKeeper.Subspace(stakingmiddlewaretypes.ModuleName)

return paramsKeeper
}
Expand Down
5 changes: 4 additions & 1 deletion app/keepers/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ import (

"github.com/CosmWasm/wasmd/x/wasm"
wasm08types "github.com/cosmos/ibc-go/v7/modules/light-clients/08-wasm/types"

// customstakingtypes "github.com/notional-labs/composable/v6/custom/staking/types"
stakingmiddleware "github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
)

// GenerateKeys generates new keys (KV Store, Transient store, and memory store).
Expand All @@ -52,8 +55,8 @@ func (appKeepers *AppKeepers) GenerateKeys() {
authtypes.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey,
govtypes.StoreKey, paramstypes.StoreKey, ibchost.StoreKey, upgradetypes.StoreKey, feegrant.StoreKey,
evidencetypes.StoreKey, ibctransfertypes.StoreKey, icqtypes.StoreKey, capabilitytypes.StoreKey, consensusparamtypes.StoreKey, wasm08types.StoreKey,
authzkeeper.StoreKey, stakingmiddleware.StoreKey,
crisistypes.StoreKey, routertypes.StoreKey, transfermiddlewaretypes.StoreKey, group.StoreKey, minttypes.StoreKey, alliancemoduletypes.StoreKey, wasm.StoreKey, ibchookstypes.StoreKey, icahosttypes.StoreKey, ratelimitmoduletypes.StoreKey, txBoundaryTypes.StoreKey,
authzkeeper.StoreKey,
)

// Define transient store keys
Expand Down
2 changes: 1 addition & 1 deletion app/test_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s TestSupport) ScopedTransferKeeper() capabilitykeeper.ScopedKeeper {
}

func (s TestSupport) StakingKeeper() *stakingkeeper.Keeper {
return s.app.StakingKeeper
return &s.app.StakingKeeper.Keeper
}

func (s TestSupport) AccountKeeper() authkeeper.AccountKeeper {
Expand Down
22 changes: 22 additions & 0 deletions app/upgrades/v6_4/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package v6_4

import (
store "github.com/cosmos/cosmos-sdk/store/types"

"github.com/notional-labs/composable/v6/app/upgrades"
customstmiddleware "github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
)

const (
// UpgradeName defines the on-chain upgrade name for the composable upgrade.
UpgradeName = "v6_4"
)

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateUpgradeHandler,
StoreUpgrades: store.StoreUpgrades{
Added: []string{customstmiddleware.StoreKey},
Deleted: []string{},
},
}
28 changes: 28 additions & 0 deletions app/upgrades/v6_4/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package v6_4

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/notional-labs/composable/v6/app/keepers"
"github.com/notional-labs/composable/v6/app/upgrades"
customstmiddleware "github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
_ upgrades.BaseAppParamManager,
_ codec.Codec,
keepers *keepers.AppKeepers,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
// Add params for custom middleware
custommiddlewareparams := customstmiddleware.Params{BlocksPerEpoch: 100, AllowUnbondAfterEpochProgressBlockNumber: 0}
keepers.StakingMiddlewareKeeper.SetParams(ctx, custommiddlewareparams)

return mm.RunMigrations(ctx, configurator, vm)
}
}
22 changes: 22 additions & 0 deletions custom/staking/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package bank

import (
"time"

abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"

// "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"

customstakingkeeper "github.com/notional-labs/composable/v6/custom/staking/keeper"
)

// Called every block, update validator set
func EndBlocker(ctx sdk.Context, k *customstakingkeeper.Keeper) []abci.ValidatorUpdate {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)

return k.BlockValidatorUpdates(ctx, ctx.BlockHeight())
}
122 changes: 122 additions & 0 deletions custom/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package keeper

import (
abcicometbft "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingmiddleware "github.com/notional-labs/composable/v6/x/stakingmiddleware/keeper"
)

type Keeper struct {
stakingkeeper.Keeper
cdc codec.BinaryCodec
Stakingmiddleware *stakingmiddleware.Keeper
authority string
}

func (k Keeper) BlockValidatorUpdates(ctx sdk.Context, height int64) []abcicometbft.ValidatorUpdate {
// Calculate validator set changes.
//
// NOTE: ApplyAndReturnValidatorSetUpdates has to come before
// UnbondAllMatureValidatorQueue.
// This fixes a bug when the unbonding period is instant (is the case in
// some of the tests). The test expected the validator to be completely
// unbonded after the Endblocker (go from Bonded -> Unbonding during
// ApplyAndReturnValidatorSetUpdates and then Unbonding -> Unbonded during
// UnbondAllMatureValidatorQueue).
params := k.Stakingmiddleware.GetParams(ctx)
shouldExecuteBatch := (height % int64(params.BlocksPerEpoch)) == 0
var validatorUpdates []abcicometbft.ValidatorUpdate
if shouldExecuteBatch {
k.Logger(ctx).Info("Should Execute ApplyAndReturnValidatorSetUpdates at height : ", height)
v, err := k.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
panic(err)
}
validatorUpdates = v
}

// unbond all mature validators from the unbonding queue
k.UnbondAllMatureValidators(ctx)

// Remove all mature unbonding delegations from the ubd queue.
matureUnbonds := k.DequeueAllMatureUBDQueue(ctx, ctx.BlockHeader().Time)
for _, dvPair := range matureUnbonds {
addr, err := sdk.ValAddressFromBech32(dvPair.ValidatorAddress)
if err != nil {
panic(err)
}
delegatorAddress := sdk.MustAccAddressFromBech32(dvPair.DelegatorAddress)

balances, err := k.CompleteUnbonding(ctx, delegatorAddress, addr)
if err != nil {
continue
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCompleteUnbonding,
sdk.NewAttribute(sdk.AttributeKeyAmount, balances.String()),
sdk.NewAttribute(types.AttributeKeyValidator, dvPair.ValidatorAddress),
sdk.NewAttribute(types.AttributeKeyDelegator, dvPair.DelegatorAddress),
),
)
}

// Remove all mature redelegations from the red queue.
matureRedelegations := k.DequeueAllMatureRedelegationQueue(ctx, ctx.BlockHeader().Time)
for _, dvvTriplet := range matureRedelegations {
valSrcAddr, err := sdk.ValAddressFromBech32(dvvTriplet.ValidatorSrcAddress)
if err != nil {
panic(err)
}
valDstAddr, err := sdk.ValAddressFromBech32(dvvTriplet.ValidatorDstAddress)
if err != nil {
panic(err)
}
delegatorAddress := sdk.MustAccAddressFromBech32(dvvTriplet.DelegatorAddress)

balances, err := k.CompleteRedelegation(
ctx,
delegatorAddress,
valSrcAddr,
valDstAddr,
)
if err != nil {
continue
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCompleteRedelegation,
sdk.NewAttribute(sdk.AttributeKeyAmount, balances.String()),
sdk.NewAttribute(types.AttributeKeyDelegator, dvvTriplet.DelegatorAddress),
sdk.NewAttribute(types.AttributeKeySrcValidator, dvvTriplet.ValidatorSrcAddress),
sdk.NewAttribute(types.AttributeKeyDstValidator, dvvTriplet.ValidatorDstAddress),
),
)
}

return validatorUpdates
}

func NewKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
ak types.AccountKeeper,
bk types.BankKeeper,
authority string,
stakingmiddleware *stakingmiddleware.Keeper,
) *Keeper {
keeper := Keeper{
Keeper: *stakingkeeper.NewKeeper(cdc, key, ak, bk, authority),
authority: authority,
Stakingmiddleware: stakingmiddleware,
cdc: cdc,
}
return &keeper
}
Loading

0 comments on commit db053a7

Please sign in to comment.