Skip to content

Commit

Permalink
Merge pull request #451 from ComposableFi/slashing_fix
Browse files Browse the repository at this point in the history
Slashing fixes
  • Loading branch information
RustNinja authored Mar 5, 2024
2 parents 85e2bf9 + 6c75174 commit 1ad4293
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ build:
go build $(BUILD_FLAGS) -o bin/centaurid ./cmd/centaurid

docker-build-debug:
@DOCKER_BUILDKIT=1 docker build -t centauri:debug -f Dockerfile .
@DOCKER_BUILDKIT=1 docker build -t centauri:local -f Dockerfile .

lint:
@find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -name '*.pb.go' -not -name '*.gw.go' | xargs go run mvdan.cc/gofumpt -w .
Expand Down
2 changes: 2 additions & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appCodec, appKeepers.keys[distrtypes.StoreKey], appKeepers.AccountKeeper, appKeepers.BankKeeper,
appKeepers.StakingKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
appKeepers.StakingKeeper.RegisterKeepers(appKeepers.DistrKeeper, appKeepers.BankKeeper)

appKeepers.SlashingKeeper = slashingkeeper.NewKeeper(
appCodec, cdc, appKeepers.keys[slashingtypes.StoreKey], appKeepers.StakingKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
Expand Down
6 changes: 6 additions & 0 deletions app/upgrades/v6_4_5/constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package v6_4_5

import (
store "github.com/cosmos/cosmos-sdk/store/types"
"github.com/notional-labs/composable/v6/app/upgrades"
ibctransfermiddleware "github.com/notional-labs/composable/v6/x/ibctransfermiddleware/types"
)

const (
Expand All @@ -12,4 +14,8 @@ const (
var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateUpgradeHandler,
StoreUpgrades: store.StoreUpgrades{
Added: []string{ibctransfermiddleware.StoreKey},
Deleted: []string{},
},
}
17 changes: 17 additions & 0 deletions app/upgrades/v6_4_5/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/notional-labs/composable/v6/app/keepers"
"github.com/notional-labs/composable/v6/app/upgrades"
ibctransfermiddleware "github.com/notional-labs/composable/v6/x/ibctransfermiddleware/types"
)

func CreateUpgradeHandler(
Expand All @@ -18,10 +19,26 @@ func CreateUpgradeHandler(
keepers *keepers.AppKeepers,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
// remove broken proposals
BrokenProposals := [3]uint64{2, 6, 11}
for _, proposal := range BrokenProposals {
keepers.GovKeeper.DeleteProposal(ctx, proposal)
}

// burn extra ppica in escrow account
// this ppica is unused because it is a native token stored in escrow account
// it was unnecessarily minted to match pica escrowed on picasso to ppica minted
// in genesis, to make initial native ppica transferrable to picasso
amount, ok := sdk.NewIntFromString("1066669217167120000000")
if ok {
coins := sdk.Coins{sdk.NewCoin("ppica", amount)}
keepers.BankKeeper.SendCoinsFromAccountToModule(ctx, sdk.MustAccAddressFromBech32("centauri12k2pyuylm9t7ugdvz67h9pg4gmmvhn5vmvgw48"), "gov", coins)
keepers.BankKeeper.BurnCoins(ctx, "gov", coins)
}

custommiddlewareparams := ibctransfermiddleware.DefaultGenesisState()
keepers.IbcTransferMiddlewareKeeper.SetParams(ctx, custommiddlewareparams.Params)

return mm.RunMigrations(ctx, configurator, vm)
}
}
22 changes: 0 additions & 22 deletions app/upgrades/v6_4_6/constants.go

This file was deleted.

28 changes: 0 additions & 28 deletions app/upgrades/v6_4_6/upgrade.go

This file was deleted.

19 changes: 0 additions & 19 deletions custom/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
transfermiddlewarekeeper "github.com/notional-labs/composable/v6/x/transfermiddleware/keeper"

alliancekeeper "github.com/terra-money/alliance/x/alliance/keeper"
alliancetypes "github.com/terra-money/alliance/x/alliance/types"
)

type Keeper struct {
Expand Down Expand Up @@ -68,16 +67,6 @@ func (k Keeper) SupplyOf(c context.Context, req *types.QuerySupplyOfRequest) (*t
ctx := sdk.UnwrapSDKContext(c)
supply := k.GetSupply(ctx, req.Denom)

if req.Denom == k.sk.BondDenom(ctx) {
assets := k.ak.GetAllAssets(ctx)
totalRewardWeights := sdk.ZeroDec()
for _, asset := range assets {
totalRewardWeights = totalRewardWeights.Add(asset.RewardWeight)
}
allianceBonded := k.ak.GetAllianceBondedAmount(ctx, k.acck.GetModuleAddress(alliancetypes.ModuleName))
supply.Amount = supply.Amount.Sub(allianceBonded)
}

return &types.QuerySupplyOfResponse{Amount: sdk.NewCoin(req.Denom, supply.Amount)}, nil
}

Expand All @@ -88,16 +77,8 @@ func (k Keeper) TotalSupply(ctx context.Context, req *types.QueryTotalSupplyRequ
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

// Get duplicate token from transfermiddeware
duplicateCoins := k.tfmk.GetTotalEscrowedToken(sdkCtx)
totalSupply = totalSupply.Sub(duplicateCoins...)

allianceBonded := k.ak.GetAllianceBondedAmount(sdkCtx, k.acck.GetModuleAddress(alliancetypes.ModuleName))
bondDenom := k.sk.BondDenom(sdkCtx)
if totalSupply.AmountOf(bondDenom).IsPositive() {
totalSupply = totalSupply.Sub(sdk.NewCoin(bondDenom, allianceBonded))
}

return &types.QueryTotalSupplyResponse{Supply: totalSupply, Pagination: pageRes}, nil
}
46 changes: 43 additions & 3 deletions custom/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package keeper

import (
"fmt"

"cosmossdk.io/math"
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"
distkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"
minttypes "github.com/notional-labs/composable/v6/x/mint/types"
stakingmiddleware "github.com/notional-labs/composable/v6/x/stakingmiddleware/keeper"
)

Expand All @@ -16,6 +20,9 @@ type Keeper struct {
cdc codec.BinaryCodec
Stakingmiddleware *stakingmiddleware.Keeper
authority string
mintKeeper minttypes.BankKeeper
distrKeeper distkeeper.Keeper
authKeeper minttypes.AccountKeeper
}

func (k Keeper) BlockValidatorUpdates(ctx sdk.Context, height int64) []abcicometbft.ValidatorUpdate {
Expand Down Expand Up @@ -117,6 +124,39 @@ func NewKeeper(
authority: authority,
Stakingmiddleware: stakingmiddleware,
cdc: cdc,
mintKeeper: nil,
distrKeeper: distkeeper.Keeper{},
authKeeper: ak,
}
return &keeper
}

func (k *Keeper) RegisterKeepers(dk distkeeper.Keeper, mk minttypes.BankKeeper) {
k.distrKeeper = dk
k.mintKeeper = mk
}

// SlashWithInfractionReason send coins to community pool
func (k Keeper) SlashWithInfractionReason(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeight, power int64, slashFactor sdk.Dec, _ types.Infraction) math.Int {
// keep slashing logic the same
amountBurned := k.Slash(ctx, consAddr, infractionHeight, power, slashFactor)
// after usual slashing and burning is done, mint burned coinds into community pool
coins := sdk.NewCoins(sdk.NewCoin(k.BondDenom(ctx), amountBurned))
err := k.mintKeeper.MintCoins(ctx, types.ModuleName, coins)
if err != nil {
k.Logger(ctx).Error("Failed to mint slashed coins: ", amountBurned)
} else {
err = k.distrKeeper.FundCommunityPool(ctx, coins, k.authKeeper.GetModuleAddress(types.ModuleName))
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Failed to fund community pool. Tokens minted to the staking module account: %d. ", amountBurned))
} else {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
minttypes.EventTypeMintSlashed,
sdk.NewAttribute(sdk.AttributeKeyAmount, amountBurned.String()),
),
)
}
}
return amountBurned
}
3 changes: 1 addition & 2 deletions tests/interchaintest/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ func GetDockerImageInfo() (repo, version string) {
if !found {
// make local-image
repo = "centauri"
branchVersion = "debug"
branchVersion = "local"
}

// github converts / to - for pushed docker images
// branchVersion = strings.ReplaceAll(branchVersion, "/", "-")
branchVersion = "latest"
return repo, branchVersion
}
7 changes: 2 additions & 5 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculatio
minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalStakingSupply)
k.SetMinter(ctx, minter)

// mint coins, update supply
// calculate how many we would mint, but we dont mint them, we take them from the prefunded account
mintedCoin := minter.BlockProvision(params)
mintedCoins := sdk.NewCoins(mintedCoin)

Expand All @@ -41,10 +41,7 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper, ic types.InflationCalculatio

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMint,
sdk.NewAttribute(types.AttributeKeyBondedRatio, bondedRatio.String()),
sdk.NewAttribute(types.AttributeKeyInflation, minter.Inflation.String()),
sdk.NewAttribute(types.AttributeKeyAnnualProvisions, minter.AnnualProvisions.String()),
types.EventTypeReward,
sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()),
),
)
Expand Down
2 changes: 2 additions & 0 deletions x/mint/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
// Minting module event types
const (
EventTypeMint = ModuleName
EventTypeReward = "reward_distributed"
EventTypeMintSlashed = "mint_slashed_into_comminity_pool"
EventAddAllowedFundAddress = "add_allowed_fund"

AttributeKeyBondedRatio = "bonded_ratio"
Expand Down

0 comments on commit 1ad4293

Please sign in to comment.