Skip to content

Commit

Permalink
feat: configure params in gno.land
Browse files Browse the repository at this point in the history
Signed-off-by: moul <[email protected]>
  • Loading branch information
moul committed Oct 11, 2024
1 parent 602245d commit 783a044
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 18 deletions.
70 changes: 70 additions & 0 deletions gno.land/cmd/gnoland/testdata/params.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# test for https://github.com/gnolang/gno/pull/2920

gnoland start

# query before adding the package
gnokey query params/vm/gno.land/r/sys/setter.foo.string
stdout 'data: $'
gnokey query params/vm/gno.land/r/sys/setter.bar.bool
stdout 'data: $'
gnokey query params/vm/gno.land/r/sys/setter.baz.int64
stdout 'data: $'

gnokey maketx addpkg -pkgdir $WORK/setter -pkgpath gno.land/r/sys/setter -gas-fee 1000000ugnot -gas-wanted 100000000 -broadcast -chainid=tendermint_test test1

# query after adding the package, but before setting values
gnokey query params/vm/gno.land/r/sys/setter.foo.string
stdout 'data: $'
gnokey query params/vm/gno.land/r/sys/setter.bar.bool
stdout 'data: $'
gnokey query params/vm/gno.land/r/sys/setter.baz.int64
stdout 'data: $'


# set foo (string)
gnokey maketx call -pkgpath gno.land/r/sys/setter -func SetFoo -args foo1 -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1
gnokey query params/vm/gno.land/r/sys/setter.foo.string
stdout 'data: "foo1"'

# override foo
gnokey maketx call -pkgpath gno.land/r/sys/setter -func SetFoo -args foo2 -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1
gnokey query params/vm/gno.land/r/sys/setter.foo.string
stdout 'data: "foo2"'


# set bar (bool)
gnokey maketx call -pkgpath gno.land/r/sys/setter -func SetBar -args true -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1
gnokey query params/vm/gno.land/r/sys/setter.bar.bool
stdout 'data: true'

# override bar
gnokey maketx call -pkgpath gno.land/r/sys/setter -func SetBar -args false -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1
gnokey query params/vm/gno.land/r/sys/setter.bar.bool
stdout 'data: false'


# set baz (bool)
gnokey maketx call -pkgpath gno.land/r/sys/setter -func SetBaz -args 1337 -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1
gnokey query params/vm/gno.land/r/sys/setter.baz.int64
stdout 'data: "1337"'

# override baz
gnokey maketx call -pkgpath gno.land/r/sys/setter -func SetBaz -args 31337 -gas-fee 1000000ugnot -gas-wanted 10000000 -broadcast -chainid=tendermint_test test1
gnokey query params/vm/gno.land/r/sys/setter.baz.int64
stdout 'data: "31337"'




# XXX: create a non-sys package that should fail?

-- setter/setter.gno --
package setter

import (
"std"
)

func SetFoo(newFoo string) { std.SetConfig("foo", newFoo) }
func SetBar(newBar bool) { std.SetConfig("bar", newBar) }
func SetBaz(newBaz int64) { std.SetConfig("baz", newBaz) }
16 changes: 10 additions & 6 deletions gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/sdk/auth"
"github.com/gnolang/gno/tm2/pkg/sdk/bank"
"github.com/gnolang/gno/tm2/pkg/sdk/params"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store"
"github.com/gnolang/gno/tm2/pkg/store/dbadapter"
Expand Down Expand Up @@ -88,12 +89,13 @@ func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) {
// Construct keepers.
acctKpr := auth.NewAccountKeeper(mainKey, ProtoGnoAccount)
bankKpr := bank.NewBankKeeper(acctKpr)
vmk := vm.NewVMKeeper(baseKey, mainKey, acctKpr, bankKpr, cfg.MaxCycles)
paramsKpr := params.NewParamsKeeper(mainKey, "vm")
vmk := vm.NewVMKeeper(baseKey, mainKey, acctKpr, bankKpr, paramsKpr, cfg.MaxCycles)

// Set InitChainer
icc := cfg.InitChainerConfig
icc.baseApp = baseApp
icc.acctKpr, icc.bankKpr, icc.vmKpr = acctKpr, bankKpr, vmk
icc.acctKpr, icc.bankKpr, icc.vmKpr, icc.paramsKpr = acctKpr, bankKpr, vmk, paramsKpr
baseApp.SetInitChainer(icc.InitChainer)

// Set AnteHandler
Expand Down Expand Up @@ -148,6 +150,7 @@ func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) {
// Set a handler Route.
baseApp.Router().AddRoute("auth", auth.NewHandler(acctKpr))
baseApp.Router().AddRoute("bank", bank.NewHandler(bankKpr))
baseApp.Router().AddRoute("params", params.NewHandler(paramsKpr))
baseApp.Router().AddRoute("vm", vm.NewHandler(vmk))

// Load latest version.
Expand Down Expand Up @@ -225,10 +228,11 @@ type InitChainerConfig struct {

// These fields are passed directly by NewAppWithOptions, and should not be
// configurable by end-users.
baseApp *sdk.BaseApp
vmKpr vm.VMKeeperI
acctKpr auth.AccountKeeperI
bankKpr bank.BankKeeperI
baseApp *sdk.BaseApp
vmKpr vm.VMKeeperI
acctKpr auth.AccountKeeperI
bankKpr bank.BankKeeperI
paramsKpr params.ParamsKeeperI
}

// InitChainer is the function that can be used as a [sdk.InitChainer].
Expand Down
13 changes: 9 additions & 4 deletions gno.land/pkg/sdk/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,33 @@ func NewSDKParams(vmk *VMKeeper, ctx sdk.Context) *SDKParams {
// - set the value.

func (prm *SDKParams) SetString(key, value string) {
if !prm.vmk.prmk.Has(prm.ctx, key) {
// if !prm.vmk.prmk.Has(prm.ctx, key) {
// XXX: bad workaround, maybe we should have a dedicated "dynamic keeper" allowing to create keys on the go?
if !prm.vmk.prmk.HasTypeKey(key) {
prm.vmk.prmk.RegisterType(params.NewParamSetPair(key, "", validateNoOp))
}
prm.vmk.prmk.Set(prm.ctx, key, value)
}

func (prm *SDKParams) SetBool(key string, value bool) {

Check warning on line 88 in gno.land/pkg/sdk/vm/builtins.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/builtins.go#L88

Added line #L88 was not covered by tests
if !prm.vmk.prmk.Has(prm.ctx, key) {
// if !prm.vmk.prmk.Has(prm.ctx, key) {
if !prm.vmk.prmk.HasTypeKey(key) {
prm.vmk.prmk.RegisterType(params.NewParamSetPair(key, true, validateNoOp))

Check warning on line 91 in gno.land/pkg/sdk/vm/builtins.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/builtins.go#L90-L91

Added lines #L90 - L91 were not covered by tests
}
prm.vmk.prmk.Set(prm.ctx, key, value)

Check warning on line 93 in gno.land/pkg/sdk/vm/builtins.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/builtins.go#L93

Added line #L93 was not covered by tests
}

func (prm *SDKParams) SetInt64(key string, value int64) {
if !prm.vmk.prmk.Has(prm.ctx, key) {
// if !prm.vmk.prmk.Has(prm.ctx, key) {
if !prm.vmk.prmk.HasTypeKey(key) {
prm.vmk.prmk.RegisterType(params.NewParamSetPair(key, int64(0), validateNoOp))
}
prm.vmk.prmk.Set(prm.ctx, key, value)
}

func (prm *SDKParams) SetUint64(key string, value uint64) {

Check warning on line 104 in gno.land/pkg/sdk/vm/builtins.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/builtins.go#L104

Added line #L104 was not covered by tests
if !prm.vmk.prmk.Has(prm.ctx, key) {
// if !prm.vmk.prmk.Has(prm.ctx, key) {
if !prm.vmk.prmk.HasTypeKey(key) {
prm.vmk.prmk.RegisterType(params.NewParamSetPair(key, uint64(0), validateNoOp))

Check warning on line 107 in gno.land/pkg/sdk/vm/builtins.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/builtins.go#L106-L107

Added lines #L106 - L107 were not covered by tests
}
prm.vmk.prmk.Set(prm.ctx, key, value)

Check warning on line 109 in gno.land/pkg/sdk/vm/builtins.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/builtins.go#L109

Added line #L109 was not covered by tests
Expand Down
1 change: 0 additions & 1 deletion gnovm/stdlibs/std/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type ParamsInterface interface {
SetInt64(key string, val int64)
SetUint64(key string, val uint64)
// XXX: GetString(key string) (string, error)?

}

func X_setConfigString(m *gno.Machine, key, val string) {
Expand Down
11 changes: 5 additions & 6 deletions tm2/pkg/sdk/params/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ func (bh paramsHandler) Process(ctx sdk.Context, msg std.Msg) sdk.Result {
//----------------------------------------
// Query

const QueryParams = "params"

func (bh paramsHandler) Query(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
switch secondPart(req.Path) {
case QueryParams:
case bh.params.prefix:
return bh.queryParam(ctx, req)
default:
res = sdk.ABCIResponseQueryFromError(
Expand All @@ -49,10 +47,11 @@ func (bh paramsHandler) queryParam(ctx sdk.Context, req abci.RequestQuery) (res
std.ErrUnknownRequest("param key is empty"))

Check warning on line 47 in tm2/pkg/sdk/params/handler.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/sdk/params/handler.go#L44-L47

Added lines #L44 - L47 were not covered by tests
}

// XXX: validate
// XXX: validate?

panic("not implemented")
val := bh.params.GetRaw(ctx, key)

Check warning on line 52 in tm2/pkg/sdk/params/handler.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/sdk/params/handler.go#L52

Added line #L52 was not covered by tests

res.Data = val
return

Check warning on line 55 in tm2/pkg/sdk/params/handler.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/sdk/params/handler.go#L54-L55

Added lines #L54 - L55 were not covered by tests
}

Expand All @@ -79,5 +78,5 @@ func thirdPartWithSlashes(path string) string {
if secondSlash == -1 {
return "" // Return original if less than two slashes

Check warning on line 79 in tm2/pkg/sdk/params/handler.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/sdk/params/handler.go#L76-L79

Added lines #L76 - L79 were not covered by tests
}
return path[strings.Index(path, "/")+secondSlash+1:]
return path[strings.Index(path, "/")+secondSlash+2:]

Check warning on line 81 in tm2/pkg/sdk/params/handler.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/sdk/params/handler.go#L81

Added line #L81 was not covered by tests
}
13 changes: 13 additions & 0 deletions tm2/pkg/sdk/params/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import (
"github.com/gnolang/gno/tm2/pkg/store"
)

type ParamsKeeperI interface {
Get(ctx sdk.Context, key string, ptr interface{})
Set(ctx sdk.Context, key string, value interface{})
}

var _ ParamsKeeperI = ParamsKeeper{}

// global paramstore Keeper.
type ParamsKeeper struct {
key store.StoreKey
Expand Down Expand Up @@ -145,10 +152,16 @@ func (pk ParamsKeeper) WithKeyTable(table KeyTable) ParamsKeeper {
return pk
}

// XXX: added, should we remove?
func (pk ParamsKeeper) RegisterType(psp ParamSetPair) {
pk.table.RegisterType(psp)

Check warning on line 157 in tm2/pkg/sdk/params/keeper.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/sdk/params/keeper.go#L156-L157

Added lines #L156 - L157 were not covered by tests
}

// XXX: added, should we remove?
func (pk ParamsKeeper) HasTypeKey(key string) bool {
return pk.table.HasKey(key)

Check warning on line 162 in tm2/pkg/sdk/params/keeper.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/sdk/params/keeper.go#L161-L162

Added lines #L161 - L162 were not covered by tests
}

// XXX: GetAllKeys
// XXX: GetAllParams
// XXX: ViewKeeper
Expand Down
7 changes: 6 additions & 1 deletion tm2/pkg/sdk/params/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (t KeyTable) RegisterType(psp ParamSetPair) KeyTable {
}

if _, ok := t.m[psp.Key]; ok {
panic("duplicate parameter key")
panic("duplicate parameter key: " + psp.Key)

Check warning on line 46 in tm2/pkg/sdk/params/table.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/sdk/params/table.go#L46

Added line #L46 was not covered by tests
}

rty := reflect.TypeOf(psp.Value)
Expand All @@ -69,4 +69,9 @@ func (t KeyTable) RegisterParamSet(ps ParamSet) KeyTable {
return t

Check warning on line 69 in tm2/pkg/sdk/params/table.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/sdk/params/table.go#L69

Added line #L69 was not covered by tests
}

func (t KeyTable) HasKey(key string) bool {
_, ok := t.m[key]
return ok

Check warning on line 74 in tm2/pkg/sdk/params/table.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/sdk/params/table.go#L72-L74

Added lines #L72 - L74 were not covered by tests
}

var isAlphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString

Check failure on line 77 in tm2/pkg/sdk/params/table.go

View workflow job for this annotation

GitHub Actions / Run Main / Go Linter / lint

var `isAlphaNumeric` is unused (unused)

0 comments on commit 783a044

Please sign in to comment.