github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/slashing/internal/keeper/signing_info.go (about)

     1  package keeper
     2  
     3  import (
     4  	"time"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/slashing/internal/types"
     8  )
     9  
    10  // GetValidatorSigningInfo retruns the ValidatorSigningInfo for a specific validator
    11  // ConsAddress
    12  func (k Keeper) GetValidatorSigningInfo(ctx sdk.Context, address sdk.ConsAddress) (info types.ValidatorSigningInfo, found bool) {
    13  	store := ctx.KVStore(k.storeKey)
    14  	bz := store.Get(types.GetValidatorSigningInfoKey(address))
    15  	if bz == nil {
    16  		found = false
    17  		return
    18  	}
    19  	k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &info)
    20  	found = true
    21  	return
    22  }
    23  
    24  // HasValidatorSigningInfo returns if a given validator has signing information
    25  // persited.
    26  func (k Keeper) HasValidatorSigningInfo(ctx sdk.Context, consAddr sdk.ConsAddress) bool {
    27  	_, ok := k.GetValidatorSigningInfo(ctx, consAddr)
    28  	return ok
    29  }
    30  
    31  // SetValidatorSigningInfo sets the validator signing info to a consensus address key
    32  func (k Keeper) SetValidatorSigningInfo(ctx sdk.Context, address sdk.ConsAddress, info types.ValidatorSigningInfo) {
    33  	store := ctx.KVStore(k.storeKey)
    34  	bz := k.cdc.MustMarshalBinaryLengthPrefixed(info)
    35  	store.Set(types.GetValidatorSigningInfoKey(address), bz)
    36  }
    37  
    38  // IterateValidatorSigningInfos iterates over the stored ValidatorSigningInfo
    39  func (k Keeper) IterateValidatorSigningInfos(ctx sdk.Context,
    40  	handler func(address sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool)) {
    41  
    42  	store := ctx.KVStore(k.storeKey)
    43  	iter := sdk.KVStorePrefixIterator(store, types.ValidatorSigningInfoKey)
    44  	defer iter.Close()
    45  	for ; iter.Valid(); iter.Next() {
    46  		address := types.GetValidatorSigningInfoAddress(iter.Key())
    47  		var info types.ValidatorSigningInfo
    48  		k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &info)
    49  		if handler(address, info) {
    50  			break
    51  		}
    52  	}
    53  }
    54  
    55  // GetValidatorMissedBlockBitArray gets the bit for the missed blocks array
    56  func (k Keeper) GetValidatorMissedBlockBitArray(ctx sdk.Context, address sdk.ConsAddress, index int64) (missed bool) {
    57  	store := ctx.KVStore(k.storeKey)
    58  	bz := store.Get(types.GetValidatorMissedBlockBitArrayKey(address, index))
    59  	if bz == nil {
    60  		// lazy: treat empty key as not missed
    61  		missed = false
    62  		return
    63  	}
    64  	k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &missed)
    65  	return
    66  }
    67  
    68  // IterateValidatorMissedBlockBitArray iterates over the signed blocks window
    69  // and performs a callback function
    70  func (k Keeper) IterateValidatorMissedBlockBitArray(ctx sdk.Context,
    71  	address sdk.ConsAddress, handler func(index int64, missed bool) (stop bool)) {
    72  
    73  	store := ctx.KVStore(k.storeKey)
    74  	index := int64(0)
    75  	// Array may be sparse
    76  	for ; index < k.SignedBlocksWindow(ctx); index++ {
    77  		var missed bool
    78  		bz := store.Get(types.GetValidatorMissedBlockBitArrayKey(address, index))
    79  		if bz == nil {
    80  			continue
    81  		}
    82  		k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &missed)
    83  		if handler(index, missed) {
    84  			break
    85  		}
    86  	}
    87  }
    88  
    89  // JailUntil attempts to set a validator's JailedUntil attribute in its signing
    90  // info. It will panic if the signing info does not exist for the validator.
    91  func (k Keeper) JailUntil(ctx sdk.Context, consAddr sdk.ConsAddress, jailTime time.Time) {
    92  	signInfo, ok := k.GetValidatorSigningInfo(ctx, consAddr)
    93  	if !ok {
    94  		panic("cannot jail validator that does not have any signing information")
    95  	}
    96  
    97  	signInfo.JailedUntil = jailTime
    98  	k.SetValidatorSigningInfo(ctx, consAddr, signInfo)
    99  }
   100  
   101  // Tombstone attempts to tombstone a validator. It will panic if signing info for
   102  // the given validator does not exist.
   103  func (k Keeper) Tombstone(ctx sdk.Context, consAddr sdk.ConsAddress) {
   104  	signInfo, ok := k.GetValidatorSigningInfo(ctx, consAddr)
   105  	if !ok {
   106  		panic("cannot tombstone validator that does not have any signing information")
   107  	}
   108  
   109  	if signInfo.Tombstoned {
   110  		panic("cannot tombstone validator that is already tombstoned")
   111  	}
   112  
   113  	signInfo.Tombstoned = true
   114  	k.SetValidatorSigningInfo(ctx, consAddr, signInfo)
   115  }
   116  
   117  // IsTombstoned returns if a given validator by consensus address is tombstoned.
   118  func (k Keeper) IsTombstoned(ctx sdk.Context, consAddr sdk.ConsAddress) bool {
   119  	signInfo, ok := k.GetValidatorSigningInfo(ctx, consAddr)
   120  	if !ok {
   121  		return false
   122  	}
   123  
   124  	return signInfo.Tombstoned
   125  }
   126  
   127  // SetValidatorMissedBlockBitArray sets the bit that checks if the validator has
   128  // missed a block in the current window
   129  func (k Keeper) SetValidatorMissedBlockBitArray(ctx sdk.Context, address sdk.ConsAddress, index int64, missed bool) {
   130  	store := ctx.KVStore(k.storeKey)
   131  	bz := k.cdc.MustMarshalBinaryLengthPrefixed(missed)
   132  	store.Set(types.GetValidatorMissedBlockBitArrayKey(address, index), bz)
   133  }
   134  
   135  // clearValidatorMissedBlockBitArray deletes every instance of ValidatorMissedBlockBitArray in the store
   136  func (k Keeper) clearValidatorMissedBlockBitArray(ctx sdk.Context, address sdk.ConsAddress) {
   137  	store := ctx.KVStore(k.storeKey)
   138  	iter := sdk.KVStorePrefixIterator(store, types.GetValidatorMissedBlockBitArrayPrefixKey(address))
   139  	defer iter.Close()
   140  	for ; iter.Valid(); iter.Next() {
   141  		store.Delete(iter.Key())
   142  	}
   143  }