-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
delete recent price from db when delisting #753
Changes from 1 commit
f7bda47
db682e3
7ad0314
d73fff5
6d930e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ type TradingPairMapper interface { | |
ListAllTradingPairs(ctx sdk.Context) []types.TradingPair | ||
UpdateRecentPrices(ctx sdk.Context, pricesStoreEvery, numPricesStored int64, lastTradePrices map[string]int64) | ||
GetRecentPrices(ctx sdk.Context, pricesStoreEvery, numPricesStored int64) map[string]*utils.FixedSizeRing | ||
DeleteRecentPrices(ctx sdk.Context, baseAsset, quoteAsset string) | ||
} | ||
|
||
var _ TradingPairMapper = mapper{} | ||
|
@@ -157,7 +158,7 @@ func (m mapper) GetRecentPrices(ctx sdk.Context, pricesStoreEvery, numPricesStor | |
} else { | ||
recordStarted = true | ||
} | ||
prices := m.decodeRecentPrices(bz, numPricesStored) | ||
prices := m.decodeRecentPrices(bz) | ||
numSymbol := len(prices.Pair) | ||
for i := 0; i < numSymbol; i++ { | ||
symbol := prices.Pair[i] | ||
|
@@ -172,6 +173,36 @@ func (m mapper) GetRecentPrices(ctx sdk.Context, pricesStoreEvery, numPricesStor | |
return recentPrices | ||
} | ||
|
||
func (m mapper) DeleteRecentPrices(ctx sdk.Context, baseAsset, quoteAsset string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just pass the trading symbol as the parameter |
||
symbolToDelete := dexUtils.Assets2TradingPair(strings.ToUpper(baseAsset), strings.ToUpper(quoteAsset)) | ||
store := ctx.KVStore(m.key) | ||
iter := sdk.KVStorePrefixIterator(store, []byte(recentPricesKeyPrefix)) | ||
defer iter.Close() | ||
|
||
for ; iter.Valid(); iter.Next() { | ||
bz := iter.Value() | ||
prices := m.decodeRecentPrices(bz) | ||
numSymbol := len(prices.Pair) | ||
for i := 0; i < numSymbol; i++ { | ||
symbol := prices.Pair[i] | ||
if symbol == symbolToDelete { | ||
prices.Pair = removePair(prices.Pair, i) | ||
prices.Price = removePrice(prices.Price, i) | ||
bz := m.cdc.MustMarshalBinaryBare(prices) | ||
store.Set(iter.Key(), bz) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
func removePair(pair []string, i int) []string { | ||
return append(pair[:i], pair[i+1:]...) | ||
} | ||
func removePrice(price []int64, i int) []int64 { | ||
return append(price[:i], price[i+1:]...) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest giving the |
||
|
||
func (m mapper) encodeRecentPrices(recentPrices map[string]int64) []byte { | ||
value := RecentPrice{} | ||
numSymbol := len(recentPrices) | ||
|
@@ -195,7 +226,7 @@ func (m mapper) encodeRecentPrices(recentPrices map[string]int64) []byte { | |
return bz | ||
} | ||
|
||
func (m mapper) decodeRecentPrices(bz []byte, numPricesStored int64) *RecentPrice { | ||
func (m mapper) decodeRecentPrices(bz []byte) *RecentPrice { | ||
value := RecentPrice{} | ||
m.cdc.MustUnmarshalBinaryBare(bz, &value) | ||
return &value | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
package store | ||
|
||
import ( | ||
"path" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
abci "github.com/tendermint/tendermint/abci/types" | ||
"github.com/tendermint/tendermint/libs/db" | ||
dbm "github.com/tendermint/tendermint/libs/db" | ||
"github.com/tendermint/tendermint/libs/log" | ||
|
||
|
@@ -123,3 +127,105 @@ func TestMapper_UpdateRecentPrices(t *testing.T) { | |
require.Equal(t, int64(5), allRecentPrices["ABC"].Count()) | ||
require.Equal(t, []interface{}{int64(10), int64(10), int64(10), int64(10), int64(10)}, allRecentPrices["ABC"].Elements()) | ||
} | ||
|
||
func TestMapper_DeleteRecentPrices(t *testing.T) { | ||
pairMapper, ctx := setup() | ||
for i := 0; i < 3000; i++ { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be much smaller if it is not a benchmark |
||
lastPrices := make(map[string]int64, 2) | ||
lastPrices["ABC_BNB"] = 10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make some changes of the prices |
||
lastPrices["ABC_EFG"] = 3 | ||
lastPrices["EFG_BNB"] = 3 | ||
ctx = ctx.WithBlockHeight(int64(2 * (i + 1))) | ||
pairMapper.UpdateRecentPrices(ctx, 2, 5, lastPrices) | ||
} | ||
|
||
allRecentPrices := pairMapper.GetRecentPrices(ctx, 2, 5) | ||
require.Equal(t, 3, len(allRecentPrices)) | ||
require.Equal(t, int64(5), allRecentPrices["ABC_BNB"].Count()) | ||
require.Equal(t, []interface{}{int64(10), int64(10), int64(10), int64(10), int64(10)}, allRecentPrices["ABC_BNB"].Elements()) | ||
require.Equal(t, int64(5), allRecentPrices["ABC_EFG"].Count()) | ||
require.Equal(t, []interface{}{int64(3), int64(3), int64(3), int64(3), int64(3)}, allRecentPrices["ABC_EFG"].Elements()) | ||
require.Equal(t, int64(5), allRecentPrices["ABC_EFG"].Count()) | ||
require.Equal(t, []interface{}{int64(3), int64(3), int64(3), int64(3), int64(3)}, allRecentPrices["EFG_BNB"].Elements()) | ||
|
||
pairMapper.DeleteRecentPrices(ctx, "ABC", "EFG") | ||
allRecentPrices = pairMapper.GetRecentPrices(ctx, 2, 5) | ||
require.Equal(t, 2, len(allRecentPrices)) | ||
require.Equal(t, int64(5), allRecentPrices["ABC_BNB"].Count()) | ||
require.Equal(t, []interface{}{int64(10), int64(10), int64(10), int64(10), int64(10)}, allRecentPrices["ABC_BNB"].Elements()) | ||
require.Equal(t, int64(5), allRecentPrices["EFG_BNB"].Count()) | ||
require.Equal(t, []interface{}{int64(3), int64(3), int64(3), int64(3), int64(3)}, allRecentPrices["EFG_BNB"].Elements()) | ||
} | ||
|
||
func TestMapper_DeleteOneRecentPrices(t *testing.T) { | ||
pairMapper, ctx := setup() | ||
for i := 0; i < 10; i++ { | ||
lastPrices := make(map[string]int64, 1) | ||
if i < 5 { | ||
lastPrices["ABC_BNB"] = 10 | ||
} | ||
ctx = ctx.WithBlockHeight(int64(2 * (i + 1))) | ||
pairMapper.UpdateRecentPrices(ctx, 2, 10, lastPrices) | ||
} | ||
|
||
allRecentPrices := pairMapper.GetRecentPrices(ctx, 2, 10) | ||
require.Equal(t, 1, len(allRecentPrices)) | ||
require.Equal(t, int64(5), allRecentPrices["ABC_BNB"].Count()) | ||
require.Equal(t, []interface{}{int64(10), int64(10), int64(10), int64(10), int64(10)}, allRecentPrices["ABC_BNB"].Elements()) | ||
|
||
pairMapper.DeleteRecentPrices(ctx, "ABC", "BNB") | ||
allRecentPrices = pairMapper.GetRecentPrices(ctx, 2, 5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the parameters (ctx, 2, 5) are different from the above (ctx, 2, 10). |
||
require.Equal(t, 0, len(allRecentPrices)) | ||
|
||
//allowed to delete again | ||
pairMapper.DeleteRecentPrices(ctx, "ABC", "BNB") | ||
allRecentPrices = pairMapper.GetRecentPrices(ctx, 2, 5) | ||
require.Equal(t, 0, len(allRecentPrices)) | ||
} | ||
|
||
func BenchmarkMapper_DeleteRecentPrices(b *testing.B) { | ||
db, pairMapper, ctx := setupForBenchTest() | ||
defer db.Close() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
pairMapper.DeleteRecentPrices(ctx, string(i), string(i)) | ||
} | ||
} | ||
|
||
func setupForBenchTest() (dbm.DB, TradingPairMapper, sdk.Context) { | ||
db, ms, key := setupLevelDbMultiStore() | ||
ctx := sdk.NewContext(ms, abci.Header{Height: 1}, sdk.RunTxModeDeliver, log.NewNopLogger()) | ||
var cdc = wire.NewCodec() | ||
cdc.RegisterConcrete(dextypes.TradingPair{}, "dex/TradingPair", nil) | ||
cdc.RegisterConcrete(RecentPrice{}, "dex/RecentPrice", nil) | ||
pairMapper := NewTradingPairMapper(cdc, key) | ||
for i := 0; i < 500; i++ { | ||
tradingPair := dextypes.NewTradingPair(string(i), string(i), 102000) | ||
pairMapper.AddTradingPair(ctx, tradingPair) | ||
} | ||
|
||
for i := 0; i < 2000; i++ { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest using some const instead to make it more readable. |
||
lastPrices := make(map[string]int64, 500) | ||
for j := 0; j < 500; j++ { | ||
lastPrices[string(j)+"_"+string(j)] = 8 | ||
} | ||
ctx = ctx.WithBlockHeight(int64(1000 * (i + 1))) | ||
pairMapper.UpdateRecentPrices(ctx, 1000, 2000, lastPrices) | ||
} | ||
|
||
return db, pairMapper, ctx | ||
} | ||
|
||
func setupLevelDbMultiStore() (dbm.DB, sdk.MultiStore, *sdk.KVStoreKey) { | ||
_, b, _, _ := runtime.Caller(0) | ||
basePath := filepath.Dir(b) | ||
db, err := db.NewGoLevelDB("test", path.Join(basePath, "data")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
key := sdk.NewKVStoreKey("pair") | ||
ms := store.NewCommitMultiStore(db) | ||
ms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db) | ||
ms.LoadLatestVersion() | ||
return db, ms, key | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better wrap a method to remove both from memory and db.