From f0192bd096efddbb04d69339c0be4402360e64fb Mon Sep 17 00:00:00 2001 From: Nikhil Vasan Date: Wed, 20 Mar 2024 07:07:40 -0700 Subject: [PATCH] evict market-params with updated pair from cache --- .../x/prices/keeper/currency_pair_id_cache.go | 9 +++ protocol/x/prices/keeper/market_param.go | 15 +++++ protocol/x/prices/keeper/market_param_test.go | 57 +++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/protocol/x/prices/keeper/currency_pair_id_cache.go b/protocol/x/prices/keeper/currency_pair_id_cache.go index ce6dbf6050..57657cd440 100644 --- a/protocol/x/prices/keeper/currency_pair_id_cache.go +++ b/protocol/x/prices/keeper/currency_pair_id_cache.go @@ -54,3 +54,12 @@ func (c *CurrencyPairIDCache) GetIDForCurrencyPair(currencyPair string) (uint64, id, found := c.currencyPairToID[currencyPair] return id, found } + +// Remove removes the currency-pair (by ID) from the cache +func (c *CurrencyPairIDCache) Remove(id uint64) { + currencyPair, found := c.idToCurrencyPair[id] + if found { + delete(c.idToCurrencyPair, id) + delete(c.currencyPairToID, currencyPair) + } +} diff --git a/protocol/x/prices/keeper/market_param.go b/protocol/x/prices/keeper/market_param.go index fe3ff3c6fa..d1ab742c2f 100644 --- a/protocol/x/prices/keeper/market_param.go +++ b/protocol/x/prices/keeper/market_param.go @@ -5,6 +5,7 @@ import ( errorsmod "cosmossdk.io/errors" "github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/metrics" + "github.com/dydxprotocol/v4-chain/protocol/lib/slinky" "cosmossdk.io/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" @@ -53,6 +54,20 @@ func (k Keeper) ModifyMarketParam( marketParamStore := k.getMarketParamStore(ctx) b := k.cdc.MustMarshal(&updatedMarketParam) marketParamStore.Set(lib.Uint32ToKey(updatedMarketParam.Id), b) + + // if the market pair has been changed, we need to update the in-memory market pair cache + if existingParam.Pair != updatedMarketParam.Pair { + // remove the old cache entry + k.currencyPairIDCache.Remove(uint64(existingParam.Id)) + + // add the new cache entry + cp, err := slinky.MarketPairToCurrencyPair(updatedMarketParam.Pair) + if err == nil { + k.currencyPairIDCache.AddCurrencyPair(uint64(updatedMarketParam.Id), cp.String()) + } else { + k.Logger(ctx).Error("failed to add currency pair to cache", "pair", updatedMarketParam.Pair) + } + } // Generate indexer event. k.GetIndexerEventManager().AddTxnEvent( diff --git a/protocol/x/prices/keeper/market_param_test.go b/protocol/x/prices/keeper/market_param_test.go index 2c1ce910b1..0d4a5352fa 100644 --- a/protocol/x/prices/keeper/market_param_test.go +++ b/protocol/x/prices/keeper/market_param_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/metrics" + "github.com/dydxprotocol/v4-chain/protocol/lib/slinky" errorsmod "cosmossdk.io/errors" @@ -45,6 +46,62 @@ func TestModifyMarketParam(t *testing.T) { } } +func TestModifyMarketParamUpdatesCache(t *testing.T) { + ctx, keeper, _, _, _, mockTimeProvider := keepertest.PricesKeepers(t) + mockTimeProvider.On("Now").Return(constants.TimeT) + ctx = ctx.WithTxBytes(constants.TestTxBytes) + + id := uint32(1) + oldParam := types.MarketParam{ + Id: id, + Pair: "foo-bar", + MinExchanges: uint32(2), + Exponent: 8, + MinPriceChangePpm: uint32(50), + ExchangeConfigJson: `{"id":"1"}`, + } + mp, err := keeper.CreateMarket(ctx, oldParam, types.MarketPrice{ + Id: id, + Exponent: 8, + Price: 1, + }) + require.NoError(t, err) + + // check that the existing entry exists + cp, err := slinky.MarketPairToCurrencyPair(mp.Pair) + require.NoError(t, err) + + // check that the existing entry exists + cpID, found := keeper.GetIDForCurrencyPair(ctx, cp) + require.True(t, found) + require.Equal(t, uint64(id), cpID) + + // modify the market param + newParam, err := keeper.ModifyMarketParam( + ctx, + types.MarketParam{ + Id: id, + Pair: "bar-foo", + MinExchanges: uint32(2), + Exponent: 8, + MinPriceChangePpm: uint32(50), + ExchangeConfigJson: `{"id":"1"}`, + }, + ) + require.NoError(t, err) + + // check that the existing entry does not exist + _, found = keeper.GetIDForCurrencyPair(ctx, cp) + require.False(t, found) + + // check that the new entry exists + cp, err = slinky.MarketPairToCurrencyPair(newParam.Pair) + require.NoError(t, err) + cpID, found = keeper.GetIDForCurrencyPair(ctx, cp) + require.True(t, found) + require.Equal(t, uint64(id), cpID) +} + func TestModifyMarketParam_Errors(t *testing.T) { validExchangeConfigJson := `{"exchanges":[{"exchangeName":"Binance","ticker":"BTCUSDT"}]}` tests := map[string]struct {