Skip to content
Open
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
19 changes: 7 additions & 12 deletions gno.land/pkg/sdk/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,32 @@ func NewSDKParams(pmk ParamsKeeperI, ctx sdk.Context) *SDKParams {

// The key has the format <module>:(<realm>:)?<paramname>.
func (prm *SDKParams) SetString(key string, value string) {
prm.willSetKeeperParams(prm.ctx, key, value)
prm.mustHaveModuleKeeper(key)
prm.pmk.SetString(prm.ctx, key, value)
}

func (prm *SDKParams) SetBool(key string, value bool) {
prm.willSetKeeperParams(prm.ctx, key, value)
prm.mustHaveModuleKeeper(key)
prm.pmk.SetBool(prm.ctx, key, value)
}

func (prm *SDKParams) SetInt64(key string, value int64) {
prm.willSetKeeperParams(prm.ctx, key, value)
prm.mustHaveModuleKeeper(key)
prm.pmk.SetInt64(prm.ctx, key, value)
}

func (prm *SDKParams) SetUint64(key string, value uint64) {
prm.willSetKeeperParams(prm.ctx, key, value)
prm.mustHaveModuleKeeper(key)
prm.pmk.SetUint64(prm.ctx, key, value)
}

func (prm *SDKParams) SetBytes(key string, value []byte) {
prm.willSetKeeperParams(prm.ctx, key, value)
prm.mustHaveModuleKeeper(key)
prm.pmk.SetBytes(prm.ctx, key, value)
}

func (prm *SDKParams) SetStrings(key string, value []string) {
prm.willSetKeeperParams(prm.ctx, key, value)
prm.mustHaveModuleKeeper(key)
prm.pmk.SetStrings(prm.ctx, key, value)
}

Expand Down Expand Up @@ -147,7 +147,7 @@ func (prm *SDKParams) UpdateStrings(key string, vals []string, add bool) {
prm.SetStrings(key, updatedList)
}

func (prm *SDKParams) willSetKeeperParams(ctx sdk.Context, key string, value any) {
func (prm *SDKParams) mustHaveModuleKeeper(key string) {
parts := strings.Split(key, ":")
if len(parts) == 0 {
panic(fmt.Sprintf("SDKParams encountered invalid param key format: %s", key))
Expand All @@ -156,9 +156,4 @@ func (prm *SDKParams) willSetKeeperParams(ctx sdk.Context, key string, value any
if !prm.pmk.IsRegistered(mname) {
panic(fmt.Sprintf("module name <%s> not registered", mname))
}
kpr := prm.pmk.GetRegisteredKeeper(mname)
if kpr != nil {
subkey := key[len(mname)+1:]
kpr.WillSetParam(prm.ctx, subkey, value)
}
Comment on lines -159 to -163
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate with what happens in tm2/pkg/sdk/params/keeper.go

}
60 changes: 59 additions & 1 deletion gno.land/pkg/sdk/vm/builtins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestParamsKeeper(t *testing.T) {
func TestParamsKeeperFail(t *testing.T) {
env := setupTestEnv()
params := NewSDKParams(env.vmk.prmk, env.ctx)

Expand Down Expand Up @@ -65,3 +65,61 @@ func TestParamsKeeper(t *testing.T) {
})
}
}

func TestParamsKeeperSuccess(t *testing.T) {
env := setupTestEnv()
params := NewSDKParams(env.vmk.prmk, env.ctx)

testCases := []struct {
name string
testFunc func()
}{
{
name: "string test module vm",
testFunc: func() {
params.SetString("vm:p", "foo")

var actual string
params.pmk.GetString(env.ctx, "vm:p", &actual)
require.Equal(t, "foo", actual)
},
},
{
name: "int64 test module vm",
testFunc: func() {
params.SetInt64("vm:p", int64(1))

var actual int64
params.pmk.GetInt64(env.ctx, "vm:p", &actual)
require.Equal(t, int64(1), actual)
},
},
{
name: "string test module auth",
testFunc: func() {
params.SetString("auth:p1", "foo")

var actual string
params.pmk.GetString(env.ctx, "auth:p1", &actual)
require.Equal(t, "foo", actual)
},
},

{
name: "string test module bank",
testFunc: func() {
params.SetString("bank:p1", "foo")

var actual string
params.pmk.GetString(env.ctx, "auth:p1", &actual)
require.Equal(t, "foo", actual)
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.testFunc()
})
}
}
1 change: 0 additions & 1 deletion gno.land/pkg/sdk/vm/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type ParamsKeeperI interface {
params.ParamsKeeperI

IsRegistered(moduleName string) bool
GetRegisteredKeeper(moduleName string) params.ParamfulKeeper
}

// Public facing function signatures.
Expand Down
13 changes: 6 additions & 7 deletions tm2/pkg/sdk/params/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,9 @@ func (pk ParamsKeeper) ForModule(moduleName string) prefixParamsKeeper {
return ppk
}

func (pk ParamsKeeper) GetRegisteredKeeper(moduleName string) ParamfulKeeper {
func (pk ParamsKeeper) GetRegisteredKeeper(moduleName string) (ParamfulKeeper, bool) {
rk, ok := pk.kprs[moduleName]
if !ok {
panic("keeper for module " + moduleName + " not registered")
}
return rk
return rk, ok
}

func (pk ParamsKeeper) Register(moduleName string, pmk ParamfulKeeper) {
Expand Down Expand Up @@ -240,12 +237,14 @@ func (pk ParamsKeeper) getIfExists(ctx sdk.Context, key string, ptr any) {

func (pk ParamsKeeper) set(ctx sdk.Context, key string, value any) {
module, rawKey := parsePrefix(key)

if module != "" {
kpr := pk.GetRegisteredKeeper(module)
if kpr != nil {
kpr, ok := pk.GetRegisteredKeeper(module)
if ok {
kpr.WillSetParam(ctx, rawKey, value)
}
}

stor := ctx.Store(pk.key)
bz := amino.MustMarshalJSON(value)
stor.Set(storeKey(key), bz)
Expand Down
Loading