Skip to content

Commit 68e54fa

Browse files
mergify[bot]fedekunzejulienrbrt
authored
feat(types): set custom GasConfig on Context for GasKVStore (backport #13826) (#13833)
* feat(types): set custom GasConfig on Context for GasKVStore (#13826) (cherry picked from commit f001b46) # Conflicts: # CHANGELOG.md # types/context.go # types/context_test.go * fix conflicts * updates Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Julien Robert <julien@rbrt.fr>
1 parent 62443b8 commit 68e54fa

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
@@ -39,6 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
3939

4040
### Improvements
4141

42+
* [#13826](https://github.com/cosmos/cosmos-sdk/pull/13826) Support custom `GasConfig` configuration for applications.
4243
* (deps) Bump Tendermint version to [v0.34.23](https://github.com/tendermint/tendermint/releases/tag/v0.34.23).
4344

4445
### Bug Fixes

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 *abci.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 *abci.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/tests/mocks"
1516
"github.com/cosmos/cosmos-sdk/testutil"
1617
"github.com/cosmos/cosmos-sdk/types"
@@ -98,6 +99,7 @@ func (s *contextTestSuite) TestContextWithCustom() {
9899
blockGasMeter := types.NewGasMeter(20000)
99100
minGasPrices := types.DecCoins{types.NewInt64DecCoin("feetoken", 1)}
100101
headerHash := []byte("headerHash")
102+
zeroGasCfg := storetypes.GasConfig{}
101103

102104
ctx = types.NewContext(nil, header, ischeck, logger)
103105
s.Require().Equal(header, ctx.BlockHeader())
@@ -110,7 +112,10 @@ func (s *contextTestSuite) TestContextWithCustom() {
110112
WithGasMeter(meter).
111113
WithMinGasPrices(minGasPrices).
112114
WithBlockGasMeter(blockGasMeter).
113-
WithHeaderHash(headerHash)
115+
WithHeaderHash(headerHash).
116+
WithKVGasConfig(zeroGasCfg).
117+
WithTransientKVGasConfig(zeroGasCfg)
118+
114119
s.Require().Equal(height, ctx.BlockHeight())
115120
s.Require().Equal(chainid, ctx.ChainID())
116121
s.Require().Equal(ischeck, ctx.IsCheckTx())
@@ -122,6 +127,8 @@ func (s *contextTestSuite) TestContextWithCustom() {
122127
s.Require().Equal(blockGasMeter, ctx.BlockGasMeter())
123128
s.Require().Equal(headerHash, ctx.HeaderHash().Bytes())
124129
s.Require().False(ctx.WithIsCheckTx(false).IsCheckTx())
130+
s.Require().Equal(zeroGasCfg, ctx.KVGasConfig())
131+
s.Require().Equal(zeroGasCfg, ctx.TransientKVGasConfig())
125132

126133
// test IsReCheckTx
127134
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
@@ -7,7 +7,6 @@ import (
77

88
"github.com/armon/go-metrics"
99

10-
storetypes "github.com/cosmos/cosmos-sdk/store/types"
1110
"github.com/cosmos/cosmos-sdk/telemetry"
1211
sdk "github.com/cosmos/cosmos-sdk/types"
1312
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
@@ -48,7 +47,7 @@ func (k msgServer) SubmitProposal(goCtx context.Context, msg *v1.MsgSubmitPropos
4847

4948
// ref: https://github.com/cosmos/cosmos-sdk/issues/9683
5049
ctx.GasMeter().ConsumeGas(
51-
3*storetypes.KVGasConfig().WriteCostPerByte*uint64(len(bytes)),
50+
3*ctx.KVGasConfig().WriteCostPerByte*uint64(len(bytes)),
5251
"submit proposal",
5352
)
5453

0 commit comments

Comments
 (0)