Skip to content

Commit

Permalink
make cpCache thread-safe + load all cps on app-start
Browse files Browse the repository at this point in the history
  • Loading branch information
nivasan1 committed Mar 21, 2024
1 parent f0192bd commit 6ad0520
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
14 changes: 14 additions & 0 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,9 @@ func New(

// Hydrate the keeper in-memory data structures.
app.hydrateKeeperInMemoryDataStructures()

// load the x/prices keeper currency-pair ID cache
app.loadCurrencyPairIDsForMarkets()
}
app.initializeRateLimiters()

Expand Down Expand Up @@ -1610,6 +1613,17 @@ func (app *App) DisableHealthMonitorForTesting() {
app.DaemonHealthMonitor.DisableForTesting()
}

// loadCurrencyPairIDsForMarkets loads the currency pair IDs for the markets from the x/prices state.
func (app *App) loadCurrencyPairIDsForMarkets() {
// Create an `uncachedCtx` where the underlying MultiStore is the `rootMultiStore`.
// We use this to load the `currencyPairIDs` with market-params from the
// x/prices state according to the underlying `rootMultiStore`.
uncachedCtx := app.BaseApp.NewUncachedContext(true, tmproto.Header{})

// Load the currency pair IDs for the markets from the x/prices state.
app.PricesKeeper.LoadCurrencyPairIDCache(uncachedCtx)
}

// hydrateMemStores hydrates the memStores used for caching state.
func (app *App) hydrateMemStores() {
// Create an `uncachedCtx` where the underlying MultiStore is the `rootMultiStore`.
Expand Down
12 changes: 10 additions & 2 deletions protocol/x/prices/keeper/currency_pair_id_cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import "sync"
import (
"sync"
)

// CurrencyPairIDCache handles the caching logic of currency-pairs to their corresponding IDs. This
// data-structure is thread-safe, allowing concurrent reads + synchronized writes.
Expand Down Expand Up @@ -55,8 +57,14 @@ func (c *CurrencyPairIDCache) GetIDForCurrencyPair(currencyPair string) (uint64,
return id, found
}

// Remove removes the currency-pair (by ID) from the cache
// Remove removes the currency-pair (by ID) from the cache. This method takes out a write lock on the
// cache or blocks until one is available before updating the cache.
func (c *CurrencyPairIDCache) Remove(id uint64) {
// acquire write lock
c.Lock()
defer c.Unlock()

// remove currency pair from cache
currencyPair, found := c.idToCurrencyPair[id]
if found {
delete(c.idToCurrencyPair, id)
Expand Down
10 changes: 10 additions & 0 deletions protocol/x/prices/keeper/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
indexerevents "github.com/dydxprotocol/v4-chain/protocol/indexer/events"
"github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/lib/slinky"
"github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
)

Expand Down Expand Up @@ -55,6 +56,15 @@ func (k Keeper) CreateMarket(
marketPriceStore := k.getMarketPriceStore(ctx)
marketPriceStore.Set(lib.Uint32ToKey(marketPrice.Id), priceBytes)

// add the pair to the currency-pair-id cache
cp, err := slinky.MarketPairToCurrencyPair(marketParam.Pair)
if err != nil {
k.Logger(ctx).Error("failed to add currency pair to cache", "pair", marketParam.Pair, "err", err)
} else {
// add the pair to the currency-pair-id cache
k.currencyPairIDCache.AddCurrencyPair(uint64(marketParam.Id), cp.String())
}

// Generate indexer event.
k.GetIndexerEventManager().AddTxnEvent(
ctx,
Expand Down
20 changes: 20 additions & 0 deletions protocol/x/prices/keeper/market_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ func (k Keeper) GetMarketParam(
return market, true
}

// LoadCurrencyPairIDCache loads the currency pair id cache from the store.
func (k Keeper) LoadCurrencyPairIDCache(ctx sdk.Context) {
marketParamStore := k.getMarketParamStore(ctx)

iterator := marketParamStore.Iterator(nil, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
marketParam := types.MarketParam{}
k.cdc.MustUnmarshal(iterator.Value(), &marketParam)

cp, err := slinky.MarketPairToCurrencyPair(marketParam.Pair)
if err == nil {
k.currencyPairIDCache.AddCurrencyPair(uint64(marketParam.Id), cp.String())
} else {
k.Logger(ctx).Error("failed to add currency pair to cache", "pair", marketParam.Pair)
}
}
}

// GetAllMarketParams returns all market params.
func (k Keeper) GetAllMarketParams(ctx sdk.Context) []types.MarketParam {
marketParamStore := k.getMarketParamStore(ctx)
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/prices/keeper/market_param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestModifyMarketParam(t *testing.T) {
}

func TestModifyMarketParamUpdatesCache(t *testing.T) {
ctx, keeper, _, _, _, mockTimeProvider := keepertest.PricesKeepers(t)
ctx, keeper, _, _, mockTimeProvider := keepertest.PricesKeepers(t)
mockTimeProvider.On("Now").Return(constants.TimeT)
ctx = ctx.WithTxBytes(constants.TestTxBytes)

Expand Down
2 changes: 1 addition & 1 deletion protocol/x/prices/keeper/slinky_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (k Keeper) GetCurrencyPairFromID(ctx sdk.Context, id uint64) (cp slinkytype
k.Logger(ctx).Error("CurrencyPairFromString", "error", err)
return cp, false
}

return cp, true
}

Expand Down

0 comments on commit 6ad0520

Please sign in to comment.