|
1 | 1 | package keeper
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "errors" |
5 | 4 | "fmt"
|
6 | 5 | "math/big"
|
7 | 6 | "sync/atomic"
|
|
54 | 53 | streamingManager streamingtypes.FullNodeStreamingManager
|
55 | 54 | finalizeBlockEventStager finalizeblock.EventStager[*types.ClobStagedFinalizeBlockEvent]
|
56 | 55 |
|
57 |
| - initialized *atomic.Bool |
58 |
| - memStoreInitialized *atomic.Bool |
| 56 | + inMemStructuresInitialized *atomic.Bool |
59 | 57 |
|
60 | 58 | Flags flags.ClobFlags
|
61 | 59 |
|
@@ -104,29 +102,28 @@ func NewKeeper(
|
104 | 102 | revshareKeeper types.RevShareKeeper,
|
105 | 103 | ) *Keeper {
|
106 | 104 | 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, |
130 | 127 | mevTelemetryConfig: MevTelemetryConfig{
|
131 | 128 | Enabled: clobFlags.MevTelemetryEnabled,
|
132 | 129 | Hosts: clobFlags.MevTelemetryHosts,
|
@@ -180,21 +177,23 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
180 | 177 | func (k Keeper) InitializeForGenesis(ctx sdk.Context) {
|
181 | 178 | }
|
182 | 179 |
|
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() |
186 | 183 | }
|
187 | 184 |
|
188 | 185 | // Initialize hydrates the clob keeper with the necessary in memory data structures.
|
189 | 186 | 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) |
191 | 193 | if alreadyInitialized {
|
192 | 194 | return
|
193 | 195 | }
|
194 | 196 |
|
195 |
| - // Initialize memstore in clobKeeper with order fill amounts and stateful orders. |
196 |
| - k.InitMemStore(ctx) |
197 |
| - |
198 | 197 | // Branch the context for hydration.
|
199 | 198 | // This means that new order matches from hydration will get added to the operations
|
200 | 199 | // queue but the corresponding state changes will be discarded.
|
@@ -254,11 +253,14 @@ func (k Keeper) ProcessStagedFinalizeBlockEvents(ctx sdk.Context) {
|
254 | 253 | // InitMemStore initializes the memstore of the `clob` keeper.
|
255 | 254 | // This is called during app initialization in `app.go`, before any ABCI calls are received.
|
256 | 255 | func (k Keeper) InitMemStore(ctx sdk.Context) {
|
257 |
| - alreadyInitialized := k.memStoreInitialized.Swap(true) |
| 256 | + alreadyInitialized := k.GetMemstoreInitialized(ctx) |
258 | 257 | if alreadyInitialized {
|
259 |
| - panic(errors.New("Memory store already initialized and is not intended to be invoked more then once.")) |
| 258 | + return |
260 | 259 | }
|
261 | 260 |
|
| 261 | + // Set memstore initialized flag. |
| 262 | + k.SetMemstoreInitialized(ctx) |
| 263 | + |
262 | 264 | memStore := ctx.KVStore(k.memKey)
|
263 | 265 | memStoreType := memStore.GetStoreType()
|
264 | 266 | if memStoreType != storetypes.StoreTypeMemory {
|
@@ -307,6 +309,19 @@ func (k Keeper) InitMemStore(ctx sdk.Context) {
|
307 | 309 | }
|
308 | 310 | }
|
309 | 311 |
|
| 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 | + |
310 | 325 | // Sets the ante handler after it has been constructed. This breaks a cycle between
|
311 | 326 | // when the ante handler is constructed and when the clob keeper is constructed.
|
312 | 327 | func (k *Keeper) SetAnteHandler(anteHandler sdk.AnteHandler) {
|
|
0 commit comments