From 08d67a0600955cb914dcc47214440924ff4af65e Mon Sep 17 00:00:00 2001 From: testinginprod Date: Wed, 7 Dec 2022 16:32:11 +0100 Subject: [PATCH 01/13] feat: Context.KVStore uses local ms field instead of calling MultiStore --- types/context.go | 6 +++--- types/context_bench_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 types/context_bench_test.go diff --git a/types/context.go b/types/context.go index fac8ed981b0d..638d07998e81 100644 --- a/types/context.go +++ b/types/context.go @@ -278,12 +278,12 @@ func (c Context) Value(key interface{}) interface{} { // KVStore fetches a KVStore from the MultiStore. func (c Context) KVStore(key storetypes.StoreKey) KVStore { - return gaskv.NewStore(c.MultiStore().GetKVStore(key), c.GasMeter(), c.kvGasConfig) + return gaskv.NewStore(c.ms.GetKVStore(key), c.GasMeter(), c.kvGasConfig) } // TransientStore fetches a TransientStore from the MultiStore. func (c Context) TransientStore(key storetypes.StoreKey) KVStore { - return gaskv.NewStore(c.MultiStore().GetKVStore(key), c.GasMeter(), c.transientKVGasConfig) + return gaskv.NewStore(c.ms.GetKVStore(key), c.GasMeter(), c.transientKVGasConfig) } // CacheContext returns a new Context with the multi-store cached and a new @@ -291,7 +291,7 @@ func (c Context) TransientStore(key storetypes.StoreKey) KVStore { // is called. Note, events are automatically emitted on the parent context's // EventManager when the caller executes the write. func (c Context) CacheContext() (cc Context, writeCache func()) { - cms := c.MultiStore().CacheMultiStore() + cms := c.ms.CacheMultiStore() cc = c.WithMultiStore(cms).WithEventManager(NewEventManager()) writeCache = func() { diff --git a/types/context_bench_test.go b/types/context_bench_test.go new file mode 100644 index 000000000000..9e114a0d5716 --- /dev/null +++ b/types/context_bench_test.go @@ -0,0 +1,18 @@ +package types_test + +import ( + "github.com/cosmos/cosmos-sdk/store/types" + "github.com/cosmos/cosmos-sdk/testutil" + "testing" +) + +func BenchmarkContext_KVStore(b *testing.B) { + key := types.NewKVStoreKey(b.Name() + "_TestCacheContext") + + ctx := testutil.DefaultContext(key, types.NewTransientStoreKey("transient_"+b.Name())) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _ = ctx.KVStore(key) + } +} From 6046a501a0fd0f6885cdd3b536c9deae264cd442 Mon Sep 17 00:00:00 2001 From: testinginprod Date: Wed, 7 Dec 2022 17:52:13 +0100 Subject: [PATCH 02/13] feat: sdk.Context KVStore and TransientStore to use pointer receiver --- types/context.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/types/context.go b/types/context.go index 638d07998e81..ed0c79a490f8 100644 --- a/types/context.go +++ b/types/context.go @@ -277,12 +277,14 @@ func (c Context) Value(key interface{}) interface{} { // ---------------------------------------------------------------------------- // KVStore fetches a KVStore from the MultiStore. -func (c Context) KVStore(key storetypes.StoreKey) KVStore { +// NOTE: Uses pointer receiver to save on execution time. +func (c *Context) KVStore(key storetypes.StoreKey) KVStore { return gaskv.NewStore(c.ms.GetKVStore(key), c.GasMeter(), c.kvGasConfig) } // TransientStore fetches a TransientStore from the MultiStore. -func (c Context) TransientStore(key storetypes.StoreKey) KVStore { +// NOTE: Uses pointer receiver to save on execution time. +func (c *Context) TransientStore(key storetypes.StoreKey) KVStore { return gaskv.NewStore(c.ms.GetKVStore(key), c.GasMeter(), c.transientKVGasConfig) } From 68a15230aeca316afdfb0e259bb6bec17ee267f6 Mon Sep 17 00:00:00 2001 From: testinginprod Date: Wed, 7 Dec 2022 18:06:15 +0100 Subject: [PATCH 03/13] feat: sdk.Context KVStore and TransientStore to gasMeter field --- types/context.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/context.go b/types/context.go index ed0c79a490f8..88fdc8020447 100644 --- a/types/context.go +++ b/types/context.go @@ -279,13 +279,13 @@ func (c Context) Value(key interface{}) interface{} { // KVStore fetches a KVStore from the MultiStore. // NOTE: Uses pointer receiver to save on execution time. func (c *Context) KVStore(key storetypes.StoreKey) KVStore { - return gaskv.NewStore(c.ms.GetKVStore(key), c.GasMeter(), c.kvGasConfig) + return gaskv.NewStore(c.ms.GetKVStore(key), c.gasMeter, c.kvGasConfig) } // TransientStore fetches a TransientStore from the MultiStore. // NOTE: Uses pointer receiver to save on execution time. func (c *Context) TransientStore(key storetypes.StoreKey) KVStore { - return gaskv.NewStore(c.ms.GetKVStore(key), c.GasMeter(), c.transientKVGasConfig) + return gaskv.NewStore(c.ms.GetKVStore(key), c.gasMeter, c.transientKVGasConfig) } // CacheContext returns a new Context with the multi-store cached and a new From 940980d311c83df1139c4b1bcddecb209e7699b3 Mon Sep 17 00:00:00 2001 From: testinginprod Date: Wed, 7 Dec 2022 19:04:52 +0100 Subject: [PATCH 04/13] feat: gaskv config as pointer --- baseapp/abci_test.go | 12 ++++++++---- store/gaskv/store.go | 8 ++++---- store/types/gas.go | 8 ++++---- store/types/gas_test.go | 2 +- types/context.go | 15 ++++++++------- x/group/internal/orm/testsupport.go | 3 ++- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index 698b12212f5b..6ac66ecf76ac 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -609,7 +609,8 @@ func TestABCI_CheckTx(t *testing.T) { require.Empty(t, r.GetEvents()) } - checkStateStore := getCheckStateCtx(suite.baseApp).KVStore(capKey1) + ctx := getCheckStateCtx(suite.baseApp) + checkStateStore := ctx.KVStore(capKey1) storedCounter := getIntFromStore(t, checkStateStore, counterKey) // ensure AnteHandler ran @@ -625,7 +626,8 @@ func TestABCI_CheckTx(t *testing.T) { suite.baseApp.EndBlock(abci.RequestEndBlock{}) suite.baseApp.Commit() - checkStateStore = getCheckStateCtx(suite.baseApp).KVStore(capKey1) + stateCtx := getCheckStateCtx(suite.baseApp) + checkStateStore = stateCtx.KVStore(capKey1) storedBytes := checkStateStore.Get(counterKey) require.Nil(t, storedBytes) } @@ -697,7 +699,8 @@ func TestABCI_DeliverTx_MultiMsg(t *testing.T) { res := suite.baseApp.DeliverTx(abci.RequestDeliverTx{Tx: txBytes}) require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) - store := getDeliverStateCtx(suite.baseApp).KVStore(capKey1) + ctx := getDeliverStateCtx(suite.baseApp) + store := ctx.KVStore(capKey1) // tx counter only incremented once txCounter := getIntFromStore(t, store, anteKey) @@ -725,7 +728,8 @@ func TestABCI_DeliverTx_MultiMsg(t *testing.T) { res = suite.baseApp.DeliverTx(abci.RequestDeliverTx{Tx: txBytes}) require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) - store = getDeliverStateCtx(suite.baseApp).KVStore(capKey1) + ctx = getDeliverStateCtx(suite.baseApp) + store = ctx.KVStore(capKey1) // tx counter only incremented once txCounter = getIntFromStore(t, store, anteKey) diff --git a/store/gaskv/store.go b/store/gaskv/store.go index 85bf598d0626..01aa16dafdfc 100644 --- a/store/gaskv/store.go +++ b/store/gaskv/store.go @@ -12,12 +12,12 @@ var _ types.KVStore = &Store{} // KVStore interface. type Store struct { gasMeter types.GasMeter - gasConfig types.GasConfig + gasConfig *types.GasConfig parent types.KVStore } // NewStore returns a reference to a new GasKVStore. -func NewStore(parent types.KVStore, gasMeter types.GasMeter, gasConfig types.GasConfig) *Store { +func NewStore(parent types.KVStore, gasMeter types.GasMeter, gasConfig *types.GasConfig) *Store { kvs := &Store{ gasMeter: gasMeter, gasConfig: gasConfig, @@ -108,11 +108,11 @@ func (gs *Store) iterator(start, end []byte, ascending bool) types.Iterator { type gasIterator struct { gasMeter types.GasMeter - gasConfig types.GasConfig + gasConfig *types.GasConfig parent types.Iterator } -func newGasIterator(gasMeter types.GasMeter, gasConfig types.GasConfig, parent types.Iterator) types.Iterator { +func newGasIterator(gasMeter types.GasMeter, gasConfig *types.GasConfig, parent types.Iterator) types.Iterator { return &gasIterator{ gasMeter: gasMeter, gasConfig: gasConfig, diff --git a/store/types/gas.go b/store/types/gas.go index 84ca542550b7..1dcf4483a434 100644 --- a/store/types/gas.go +++ b/store/types/gas.go @@ -228,8 +228,8 @@ type GasConfig struct { } // KVGasConfig returns a default gas config for KVStores. -func KVGasConfig() GasConfig { - return GasConfig{ +func KVGasConfig() *GasConfig { + return &GasConfig{ HasCost: 1000, DeleteCost: 1000, ReadCostFlat: 1000, @@ -241,8 +241,8 @@ func KVGasConfig() GasConfig { } // TransientGasConfig returns a default gas config for TransientStores. -func TransientGasConfig() GasConfig { - return GasConfig{ +func TransientGasConfig() *GasConfig { + return &GasConfig{ HasCost: 100, DeleteCost: 100, ReadCostFlat: 100, diff --git a/store/types/gas_test.go b/store/types/gas_test.go index f4b5a6abe5ba..11f943396dd1 100644 --- a/store/types/gas_test.go +++ b/store/types/gas_test.go @@ -111,7 +111,7 @@ func TestAddUint64Overflow(t *testing.T) { func TestTransientGasConfig(t *testing.T) { t.Parallel() config := TransientGasConfig() - require.Equal(t, config, GasConfig{ + require.Equal(t, config, &GasConfig{ HasCost: 100, DeleteCost: 100, ReadCostFlat: 100, diff --git a/types/context.go b/types/context.go index 88fdc8020447..c9d6f6fb2991 100644 --- a/types/context.go +++ b/types/context.go @@ -39,8 +39,8 @@ type Context struct { consParams *tmproto.ConsensusParams eventManager *EventManager priority int64 // The tx priority, only relevant in CheckTx - kvGasConfig storetypes.GasConfig - transientKVGasConfig storetypes.GasConfig + kvGasConfig *storetypes.GasConfig + transientKVGasConfig *storetypes.GasConfig } // Proposed rename, not done to avoid API breakage @@ -62,8 +62,8 @@ func (c Context) IsReCheckTx() bool { return c.recheckT func (c Context) MinGasPrices() DecCoins { return c.minGasPrice } func (c Context) EventManager() *EventManager { return c.eventManager } func (c Context) Priority() int64 { return c.priority } -func (c Context) KVGasConfig() storetypes.GasConfig { return c.kvGasConfig } -func (c Context) TransientKVGasConfig() storetypes.GasConfig { return c.transientKVGasConfig } +func (c Context) KVGasConfig() storetypes.GasConfig { return *c.kvGasConfig } +func (c Context) TransientKVGasConfig() storetypes.GasConfig { return *c.transientKVGasConfig } // clone the header before returning func (c Context) BlockHeader() tmproto.Header { @@ -203,14 +203,14 @@ func (c Context) WithBlockGasMeter(meter GasMeter) Context { // WithKVGasConfig returns a Context with an updated gas configuration for // the KVStore func (c Context) WithKVGasConfig(gasConfig storetypes.GasConfig) Context { - c.kvGasConfig = gasConfig + c.kvGasConfig = &gasConfig return c } // WithTransientKVGasConfig returns a Context with an updated gas configuration for // the transient KVStore func (c Context) WithTransientKVGasConfig(gasConfig storetypes.GasConfig) Context { - c.transientKVGasConfig = gasConfig + c.transientKVGasConfig = &gasConfig return c } @@ -279,7 +279,8 @@ func (c Context) Value(key interface{}) interface{} { // KVStore fetches a KVStore from the MultiStore. // NOTE: Uses pointer receiver to save on execution time. func (c *Context) KVStore(key storetypes.StoreKey) KVStore { - return gaskv.NewStore(c.ms.GetKVStore(key), c.gasMeter, c.kvGasConfig) + kv := c.ms.GetKVStore(key) + return gaskv.NewStore(kv, c.gasMeter, c.kvGasConfig) } // TransientStore fetches a TransientStore from the MultiStore. diff --git a/x/group/internal/orm/testsupport.go b/x/group/internal/orm/testsupport.go index 1b4a6847898a..192cc0a197b8 100644 --- a/x/group/internal/orm/testsupport.go +++ b/x/group/internal/orm/testsupport.go @@ -86,7 +86,8 @@ func NewGasCountingMockContext() *GasCountingMockContext { } func (g GasCountingMockContext) KVStore(store sdk.KVStore) sdk.KVStore { - return gaskv.NewStore(store, g.GasMeter, storetypes.KVGasConfig()) + gasConfig := storetypes.KVGasConfig() + return gaskv.NewStore(store, g.GasMeter, gasConfig) } func (g GasCountingMockContext) GasConsumed() storetypes.Gas { From 8a9565fd0aca7909851fc90053e2cb8e25583761 Mon Sep 17 00:00:00 2001 From: testinginprod Date: Thu, 8 Dec 2022 05:09:57 +0100 Subject: [PATCH 05/13] chore: code cleanup --- baseapp/abci_test.go | 4 ++-- x/group/internal/orm/testsupport.go | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index 6ac66ecf76ac..c225cab2db22 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -626,8 +626,8 @@ func TestABCI_CheckTx(t *testing.T) { suite.baseApp.EndBlock(abci.RequestEndBlock{}) suite.baseApp.Commit() - stateCtx := getCheckStateCtx(suite.baseApp) - checkStateStore = stateCtx.KVStore(capKey1) + ctx = getCheckStateCtx(suite.baseApp) + checkStateStore = ctx.KVStore(capKey1) storedBytes := checkStateStore.Get(counterKey) require.Nil(t, storedBytes) } diff --git a/x/group/internal/orm/testsupport.go b/x/group/internal/orm/testsupport.go index 192cc0a197b8..1b4a6847898a 100644 --- a/x/group/internal/orm/testsupport.go +++ b/x/group/internal/orm/testsupport.go @@ -86,8 +86,7 @@ func NewGasCountingMockContext() *GasCountingMockContext { } func (g GasCountingMockContext) KVStore(store sdk.KVStore) sdk.KVStore { - gasConfig := storetypes.KVGasConfig() - return gaskv.NewStore(store, g.GasMeter, gasConfig) + return gaskv.NewStore(store, g.GasMeter, storetypes.KVGasConfig()) } func (g GasCountingMockContext) GasConsumed() storetypes.Gas { From 0b5e022f886f7416116c09ed7a6061b0ed09bddd Mon Sep 17 00:00:00 2001 From: testinginprod Date: Mon, 19 Dec 2022 10:28:06 +0100 Subject: [PATCH 06/13] revert: behavioural changes --- store/gaskv/store.go | 8 ++++---- store/types/gas.go | 8 ++++---- types/context.go | 16 ++++++++-------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/store/gaskv/store.go b/store/gaskv/store.go index 01aa16dafdfc..85bf598d0626 100644 --- a/store/gaskv/store.go +++ b/store/gaskv/store.go @@ -12,12 +12,12 @@ var _ types.KVStore = &Store{} // KVStore interface. type Store struct { gasMeter types.GasMeter - gasConfig *types.GasConfig + gasConfig types.GasConfig parent types.KVStore } // NewStore returns a reference to a new GasKVStore. -func NewStore(parent types.KVStore, gasMeter types.GasMeter, gasConfig *types.GasConfig) *Store { +func NewStore(parent types.KVStore, gasMeter types.GasMeter, gasConfig types.GasConfig) *Store { kvs := &Store{ gasMeter: gasMeter, gasConfig: gasConfig, @@ -108,11 +108,11 @@ func (gs *Store) iterator(start, end []byte, ascending bool) types.Iterator { type gasIterator struct { gasMeter types.GasMeter - gasConfig *types.GasConfig + gasConfig types.GasConfig parent types.Iterator } -func newGasIterator(gasMeter types.GasMeter, gasConfig *types.GasConfig, parent types.Iterator) types.Iterator { +func newGasIterator(gasMeter types.GasMeter, gasConfig types.GasConfig, parent types.Iterator) types.Iterator { return &gasIterator{ gasMeter: gasMeter, gasConfig: gasConfig, diff --git a/store/types/gas.go b/store/types/gas.go index 1dcf4483a434..84ca542550b7 100644 --- a/store/types/gas.go +++ b/store/types/gas.go @@ -228,8 +228,8 @@ type GasConfig struct { } // KVGasConfig returns a default gas config for KVStores. -func KVGasConfig() *GasConfig { - return &GasConfig{ +func KVGasConfig() GasConfig { + return GasConfig{ HasCost: 1000, DeleteCost: 1000, ReadCostFlat: 1000, @@ -241,8 +241,8 @@ func KVGasConfig() *GasConfig { } // TransientGasConfig returns a default gas config for TransientStores. -func TransientGasConfig() *GasConfig { - return &GasConfig{ +func TransientGasConfig() GasConfig { + return GasConfig{ HasCost: 100, DeleteCost: 100, ReadCostFlat: 100, diff --git a/types/context.go b/types/context.go index c9d6f6fb2991..a21972c7ebdd 100644 --- a/types/context.go +++ b/types/context.go @@ -39,8 +39,8 @@ type Context struct { consParams *tmproto.ConsensusParams eventManager *EventManager priority int64 // The tx priority, only relevant in CheckTx - kvGasConfig *storetypes.GasConfig - transientKVGasConfig *storetypes.GasConfig + kvGasConfig storetypes.GasConfig + transientKVGasConfig storetypes.GasConfig } // Proposed rename, not done to avoid API breakage @@ -62,8 +62,8 @@ func (c Context) IsReCheckTx() bool { return c.recheckT func (c Context) MinGasPrices() DecCoins { return c.minGasPrice } func (c Context) EventManager() *EventManager { return c.eventManager } func (c Context) Priority() int64 { return c.priority } -func (c Context) KVGasConfig() storetypes.GasConfig { return *c.kvGasConfig } -func (c Context) TransientKVGasConfig() storetypes.GasConfig { return *c.transientKVGasConfig } +func (c Context) KVGasConfig() storetypes.GasConfig { return c.kvGasConfig } +func (c Context) TransientKVGasConfig() storetypes.GasConfig { return c.transientKVGasConfig } // clone the header before returning func (c Context) BlockHeader() tmproto.Header { @@ -203,14 +203,14 @@ func (c Context) WithBlockGasMeter(meter GasMeter) Context { // WithKVGasConfig returns a Context with an updated gas configuration for // the KVStore func (c Context) WithKVGasConfig(gasConfig storetypes.GasConfig) Context { - c.kvGasConfig = &gasConfig + c.kvGasConfig = gasConfig return c } // WithTransientKVGasConfig returns a Context with an updated gas configuration for // the transient KVStore func (c Context) WithTransientKVGasConfig(gasConfig storetypes.GasConfig) Context { - c.transientKVGasConfig = &gasConfig + c.transientKVGasConfig = gasConfig return c } @@ -278,14 +278,14 @@ func (c Context) Value(key interface{}) interface{} { // KVStore fetches a KVStore from the MultiStore. // NOTE: Uses pointer receiver to save on execution time. -func (c *Context) KVStore(key storetypes.StoreKey) KVStore { +func (c Context) KVStore(key storetypes.StoreKey) KVStore { kv := c.ms.GetKVStore(key) return gaskv.NewStore(kv, c.gasMeter, c.kvGasConfig) } // TransientStore fetches a TransientStore from the MultiStore. // NOTE: Uses pointer receiver to save on execution time. -func (c *Context) TransientStore(key storetypes.StoreKey) KVStore { +func (c Context) TransientStore(key storetypes.StoreKey) KVStore { return gaskv.NewStore(c.ms.GetKVStore(key), c.gasMeter, c.transientKVGasConfig) } From f1fd7fb19997f43c4b72235f4559aa6d3dc01b9d Mon Sep 17 00:00:00 2001 From: testinginprod Date: Mon, 19 Dec 2022 10:31:24 +0100 Subject: [PATCH 07/13] revert: docs --- types/context.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/types/context.go b/types/context.go index a21972c7ebdd..8c015c362e03 100644 --- a/types/context.go +++ b/types/context.go @@ -277,14 +277,12 @@ func (c Context) Value(key interface{}) interface{} { // ---------------------------------------------------------------------------- // KVStore fetches a KVStore from the MultiStore. -// NOTE: Uses pointer receiver to save on execution time. func (c Context) KVStore(key storetypes.StoreKey) KVStore { kv := c.ms.GetKVStore(key) return gaskv.NewStore(kv, c.gasMeter, c.kvGasConfig) } // TransientStore fetches a TransientStore from the MultiStore. -// NOTE: Uses pointer receiver to save on execution time. func (c Context) TransientStore(key storetypes.StoreKey) KVStore { return gaskv.NewStore(c.ms.GetKVStore(key), c.gasMeter, c.transientKVGasConfig) } From 0ae513f4aabb6dc45d7185a53c70f80e826434ed Mon Sep 17 00:00:00 2001 From: testinginprod Date: Mon, 19 Dec 2022 10:34:39 +0100 Subject: [PATCH 08/13] revert: tests --- baseapp/abci_test.go | 12 ++++-------- store/types/gas_test.go | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index c225cab2db22..698b12212f5b 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -609,8 +609,7 @@ func TestABCI_CheckTx(t *testing.T) { require.Empty(t, r.GetEvents()) } - ctx := getCheckStateCtx(suite.baseApp) - checkStateStore := ctx.KVStore(capKey1) + checkStateStore := getCheckStateCtx(suite.baseApp).KVStore(capKey1) storedCounter := getIntFromStore(t, checkStateStore, counterKey) // ensure AnteHandler ran @@ -626,8 +625,7 @@ func TestABCI_CheckTx(t *testing.T) { suite.baseApp.EndBlock(abci.RequestEndBlock{}) suite.baseApp.Commit() - ctx = getCheckStateCtx(suite.baseApp) - checkStateStore = ctx.KVStore(capKey1) + checkStateStore = getCheckStateCtx(suite.baseApp).KVStore(capKey1) storedBytes := checkStateStore.Get(counterKey) require.Nil(t, storedBytes) } @@ -699,8 +697,7 @@ func TestABCI_DeliverTx_MultiMsg(t *testing.T) { res := suite.baseApp.DeliverTx(abci.RequestDeliverTx{Tx: txBytes}) require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) - ctx := getDeliverStateCtx(suite.baseApp) - store := ctx.KVStore(capKey1) + store := getDeliverStateCtx(suite.baseApp).KVStore(capKey1) // tx counter only incremented once txCounter := getIntFromStore(t, store, anteKey) @@ -728,8 +725,7 @@ func TestABCI_DeliverTx_MultiMsg(t *testing.T) { res = suite.baseApp.DeliverTx(abci.RequestDeliverTx{Tx: txBytes}) require.True(t, res.IsOK(), fmt.Sprintf("%v", res)) - ctx = getDeliverStateCtx(suite.baseApp) - store = ctx.KVStore(capKey1) + store = getDeliverStateCtx(suite.baseApp).KVStore(capKey1) // tx counter only incremented once txCounter = getIntFromStore(t, store, anteKey) diff --git a/store/types/gas_test.go b/store/types/gas_test.go index 11f943396dd1..f4b5a6abe5ba 100644 --- a/store/types/gas_test.go +++ b/store/types/gas_test.go @@ -111,7 +111,7 @@ func TestAddUint64Overflow(t *testing.T) { func TestTransientGasConfig(t *testing.T) { t.Parallel() config := TransientGasConfig() - require.Equal(t, config, &GasConfig{ + require.Equal(t, config, GasConfig{ HasCost: 100, DeleteCost: 100, ReadCostFlat: 100, From cd7c27fbcba0ed38d8f19db225b0071b8debeb50 Mon Sep 17 00:00:00 2001 From: testinginprod Date: Mon, 19 Dec 2022 10:37:02 +0100 Subject: [PATCH 09/13] add: transient tests --- types/context_bench_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/types/context_bench_test.go b/types/context_bench_test.go index 9e114a0d5716..6d836a6c0b03 100644 --- a/types/context_bench_test.go +++ b/types/context_bench_test.go @@ -16,3 +16,14 @@ func BenchmarkContext_KVStore(b *testing.B) { _ = ctx.KVStore(key) } } + +func BenchmarkContext_TransientStore(b *testing.B) { + key := types.NewKVStoreKey(b.Name() + "_TestCacheContext") + + ctx := testutil.DefaultContext(key, types.NewTransientStoreKey("transient_"+b.Name())) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _ = ctx.TransientStore(key) + } +} From d2ffbb453e2ea32c74280e9e097a42d4b4e8ed2c Mon Sep 17 00:00:00 2001 From: testinginprod Date: Mon, 19 Dec 2022 10:39:14 +0100 Subject: [PATCH 10/13] revert: oneline instantiation --- types/context.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/types/context.go b/types/context.go index 8c015c362e03..6483304fed85 100644 --- a/types/context.go +++ b/types/context.go @@ -278,8 +278,7 @@ func (c Context) Value(key interface{}) interface{} { // KVStore fetches a KVStore from the MultiStore. func (c Context) KVStore(key storetypes.StoreKey) KVStore { - kv := c.ms.GetKVStore(key) - return gaskv.NewStore(kv, c.gasMeter, c.kvGasConfig) + return gaskv.NewStore(c.ms.GetKVStore(key), c.gasMeter, c.kvGasConfig) } // TransientStore fetches a TransientStore from the MultiStore. From 6f094351e5fde5fea59ff97fa785b3a847dd6ec3 Mon Sep 17 00:00:00 2001 From: testinginprod Date: Mon, 19 Dec 2022 11:06:35 +0100 Subject: [PATCH 11/13] add: cache context bench --- types/context_bench_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/types/context_bench_test.go b/types/context_bench_test.go index 6d836a6c0b03..374b235de48a 100644 --- a/types/context_bench_test.go +++ b/types/context_bench_test.go @@ -27,3 +27,14 @@ func BenchmarkContext_TransientStore(b *testing.B) { _ = ctx.TransientStore(key) } } + +func BenchmarkContext_CacheContext(b *testing.B) { + key := types.NewKVStoreKey(b.Name() + "_TestCacheContext") + + ctx := testutil.DefaultContext(key, types.NewTransientStoreKey("transient_"+b.Name())) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, _ = ctx.CacheContext() + } +} From 2052646541852db1f0cfb4fdea1758bb1b8665bf Mon Sep 17 00:00:00 2001 From: testinginprod Date: Mon, 19 Dec 2022 16:45:13 +0100 Subject: [PATCH 12/13] chore: lint --- types/context_bench_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/context_bench_test.go b/types/context_bench_test.go index 374b235de48a..f316d3daaed1 100644 --- a/types/context_bench_test.go +++ b/types/context_bench_test.go @@ -1,9 +1,10 @@ package types_test import ( + "testing" + "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/testutil" - "testing" ) func BenchmarkContext_KVStore(b *testing.B) { From 6f89bcd427f94da89d4f8faf720d04ae1a5cbfaa Mon Sep 17 00:00:00 2001 From: testinginprod <98415576+testinginprod@users.noreply.github.com> Date: Mon, 19 Dec 2022 18:36:02 +0100 Subject: [PATCH 13/13] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50e391a371e0..e550e231ba88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,7 +58,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#13473](https://github.com/cosmos/cosmos-sdk/pull/13473) ADR-038: Go plugin system proposal ### Improvements - +* (types) [#14354](https://github.com/cosmos/cosmos-sdk/pull/14354) - improve performance on Context.KVStore and Context.TransientStore by 40% * (crypto/keyring) [#14151](https://github.com/cosmos/cosmos-sdk/pull/14151) Move keys presentation from `crypto/keyring` to `client/keys` * (types) [#14163](https://github.com/cosmos/cosmos-sdk/pull/14163) Refactor `(coins Coins) Validate()` to avoid unnecessary map. * (signing) [#14087](https://github.com/cosmos/cosmos-sdk/pull/14087) Add SignModeHandlerWithContext interface with a new `GetSignBytesWithContext` to get the sign bytes using `context.Context` as an argument to access state.