Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant Length Key Prefixes #242

Merged
merged 14 commits into from
Aug 2, 2022
4 changes: 2 additions & 2 deletions x/ccv/consumer/keeper/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (k Keeper) GetLastTransmissionBlockHeight(ctx sdk.Context) (
*types.LastTransmissionBlockHeight, error) {

store := ctx.KVStore(k.storeKey)
bz := store.Get(types.LastDistributionTransmissionKey)
bz := store.Get(types.LastDistributionTransmissionKey())
ltbh := &types.LastTransmissionBlockHeight{}
if bz != nil {
err := ltbh.Unmarshal(bz)
Expand All @@ -117,7 +117,7 @@ func (k Keeper) SetLastTransmissionBlockHeight(ctx sdk.Context,
if err != nil {
return err
}
store.Set(types.LastDistributionTransmissionKey, bz)
store.Set(types.LastDistributionTransmissionKey(), bz)
return nil
}

Expand Down
22 changes: 11 additions & 11 deletions x/ccv/consumer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ func (k Keeper) BindPort(ctx sdk.Context, portID string) error {
// GetPort returns the portID for the transfer module. Used in ExportGenesis
func (k Keeper) GetPort(ctx sdk.Context) string {
store := ctx.KVStore(k.storeKey)
return string(store.Get(types.PortKey))
return string(store.Get(types.PortKey()))
}

// SetPort sets the portID for the transfer module. Used in InitGenesis
func (k Keeper) SetPort(ctx sdk.Context, portID string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.PortKey, []byte(portID))
store.Set(types.PortKey(), []byte(portID))
}

// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function
Expand Down Expand Up @@ -237,11 +237,11 @@ func (k Keeper) DeletePendingChanges(ctx sdk.Context) {
// IteratePacketMaturityTime iterates through the VSC packet maturity times set in the store
func (k Keeper) IteratePacketMaturityTime(ctx sdk.Context, cb func(vscId, timeNs uint64) bool) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.PacketMaturityTimePrefix))
iterator := sdk.KVStorePrefixIterator(store, types.PacketMaturityTimePrefix())

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
seqBytes := iterator.Key()[len([]byte(types.PacketMaturityTimePrefix)):]
seqBytes := iterator.Key()[len(types.PacketMaturityTimePrefix()):]
seq := binary.BigEndian.Uint64(seqBytes)

timeNs := binary.BigEndian.Uint64(iterator.Value())
Expand Down Expand Up @@ -351,13 +351,13 @@ func (k Keeper) SetCCValidator(ctx sdk.Context, v types.CrossChainValidator) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&v)

store.Set(types.GetCrossChainValidatorKey(v.Address), bz)
store.Set(types.CrossChainValidatorKey(v.Address), bz)
}

// GetCCValidator returns a cross-chain validator for a given address
func (k Keeper) GetCCValidator(ctx sdk.Context, addr []byte) (validator types.CrossChainValidator, found bool) {
store := ctx.KVStore(k.storeKey)
v := store.Get(types.GetCrossChainValidatorKey(addr))
v := store.Get(types.CrossChainValidatorKey(addr))
if v == nil {
return
}
Expand All @@ -370,13 +370,13 @@ func (k Keeper) GetCCValidator(ctx sdk.Context, addr []byte) (validator types.Cr
// DeleteCCValidator deletes a cross-chain validator for a given address
func (k Keeper) DeleteCCValidator(ctx sdk.Context, addr []byte) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetCrossChainValidatorKey(addr))
store.Delete(types.CrossChainValidatorKey(addr))
}

// GetAllCCValidator returns all cross-chain validators
func (k Keeper) GetAllCCValidator(ctx sdk.Context) (validators []types.CrossChainValidator) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.CrossChainValidatorPrefix))
iterator := sdk.KVStorePrefixIterator(store, types.CrossChainValidatorPrefix())

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand All @@ -396,13 +396,13 @@ func (k Keeper) SetPendingSlashRequests(ctx sdk.Context, requests []types.SlashR
if err != nil {
panic(fmt.Errorf("failed to encode slash request json: %w", err))
}
store.Set([]byte(types.PendingSlashRequestsPrefix), buf.Bytes())
store.Set(types.PendingSlashRequestsPrefix(), buf.Bytes())
}

// GetPendingSlashRequest returns the pending slash requests in store
func (k Keeper) GetPendingSlashRequests(ctx sdk.Context) (requests []types.SlashRequest) {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte(types.PendingSlashRequestsPrefix))
bz := store.Get(types.PendingSlashRequestsPrefix())
if bz == nil {
return
}
Expand All @@ -425,5 +425,5 @@ func (k Keeper) AppendPendingSlashRequests(ctx sdk.Context, req types.SlashReque
// ClearPendingSlashRequests clears the pending slash requests in store
func (k Keeper) ClearPendingSlashRequests(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
store.Delete([]byte(types.PendingSlashRequestsPrefix))
store.Delete(types.PendingSlashRequestsPrefix())
}
4 changes: 2 additions & 2 deletions x/ccv/consumer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (k Keeper) OnRecvVSCPacket(ctx sdk.Context, packet channeltypes.Packet, new
func (k Keeper) UnbondMaturePackets(ctx sdk.Context) error {

store := ctx.KVStore(k.storeKey)
maturityIterator := sdk.KVStorePrefixIterator(store, []byte(types.PacketMaturityTimePrefix))
maturityIterator := sdk.KVStorePrefixIterator(store, types.PacketMaturityTimePrefix())
defer maturityIterator.Close()

currentTime := uint64(ctx.BlockTime().UnixNano())
Expand All @@ -84,7 +84,7 @@ func (k Keeper) UnbondMaturePackets(ctx sdk.Context) error {
}

for maturityIterator.Valid() {
vscId := types.GetIdFromPacketMaturityTimeKey(maturityIterator.Key())
vscId := types.IdFromPacketMaturityTimeKey(maturityIterator.Key())
if currentTime >= binary.BigEndian.Uint64(maturityIterator.Value()) {
// send VSCMatured packet
// - construct validator set change packet data
Expand Down
6 changes: 3 additions & 3 deletions x/ccv/consumer/keeper/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (k Keeper) UnbondingTime(ctx sdk.Context) time.Duration {
// GetHistoricalInfo gets the historical info at a given height
func (k Keeper) GetHistoricalInfo(ctx sdk.Context, height int64) (stakingtypes.HistoricalInfo, bool) {
store := ctx.KVStore(k.storeKey)
key := types.GetHistoricalInfoKey(height)
key := types.HistoricalInfoKey(height)

value := store.Get(key)
if value == nil {
Expand All @@ -136,7 +136,7 @@ func (k Keeper) GetHistoricalInfo(ctx sdk.Context, height int64) (stakingtypes.H
// SetHistoricalInfo sets the historical info at a given height
func (k Keeper) SetHistoricalInfo(ctx sdk.Context, height int64, hi *stakingtypes.HistoricalInfo) {
store := ctx.KVStore(k.storeKey)
key := types.GetHistoricalInfoKey(height)
key := types.HistoricalInfoKey(height)
value := k.cdc.MustMarshal(hi)

store.Set(key, value)
Expand All @@ -145,7 +145,7 @@ func (k Keeper) SetHistoricalInfo(ctx sdk.Context, height int64, hi *stakingtype
// DeleteHistoricalInfo deletes the historical info at a given height
func (k Keeper) DeleteHistoricalInfo(ctx sdk.Context, height int64) {
store := ctx.KVStore(k.storeKey)
key := types.GetHistoricalInfoKey(height)
key := types.HistoricalInfoKey(height)

store.Delete(key)
}
Expand Down
147 changes: 98 additions & 49 deletions x/ccv/consumer/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,101 +23,150 @@ const (
// QuerierRoute is the querier route for IBC consumer
QuerierRoute = ModuleName

// UnbondingTimeKeyString is the key for storing the unbonding period
UnbondingTimeKeyString = "unbondingtime"
// HistoricalEntries is set to 10000 like the staking module parameter DefaultHistoricalEntries
HistoricalEntries uint32 = 10000

// ProviderClientKeyString is the key for storing the clientID of the provider client
ProviderClientKeyString = "providerclient"
// ConsumerRedistributeName the root string for the consumer-redistribution account address
ConsumerRedistributeName = "cons_redistribute"

// ProviderChannelKeyString is the key for storing the channelID of the CCV channel
ProviderChannelKeyString = "providerchannel"
// ConsumerToSendToProviderName is a "buffer" address for outgoing fees to be transferred to the provider chain
ConsumerToSendToProviderName = "cons_to_send_to_provider"
)

// PendingChangesKeyString is the key that will store any pending validator set changes
// received over CCV channel but not yet flushed over ABCI
PendingChangesKeyString = "pendingchanges"
// Iota generated keys/key prefixes (as a byte), supports 256 possible values
const (
// PortByteKey defines the byte key to store the port ID in store
PortByteKey byte = iota

// PacketMaturityTimePrefix is the key prefix that will store maturity time for each received VSC packet
PacketMaturityTimePrefix = "packetmaturitytime"
// LastDistributionTransmissionByteKey defines the byte key to store the last distribution transmission
LastDistributionTransmissionByteKey

// HistoricalEntries is set to 10000 like the staking module parameter DefaultHistoricalEntries
HistoricalEntries uint32 = 10000
// UnbondingTimeKeyString is the byte key for storing the unbonding period
UnbondingTimeByteKey

// HeightValsetUpdateIDPrefix is the key prefix that will store the mapping from block height to valset update ID
HeightValsetUpdateIDPrefix = "heightvalsetupdateid"
// ProviderClientKeyString is the byte key for storing the clientID of the provider client
ProviderClientByteKey

// OutstandingDowntimePrefix is the key prefix that will store the validators outstanding downtime by consensus address
OutstandingDowntimePrefix = "outstandingdowntime"
// ProviderChannelKeyString is the byte key for storing the channelID of the CCV channel
ProviderChannelByteKey

// CrossChainValidatorPrefix is the key prefix that will store cross-chain validators by consensus address
CrossChainValidatorPrefix = "crosschainvalidator"
// PendingChangesKeyString is the byte key that will store any pending validator set changes
// received over CCV channel but not yet flushed over ABCI
PendingChangesByteKey

// ConsumerRedistributeName the root string for the consumer-redistribution account address
ConsumerRedistributeName = "cons_redistribute"
// HistoricalInfoKey is the byte prefix that will store the historical info for a given height
HistoricalInfoBytePrefix

// ConsumerToSendToProviderName is a "buffer" address for outgoing fees to be transferred to the provider chain.
ConsumerToSendToProviderName = "cons_to_send_to_provider"
// PacketMaturityTimePrefix is the byte prefix that will store maturity time for each received VSC packet
PacketMaturityTimeBytePrefix

// HeightValsetUpdateIDPrefix is the byte prefix that will store the mapping from block height to valset update ID
HeightValsetUpdateIDBytePrefix

// PendingSlashRequestsPrefix is the prefix that will store a list of slash request that must be sent
// OutstandingDowntimePrefix is the byte prefix that will store the validators outstanding downtime by consensus address
OutstandingDowntimeBytePrefix

// PendingSlashRequestsPrefix is the byte prefix that will store a list of slash request that must be sent
// to the provider chain once the CCV channel is established
PendingSlashRequestsPrefix = "pendingslashrequests"
PendingSlashRequestsBytePrefix

// HistoricalInfoKey is the key prefix that will store the historical info for a given height
HistoricalInfoKey = "historicalinfokey"
// CrossChainValidatorPrefix is the byte prefix that will store cross-chain validators by consensus address
CrossChainValidatorBytePrefix
)

var (
// PortKey defines the key to store the port ID in store
PortKey = []byte{0x01}
LastDistributionTransmissionKey = []byte{0x02}
)
// Returns the key to the port ID in the store
func PortKey() []byte {
return []byte{PortByteKey}
}

// UnbondingTimeKey returns the key for storing the unbonding period
func UnbondingTimeKey() []byte {
return []byte(UnbondingTimeKeyString)
// Returns the key to the last distribution transmission in the store
func LastDistributionTransmissionKey() []byte {
return []byte{LastDistributionTransmissionByteKey}
}

// ProviderChannelKey returns the key for storing channelID of the provider chain.
func ProviderChannelKey() []byte {
return []byte(ProviderChannelKeyString)
// UnbondingTimeKey returns the key for storing the unbonding period
func UnbondingTimeKey() []byte {
return []byte{UnbondingTimeByteKey}
}

// ProviderClientKey returns the key for storing clientID of the provider
func ProviderClientKey() []byte {
return []byte(ProviderClientKeyString)
return []byte{ProviderClientByteKey}
}

// ProviderChannelKey returns the key for storing channelID of the provider chain
func ProviderChannelKey() []byte {
return []byte{ProviderChannelByteKey}
}

// PendingChangesKey returns the key for storing pending validator set changes
func PendingChangesKey() []byte {
return []byte(PendingChangesKeyString)
return []byte{PendingChangesByteKey}
}

// Returns the historical info key prefix for historical info on each height
shaspitz marked this conversation as resolved.
Show resolved Hide resolved
func HistoricalInfoPrefix() []byte {
shaspitz marked this conversation as resolved.
Show resolved Hide resolved
return []byte{HistoricalInfoBytePrefix}
}

// Returns the key prefix of maturity times stored for each received VSC packet
func PacketMaturityTimePrefix() []byte {
return []byte{PacketMaturityTimeBytePrefix}
}

// Returns the key prefix for the mapping from block height to valset update ID
func HeightValsetUpdateIDPrefix() []byte {
return []byte{HeightValsetUpdateIDBytePrefix}
}

// Returns the key prefix for validators' outstanding downtime by consensus address
func OutstandingDowntimePrefix() []byte {
return []byte{OutstandingDowntimeBytePrefix}
}

// Returns the key prefix for a list of slash request that must be sent
// to the provider chain once the CCV channel is established
func PendingSlashRequestsPrefix() []byte {
return []byte{PendingSlashRequestsBytePrefix}
}

// Returns the key prefix for cross-chain validators by consensus address
func CrossChainValidatorPrefix() []byte {
return []byte{CrossChainValidatorBytePrefix}
}

// PacketMaturityTimeKey returns the key for storing maturity time for a given received VSC packet id
func PacketMaturityTimeKey(id uint64) []byte {
seqBytes := make([]byte, 8)
binary.BigEndian.PutUint64(seqBytes, id)
return append([]byte(PacketMaturityTimePrefix), seqBytes...)
return append(PacketMaturityTimePrefix(), seqBytes...)
}

func GetIdFromPacketMaturityTimeKey(key []byte) uint64 {
return binary.BigEndian.Uint64(key[len(PacketMaturityTimePrefix):])
// Returns the packet id corresponding to a maturity time full key (including prefix)
func IdFromPacketMaturityTimeKey(key []byte) uint64 {
return binary.BigEndian.Uint64(key[len(PacketMaturityTimePrefix()):])
}

// Returns the key to a valset update ID for a given block height
func HeightValsetUpdateIDKey(height uint64) []byte {
hBytes := make([]byte, 8)
binary.BigEndian.PutUint64(hBytes, height)
return append([]byte(HeightValsetUpdateIDPrefix), hBytes...)
return append(HeightValsetUpdateIDPrefix(), hBytes...)
}

// Returns the key to a validators' outstanding downtime by consensus address
func OutstandingDowntimeKey(v sdk.ConsAddress) []byte {
return append([]byte(OutstandingDowntimePrefix), address.MustLengthPrefix(v.Bytes())...)
return append(OutstandingDowntimePrefix(), address.MustLengthPrefix(v.Bytes())...)
}

func GetCrossChainValidatorKey(addr []byte) []byte {
return append([]byte(CrossChainValidatorPrefix), addr...)
// Returns the key to a cross chain validator by consensus address
func CrossChainValidatorKey(addr []byte) []byte {
return append(CrossChainValidatorPrefix(), addr...)
}

func GetHistoricalInfoKey(height int64) []byte {
// Returns the key to historical info to a given block height
func HistoricalInfoKey(height int64) []byte {
hBytes := make([]byte, 8)
binary.BigEndian.PutUint64(hBytes, uint64(height))
return append([]byte(HistoricalInfoKey), hBytes...)
return append(HistoricalInfoPrefix(), hBytes...)
}
Loading