-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: CNS-delegators-slashing-hook-fix (#1356)
* stop using the hook and instead run on beginblock * cr changes * lint --------- Co-authored-by: Yarom Swisa <[email protected] git config --global user.name Yarom> Co-authored-by: Elad Gildnur <[email protected]> Co-authored-by: Yaroms <[email protected]>
- Loading branch information
1 parent
7c9c530
commit 976bbd1
Showing
10 changed files
with
115 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
evidenceTypes "github.com/cosmos/cosmos-sdk/x/evidence/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 { | ||
case abci.MisbehaviorType_DUPLICATE_VOTE, abci.MisbehaviorType_LIGHT_CLIENT_ATTACK: | ||
evidence := evidenceTypes.FromABCIEvidence(tmEvidence) | ||
evidenceEq, ok := evidence.(*evidenceTypes.Equivocation) | ||
if ok { | ||
k.BalanceValidatorsDelegators(ctx, evidenceEq) | ||
} | ||
|
||
default: | ||
k.Logger(ctx).Error(fmt.Sprintf("ignored unknown evidence type: %s", tmEvidence.Type)) | ||
} | ||
} | ||
} | ||
|
||
func (k Keeper) BalanceValidatorsDelegators(ctx sdk.Context, evidence *evidenceTypes.Equivocation) { | ||
consAddr := evidence.GetConsensusAddress() | ||
|
||
validator := k.stakingKeeper.ValidatorByConsAddr(ctx, consAddr) | ||
if validator == nil || validator.GetOperator().Empty() { | ||
return | ||
} | ||
|
||
delegators := k.stakingKeeper.GetValidatorDelegations(ctx, validator.GetOperator()) | ||
for _, delegator := range delegators { | ||
delAddr := delegator.GetDelegatorAddr() | ||
k.BalanceDelegator(ctx, delAddr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters