Skip to content

Commit

Permalink
IS up port
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Sep 1, 2022
1 parent ab33342 commit da53124
Show file tree
Hide file tree
Showing 20 changed files with 1,020 additions and 59 deletions.
10 changes: 8 additions & 2 deletions proto/cosmos/staking/v1beta1/staking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ message Validator {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];

// strictly positive if this validator's unbonding has been stopped by external modules
int64 unbonding_on_hold_ref_count = 12;

// list of unbonding ids, each uniquely identifing an unbonding of this validator
repeated uint64 unbonding_ids = 13;
}

// BondStatus is the status of a validator.
Expand Down Expand Up @@ -216,7 +222,7 @@ message UnbondingDelegation {
// validator_address is the bech32-encoded address of the validator.
string validator_address = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// entries are the unbonding delegation entries.
repeated UnbondingDelegationEntry entries = 3 [(gogoproto.nullable) = false]; // unbonding delegation entries
repeated UnbondingDelegationEntry entries = 3 [(gogoproto.nullable) = false]; // unbonding delegation entries
}

// UnbondingDelegationEntry defines an unbonding object with relevant metadata.
Expand Down Expand Up @@ -279,7 +285,7 @@ message Redelegation {
// validator_dst_address is the validator redelegation destination operator address.
string validator_dst_address = 3 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// entries are the redelegation entries.
repeated RedelegationEntry entries = 4 [(gogoproto.nullable) = false]; // redelegation entries
repeated RedelegationEntry entries = 4 [(gogoproto.nullable) = false]; // redelegation entries
}

// Params defines the parameters for the x/staking module.
Expand Down
17 changes: 17 additions & 0 deletions proto/cosmos/staking/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos/staking/v1beta1/staking.proto";
import "tendermint/abci/types.proto";

import "cosmos/msg/v1/msg.proto";

Expand Down Expand Up @@ -188,3 +189,19 @@ message MsgUpdateParams {
//
// Since: cosmos-sdk 0.47
message MsgUpdateParamsResponse {};

// InfractionType indicates the infraction type a validator commited.
enum InfractionType {
option (gogoproto.goproto_enum_prefix) = false;

// UNSPECIFIED defines an empty infraction type.
INFRACTION_TYPE_UNSPECIFIED = 0 [(gogoproto.enumvalue_customname) = "InfractionEmpty"];
// DOUBLE_SIGN defines a validator that double-signs a block.
INFRACTION_TYPE_DOUBLE_SIGN = 1 [(gogoproto.enumvalue_customname) = "DoubleSign"];
// DOWNTIME defines a validator that missed signing too many blocks.
INFRACTION_TYPE_DOWNTIME = 2 [(gogoproto.enumvalue_customname) = "Downtime"];
}

message ValidatorUpdates {
repeated tendermint.abci.ValidatorUpdate updates = 1 [(gogoproto.nullable) = false];
}
4 changes: 4 additions & 0 deletions x/distribution/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,7 @@ func (h Hooks) AfterValidatorBeginUnbonding(_ sdk.Context, _ sdk.ConsAddress, _
func (h Hooks) BeforeDelegationRemoved(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
return nil
}

func (h Hooks) AfterUnbondingInitiated(_ sdk.Context, _ uint64) error {
return nil
}
4 changes: 4 additions & 0 deletions x/slashing/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,7 @@ func (h Hooks) AfterDelegationModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.Va
func (h Hooks) BeforeValidatorSlashed(_ sdk.Context, _ sdk.ValAddress, _ sdk.Dec) error {
return nil
}

func (h Hooks) AfterUnbondingInitiated(_ sdk.Context, _ uint64) error {
return nil
}
27 changes: 21 additions & 6 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,21 @@ func (k Keeper) SetUnbondingDelegationEntry(
creationHeight int64, minTime time.Time, balance math.Int,
) types.UnbondingDelegation {
ubd, found := k.GetUnbondingDelegation(ctx, delegatorAddr, validatorAddr)
id := k.IncrementUnbondingId(ctx)
if found {
ubd.AddEntry(creationHeight, minTime, balance)
ubd.AddEntry(creationHeight, minTime, balance, id)
} else {
ubd = types.NewUnbondingDelegation(delegatorAddr, validatorAddr, creationHeight, minTime, balance)
ubd = types.NewUnbondingDelegation(delegatorAddr, validatorAddr, creationHeight, minTime, balance, id)
}

k.SetUnbondingDelegation(ctx, ubd)

// Add to the UBDByUnbondingOp index to look up the UBD by the UBDE ID
k.SetUnbondingDelegationByUnbondingId(ctx, ubd, id)

// Call hook
k.AfterUnbondingInitiated(ctx, id)

return ubd
}

Expand Down Expand Up @@ -488,15 +495,22 @@ func (k Keeper) SetRedelegationEntry(ctx sdk.Context,
sharesSrc, sharesDst sdk.Dec,
) types.Redelegation {
red, found := k.GetRedelegation(ctx, delegatorAddr, validatorSrcAddr, validatorDstAddr)
id := k.IncrementUnbondingId(ctx)
if found {
red.AddEntry(creationHeight, minTime, balance, sharesDst)
red.AddEntry(creationHeight, minTime, balance, sharesDst, id)
} else {
red = types.NewRedelegation(delegatorAddr, validatorSrcAddr,
validatorDstAddr, creationHeight, minTime, balance, sharesDst)
validatorDstAddr, creationHeight, minTime, balance, sharesDst, id)
}

k.SetRedelegation(ctx, red)

// Add to the UBDByEntry index to look up the UBD by the UBDE ID
k.SetRedelegationByUnbondingId(ctx, red, id)

// Call hook
k.AfterUnbondingInitiated(ctx, id)

return red
}

Expand Down Expand Up @@ -846,7 +860,7 @@ func (k Keeper) CompleteUnbonding(ctx sdk.Context, delAddr sdk.AccAddress, valAd
// loop through all the entries and complete unbonding mature entries
for i := 0; i < len(ubd.Entries); i++ {
entry := ubd.Entries[i]
if entry.IsMature(ctxTime) {
if entry.IsMature(ctxTime) && !entry.OnHold() {
ubd.RemoveEntry(int64(i))
i--

Expand Down Expand Up @@ -950,9 +964,10 @@ func (k Keeper) CompleteRedelegation(
// loop through all the entries and complete mature redelegation entries
for i := 0; i < len(red.Entries); i++ {
entry := red.Entries[i]
if entry.IsMature(ctxTime) {
if entry.IsMature(ctxTime) && !entry.OnHold() {
red.RemoveEntry(int64(i))
i--
k.DeleteUnbondingIndex(ctx, entry.UnbondingId)

if !entry.InitialBalance.IsZero() {
balances = balances.Add(sdk.NewCoin(bondDenom, entry.InitialBalance))
Expand Down
8 changes: 8 additions & 0 deletions x/staking/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@ func (k Keeper) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress,
}
return nil
}

// This is called when an UnbondingDelegationEntry is first created
func (k Keeper) AfterUnbondingInitiated(ctx sdk.Context, id uint64) error {
if k.hooks != nil {
return k.hooks.AfterUnbondingInitiated(ctx, id)
}
return nil
}
17 changes: 17 additions & 0 deletions x/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"cosmossdk.io/math"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -101,3 +102,19 @@ func (k Keeper) SetLastTotalPower(ctx sdk.Context, power math.Int) {
func (k Keeper) GetAuthority() string {
return k.authority
}

// SetValidatorUpdates sets the ABCI validator power updates for the current block.
func (k Keeper) SetValidatorUpdates(ctx sdk.Context, valUpdates []abci.ValidatorUpdate) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&types.ValidatorUpdates{Updates: valUpdates})
store.Set(types.ValidatorUpdatesKey, bz)
}

// GetValidatorUpdates returns the ABCI validator power updates within the current block.
func (k Keeper) GetValidatorUpdates(ctx sdk.Context) []abci.ValidatorUpdate {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.ValidatorUpdatesKey)
var valUpdates types.ValidatorUpdates
k.cdc.MustUnmarshal(bz, &valUpdates)
return valUpdates.Updates
}
6 changes: 3 additions & 3 deletions x/staking/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
//
// Infraction was committed at the current height or at a past height,
// not at a height in the future
func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeight int64, power int64, slashFactor sdk.Dec) math.Int {
func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeight int64, power int64, slashFactor sdk.Dec, _ types.InfractionType) math.Int {
logger := k.Logger(ctx)

if slashFactor.IsNegative() {
Expand Down Expand Up @@ -187,7 +187,7 @@ func (k Keeper) SlashUnbondingDelegation(ctx sdk.Context, unbondingDelegation ty
continue
}

if entry.IsMature(now) {
if entry.IsMature(now) && !entry.OnHold() {
// Unbonding delegation no longer eligible for slashing, skip it
continue
}
Expand Down Expand Up @@ -241,7 +241,7 @@ func (k Keeper) SlashRedelegation(ctx sdk.Context, srcValidator types.Validator,
continue
}

if entry.IsMature(now) {
if entry.IsMature(now) && !entry.OnHold() {
// Redelegation no longer eligible for slashing, skip it
continue
}
Expand Down
Loading

0 comments on commit da53124

Please sign in to comment.