Skip to content

Commit

Permalink
chore: migrate x/randonmess to collections
Browse files Browse the repository at this point in the history
  • Loading branch information
jim380 authored and hacheigriega committed Apr 24, 2024
1 parent 69b403f commit d45890e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 55 deletions.
7 changes: 6 additions & 1 deletion x/randomness/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, data types.GenesisState) {

// ExportGenesis extracts data from store to genesis state.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
seed, err := k.GetSeed(ctx)
if err != nil {
return types.GenesisState{}
}

return types.GenesisState{
Seed: k.GetSeed(ctx),
Seed: seed,
}
}
10 changes: 8 additions & 2 deletions x/randomness/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func (h *ProposalHandler) PrepareProposalHandler(

// Seed transaction
// alpha = (seed_{i-1} || timestamp)
prevSeed := keeper.GetSeed(ctx)
prevSeed, err := keeper.GetSeed(ctx)
if err != nil {
return nil, err
}
if prevSeed == "" {
return nil, fmt.Errorf("previous seed is empty - this should never happen")
}
Expand Down Expand Up @@ -161,7 +164,10 @@ func (h *ProposalHandler) ProcessProposalHandler(
return nil, err
}

prevSeed := keeper.GetSeed(ctx)
prevSeed, err := keeper.GetSeed(ctx)
if err != nil {
return nil, err
}
if prevSeed == "" {
panic("seed should never be empty")
}
Expand Down
58 changes: 21 additions & 37 deletions x/randomness/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"fmt"

"cosmossdk.io/collections"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand All @@ -15,65 +16,48 @@ import (
)

type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
Schema collections.Schema
Seeds collections.Item[string]
ValidatorVRFPubKeys collections.Map[string, cryptotypes.PubKey]
}

func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey) *Keeper {
func NewKeeper(cdc codec.BinaryCodec, storeService storetypes.KVStoreService) *Keeper {
sb := collections.NewSchemaBuilder(storeService)

return &Keeper{
cdc: cdc,
storeKey: storeKey,
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)),
}
}

// GetSeed returns the seed.
func (k Keeper) GetSeed(ctx sdk.Context) string {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyPrefixSeed)
return string(bz)
func (k Keeper) GetSeed(ctx sdk.Context) (string, error) {
seed, err := k.Seeds.Get(ctx)
if err != nil {
return "", err
}

return seed, nil
}

// SetSeed stores the seed.
func (k Keeper) SetSeed(ctx sdk.Context, seed string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyPrefixSeed, []byte(seed))
func (k Keeper) SetSeed(ctx sdk.Context, seed string) error {
return k.Seeds.Set(ctx, seed)
}

// 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) {
addr, err := sdk.ConsAddressFromBech32(consensusAddr)
vrfPubKey, err := k.ValidatorVRFPubKeys.Get(ctx, consensusAddr)
if err != nil {
return nil, err
}

store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetValidatorVRFKey(addr))
if bz == nil {
return nil, fmt.Errorf("vrf pubkey not found for %s", consensusAddr)
}

var vrfPubKey cryptotypes.PubKey
err = k.cdc.UnmarshalInterface(bz, &vrfPubKey)
if err != nil {
return nil, err
}
return vrfPubKey, nil
}

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

store := sdk.UnwrapSDKContext(goCtx).KVStore(k.storeKey)
bz, err := k.cdc.MarshalInterface(vrfPubKey)
if err != nil {
return err
}
store.Set(types.GetValidatorVRFKey(addr), bz)
return nil
return k.ValidatorVRFPubKeys.Set(goCtx, consensusAddr, vrfPubKey)
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
Expand Down
7 changes: 6 additions & 1 deletion x/randomness/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ func NewQuerierImpl(keeper Keeper) *Querier {

func (q Querier) Seed(c context.Context, _ *types.QuerySeedRequest) (*types.QuerySeedResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
seed, err := q.GetSeed(ctx)
if err != nil {
return nil, err
}

return &types.QuerySeedResponse{
Seed: q.GetSeed(ctx),
Seed: seed,
BlockHeight: ctx.BlockHeight(),
}, nil
}
17 changes: 3 additions & 14 deletions x/randomness/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)
import "cosmossdk.io/collections"

const (
// ModuleName defines the module name
Expand All @@ -16,13 +13,5 @@ const (
RouterKey = ModuleName
)

// KeyPrefixSeed defines prefix to store the current block's seed.
var KeyPrefixSeed = []byte{0x00}

// KeyPrefixValidatorVRF defines prefix to store the validator VRF object.
var KeyPrefixValidatorVRF = []byte{0x01}

// GetValidatorVRFKey gets the key for the validator VRF object.
func GetValidatorVRFKey(consensusAddr sdk.ConsAddress) []byte {
return append(KeyPrefixValidatorVRF, address.MustLengthPrefix(consensusAddr)...)
}
var SeedPrefix = collections.NewPrefix(0)
var ValidatorVRFPrefix = collections.NewPrefix(1)

0 comments on commit d45890e

Please sign in to comment.