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

add delegation store to custom staking middleware #303

Merged
merged 21 commits into from
Dec 22, 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
e8a72b9
add delegation store to custom staking middleware
RustNinja Dec 18, 2023
38c7444
call delegation keeper store method from custom staking.
RustNinja Dec 19, 2023
e187b8e
linted, with the exception of unreachable code
faddat Dec 19, 2023
0a40503
iterate delegation.
RustNinja Dec 19, 2023
007db4d
Merge pull request #311 from ComposableFi/iterate-delegation
RustNinja Dec 19, 2023
1212864
iterate delegation storage
RustNinja Dec 19, 2023
78e50e1
Merge pull request #312 from ComposableFi/iterate-delegation-storage
RustNinja Dec 19, 2023
a93ec9f
override end block custom staking module implementation
RustNinja Dec 20, 2023
81a2901
Merge pull request #314 from ComposableFi/override-end-blocker-custom…
RustNinja Dec 20, 2023
fad57ec
add logic to app module to batch delegation.
RustNinja Dec 21, 2023
a290f32
Merge pull request #315 from ComposableFi/endBlock-logic-to-perform-b…
RustNinja Dec 21, 2023
b6281a5
introduce unbondong/redelegation/cancel unbonding filed to genesis
RustNinja Dec 21, 2023
7b96c6e
Merge pull request #316 from ComposableFi/finish-geneis-staking-middl…
RustNinja Dec 21, 2023
1a22e18
add logic to EndBlock custom staking wrapper to dequeue all request.
RustNinja Dec 21, 2023
3ba9ca9
Merge pull request #317 from ComposableFi/end-block-dequeue-all
RustNinja Dec 21, 2023
6dafbcf
add msg epoch params
RustNinja Dec 21, 2023
1af77d8
compete EndBlock for custom staking module. batch bond/unbond/redelegate
RustNinja Dec 21, 2023
0f073ca
Merge pull request #318 from ComposableFi/msg-epoch-params
RustNinja Dec 21, 2023
b5658e5
introduce new end block logic to delay voting power update
RustNinja Dec 22, 2023
447c600
refactor custom staking module, remove unnecessary keeper's functions
RustNinja Dec 22, 2023
08ebecd
Merge pull request #319 from ComposableFi/voting-power-end-block-delay
RustNinja Dec 22, 2023
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 app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ 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)),
customstaking.NewAppModule(appCodec, *&app.CustomStakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
customstaking.NewAppModule(appCodec, app.CustomStakingKeeper, 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),
Expand Down
2 changes: 1 addition & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.StakingMiddlewareKeeper = stakingmiddleware.NewKeeper(appCodec, appKeepers.keys[stakingmiddlewaretypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())

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

appKeepers.DistrKeeper = distrkeeper.NewKeeper(
Expand Down
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())
Dismissed Show dismissed Hide dismissed
}
124 changes: 92 additions & 32 deletions custom/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,65 +1,125 @@
package keeper

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

mintkeeper "github.com/notional-labs/composable/v6/x/mint/keeper"
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
acck accountkeeper.AccountKeeper
mintkeeper *mintkeeper.Keeper
stakingmiddleware *stakingmiddleware.Keeper
Stakingmiddleware *stakingmiddleware.Keeper
authority string
}

// func NewBaseKeeper(
// cdc codec.BinaryCodec,
// key storetypes.StoreKey,
// ak types.AccountKeeper,
// acck accountkeeper.AccountKeeper,
// bk bankkeeper.Keeper,
// authority string,
// ) Keeper {
// keeper := Keeper{
// Keeper: *stakingkeeper.NewKeeper(cdc, key, ak, bk, authority),
// acck: acck,
// }
// return keeper
// }
func (k Keeper) BlockValidatorUpdates(ctx sdk.Context, hight 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).
println("BlockValidatorUpdates Custom Staking Module")
params := k.Stakingmiddleware.GetParams(ctx)
println("BlocksPerEpoch: ", params.BlocksPerEpoch)
should_execute_batch := (hight % int64(params.BlocksPerEpoch)) == 0
var validatorUpdates []abcicometbft.ValidatorUpdate
if should_execute_batch {
println("Should Execute Batch: ", hight)
v, err := k.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
panic(err)
Dismissed Show dismissed Hide dismissed
}
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)
Dismissed Show dismissed Hide dismissed
}
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)
Dismissed Show dismissed Hide dismissed
}
valDstAddr, err := sdk.ValAddressFromBech32(dvvTriplet.ValidatorDstAddress)
if err != nil {
panic(err)
Dismissed Show dismissed Hide dismissed
}
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,
staking stakingkeeper.Keeper,
acck accountkeeper.AccountKeeper,
mintkeeper *mintkeeper.Keeper,
stakingmiddleware *stakingmiddleware.Keeper,
authority string,
) Keeper {
keeper := Keeper{
Keeper: staking,
acck: acck,
authority: authority,
mintkeeper: mintkeeper,
stakingmiddleware: stakingmiddleware,
Stakingmiddleware: stakingmiddleware,
cdc: cdc,
}
return keeper
}

// func (k *Keeper) RegisterKeepers(akk banktypes.StakingKeeper) {
// k.acck = sk
// }

// func (k Keeper) StoreDelegation(ctx sdk.Context, delegation types.Delegation) {
// delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress)

// store := ctx.KVStore(k.storeKey)
// b := types.MustMarshalDelegation(k.cdc, delegation)
// store.Set(customstakingtypes.GetDelegationKey(delegatorAddress, delegation.GetValidatorAddr()), b)
// }
24 changes: 1 addition & 23 deletions custom/staking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package keeper
import (
"context"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand All @@ -29,27 +27,7 @@ func (k msgServer) EditValidator(goCtx context.Context, msg *types.MsgEditValida
}

func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*types.MsgDelegateResponse, error) {

ctx := sdk.UnwrapSDKContext(goCtx)

// bondDenom := k.BondDenom(ctx)
// if msg.Amount.Denom != bondDenom {
// return nil, sdkerrors.Wrapf(
// sdkerrors.ErrInvalidRequest, "invalid coin denomination: got %s, expected %s", msg.Amount.Denom, bondDenom,
// )
// }

// delegation := types.Delegation{
// DelegatorAddress: msg.DelegatorAddress,
// ValidatorAddress: msg.ValidatorAddress,
// Shares: msg.Amount.Amount.ToLegacyDec(),
// }
k.mintkeeper.SetLastTotalPower(ctx, math.Int{})
k.stakingmiddleware.SetLastTotalPower(ctx, math.Int{})

return &types.MsgDelegateResponse{}, nil
// return nil, fmt.Errorf("My custom error: Nikita")
// return k.msgServer.Delegate(goCtx, msg)
return k.msgServer.Delegate(goCtx, msg)
}

func (k msgServer) BeginRedelegate(goCtx context.Context, msg *types.MsgBeginRedelegate) (*types.MsgBeginRedelegateResponse, error) {
Expand Down
29 changes: 18 additions & 11 deletions custom/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,35 @@
import (
"fmt"

abcitype "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
stakingmodule "github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/exported"
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

// custombankkeeper "github.com/notional-labs/composable/v6/custom/bank/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
customstakingkeeper "github.com/notional-labs/composable/v6/custom/staking/keeper"
)

// AppModule wraps around the bank module and the bank keeper to return the right total supply
type AppModule struct {
stakingmodule.AppModule
keeper customstakingkeeper.Keeper
subspace exported.Subspace
keeper customstakingkeeper.Keeper
subspace exported.Subspace
msgServer stakingtypes.MsgServer
}

// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, keeper customstakingkeeper.Keeper, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, ss exported.Subspace) AppModule {
func NewAppModule(cdc codec.Codec, keeper customstakingkeeper.Keeper, accountKeeper stakingtypes.AccountKeeper, bankKeeper stakingtypes.BankKeeper, ss exported.Subspace) AppModule {
stakingModule := stakingmodule.NewAppModule(cdc, &keeper.Keeper, accountKeeper, bankKeeper, ss)
return AppModule{
AppModule: stakingModule,
keeper: keeper,
subspace: ss,
msgServer: stakingkeeper.NewMsgServerImpl(&keeper.Keeper),
}
}

Expand All @@ -37,20 +40,24 @@
// when trying to force this custom keeper into a bankkeeper.BaseKeeper
func (am AppModule) RegisterServices(cfg module.Configurator) {
// types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(&am.keeper))
types.RegisterMsgServer(cfg.MsgServer(), customstakingkeeper.NewMsgServerImpl(am.keeper.Keeper, am.keeper))
querier := keeper.Querier{Keeper: &am.keeper.Keeper}
types.RegisterQueryServer(cfg.QueryServer(), querier)
stakingtypes.RegisterMsgServer(cfg.MsgServer(), customstakingkeeper.NewMsgServerImpl(am.keeper.Keeper, am.keeper))
querier := stakingkeeper.Querier{Keeper: &am.keeper.Keeper}
stakingtypes.RegisterQueryServer(cfg.QueryServer(), querier)

m := stakingkeeper.NewMigrator(&am.keeper.Keeper, am.subspace)
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
if err := cfg.RegisterMigration(stakingtypes.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate x/staking from version 1 to 2: %v", err))
}

if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil {
if err := cfg.RegisterMigration(stakingtypes.ModuleName, 2, m.Migrate2to3); err != nil {
panic(fmt.Sprintf("failed to migrate x/staking from version 2 to 3: %v", err))
}

if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil {
if err := cfg.RegisterMigration(stakingtypes.ModuleName, 3, m.Migrate3to4); err != nil {
panic(fmt.Sprintf("failed to migrate x/staking from version 3 to 4: %v", err))
}
}

func (am AppModule) EndBlock(ctx sdk.Context, _abc abcitype.RequestEndBlock) []abcitype.ValidatorUpdate {
return EndBlocker(ctx, &am.keeper)
Dismissed Show dismissed Hide dismissed
}
10 changes: 0 additions & 10 deletions custom/staking/types/keeper_interfaces.go

This file was deleted.

7 changes: 0 additions & 7 deletions proto/centauri/mint/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,4 @@ message GenesisState {
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.nullable) = false
];

// last_total_power tracks the total amounts of bonded tokens recorded during
// the previous end block.
bytes last_total_power = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}
9 changes: 3 additions & 6 deletions proto/centauri/stakingmiddleware/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ syntax = "proto3";
package centauri.stakingmiddleware.v1beta1;

import "gogoproto/gogo.proto";
import "centauri/stakingmiddleware/v1beta1/stakingmiddleware.proto";
import "amino/amino.proto";


option go_package = "x/stakingmiddleware/types";

// GenesisState defines the stakingmiddleware module's genesis state.
message GenesisState {
// last_total_power tracks the total amounts of bonded tokens recorded during
// the previous end block.
bytes last_total_power = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
Params params = 1 [ (gogoproto.nullable) = false ];
}
11 changes: 7 additions & 4 deletions proto/centauri/stakingmiddleware/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ package centauri.stakingmiddleware.v1beta1;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "centauri/stakingmiddleware/v1beta1/stakingmiddleware.proto";

option go_package = "x/stakingmiddleware/types";

// Query provides defines the gRPC querier service.
service Query {
// Params returns the total set of minting parameters.
rpc Power(QueryPowerRequest) returns (QueryPowerResponse) {
option (google.api.http).get = "/cosmos/stakingmiddleware/v1beta1/params";
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/cosmos/mint/v1beta1/params";
}
}

// QueryParamsRequest is the request type for the Query/Params RPC method.
message QueryPowerRequest {}
message QueryParamsRequest {}

// QueryParamsResponse is the response type for the Query/Params RPC method.
message QueryPowerResponse {
message QueryParamsResponse {
// params defines the parameters of the module.
Params params = 1 [ (gogoproto.nullable) = false ];
}
Loading
Loading