Merge PR #5597: Include Amount in Complete Unbonding/Redelegation Events

This commit is contained in:
Alexander Bezobchuk 2020-01-31 11:12:04 -05:00 committed by GitHub
parent c7a6299eb2
commit ce16e0a99d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 29 deletions

View File

@ -62,6 +62,9 @@ balances or a single balance by denom when the `denom` query parameter is presen
### Improvements ### Improvements
* (modules) [\#5597](https://github.com/cosmos/cosmos-sdk/pull/5597) Add `amount` event attribute to the `complete_unbonding`
and `complete_redelegation` events that reflect the total balances of the completed unbondings and redelegations
respectively.
* (types) [\#5581](https://github.com/cosmos/cosmos-sdk/pull/5581) Add convenience functions {,Must}Bech32ifyAddressBytes. * (types) [\#5581](https://github.com/cosmos/cosmos-sdk/pull/5581) Add convenience functions {,Must}Bech32ifyAddressBytes.
* (staking) [\#5584](https://github.com/cosmos/cosmos-sdk/pull/5584) Add util function `ToTmValidator` that converts a `staking.Validator` type to `*tmtypes.Validator`. * (staking) [\#5584](https://github.com/cosmos/cosmos-sdk/pull/5584) Add util function `ToTmValidator` that converts a `staking.Validator` type to `*tmtypes.Validator`.
* (client) [\#5585](https://github.com/cosmos/cosmos-sdk/pull/5585) IBC additions: * (client) [\#5585](https://github.com/cosmos/cosmos-sdk/pull/5585) IBC additions:

View File

@ -660,14 +660,17 @@ func (k Keeper) Undelegate(
return completionTime, nil return completionTime, nil
} }
// CompleteUnbonding completes the unbonding of all mature entries in the // CompleteUnbondingWithAmount completes the unbonding of all mature entries in
// retrieved unbonding delegation object. // the retrieved unbonding delegation object and returns the total unbonding
func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error { // balance or an error upon failure.
func (k Keeper) CompleteUnbondingWithAmount(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) (sdk.Coins, error) {
ubd, found := k.GetUnbondingDelegation(ctx, delAddr, valAddr) ubd, found := k.GetUnbondingDelegation(ctx, delAddr, valAddr)
if !found { if !found {
return types.ErrNoUnbondingDelegation return nil, types.ErrNoUnbondingDelegation
} }
bondDenom := k.GetParams(ctx).BondDenom
balances := sdk.NewCoins()
ctxTime := ctx.BlockHeader().Time ctxTime := ctx.BlockHeader().Time
// loop through all the entries and complete unbonding mature entries // loop through all the entries and complete unbonding mature entries
@ -679,11 +682,15 @@ func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress, valAd
// track undelegation only when remaining or truncated shares are non-zero // track undelegation only when remaining or truncated shares are non-zero
if !entry.Balance.IsZero() { if !entry.Balance.IsZero() {
amt := sdk.NewCoins(sdk.NewCoin(k.GetParams(ctx).BondDenom, entry.Balance)) amt := sdk.NewCoin(bondDenom, entry.Balance)
err := k.supplyKeeper.UndelegateCoinsFromModuleToAccount(ctx, types.NotBondedPoolName, ubd.DelegatorAddress, amt) err := k.supplyKeeper.UndelegateCoinsFromModuleToAccount(
ctx, types.NotBondedPoolName, ubd.DelegatorAddress, sdk.NewCoins(amt),
)
if err != nil { if err != nil {
return err return nil, err
} }
balances = balances.Add(amt)
} }
} }
} }
@ -695,7 +702,14 @@ func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress, valAd
k.SetUnbondingDelegation(ctx, ubd) k.SetUnbondingDelegation(ctx, ubd)
} }
return nil return balances, nil
}
// CompleteUnbonding performs the same logic as CompleteUnbondingWithAmount except
// it does not return the total unbonding amount.
func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
_, err := k.CompleteUnbondingWithAmount(ctx, delAddr, valAddr)
return err
} }
// begin unbonding / redelegation; create a redelegation record // begin unbonding / redelegation; create a redelegation record
@ -755,17 +769,20 @@ func (k Keeper) BeginRedelegation(
return completionTime, nil return completionTime, nil
} }
// CompleteRedelegation completes the unbonding of all mature entries in the // CompleteRedelegationWithAmount completes the redelegations of all mature entries in the
// retrieved unbonding delegation object. // retrieved redelegation object and returns the total redelegation (initial)
func (k Keeper) CompleteRedelegation( // balance or an error upon failure.
func (k Keeper) CompleteRedelegationWithAmount(
ctx sdk.Context, delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress, ctx sdk.Context, delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress,
) error { ) (sdk.Coins, error) {
red, found := k.GetRedelegation(ctx, delAddr, valSrcAddr, valDstAddr) red, found := k.GetRedelegation(ctx, delAddr, valSrcAddr, valDstAddr)
if !found { if !found {
return types.ErrNoRedelegation return nil, types.ErrNoRedelegation
} }
bondDenom := k.GetParams(ctx).BondDenom
balances := sdk.NewCoins()
ctxTime := ctx.BlockHeader().Time ctxTime := ctx.BlockHeader().Time
// loop through all the entries and complete mature redelegation entries // loop through all the entries and complete mature redelegation entries
@ -774,6 +791,10 @@ func (k Keeper) CompleteRedelegation(
if entry.IsMature(ctxTime) { if entry.IsMature(ctxTime) {
red.RemoveEntry(int64(i)) red.RemoveEntry(int64(i))
i-- i--
if !entry.InitialBalance.IsZero() {
balances = balances.Add(sdk.NewCoin(bondDenom, entry.InitialBalance))
}
} }
} }
@ -784,7 +805,17 @@ func (k Keeper) CompleteRedelegation(
k.SetRedelegation(ctx, red) k.SetRedelegation(ctx, red)
} }
return nil return balances, nil
}
// CompleteRedelegation performs the same logic as CompleteRedelegationWithAmount
// except it does not return the total redelegation amount.
func (k Keeper) CompleteRedelegation(
ctx sdk.Context, delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress,
) error {
_, err := k.CompleteRedelegationWithAmount(ctx, delAddr, valSrcAddr, valDstAddr)
return err
} }
// ValidateUnbondAmount validates that a given unbond or redelegation amount is // ValidateUnbondAmount validates that a given unbond or redelegation amount is

View File

@ -31,7 +31,7 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate {
// Remove all mature unbonding delegations from the ubd queue. // Remove all mature unbonding delegations from the ubd queue.
matureUnbonds := k.DequeueAllMatureUBDQueue(ctx, ctx.BlockHeader().Time) matureUnbonds := k.DequeueAllMatureUBDQueue(ctx, ctx.BlockHeader().Time)
for _, dvPair := range matureUnbonds { for _, dvPair := range matureUnbonds {
err := k.CompleteUnbonding(ctx, dvPair.DelegatorAddress, dvPair.ValidatorAddress) balances, err := k.CompleteUnbondingWithAmount(ctx, dvPair.DelegatorAddress, dvPair.ValidatorAddress)
if err != nil { if err != nil {
continue continue
} }
@ -39,6 +39,7 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate {
ctx.EventManager().EmitEvent( ctx.EventManager().EmitEvent(
sdk.NewEvent( sdk.NewEvent(
types.EventTypeCompleteUnbonding, types.EventTypeCompleteUnbonding,
sdk.NewAttribute(sdk.AttributeKeyAmount, balances.String()),
sdk.NewAttribute(types.AttributeKeyValidator, dvPair.ValidatorAddress.String()), sdk.NewAttribute(types.AttributeKeyValidator, dvPair.ValidatorAddress.String()),
sdk.NewAttribute(types.AttributeKeyDelegator, dvPair.DelegatorAddress.String()), sdk.NewAttribute(types.AttributeKeyDelegator, dvPair.DelegatorAddress.String()),
), ),
@ -48,8 +49,12 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate {
// Remove all mature redelegations from the red queue. // Remove all mature redelegations from the red queue.
matureRedelegations := k.DequeueAllMatureRedelegationQueue(ctx, ctx.BlockHeader().Time) matureRedelegations := k.DequeueAllMatureRedelegationQueue(ctx, ctx.BlockHeader().Time)
for _, dvvTriplet := range matureRedelegations { for _, dvvTriplet := range matureRedelegations {
err := k.CompleteRedelegation(ctx, dvvTriplet.DelegatorAddress, balances, err := k.CompleteRedelegationWithAmount(
dvvTriplet.ValidatorSrcAddress, dvvTriplet.ValidatorDstAddress) ctx,
dvvTriplet.DelegatorAddress,
dvvTriplet.ValidatorSrcAddress,
dvvTriplet.ValidatorDstAddress,
)
if err != nil { if err != nil {
continue continue
} }
@ -57,6 +62,7 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate {
ctx.EventManager().EmitEvent( ctx.EventManager().EmitEvent(
sdk.NewEvent( sdk.NewEvent(
types.EventTypeCompleteRedelegation, types.EventTypeCompleteRedelegation,
sdk.NewAttribute(sdk.AttributeKeyAmount, balances.String()),
sdk.NewAttribute(types.AttributeKeyDelegator, dvvTriplet.DelegatorAddress.String()), sdk.NewAttribute(types.AttributeKeyDelegator, dvvTriplet.DelegatorAddress.String()),
sdk.NewAttribute(types.AttributeKeySrcValidator, dvvTriplet.ValidatorSrcAddress.String()), sdk.NewAttribute(types.AttributeKeySrcValidator, dvvTriplet.ValidatorSrcAddress.String()),
sdk.NewAttribute(types.AttributeKeyDstValidator, dvvTriplet.ValidatorDstAddress.String()), sdk.NewAttribute(types.AttributeKeyDstValidator, dvvTriplet.ValidatorDstAddress.String()),

View File

@ -8,20 +8,22 @@ The staking module emits the following events:
## EndBlocker ## EndBlocker
| Type | Attribute Key | Attribute Value | | Type | Attribute Key | Attribute Value |
|-----------------------|-----------------------|-----------------------| | --------------------- | --------------------- | ------------------------- |
| complete_unbonding | validator | {validatorAddress} | | complete_unbonding | amount | {totalUnbondingAmount} |
| complete_unbonding | delegator | {delegatorAddress} | | complete_unbonding | validator | {validatorAddress} |
| complete_redelegation | source_validator | {srcValidatorAddress} | | complete_unbonding | delegator | {delegatorAddress} |
| complete_redelegation | destination_validator | {dstValidatorAddress} | | complete_redelegation | amount | {totalRedelegationAmount} |
| complete_redelegation | delegator | {delegatorAddress} | | complete_redelegation | source_validator | {srcValidatorAddress} |
| complete_redelegation | destination_validator | {dstValidatorAddress} |
| complete_redelegation | delegator | {delegatorAddress} |
## Handlers ## Handlers
### MsgCreateValidator ### MsgCreateValidator
| Type | Attribute Key | Attribute Value | | Type | Attribute Key | Attribute Value |
|------------------|---------------|--------------------| | ---------------- | ------------- | ------------------ |
| create_validator | validator | {validatorAddress} | | create_validator | validator | {validatorAddress} |
| create_validator | amount | {delegationAmount} | | create_validator | amount | {delegationAmount} |
| message | module | staking | | message | module | staking |
@ -31,7 +33,7 @@ The staking module emits the following events:
### MsgEditValidator ### MsgEditValidator
| Type | Attribute Key | Attribute Value | | Type | Attribute Key | Attribute Value |
|----------------|---------------------|---------------------| | -------------- | ------------------- | ------------------- |
| edit_validator | commission_rate | {commissionRate} | | edit_validator | commission_rate | {commissionRate} |
| edit_validator | min_self_delegation | {minSelfDelegation} | | edit_validator | min_self_delegation | {minSelfDelegation} |
| message | module | staking | | message | module | staking |
@ -41,7 +43,7 @@ The staking module emits the following events:
### MsgDelegate ### MsgDelegate
| Type | Attribute Key | Attribute Value | | Type | Attribute Key | Attribute Value |
|----------|---------------|--------------------| | -------- | ------------- | ------------------ |
| delegate | validator | {validatorAddress} | | delegate | validator | {validatorAddress} |
| delegate | amount | {delegationAmount} | | delegate | amount | {delegationAmount} |
| message | module | staking | | message | module | staking |
@ -51,7 +53,7 @@ The staking module emits the following events:
### MsgUndelegate ### MsgUndelegate
| Type | Attribute Key | Attribute Value | | Type | Attribute Key | Attribute Value |
|---------|---------------------|--------------------| | ------- | ------------------- | ------------------ |
| unbond | validator | {validatorAddress} | | unbond | validator | {validatorAddress} |
| unbond | amount | {unbondAmount} | | unbond | amount | {unbondAmount} |
| unbond | completion_time [0] | {completionTime} | | unbond | completion_time [0] | {completionTime} |
@ -64,7 +66,7 @@ The staking module emits the following events:
### MsgBeginRedelegate ### MsgBeginRedelegate
| Type | Attribute Key | Attribute Value | | Type | Attribute Key | Attribute Value |
|------------|-----------------------|-----------------------| | ---------- | --------------------- | --------------------- |
| redelegate | source_validator | {srcValidatorAddress} | | redelegate | source_validator | {srcValidatorAddress} |
| redelegate | destination_validator | {dstValidatorAddress} | | redelegate | destination_validator | {dstValidatorAddress} |
| redelegate | amount | {unbondAmount} | | redelegate | amount | {unbondAmount} |