github.com/cosmos/cosmos-sdk@v0.50.10/x/slashing/keeper/unjail.go (about)

     1  package keeper
     2  
     3  import (
     4  	"context"
     5  
     6  	"cosmossdk.io/errors"
     7  
     8  	sdk "github.com/cosmos/cosmos-sdk/types"
     9  	"github.com/cosmos/cosmos-sdk/x/slashing/types"
    10  )
    11  
    12  // Unjail calls the staking Unjail function to unjail a validator if the
    13  // jailed period has concluded
    14  func (k Keeper) Unjail(ctx context.Context, validatorAddr sdk.ValAddress) error {
    15  	validator, err := k.sk.Validator(ctx, validatorAddr)
    16  	if err != nil {
    17  		return err
    18  	}
    19  	if validator == nil {
    20  		return types.ErrNoValidatorForAddress
    21  	}
    22  
    23  	// cannot be unjailed if no self-delegation exists
    24  	selfDel, err := k.sk.Delegation(ctx, sdk.AccAddress(validatorAddr), validatorAddr)
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	if selfDel == nil {
    30  		return types.ErrMissingSelfDelegation
    31  	}
    32  
    33  	tokens := validator.TokensFromShares(selfDel.GetShares()).TruncateInt()
    34  	minSelfBond := validator.GetMinSelfDelegation()
    35  	if tokens.LT(minSelfBond) {
    36  		return errors.Wrapf(
    37  			types.ErrSelfDelegationTooLowToUnjail, "%s less than %s", tokens, minSelfBond,
    38  		)
    39  	}
    40  
    41  	// cannot be unjailed if not jailed
    42  	if !validator.IsJailed() {
    43  		return types.ErrValidatorNotJailed
    44  	}
    45  
    46  	consAddr, err := validator.GetConsAddr()
    47  	if err != nil {
    48  		return err
    49  	}
    50  	// If the validator has a ValidatorSigningInfo object that signals that the
    51  	// validator was bonded and so we must check that the validator is not tombstoned
    52  	// and can be unjailed at the current block.
    53  	//
    54  	// A validator that is jailed but has no ValidatorSigningInfo object signals
    55  	// that the validator was never bonded and must've been jailed due to falling
    56  	// below their minimum self-delegation. The validator can unjail at any point
    57  	// assuming they've now bonded above their minimum self-delegation.
    58  	info, err := k.GetValidatorSigningInfo(ctx, consAddr)
    59  	if err == nil {
    60  		// cannot be unjailed if tombstoned
    61  		if info.Tombstoned {
    62  			return types.ErrValidatorJailed
    63  		}
    64  
    65  		// cannot be unjailed until out of jail
    66  		sdkCtx := sdk.UnwrapSDKContext(ctx)
    67  		if sdkCtx.BlockHeader().Time.Before(info.JailedUntil) {
    68  			return types.ErrValidatorJailed
    69  		}
    70  	}
    71  
    72  	return k.sk.Unjail(ctx, consAddr)
    73  }