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: dynamic gas price, keeper implementation #2838

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion contribs/gnodev/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gnolang/gno/contribs/gnodev

go 1.22
go 1.22.0

replace github.com/gnolang/gno => ../..

Expand Down
23 changes: 9 additions & 14 deletions contribs/gnodev/pkg/dev/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,9 @@ func NewDevNode(ctx context.Context, cfg *NodeConfig) (*Node, error) {
initialState: cfg.InitialTxs,
currentStateIndex: len(cfg.InitialTxs),
}

// generate genesis state
genesis := gnoland.GnoGenesisState{
Balances: cfg.BalancesList,
Txs: append(pkgsTxs, cfg.InitialTxs...),
}
genesis := gnoland.DefaultGenState()
genesis.Balances = cfg.BalancesList
genesis.Txs = append(pkgsTxs, cfg.InitialTxs...)

if err := devnode.rebuildNode(ctx, genesis); err != nil {
return nil, fmt.Errorf("unable to initialize the node: %w", err)
Expand Down Expand Up @@ -270,10 +267,9 @@ func (n *Node) Reset(ctx context.Context) error {

// Append initialTxs
txs := append(pkgsTxs, n.initialState...)
genesis := gnoland.GnoGenesisState{
Balances: n.config.BalancesList,
Txs: txs,
}
genesis := gnoland.DefaultGenState()
genesis.Balances = n.config.BalancesList
genesis.Txs = txs

// Reset the node with the new genesis state.
err = n.rebuildNode(ctx, genesis)
Expand Down Expand Up @@ -410,10 +406,9 @@ func (n *Node) rebuildNodeFromState(ctx context.Context) error {
}

// Create genesis with loaded pkgs + previous state
genesis := gnoland.GnoGenesisState{
Balances: n.config.BalancesList,
Txs: append(pkgsTxs, state...),
}
genesis := gnoland.DefaultGenState()
genesis.Balances = n.config.BalancesList
genesis.Txs = append(pkgsTxs, state...)

// Reset the node with the new genesis state.
err = n.rebuildNode(ctx, genesis)
Expand Down
16 changes: 8 additions & 8 deletions contribs/gnodev/pkg/dev/node_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ func (n *Node) MoveBy(ctx context.Context, x int) error {
newState := n.state[:newIndex]

// Create genesis with loaded pkgs + previous state
genesis := gnoland.GnoGenesisState{
Balances: n.config.BalancesList,
Txs: append(pkgsTxs, newState...),
}
genesis := gnoland.DefaultGenState()
genesis.Balances = n.config.BalancesList
genesis.Txs = append(pkgsTxs, newState...)

// Reset the node with the new genesis state.
if err = n.rebuildNode(ctx, genesis); err != nil {
Expand Down Expand Up @@ -133,10 +132,11 @@ func (n *Node) ExportStateAsGenesis(ctx context.Context) (*bft.GenesisDoc, error

// Get current blockstore state
doc := *n.Node.GenesisDoc() // copy doc
doc.AppState = gnoland.GnoGenesisState{
Balances: n.config.BalancesList,
Txs: state,
}

genState := doc.AppState.(gnoland.GnoGenesisState)
genState.Balances = n.config.BalancesList
genState.Txs = state
doc.AppState = genState

return &doc, nil
}
8 changes: 4 additions & 4 deletions gno.land/cmd/gnoland/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,10 @@ func generateGenesisFile(genesisFile string, pk crypto.PubKey, c *startCfg) erro
genesisTxs = append(pkgsTxs, genesisTxs...)

// Construct genesis AppState.
gen.AppState = gnoland.GnoGenesisState{
Balances: balances,
Txs: genesisTxs,
}
defaultGenState := gnoland.DefaultGenState()
defaultGenState.Balances = balances
defaultGenState.Txs = genesisTxs
gen.AppState = defaultGenState

// Write genesis state
if err := gen.SaveAs(genesisFile); err != nil {
Expand Down
18 changes: 9 additions & 9 deletions gno.land/pkg/gnoclient/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestCallSingle_Integration(t *testing.T) {

// Make Tx config
baseCfg := BaseTxCfg{
GasFee: ugnot.ValueString(10000),
GasFee: ugnot.ValueString(800000),
GasWanted: 8000000,
AccountNumber: 0,
SequenceNumber: 0,
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestCallMultiple_Integration(t *testing.T) {

// Make Tx config
baseCfg := BaseTxCfg{
GasFee: ugnot.ValueString(10000),
GasFee: ugnot.ValueString(800000),
GasWanted: 8000000,
AccountNumber: 0,
SequenceNumber: 0,
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestSendSingle_Integration(t *testing.T) {

// Make Tx config
baseCfg := BaseTxCfg{
GasFee: ugnot.ValueString(10000),
GasFee: ugnot.ValueString(800000),
GasWanted: 8000000,
AccountNumber: 0,
SequenceNumber: 0,
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestSendMultiple_Integration(t *testing.T) {

// Make Tx config
baseCfg := BaseTxCfg{
GasFee: ugnot.ValueString(10000),
GasFee: ugnot.ValueString(800000),
GasWanted: 8000000,
AccountNumber: 0,
SequenceNumber: 0,
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestRunSingle_Integration(t *testing.T) {

// Make Tx config
baseCfg := BaseTxCfg{
GasFee: ugnot.ValueString(10000),
GasFee: ugnot.ValueString(800000),
GasWanted: 8000000,
AccountNumber: 0,
SequenceNumber: 0,
Expand Down Expand Up @@ -358,7 +358,7 @@ func TestRunMultiple_Integration(t *testing.T) {

// Make Tx config
baseCfg := BaseTxCfg{
GasFee: ugnot.ValueString(10000),
GasFee: ugnot.ValueString(800000),
GasWanted: 8000000,
AccountNumber: 0,
SequenceNumber: 0,
Expand Down Expand Up @@ -451,7 +451,7 @@ func TestAddPackageSingle_Integration(t *testing.T) {

// Make Tx config
baseCfg := BaseTxCfg{
GasFee: ugnot.ValueString(10000),
GasFee: ugnot.ValueString(800000),
GasWanted: 8000000,
AccountNumber: 0,
SequenceNumber: 0,
Expand Down Expand Up @@ -536,7 +536,7 @@ func TestAddPackageMultiple_Integration(t *testing.T) {

// Make Tx config
baseCfg := BaseTxCfg{
GasFee: ugnot.ValueString(10000),
GasFee: ugnot.ValueString(800000),
GasWanted: 8000000,
AccountNumber: 0,
SequenceNumber: 0,
Expand All @@ -556,7 +556,7 @@ func Echo(str string) string {
body2 := `package hello

func Hello(str string) string {
return "Hello " + str + "!"
return "Hello " + str + "!"
}`

caller, err := client.Signer.Info()
Expand Down
34 changes: 30 additions & 4 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 @@ -86,14 +87,16 @@ func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) {
baseApp.MountStoreWithDB(baseKey, dbadapter.StoreConstructor, cfg.DB)

// Construct keepers.
acctKpr := auth.NewAccountKeeper(mainKey, ProtoGnoAccount)
paramsKpr := params.NewKeeper(mainKey, nil)
acctKpr := auth.NewAccountKeeper(mainKey, paramsKpr, ProtoGnoAccount)
gpKpr := auth.NewGasPriceKeeper(mainKey)
bankKpr := bank.NewBankKeeper(acctKpr)
vmk := vm.NewVMKeeper(baseKey, mainKey, acctKpr, bankKpr, 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.gpKpr = acctKpr, bankKpr, vmk, gpKpr
baseApp.SetInitChainer(icc.InitChainer)

// Set AnteHandler
Expand All @@ -107,9 +110,11 @@ func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) {
func(ctx sdk.Context, tx std.Tx, simulate bool) (
newCtx sdk.Context, res sdk.Result, abort bool,
) {
// Add last gas price in the context
ctx = ctx.WithValue(auth.GasPriceContextKey{}, gpKpr.LastGasPrice(ctx))
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a refactor is necessary generally, for this and other key-value pairs being put in the std.Context instance, but it seems like data that is always necessary should be part of the Context struct's definition. This approach still makes sense for storing key-value pairs for module data that is not required.

Copy link
Contributor Author

@piux2 piux2 Oct 11, 2024

Choose a reason for hiding this comment

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

this approach still makes sense for storing key-value pairs for module data that is not required.

Good point. We can address the refactor in a separate PR later


// Override auth params.
ctx = ctx.
WithValue(auth.AuthParamsContextKey{}, auth.DefaultParams())
ctx = ctx.WithValue(auth.AuthParamsContextKey{}, acctKpr.GetParams(ctx))
// Continue on with default auth ante handler.
newCtx, res, abort = authAnteHandler(ctx, tx, simulate)
return
Expand Down Expand Up @@ -140,6 +145,8 @@ func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) {
baseApp.SetEndBlocker(
EndBlocker(
c,
acctKpr,
gpKpr,
vmk,
baseApp,
),
Expand Down Expand Up @@ -229,6 +236,7 @@ type InitChainerConfig struct {
vmKpr vm.VMKeeperI
acctKpr auth.AccountKeeperI
bankKpr bank.BankKeeperI
gpKpr auth.GasPriceKeeperI
}

// InitChainer is the function that can be used as a [sdk.InitChainer].
Expand Down Expand Up @@ -286,6 +294,10 @@ func (cfg InitChainerConfig) loadAppState(ctx sdk.Context, appState any) ([]abci
if !ok {
return nil, fmt.Errorf("invalid AppState of type %T", appState)
}
cfg.acctKpr.InitGenesis(ctx, state.Auth)
params := cfg.acctKpr.GetParams(ctx)
ctx = ctx.WithValue(auth.AuthParamsContextKey{}, params)
auth.InitChainer(ctx, cfg.gpKpr.(auth.GasPriceKeeper), params.InitialGasPrice)

// Parse and set genesis state balances
for _, bal := range state.Balances {
Expand Down Expand Up @@ -335,19 +347,33 @@ type endBlockerApp interface {
// validator set changes
func EndBlocker(
collector *collector[validatorUpdate],
acctKpr auth.AccountKeeperI,
gpKpr auth.GasPriceKeeperI,
vmk vm.VMKeeperI,
app endBlockerApp,
) func(
ctx sdk.Context,
req abci.RequestEndBlock,
) abci.ResponseEndBlock {
return func(ctx sdk.Context, _ abci.RequestEndBlock) abci.ResponseEndBlock {
// set the auth params value in the ctx. The EndBlocker will use InitialGasPrice in
// the params to calculate the updated gas price.
if acctKpr != nil {
ctx = ctx.WithValue(auth.AuthParamsContextKey{}, acctKpr.GetParams(ctx))
}
if acctKpr != nil && gpKpr != nil {
auth.EndBlocker(ctx, gpKpr)
}
// Check if there was a valset change
if len(collector.getEvents()) == 0 {
// No valset updates
return abci.ResponseEndBlock{}
}

if vmk == nil {
return abci.ResponseEndBlock{}
}

// Run the VM to get the updates from the chain
response, err := vmk.QueryEval(
ctx,
Expand Down
Loading