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 95ab0bd
Show file tree
Hide file tree
Showing 6 changed files with 114 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) {
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))
}
prm.vmk.prmk.Set(prm.ctx, key, value)
}

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) {
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))
}
prm.vmk.prmk.Set(prm.ctx, key, value)
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"))
}

// XXX: validate
// XXX: validate?

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

res.Data = val
return
}

Expand All @@ -79,5 +78,5 @@ func thirdPartWithSlashes(path string) string {
if secondSlash == -1 {
return "" // Return original if less than two slashes
}
return path[strings.Index(path, "/")+secondSlash+1:]
return path[strings.Index(path, "/")+secondSlash+2:]
}
15 changes: 14 additions & 1 deletion 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
}

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

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

// 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)
}

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

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

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 95ab0bd

Please sign in to comment.