Skip to content

Commit

Permalink
Merge PR #5597: Include Amount in Complete Unbonding/Redelegation Events
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez committed Feb 7, 2020
1 parent 96de834 commit 94c1a10
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 34 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [v0.37.7] - TBD

### 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.

### Bug Fixes

* (x/bank) [\#5531](https://github.com/cosmos/cosmos-sdk/issues/5531) Added missing amount event to MsgMultiSend, emitted for each output.
Expand Down
26 changes: 14 additions & 12 deletions docs/spec/staking/06_events.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ The staking module emits the following events:

## EndBlocker

| Type | Attribute Key | Attribute Value |
|-----------------------|-----------------------|-----------------------|
| complete_unbonding | validator | {validatorAddress} |
| complete_unbonding | delegator | {delegatorAddress} |
| complete_redelegation | source_validator | {srcValidatorAddress} |
| complete_redelegation | destination_validator | {dstValidatorAddress} |
| complete_redelegation | delegator | {delegatorAddress} |
| Type | Attribute Key | Attribute Value |
| --------------------- | --------------------- | ------------------------- |
| complete_unbonding | amount | {totalUnbondingAmount} |
| complete_unbonding | validator | {validatorAddress} |
| complete_unbonding | delegator | {delegatorAddress} |
| complete_redelegation | amount | {totalRedelegationAmount} |
| complete_redelegation | source_validator | {srcValidatorAddress} |
| complete_redelegation | destination_validator | {dstValidatorAddress} |
| complete_redelegation | delegator | {delegatorAddress} |

## Handlers

### MsgCreateValidator

| Type | Attribute Key | Attribute Value |
|------------------|---------------|--------------------|
| ---------------- | ------------- | ------------------ |
| create_validator | validator | {validatorAddress} |
| create_validator | amount | {delegationAmount} |
| message | module | staking |
Expand All @@ -27,7 +29,7 @@ The staking module emits the following events:
### MsgEditValidator

| Type | Attribute Key | Attribute Value |
|----------------|---------------------|---------------------|
| -------------- | ------------------- | ------------------- |
| edit_validator | commission_rate | {commissionRate} |
| edit_validator | min_self_delegation | {minSelfDelegation} |
| message | module | staking |
Expand All @@ -37,7 +39,7 @@ The staking module emits the following events:
### MsgDelegate

| Type | Attribute Key | Attribute Value |
|----------|---------------|--------------------|
| -------- | ------------- | ------------------ |
| delegate | validator | {validatorAddress} |
| delegate | amount | {delegationAmount} |
| message | module | staking |
Expand All @@ -47,7 +49,7 @@ The staking module emits the following events:
### MsgUndelegate

| Type | Attribute Key | Attribute Value |
|---------|---------------------|--------------------|
| ------- | ------------------- | ------------------ |
| unbond | validator | {validatorAddress} |
| unbond | amount | {unbondAmount} |
| unbond | completion_time [0] | {completionTime} |
Expand All @@ -60,7 +62,7 @@ The staking module emits the following events:
### MsgBeginRedelegate

| Type | Attribute Key | Attribute Value |
|------------|-----------------------|-----------------------|
| ---------- | --------------------- | --------------------- |
| redelegate | source_validator | {srcValidatorAddress} |
| redelegate | destination_validator | {dstValidatorAddress} |
| redelegate | amount | {unbondAmount} |
Expand Down
12 changes: 9 additions & 3 deletions x/staking/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) []abci.ValidatorUpdate {
// Remove all mature unbonding delegations from the ubd queue.
matureUnbonds := k.DequeueAllMatureUBDQueue(ctx, ctx.BlockHeader().Time)
for _, dvPair := range matureUnbonds {
err := k.CompleteUnbonding(ctx, dvPair.DelegatorAddress, dvPair.ValidatorAddress)
balances, err := k.CompleteUnbondingWithAmount(ctx, dvPair.DelegatorAddress, dvPair.ValidatorAddress)
if err != nil {
continue
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCompleteUnbonding,
sdk.NewAttribute(sdk.AttributeKeyAmount, balances.String()),
sdk.NewAttribute(types.AttributeKeyValidator, dvPair.ValidatorAddress.String()),
sdk.NewAttribute(types.AttributeKeyDelegator, dvPair.DelegatorAddress.String()),
),
Expand All @@ -76,15 +77,20 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) []abci.ValidatorUpdate {
// Remove all mature redelegations from the red queue.
matureRedelegations := k.DequeueAllMatureRedelegationQueue(ctx, ctx.BlockHeader().Time)
for _, dvvTriplet := range matureRedelegations {
err := k.CompleteRedelegation(ctx, dvvTriplet.DelegatorAddress,
dvvTriplet.ValidatorSrcAddress, dvvTriplet.ValidatorDstAddress)
balances, err := k.CompleteRedelegationWithAmount(
ctx,
dvvTriplet.DelegatorAddress,
dvvTriplet.ValidatorSrcAddress,
dvvTriplet.ValidatorDstAddress,
)
if err != nil {
continue
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCompleteRedelegation,
sdk.NewAttribute(sdk.AttributeKeyAmount, balances.String()),
sdk.NewAttribute(types.AttributeKeyDelegator, dvvTriplet.DelegatorAddress.String()),
sdk.NewAttribute(types.AttributeKeySrcValidator, dvvTriplet.ValidatorSrcAddress.String()),
sdk.NewAttribute(types.AttributeKeyDstValidator, dvvTriplet.ValidatorDstAddress.String()),
Expand Down
63 changes: 48 additions & 15 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,16 +656,20 @@ func (k Keeper) Undelegate(
return completionTime, nil
}

// CompleteUnbonding completes the unbonding of all mature entries in the
// retrieved unbonding delegation object.
func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress,
valAddr sdk.ValAddress) sdk.Error {
// CompleteUnbondingWithAmount completes the unbonding of all mature entries in
// the retrieved unbonding delegation object and returns the total unbonding
// balance or an error upon failure.
func (k Keeper) CompleteUnbondingWithAmount(
ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress,
) (sdk.Coins, sdk.Error) {

ubd, found := k.GetUnbondingDelegation(ctx, delAddr, valAddr)
if !found {
return types.ErrNoUnbondingDelegation(k.Codespace())
return nil, types.ErrNoUnbondingDelegation(k.Codespace())
}

bondDenom := k.GetParams(ctx).BondDenom
balances := sdk.NewCoins()
ctxTime := ctx.BlockHeader().Time

// loop through all the entries and complete unbonding mature entries
Expand All @@ -677,11 +681,15 @@ func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress,

// track undelegation only when remaining or truncated shares are non-zero
if !entry.Balance.IsZero() {
amt := sdk.NewCoins(sdk.NewCoin(k.GetParams(ctx).BondDenom, entry.Balance))
err := k.supplyKeeper.UndelegateCoinsFromModuleToAccount(ctx, types.NotBondedPoolName, ubd.DelegatorAddress, amt)
amt := sdk.NewCoins(sdk.NewCoin(bondDenom, entry.Balance))
err := k.supplyKeeper.UndelegateCoinsFromModuleToAccount(
ctx, types.NotBondedPoolName, ubd.DelegatorAddress, amt,
)
if err != nil {
return err
return nil, err
}

balances = balances.Add(amt)
}
}
}
Expand All @@ -693,7 +701,14 @@ func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress,
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) sdk.Error {
_, err := k.CompleteUnbondingWithAmount(ctx, delAddr, valAddr)
return err
}

// begin unbonding / redelegation; create a redelegation record
Expand Down Expand Up @@ -751,16 +766,20 @@ func (k Keeper) BeginRedelegation(ctx sdk.Context, delAddr sdk.AccAddress,
return completionTime, nil
}

// CompleteRedelegation completes the unbonding of all mature entries in the
// retrieved unbonding delegation object.
func (k Keeper) CompleteRedelegation(ctx sdk.Context, delAddr sdk.AccAddress,
valSrcAddr, valDstAddr sdk.ValAddress) sdk.Error {
// CompleteRedelegationWithAmount completes the redelegations of all mature entries
// in the retrieved redelegation object and returns the total redelegation (initial)
// balance or an error upon failure.
func (k Keeper) CompleteRedelegationWithAmount(
ctx sdk.Context, delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress,
) (sdk.Coins, sdk.Error) {

red, found := k.GetRedelegation(ctx, delAddr, valSrcAddr, valDstAddr)
if !found {
return types.ErrNoRedelegation(k.Codespace())
return nil, types.ErrNoRedelegation(k.Codespace())
}

bondDenom := k.GetParams(ctx).BondDenom
balances := sdk.NewCoins()
ctxTime := ctx.BlockHeader().Time

// loop through all the entries and complete mature redelegation entries
Expand All @@ -769,6 +788,10 @@ func (k Keeper) CompleteRedelegation(ctx sdk.Context, delAddr sdk.AccAddress,
if entry.IsMature(ctxTime) {
red.RemoveEntry(int64(i))
i--

if !entry.InitialBalance.IsZero() {
balances = balances.Add(sdk.NewCoins(sdk.NewCoin(bondDenom, entry.InitialBalance)))
}
}
}

Expand All @@ -779,7 +802,17 @@ func (k Keeper) CompleteRedelegation(ctx sdk.Context, delAddr sdk.AccAddress,
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,
) sdk.Error {

_, err := k.CompleteRedelegationWithAmount(ctx, delAddr, valSrcAddr, valDstAddr)
return err
}

// ValidateUnbondAmount validates that a given unbond or redelegation amount is
Expand Down
7 changes: 3 additions & 4 deletions x/staking/keeper/val_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
// at the previous block height or were removed from the validator set entirely
// are returned to Tendermint.
func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []abci.ValidatorUpdate) {

store := ctx.KVStore(k.storeKey)
maxValidators := k.GetParams(ctx).MaxValidators
totalPower := sdk.ZeroInt()
Expand Down Expand Up @@ -119,11 +118,11 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab
// Update the pools based on the recent updates in the validator set:
// - The tokens from the non-bonded candidates that enter the new validator set need to be transferred
// to the Bonded pool.
// - The tokens from the bonded validators that are being kicked out from the validator set
// - The tokens from the bonded validators that are being kicked out from the validator set
// need to be transferred to the NotBonded pool.
switch {
// Compare and subtract the respective amounts to only perform one transfer.
// This is done in order to avoid doing multiple updates inside each iterator/loop.
// Compare and subtract the respective amounts to only perform one transfer.
// This is done in order to avoid doing multiple updates inside each iterator/loop.
case amtFromNotBondedToBonded.GT(amtFromBondedToNotBonded):
k.notBondedTokensToBonded(ctx, amtFromNotBondedToBonded.Sub(amtFromBondedToNotBonded))
case amtFromNotBondedToBonded.LT(amtFromBondedToNotBonded):
Expand Down

0 comments on commit 94c1a10

Please sign in to comment.