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

feat: add a global params store module #2710

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions tm2/pkg/sdk/params/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package params

import (
"log/slog"

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/store"
)

const (
ModuleName = "params"

StoreKey = "params"

// ValueStorePrevfix is "/pv/" for param value.
ValueStoreKeyPrefix = "/pv/"
)

func ValueStoreKey(key string) []byte {
return append([]byte(ValueStoreKeyPrefix), []byte(key)...)
}

// Keeper of the global param store.
type Keeper struct {
key store.StoreKey
keyMapper KeyMapper
}

// NewKeeper constructs a params keeper
func NewKeeper(key store.StoreKey, keyMapper KeyMapper) Keeper {
return Keeper{
key: key,
keyMapper: keyMapper,
}
}

// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) *slog.Logger {
return ctx.Logger().With("module", ModuleName)
}

// GetParam gets a param value from the global param store.
func (k Keeper) GetParam(ctx sdk.Context, key string, target interface{}) (bool, error) {
stor := ctx.Store(k.key)
if k.keyMapper != nil {
key = k.keyMapper.Map(key)
}

bz := stor.Get(ValueStoreKey(key))
if bz == nil {
return false, nil
}

return true, amino.Unmarshal(bz, target)
}

// SetParam sets a param value to the global param store.
func (k Keeper) SetParam(ctx sdk.Context, key string, param interface{}) error {
bz, err := amino.Marshal(param)
if err != nil {
return err
}

stor := ctx.Store(k.key)
if k.keyMapper != nil {
key = k.keyMapper.Map(key)
}

stor.Set(ValueStoreKey(key), bz)
return nil
}
8 changes: 8 additions & 0 deletions tm2/pkg/sdk/params/keymapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package params

// KeyMapper is used to map one key string to another.
type KeyMapper interface {
// Map does a transformation on an input key to produce the key
// appropriate for accessing a param keeper's storage instance.
Map(key string) string
}
Loading