Skip to content

Commit

Permalink
cr changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroms committed Apr 17, 2024
1 parent 9970a8a commit 68e6915
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 64 deletions.
46 changes: 46 additions & 0 deletions x/dualstaking/keeper/balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/x/dualstaking/types"
)

func (k Keeper) BalanceDelegator(ctx sdk.Context, delegator sdk.AccAddress) (int, error) {
diff, providers, err := k.VerifyDelegatorBalance(ctx, delegator)
if err != nil {
return providers, err
}

// if diff is zero, do nothing, this is a redelegate
if diff.IsZero() {
return providers, nil
} else if diff.IsPositive() {
// less provider delegations,a delegation operation was done, delegate to empty provider
err = k.delegate(ctx, delegator.String(), types.EMPTY_PROVIDER, types.EMPTY_PROVIDER_CHAINID,
sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), diff))
if err != nil {
return providers, err
}
} else if diff.IsNegative() {
// more provider delegation, unbond operation was done, unbond from providers
err = k.UnbondUniformProviders(ctx, delegator.String(), sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), diff.Neg()))
if err != nil {
return providers, err
}
}

diff, _, err = k.VerifyDelegatorBalance(ctx, delegator)
if err != nil {
return providers, err
}
// now it needs to be zero
if !diff.IsZero() {
return providers, utils.LavaFormatError("validator and provider balances are not balanced", nil,
utils.Attribute{Key: "delegator", Value: delegator.String()},
utils.Attribute{Key: "diff", Value: diff.String()},
)
}

return providers, nil
}
39 changes: 2 additions & 37 deletions x/dualstaking/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"fmt"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -63,43 +62,9 @@ func (h Hooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress,
return nil
}

var diff math.Int
var err error
diff, providers, err = h.k.VerifyDelegatorBalance(ctx, delAddr)
if err != nil {
return err
}

// if diff is zero, do nothing, this is a redelegate
if diff.IsZero() {
return nil
} else if diff.IsPositive() {
// less provider delegations,a delegation operation was done, delegate to empty provider
err = h.k.delegate(ctx, delAddr.String(), types.EMPTY_PROVIDER, types.EMPTY_PROVIDER_CHAINID,
sdk.NewCoin(h.k.stakingKeeper.BondDenom(ctx), diff))
if err != nil {
return err
}
} else if diff.IsNegative() {
// more provider delegation, unbond operation was done, unbond from providers
err = h.k.UnbondUniformProviders(ctx, delAddr.String(), sdk.NewCoin(h.k.stakingKeeper.BondDenom(ctx), diff.Neg()))
if err != nil {
return err
}
}

diff, _, err = h.k.VerifyDelegatorBalance(ctx, delAddr)
if err != nil {
return err
}
// now it needs to be zero
if !diff.IsZero() {
return utils.LavaFormatError("validator and provider balances are not balanced", nil,
utils.Attribute{Key: "delegator", Value: delAddr.String()},
utils.Attribute{Key: "diff", Value: diff.String()},
)
}
return nil
providers, err = h.k.BalanceDelegator(ctx, delAddr)
return err
}

func (h Hooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) error {
Expand Down
29 changes: 2 additions & 27 deletions x/dualstaking/keeper/slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
evidenceTypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/x/dualstaking/types"
)

// balance delegators dualstaking after potential validators slashing
func (k Keeper) HandleSlashedValidators(ctx sdk.Context, req abci.RequestBeginBlock) {
for _, tmEvidence := range req.ByzantineValidators {
switch tmEvidence.Type {
Expand All @@ -34,30 +33,6 @@ func (k Keeper) BalanceValidatorsDelegators(ctx sdk.Context, evidence *evidenceT
delegators := k.stakingKeeper.GetValidatorDelegations(ctx, validator.GetOperator())
for _, delegator := range delegators {
delAddr := delegator.GetDelegatorAddr()
diff, _, err := k.VerifyDelegatorBalance(ctx, delAddr)
if err != nil {
utils.LavaFormatError("failed to verify delegator balance after slashing", err)
return
}

// if diff is zero, do nothing, this is a redelegate
if diff.IsZero() {
return
} else if diff.IsPositive() {
// less provider delegations,a delegation operation was done, delegate to empty provider
err = k.delegate(ctx, delAddr.String(), types.EMPTY_PROVIDER, types.EMPTY_PROVIDER_CHAINID,
sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), diff))
if err != nil {
utils.LavaFormatError("failed to delegate delegator balance after slashing", err)
return
}
} else if diff.IsNegative() {
// more provider delegation, unbond operation was done, unbond from providers
err = k.UnbondUniformProviders(ctx, delAddr.String(), sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), diff.Neg()))
if err != nil {
utils.LavaFormatError("failed to unbond delegattor balance after slashing", err)
return
}
}
k.BalanceDelegator(ctx, delAddr)
}
}

0 comments on commit 68e6915

Please sign in to comment.