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(tm2): add sdk/params module #2920

Merged
merged 31 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b930513
feat(tm2/sdk/params): initial version
moul Oct 7, 2024
602245d
feat: inject ParamsKeeper in VMKeeper
moul Oct 9, 2024
783a044
feat: configure params in gno.land
moul Oct 10, 2024
072aa12
Merge branch 'master' into dev/moul/mod-params
moul Oct 20, 2024
89e046c
chore: remove unused param
moul Oct 21, 2024
5d1e36a
chore: disable --flag-config-path
moul Oct 21, 2024
e5a869d
Merge branch 'master' into dev/moul/mod-params
moul Oct 21, 2024
581ef26
chore: fixup
moul Oct 21, 2024
a39d172
chore: fixup
moul Oct 21, 2024
0342b61
chore: fixup
moul Oct 21, 2024
dd8ca08
chore: remove vmkeeper.maxcycles
moul Oct 21, 2024
e573b6a
chore: fixup
moul Oct 21, 2024
993d213
chore: fixup
moul Oct 21, 2024
ee8c778
Merge branch 'dev/moul/remove-max-cycles' into dev/moul/mod-params
moul Oct 21, 2024
95384ae
chore: fixup
moul Oct 22, 2024
a4608eb
chore: fixup
moul Oct 22, 2024
9f18bad
chore: rename config -> param
moul Oct 22, 2024
5ba066e
chore: fixup
moul Oct 22, 2024
4e097fc
Merge remote-tracking branch 'origin/master' into dev/moul/mod-params
moul Oct 22, 2024
5260059
chore: ignore generated files from codecov
moul Oct 22, 2024
309806f
chore: fixup
moul Oct 22, 2024
911c27f
Merge branch 'master' into dev/moul/mod-params
moul Oct 22, 2024
94cf1a4
Merge remote-tracking branch 'origin/master' into dev/moul/mod-params
moul Oct 22, 2024
604466c
chore: fixup
moul Oct 22, 2024
8c5b00a
chore: fixup
moul Oct 22, 2024
4a1c0bb
chore: fixup
moul Oct 22, 2024
86b4fbd
chore: fixup
moul Oct 22, 2024
902eb9c
chore: fixup
moul Oct 23, 2024
d38c6b8
chore: fixup
moul Oct 23, 2024
dc37fe0
chore: fixup
moul Oct 23, 2024
060a3be
chore: fixup
moul Oct 23, 2024
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
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) }
17 changes: 10 additions & 7 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 All @@ -34,7 +35,6 @@ type AppOptions struct {
DB dbm.DB // required
Logger *slog.Logger // required
EventSwitch events.EventSwitch // required
MaxCycles int64 // hard limit for cycles in GnoVM
InitChainerConfig // options related to InitChainer
}

Expand Down Expand Up @@ -88,12 +88,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)

// 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 +149,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 +227,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
1 change: 0 additions & 1 deletion gno.land/pkg/gnoland/node_inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ func NewInMemoryNode(logger *slog.Logger, cfg *InMemoryNodeConfig) (*node.Node,
// Initialize the application with the provided options
gnoApp, err := NewAppWithOptions(&AppOptions{
Logger: logger,
MaxCycles: cfg.GenesisMaxVMCycles,
DB: cfg.DB,
EventSwitch: evsw,
InitChainerConfig: cfg.InitChainerConfig,
Expand Down
File renamed without changes.
55 changes: 55 additions & 0 deletions gno.land/pkg/sdk/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/sdk/params"
"github.com/gnolang/gno/tm2/pkg/std"
)

Expand Down Expand Up @@ -55,3 +56,57 @@
panic(err)
}
}

// ----------------------------------------
// SDKParams

type SDKParams struct {
vmk *VMKeeper
ctx sdk.Context
}

func NewSDKParams(vmk *VMKeeper, ctx sdk.Context) *SDKParams {
return &SDKParams{
vmk: vmk,
ctx: ctx,
}
}

// SetXXX helpers:
// - dynamically register a new key with the corresponding type in the paramset table (only once).
// - set the value.

func (prm *SDKParams) SetString(key, value string) {
// 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.HasTypeKey(key) {
prm.vmk.prmk.RegisterType(params.NewParamSetPair(key, true, validateNoOp))
}
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#L88-L93

Added lines #L88 - L93 were not covered by tests
}

func (prm *SDKParams) SetInt64(key string, value int64) {
// 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.HasTypeKey(key) {
prm.vmk.prmk.RegisterType(params.NewParamSetPair(key, uint64(0), validateNoOp))
}
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#L104-L109

Added lines #L104 - L109 were not covered by tests
}

func validateNoOp(_ interface{}) error { return nil }

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L112 was not covered by tests
4 changes: 3 additions & 1 deletion gno.land/pkg/sdk/vm/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gnolang/gno/tm2/pkg/sdk"
authm "github.com/gnolang/gno/tm2/pkg/sdk/auth"
bankm "github.com/gnolang/gno/tm2/pkg/sdk/bank"
paramsm "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 @@ -47,7 +48,8 @@ func _setupTestEnv(cacheStdlibs bool) testEnv {
ctx := sdk.NewContext(sdk.RunTxModeDeliver, ms, &bft.Header{ChainID: "test-chain-id"}, log.NewNoopLogger())
acck := authm.NewAccountKeeper(iavlCapKey, std.ProtoBaseAccount)
bank := bankm.NewBankKeeper(acck)
vmk := NewVMKeeper(baseCapKey, iavlCapKey, acck, bank, 100_000_000)
prmk := paramsm.NewParamsKeeper(iavlCapKey, "params")
vmk := NewVMKeeper(baseCapKey, iavlCapKey, acck, bank, prmk)

mcw := ms.MultiCacheWrap()
vmk.Initialize(log.NewNoopLogger(), mcw)
Expand Down
2 changes: 1 addition & 1 deletion gno.land/pkg/sdk/vm/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestAddPkgDeliverTx(t *testing.T) {
assert.True(t, res.IsOK())

// NOTE: let's try to keep this bellow 100_000 :)
assert.Equal(t, int64(92825), gasDeliver)
assert.Equal(t, int64(93825), gasDeliver)
}

// Enough gas for a failed transaction.
Expand Down
Loading
Loading