Skip to content

Commit 36c4da5

Browse files
authored
Move memstore hydration initialized flag to memstore (#2681)
1 parent b2674f5 commit 36c4da5

File tree

7 files changed

+62
-47
lines changed

7 files changed

+62
-47
lines changed

protocol/mocks/ClobKeeper.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protocol/x/clob/ante/clob.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (cd ClobDecorator) AnteHandle(
6565
}
6666

6767
// Disable order placement and cancelation processing if the clob keeper is not initialized.
68-
if !cd.clobKeeper.IsInitialized() {
68+
if !cd.clobKeeper.IsInMemStructuresInitialized() {
6969
return ctx, errorsmod.Wrap(
7070
types.ErrClobNotInitialized,
7171
"clob keeper is not initialized. Please wait for the next block.",

protocol/x/clob/ante/clob_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func runTestCase(t *testing.T, tc TestCase) {
5353
// Setup AnteHandler.
5454
mockClobKeeper := &mocks.ClobKeeper{}
5555
mockClobKeeper.On("Logger", mock.Anything).Return(log.NewNopLogger()).Maybe()
56-
mockClobKeeper.On("IsInitialized").Return(true).Maybe()
56+
mockClobKeeper.On("IsInMemStructuresInitialized").Return(true).Maybe()
5757
cd := ante.NewClobDecorator(mockClobKeeper)
5858
antehandler := sdk.ChainAnteDecorators(cd)
5959
if tc.setupMocks != nil {

protocol/x/clob/keeper/keeper.go

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package keeper
22

33
import (
4-
"errors"
54
"fmt"
65
"math/big"
76
"sync/atomic"
@@ -54,8 +53,7 @@ type (
5453
streamingManager streamingtypes.FullNodeStreamingManager
5554
finalizeBlockEventStager finalizeblock.EventStager[*types.ClobStagedFinalizeBlockEvent]
5655

57-
initialized *atomic.Bool
58-
memStoreInitialized *atomic.Bool
56+
inMemStructuresInitialized *atomic.Bool
5957

6058
Flags flags.ClobFlags
6159

@@ -104,29 +102,28 @@ func NewKeeper(
104102
revshareKeeper types.RevShareKeeper,
105103
) *Keeper {
106104
keeper := &Keeper{
107-
cdc: cdc,
108-
storeKey: storeKey,
109-
memKey: memKey,
110-
transientStoreKey: transientStoreKey,
111-
authorities: lib.UniqueSliceToSet(authorities),
112-
MemClob: memClob,
113-
PerpetualIdToClobPairId: make(map[uint32][]types.ClobPairId),
114-
subaccountsKeeper: subaccountsKeeper,
115-
assetsKeeper: assetsKeeper,
116-
blockTimeKeeper: blockTimeKeeper,
117-
bankKeeper: bankKeeper,
118-
feeTiersKeeper: feeTiersKeeper,
119-
perpetualsKeeper: perpetualsKeeper,
120-
pricesKeeper: pricesKeeper,
121-
statsKeeper: statsKeeper,
122-
rewardsKeeper: rewardsKeeper,
123-
affiliatesKeeper: affiliatesKeeper,
124-
accountPlusKeeper: accountPlusKeeper,
125-
indexerEventManager: indexerEventManager,
126-
streamingManager: streamingManager,
127-
memStoreInitialized: &atomic.Bool{}, // False by default.
128-
initialized: &atomic.Bool{}, // False by default.
129-
txDecoder: txDecoder,
105+
cdc: cdc,
106+
storeKey: storeKey,
107+
memKey: memKey,
108+
transientStoreKey: transientStoreKey,
109+
authorities: lib.UniqueSliceToSet(authorities),
110+
MemClob: memClob,
111+
PerpetualIdToClobPairId: make(map[uint32][]types.ClobPairId),
112+
subaccountsKeeper: subaccountsKeeper,
113+
assetsKeeper: assetsKeeper,
114+
blockTimeKeeper: blockTimeKeeper,
115+
bankKeeper: bankKeeper,
116+
feeTiersKeeper: feeTiersKeeper,
117+
perpetualsKeeper: perpetualsKeeper,
118+
pricesKeeper: pricesKeeper,
119+
statsKeeper: statsKeeper,
120+
rewardsKeeper: rewardsKeeper,
121+
affiliatesKeeper: affiliatesKeeper,
122+
accountPlusKeeper: accountPlusKeeper,
123+
indexerEventManager: indexerEventManager,
124+
streamingManager: streamingManager,
125+
inMemStructuresInitialized: &atomic.Bool{}, // False by default.
126+
txDecoder: txDecoder,
130127
mevTelemetryConfig: MevTelemetryConfig{
131128
Enabled: clobFlags.MevTelemetryEnabled,
132129
Hosts: clobFlags.MevTelemetryHosts,
@@ -180,21 +177,23 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
180177
func (k Keeper) InitializeForGenesis(ctx sdk.Context) {
181178
}
182179

183-
// IsInitialized returns whether the clob keeper has been hydrated.
184-
func (k Keeper) IsInitialized() bool {
185-
return k.initialized.Load()
180+
// IsInMemStructuresInitialized returns whether the clob keeper has been hydrated.
181+
func (k Keeper) IsInMemStructuresInitialized() bool {
182+
return k.inMemStructuresInitialized.Load()
186183
}
187184

188185
// Initialize hydrates the clob keeper with the necessary in memory data structures.
189186
func (k Keeper) Initialize(ctx sdk.Context) {
190-
alreadyInitialized := k.initialized.Swap(true)
187+
// Initialize memstore in clobKeeper with order fill amounts and stateful orders.
188+
k.InitMemStore(ctx)
189+
190+
// Code below hydrates the in memory data structures and is not rolled back even if
191+
// the block execution is discarded by OE. Therefore, they are only called once.
192+
alreadyInitialized := k.inMemStructuresInitialized.Swap(true)
191193
if alreadyInitialized {
192194
return
193195
}
194196

195-
// Initialize memstore in clobKeeper with order fill amounts and stateful orders.
196-
k.InitMemStore(ctx)
197-
198197
// Branch the context for hydration.
199198
// This means that new order matches from hydration will get added to the operations
200199
// queue but the corresponding state changes will be discarded.
@@ -254,11 +253,14 @@ func (k Keeper) ProcessStagedFinalizeBlockEvents(ctx sdk.Context) {
254253
// InitMemStore initializes the memstore of the `clob` keeper.
255254
// This is called during app initialization in `app.go`, before any ABCI calls are received.
256255
func (k Keeper) InitMemStore(ctx sdk.Context) {
257-
alreadyInitialized := k.memStoreInitialized.Swap(true)
256+
alreadyInitialized := k.GetMemstoreInitialized(ctx)
258257
if alreadyInitialized {
259-
panic(errors.New("Memory store already initialized and is not intended to be invoked more then once."))
258+
return
260259
}
261260

261+
// Set memstore initialized flag.
262+
k.SetMemstoreInitialized(ctx)
263+
262264
memStore := ctx.KVStore(k.memKey)
263265
memStoreType := memStore.GetStoreType()
264266
if memStoreType != storetypes.StoreTypeMemory {
@@ -307,6 +309,19 @@ func (k Keeper) InitMemStore(ctx sdk.Context) {
307309
}
308310
}
309311

312+
func (k Keeper) GetMemstoreInitialized(ctx sdk.Context) bool {
313+
store := ctx.KVStore(k.memKey)
314+
return store.Has([]byte(types.KeyMemstoreInitialized))
315+
}
316+
317+
func (k Keeper) SetMemstoreInitialized(ctx sdk.Context) {
318+
store := ctx.KVStore(k.memKey)
319+
store.Set(
320+
[]byte(types.KeyMemstoreInitialized),
321+
[]byte{1},
322+
)
323+
}
324+
310325
// Sets the ante handler after it has been constructed. This breaks a cycle between
311326
// when the ante handler is constructed and when the clob keeper is constructed.
312327
func (k *Keeper) SetAnteHandler(anteHandler sdk.AnteHandler) {

protocol/x/clob/keeper/keeper_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ func TestInitMemStore_OnlyAllowedOnce(t *testing.T) {
3131

3232
ks.ClobKeeper.InitMemStore(ks.Ctx)
3333

34-
// Initializing a second time causes a panic
35-
require.Panics(t, func() {
36-
ks.ClobKeeper.InitMemStore(ks.Ctx)
37-
})
34+
require.True(t, ks.ClobKeeper.GetMemstoreInitialized(ks.Ctx))
3835
}
3936

4037
func TestInitMemStore_StatefulOrderCount(t *testing.T) {

protocol/x/clob/types/clob_keeper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type ClobKeeper interface {
1313
LiquidationsKeeper
1414
LiquidationsConfigKeeper
1515

16-
IsInitialized() bool
16+
IsInMemStructuresInitialized() bool
1717
Initialize(ctx sdk.Context)
1818

1919
AddOrderToOrderbookSubaccountUpdatesCheck(

protocol/x/clob/types/keys.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ const (
7979

8080
// Memstore
8181
const (
82+
// KeyMemstoreInitialized is the key to check if the memstore has been initialized.
83+
KeyMemstoreInitialized = "MemstoreInit"
84+
8285
// ProcessProposerMatchesEventsKey is the key to retrieve information about how to update
8386
// memclob state based on the latest block.
8487
ProcessProposerMatchesEventsKey = "ProposerEvents"

0 commit comments

Comments
 (0)