Skip to content

Commit

Permalink
fix: validator vrf key prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
jim380 authored and hacheigriega committed Apr 24, 2024
1 parent d45890e commit 3867f59
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
27 changes: 23 additions & 4 deletions x/randomness/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"

"github.com/sedaprotocol/seda-chain/x/randomness/types"
)

var SeedPrefix = collections.NewPrefix(0)
var ValidatorVRFPrefix = collections.NewPrefix(1)

// GetValidatorVRFKeyFull gets the key for the validator VRF object.
func GetValidatorVRFKeyFull(consensusAddr sdk.ConsAddress) []byte {
return append(ValidatorVRFPrefix, address.MustLengthPrefix(consensusAddr)...)
}

type Keeper struct {
Schema collections.Schema
Seeds collections.Item[string]
Expand All @@ -25,8 +34,8 @@ func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService) *K
sb := collections.NewSchemaBuilder(storeService)

return &Keeper{
Seeds: collections.NewItem(sb, types.SeedPrefix, "seeds", collections.StringValue),
ValidatorVRFPubKeys: collections.NewMap(sb, types.ValidatorVRFPrefix, "validator-vrf-pubkeys", collections.StringKey, codec.CollInterfaceValue[cryptotypes.PubKey](cdc)),
Seeds: collections.NewItem(sb, SeedPrefix, "seeds", collections.StringValue),
ValidatorVRFPubKeys: collections.NewMap(sb, ValidatorVRFPrefix, "validator-vrf-pubkeys", collections.StringKey, codec.CollInterfaceValue[cryptotypes.PubKey](cdc)),
}
}

Expand All @@ -48,7 +57,12 @@ func (k Keeper) SetSeed(ctx sdk.Context, seed string) error {
// GetValidatorVRFPubKey retrieves from the store the VRF public key
// corresponding to the given validator consensus address.
func (k Keeper) GetValidatorVRFPubKey(ctx sdk.Context, consensusAddr string) (cryptotypes.PubKey, error) {
vrfPubKey, err := k.ValidatorVRFPubKeys.Get(ctx, consensusAddr)
addr, err := sdk.ConsAddressFromBech32(consensusAddr)
if err != nil {
return nil, err
}
validatorVRFKeyPrefixFull := GetValidatorVRFKeyFull(addr)
vrfPubKey, err := k.ValidatorVRFPubKeys.Get(ctx, string(validatorVRFKeyPrefixFull))
if err != nil {
return nil, err
}
Expand All @@ -57,7 +71,12 @@ func (k Keeper) GetValidatorVRFPubKey(ctx sdk.Context, consensusAddr string) (cr
}

func (k Keeper) SetValidatorVRFPubKey(goCtx context.Context, consensusAddr string, vrfPubKey cryptotypes.PubKey) error {
return k.ValidatorVRFPubKeys.Set(goCtx, consensusAddr, vrfPubKey)
addr, err := sdk.ConsAddressFromBech32(consensusAddr)
if err != nil {
return err
}
validatorVRFKeyPrefixFull := GetValidatorVRFKeyFull(addr)
return k.ValidatorVRFPubKeys.Set(goCtx, string(validatorVRFKeyPrefixFull), vrfPubKey)
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
Expand Down
5 changes: 0 additions & 5 deletions x/randomness/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package types

import "cosmossdk.io/collections"

const (
// ModuleName defines the module name
ModuleName = "randomness"
Expand All @@ -12,6 +10,3 @@ const (
// RouterKey defines the module's message routing key
RouterKey = ModuleName
)

var SeedPrefix = collections.NewPrefix(0)
var ValidatorVRFPrefix = collections.NewPrefix(1)

0 comments on commit 3867f59

Please sign in to comment.