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

     1  package types
     2  
     3  import (
     4  	"encoding/binary"
     5  
     6  	sdk "github.com/cosmos/cosmos-sdk/types"
     7  	"github.com/cosmos/cosmos-sdk/types/address"
     8  	"github.com/cosmos/cosmos-sdk/types/kv"
     9  )
    10  
    11  const (
    12  	// ModuleName is the name of the module
    13  	ModuleName = "slashing"
    14  
    15  	// StoreKey is the store key string for slashing
    16  	StoreKey = ModuleName
    17  
    18  	// RouterKey is the message route for slashing
    19  	RouterKey = ModuleName
    20  
    21  	// MissedBlockBitmapChunkSize defines the chunk size, in number of bits, of a
    22  	// validator missed block bitmap. Chunks are used to reduce the storage and
    23  	// write overhead of IAVL nodes. The total size of the bitmap is roughly in
    24  	// the range [0, SignedBlocksWindow) where each bit represents a block. A
    25  	// validator's IndexOffset modulo the SignedBlocksWindow is used to retrieve
    26  	// the chunk in that bitmap range. Once the chunk is retrieved, the same index
    27  	// is used to check or flip a bit, where if a bit is set, it indicates the
    28  	// validator missed that block.
    29  	//
    30  	// For a bitmap of N items, i.e. a validator's signed block window, the amount
    31  	// of write complexity per write with a factor of f being the overhead of
    32  	// IAVL being un-optimized, i.e. 2-4, is as follows:
    33  	//
    34  	// ChunkSize + (f * 256 <IAVL leaf hash>) + 256 * log_2(N / ChunkSize)
    35  	//
    36  	// As for the storage overhead, with the same factor f, it is as follows:
    37  	// (N - 256) + (N / ChunkSize) * (512 * f)
    38  	MissedBlockBitmapChunkSize = 1024 // 2^10 bits
    39  )
    40  
    41  // Keys for slashing store
    42  // Items are stored with the following key: values
    43  //
    44  // - 0x01<consAddrLen (1 Byte)><consAddress_Bytes>: ValidatorSigningInfo
    45  //
    46  // - 0x02<consAddrLen (1 Byte)><consAddress_Bytes><chunk_index>: bitmap_chunk
    47  //
    48  // - 0x03<accAddrLen (1 Byte)><accAddr_Bytes>: cryptotypes.PubKey
    49  
    50  var (
    51  	ParamsKey                           = []byte{0x00} // Prefix for params key
    52  	ValidatorSigningInfoKeyPrefix       = []byte{0x01} // Prefix for signing info
    53  	ValidatorMissedBlockBitmapKeyPrefix = []byte{0x02} // Prefix for missed block bitmap
    54  	AddrPubkeyRelationKeyPrefix         = []byte{0x03} // Prefix for address-pubkey relation
    55  )
    56  
    57  // ValidatorSigningInfoKey - stored by *Consensus* address (not operator address)
    58  func ValidatorSigningInfoKey(v sdk.ConsAddress) []byte {
    59  	return append(ValidatorSigningInfoKeyPrefix, address.MustLengthPrefix(v.Bytes())...)
    60  }
    61  
    62  // ValidatorSigningInfoAddress - extract the address from a validator signing info key
    63  func ValidatorSigningInfoAddress(key []byte) (v sdk.ConsAddress) {
    64  	// Remove prefix and address length.
    65  	kv.AssertKeyAtLeastLength(key, 3)
    66  	addr := key[2:]
    67  
    68  	return sdk.ConsAddress(addr)
    69  }
    70  
    71  // ValidatorMissedBlockBitmapPrefixKey returns the key prefix for a validator's
    72  // missed block bitmap.
    73  func ValidatorMissedBlockBitmapPrefixKey(v sdk.ConsAddress) []byte {
    74  	return append(ValidatorMissedBlockBitmapKeyPrefix, address.MustLengthPrefix(v.Bytes())...)
    75  }
    76  
    77  // ValidatorMissedBlockBitmapKey returns the key for a validator's missed block
    78  // bitmap chunk.
    79  func ValidatorMissedBlockBitmapKey(v sdk.ConsAddress, chunkIndex int64) []byte {
    80  	bz := make([]byte, 8)
    81  	binary.LittleEndian.PutUint64(bz, uint64(chunkIndex))
    82  
    83  	return append(ValidatorMissedBlockBitmapPrefixKey(v), bz...)
    84  }
    85  
    86  // AddrPubkeyRelationKey gets pubkey relation key used to get the pubkey from the address
    87  func AddrPubkeyRelationKey(addr []byte) []byte {
    88  	return append(AddrPubkeyRelationKeyPrefix, address.MustLengthPrefix(addr)...)
    89  }