Skip to content

Commit f001b46

Browse files
authored
feat(types): set custom GasConfig on Context for GasKVStore (cosmos#13826)
1 parent d32d896 commit f001b46

File tree

4 files changed

+72
-45
lines changed

4 files changed

+72
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5858

5959
### Improvements
6060

61+
* [#13826](https://github.com/cosmos/cosmos-sdk/pull/13826) Support custom `GasConfig` configuration for applications.
6162
* [#13619](https://github.com/cosmos/cosmos-sdk/pull/13619) Add new function called LogDeferred to report errors in defers. Use the function in x/bank files.
6263
* (tools) [#13603](https://github.com/cosmos/cosmos-sdk/pull/13603) Rename cosmovisor package name to `cosmossdk.io/tools/cosmovisor`. The new tool directory contains Cosmos SDK tools.
6364
* (deps) [#13397](https://github.com/cosmos/cosmos-sdk/pull/13397) Bump Go version minimum requirement to `1.19`.

types/context.go

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,47 @@ but please do not over-use it. We try to keep all data structured
2323
and standard additions here would be better just to add to the Context struct
2424
*/
2525
type Context struct {
26-
baseCtx context.Context
27-
ms MultiStore
28-
header tmproto.Header
29-
headerHash tmbytes.HexBytes
30-
chainID string
31-
txBytes []byte
32-
logger log.Logger
33-
voteInfo []abci.VoteInfo
34-
gasMeter GasMeter
35-
blockGasMeter GasMeter
36-
checkTx bool
37-
recheckTx bool // if recheckTx == true, then checkTx must also be true
38-
minGasPrice DecCoins
39-
consParams *tmproto.ConsensusParams
40-
eventManager *EventManager
41-
priority int64 // The tx priority, only relevant in CheckTx
26+
baseCtx context.Context
27+
ms MultiStore
28+
header tmproto.Header
29+
headerHash tmbytes.HexBytes
30+
chainID string
31+
txBytes []byte
32+
logger log.Logger
33+
voteInfo []abci.VoteInfo
34+
gasMeter GasMeter
35+
blockGasMeter GasMeter
36+
checkTx bool
37+
recheckTx bool // if recheckTx == true, then checkTx must also be true
38+
minGasPrice DecCoins
39+
consParams *tmproto.ConsensusParams
40+
eventManager *EventManager
41+
priority int64 // The tx priority, only relevant in CheckTx
42+
kvGasConfig storetypes.GasConfig
43+
transientKVGasConfig storetypes.GasConfig
4244
}
4345

4446
// Proposed rename, not done to avoid API breakage
4547
type Request = Context
4648

4749
// Read-only accessors
48-
func (c Context) Context() context.Context { return c.baseCtx }
49-
func (c Context) MultiStore() MultiStore { return c.ms }
50-
func (c Context) BlockHeight() int64 { return c.header.Height }
51-
func (c Context) BlockTime() time.Time { return c.header.Time }
52-
func (c Context) ChainID() string { return c.chainID }
53-
func (c Context) TxBytes() []byte { return c.txBytes }
54-
func (c Context) Logger() log.Logger { return c.logger }
55-
func (c Context) VoteInfos() []abci.VoteInfo { return c.voteInfo }
56-
func (c Context) GasMeter() GasMeter { return c.gasMeter }
57-
func (c Context) BlockGasMeter() GasMeter { return c.blockGasMeter }
58-
func (c Context) IsCheckTx() bool { return c.checkTx }
59-
func (c Context) IsReCheckTx() bool { return c.recheckTx }
60-
func (c Context) MinGasPrices() DecCoins { return c.minGasPrice }
61-
func (c Context) EventManager() *EventManager { return c.eventManager }
62-
func (c Context) Priority() int64 { return c.priority }
50+
func (c Context) Context() context.Context { return c.baseCtx }
51+
func (c Context) MultiStore() MultiStore { return c.ms }
52+
func (c Context) BlockHeight() int64 { return c.header.Height }
53+
func (c Context) BlockTime() time.Time { return c.header.Time }
54+
func (c Context) ChainID() string { return c.chainID }
55+
func (c Context) TxBytes() []byte { return c.txBytes }
56+
func (c Context) Logger() log.Logger { return c.logger }
57+
func (c Context) VoteInfos() []abci.VoteInfo { return c.voteInfo }
58+
func (c Context) GasMeter() GasMeter { return c.gasMeter }
59+
func (c Context) BlockGasMeter() GasMeter { return c.blockGasMeter }
60+
func (c Context) IsCheckTx() bool { return c.checkTx }
61+
func (c Context) IsReCheckTx() bool { return c.recheckTx }
62+
func (c Context) MinGasPrices() DecCoins { return c.minGasPrice }
63+
func (c Context) EventManager() *EventManager { return c.eventManager }
64+
func (c Context) Priority() int64 { return c.priority }
65+
func (c Context) KVGasConfig() storetypes.GasConfig { return c.kvGasConfig }
66+
func (c Context) TransientKVGasConfig() storetypes.GasConfig { return c.transientKVGasConfig }
6367

6468
// clone the header before returning
6569
func (c Context) BlockHeader() tmproto.Header {
@@ -95,15 +99,17 @@ func NewContext(ms MultiStore, header tmproto.Header, isCheckTx bool, logger log
9599
// https://github.com/gogo/protobuf/issues/519
96100
header.Time = header.Time.UTC()
97101
return Context{
98-
baseCtx: context.Background(),
99-
ms: ms,
100-
header: header,
101-
chainID: header.ChainID,
102-
checkTx: isCheckTx,
103-
logger: logger,
104-
gasMeter: storetypes.NewInfiniteGasMeter(),
105-
minGasPrice: DecCoins{},
106-
eventManager: NewEventManager(),
102+
baseCtx: context.Background(),
103+
ms: ms,
104+
header: header,
105+
chainID: header.ChainID,
106+
checkTx: isCheckTx,
107+
logger: logger,
108+
gasMeter: storetypes.NewInfiniteGasMeter(),
109+
minGasPrice: DecCoins{},
110+
eventManager: NewEventManager(),
111+
kvGasConfig: storetypes.KVGasConfig(),
112+
transientKVGasConfig: storetypes.TransientGasConfig(),
107113
}
108114
}
109115

@@ -194,6 +200,20 @@ func (c Context) WithBlockGasMeter(meter GasMeter) Context {
194200
return c
195201
}
196202

203+
// WithKVGasConfig returns a Context with an updated gas configuration for
204+
// the KVStore
205+
func (c Context) WithKVGasConfig(gasConfig storetypes.GasConfig) Context {
206+
c.kvGasConfig = gasConfig
207+
return c
208+
}
209+
210+
// WithTransientKVGasConfig returns a Context with an updated gas configuration for
211+
// the transient KVStore
212+
func (c Context) WithTransientKVGasConfig(gasConfig storetypes.GasConfig) Context {
213+
c.transientKVGasConfig = gasConfig
214+
return c
215+
}
216+
197217
// WithIsCheckTx enables or disables CheckTx value for verifying transactions and returns an updated Context
198218
func (c Context) WithIsCheckTx(isCheckTx bool) Context {
199219
c.checkTx = isCheckTx
@@ -258,12 +278,12 @@ func (c Context) Value(key interface{}) interface{} {
258278

259279
// KVStore fetches a KVStore from the MultiStore.
260280
func (c Context) KVStore(key storetypes.StoreKey) KVStore {
261-
return gaskv.NewStore(c.MultiStore().GetKVStore(key), c.GasMeter(), storetypes.KVGasConfig())
281+
return gaskv.NewStore(c.MultiStore().GetKVStore(key), c.GasMeter(), c.kvGasConfig)
262282
}
263283

264284
// TransientStore fetches a TransientStore from the MultiStore.
265285
func (c Context) TransientStore(key storetypes.StoreKey) KVStore {
266-
return gaskv.NewStore(c.MultiStore().GetKVStore(key), c.GasMeter(), storetypes.TransientGasConfig())
286+
return gaskv.NewStore(c.MultiStore().GetKVStore(key), c.GasMeter(), c.transientKVGasConfig)
267287
}
268288

269289
// CacheContext returns a new Context with the multi-store cached and a new

types/context_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
1212

1313
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
14+
storetypes "github.com/cosmos/cosmos-sdk/store/types"
1415
"github.com/cosmos/cosmos-sdk/testutil"
1516
"github.com/cosmos/cosmos-sdk/testutil/mock"
1617
"github.com/cosmos/cosmos-sdk/types"
@@ -92,6 +93,7 @@ func (s *contextTestSuite) TestContextWithCustom() {
9293
blockGasMeter := types.NewGasMeter(20000)
9394
minGasPrices := types.DecCoins{types.NewInt64DecCoin("feetoken", 1)}
9495
headerHash := []byte("headerHash")
96+
zeroGasCfg := storetypes.GasConfig{}
9597

9698
ctx = types.NewContext(nil, header, ischeck, logger)
9799
s.Require().Equal(header, ctx.BlockHeader())
@@ -104,7 +106,10 @@ func (s *contextTestSuite) TestContextWithCustom() {
104106
WithGasMeter(meter).
105107
WithMinGasPrices(minGasPrices).
106108
WithBlockGasMeter(blockGasMeter).
107-
WithHeaderHash(headerHash)
109+
WithHeaderHash(headerHash).
110+
WithKVGasConfig(zeroGasCfg).
111+
WithTransientKVGasConfig(zeroGasCfg)
112+
108113
s.Require().Equal(height, ctx.BlockHeight())
109114
s.Require().Equal(chainid, ctx.ChainID())
110115
s.Require().Equal(ischeck, ctx.IsCheckTx())
@@ -116,6 +121,8 @@ func (s *contextTestSuite) TestContextWithCustom() {
116121
s.Require().Equal(blockGasMeter, ctx.BlockGasMeter())
117122
s.Require().Equal(headerHash, ctx.HeaderHash().Bytes())
118123
s.Require().False(ctx.WithIsCheckTx(false).IsCheckTx())
124+
s.Require().Equal(zeroGasCfg, ctx.KVGasConfig())
125+
s.Require().Equal(zeroGasCfg, ctx.TransientKVGasConfig())
119126

120127
// test IsReCheckTx
121128
s.Require().False(ctx.IsReCheckTx())

x/gov/keeper/msg_server.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"cosmossdk.io/errors"
99
"github.com/armon/go-metrics"
1010

11-
storetypes "github.com/cosmos/cosmos-sdk/store/types"
1211
"github.com/cosmos/cosmos-sdk/telemetry"
1312
sdk "github.com/cosmos/cosmos-sdk/types"
1413
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
@@ -53,7 +52,7 @@ func (k msgServer) SubmitProposal(goCtx context.Context, msg *v1.MsgSubmitPropos
5352

5453
// ref: https://github.com/cosmos/cosmos-sdk/issues/9683
5554
ctx.GasMeter().ConsumeGas(
56-
3*storetypes.KVGasConfig().WriteCostPerByte*uint64(len(bytes)),
55+
3*ctx.KVGasConfig().WriteCostPerByte*uint64(len(bytes)),
5756
"submit proposal",
5857
)
5958

0 commit comments

Comments
 (0)