From 7963ebfb97512cbdaa80574e1e6bb5a360e8cbd4 Mon Sep 17 00:00:00 2001 From: Erheng Lu Date: Tue, 5 May 2020 01:13:17 +0800 Subject: [PATCH] fix test --- app/pub/helpers.go | 3 ++- plugins/dex/matcheng/engine_new.go | 6 ++--- plugins/dex/order/handler_test.go | 38 +++++++++++++++------------- plugins/dex/order/keeper.go | 2 +- plugins/dex/order/keeper_recovery.go | 1 + plugins/dex/order/keeper_test.go | 26 +++++++++++++++---- plugins/dex/order/mini_keeper.go | 10 -------- 7 files changed, 49 insertions(+), 37 deletions(-) diff --git a/app/pub/helpers.go b/app/pub/helpers.go index 87c5ce69e..6f44729d7 100644 --- a/app/pub/helpers.go +++ b/app/pub/helpers.go @@ -314,6 +314,7 @@ func MatchAndAllocateAllForPublish(dexKeeper *orderPkg.Keeper, dexMiniKeeper *or func extractTradesToPublish(dexKeeper orderPkg.DexOrderKeeper, ctx sdk.Context, tradeHeight int64, tradeIdx *int) []*Trade { tradesToPublish := make([]*Trade, 0) + //TODO reduce call of ListAllTradingPairs for _, pair := range dexKeeper.GetPairMapper().ListAllTradingPairs(ctx) { symbol := pair.GetSymbol() matchEngTrades, _ := dexKeeper.GetLastTrades(tradeHeight, symbol) @@ -330,7 +331,7 @@ func extractTradesToPublish(dexKeeper orderPkg.DexOrderKeeper, ctx sdk.Context, } t := &Trade{ - Id: fmt.Sprintf("%d-%d", tradeHeight, tradeIdx), + Id: fmt.Sprintf("%d-%d", tradeHeight, *tradeIdx), Symbol: symbol, Sid: trade.Sid, Bid: trade.Bid, diff --git a/plugins/dex/matcheng/engine_new.go b/plugins/dex/matcheng/engine_new.go index d481dc7af..b0ddd915e 100644 --- a/plugins/dex/matcheng/engine_new.go +++ b/plugins/dex/matcheng/engine_new.go @@ -120,10 +120,10 @@ func dropRedundantQty(orders []OrderPart, toDropQty, lotSize int64) error { func findTakerStartIdx(lastMatchHeight int64, orders []OrderPart) (idx int, makerTotal int64) { i, k := 0, len(orders) for ; i < k; i++ { - if orders[i].Time >= lastMatchHeight { - return i, makerTotal - } else { + if orders[i].Time <= lastMatchHeight { makerTotal += orders[i].nxtTrade + } else { + return i, makerTotal } } return i, makerTotal diff --git a/plugins/dex/order/handler_test.go b/plugins/dex/order/handler_test.go index d81703204..24364c50d 100644 --- a/plugins/dex/order/handler_test.go +++ b/plugins/dex/order/handler_test.go @@ -16,22 +16,24 @@ import ( "github.com/binance-chain/node/common" "github.com/binance-chain/node/plugins/dex/store" "github.com/binance-chain/node/plugins/dex/types" + dextypes "github.com/binance-chain/node/plugins/dex/types" "github.com/binance-chain/node/wire" ) -func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) { +func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey, *sdk.KVStoreKey) { db := dbm.NewMemDB() key := sdk.NewKVStoreKey("pair") // TODO: can this be "pairs" as in the constant? key2 := sdk.NewKVStoreKey(common.AccountStoreName) + key3 := sdk.NewKVStoreKey(common.DexStoreName) ms := cstore.NewCommitMultiStore(db) ms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db) ms.MountStoreWithDB(key2, sdk.StoreTypeIAVL, db) ms.LoadLatestVersion() - return ms, key, key2 + return ms, key, key2, key3 } -func setupMappers() (store.TradingPairMapper, auth.AccountKeeper, sdk.Context) { - ms, key, key2 := setupMultiStore() +func setupMappers() (store.TradingPairMapper, auth.AccountKeeper, sdk.Context, DexOrderKeeper) { + ms, key, key2, key3 := setupMultiStore() var cdc = wire.NewCodec() auth.RegisterBaseAccount(cdc) cdc.RegisterConcrete(types.TradingPair{}, "dex/TradingPair", nil) @@ -39,7 +41,9 @@ func setupMappers() (store.TradingPairMapper, auth.AccountKeeper, sdk.Context) { accMapper := auth.NewAccountKeeper(cdc, key2, auth.ProtoBaseAccount) accountCache := getAccountCache(cdc, ms, key2) ctx := sdk.NewContext(ms, abci.Header{}, sdk.RunTxModeDeliver, log.NewNopLogger()).WithAccountCache(accountCache) - return pairMapper, accMapper, ctx + globalKeeper := NewGlobalKeeper(cdc, accMapper, false) + keeper := NewKeeper(key3, pairMapper, sdk.NewCodespacer().RegisterNext(dextypes.DefaultCodespace), 2, cdc, globalKeeper) + return pairMapper, accMapper, ctx, keeper } func setupAccount(ctx sdk.Context, accMapper auth.AccountKeeper) (sdk.Account, sdk.AccAddress) { @@ -61,7 +65,7 @@ func setupAccount(ctx sdk.Context, accMapper auth.AccountKeeper) (sdk.Account, s } func TestHandler_ValidateOrder_OrderNotExist(t *testing.T) { - pairMapper, accMapper, ctx := setupMappers() + pairMapper, accMapper, ctx, keeper := setupMappers() pair := types.NewTradingPair("AAA-000", "BNB", 1e8) err := pairMapper.AddTradingPair(ctx, pair) require.NoError(t, err) @@ -76,13 +80,13 @@ func TestHandler_ValidateOrder_OrderNotExist(t *testing.T) { Id: fmt.Sprintf("%X-0", acc.GetAddress()), } - err = validateOrder(ctx, pairMapper, acc, msg) + err = keeper.validateOrder(ctx, acc, msg) require.Error(t, err) require.Equal(t, fmt.Sprintf("trading pair not found: %s", msg.Symbol), err.Error()) } func TestHandler_ValidateOrder_WrongSymbol(t *testing.T) { - pairMapper, _, ctx := setupMappers() + pairMapper, _, ctx, keeper := setupMappers() msgs := []NewOrderMsg{ { @@ -103,14 +107,14 @@ func TestHandler_ValidateOrder_WrongSymbol(t *testing.T) { } for _, msg := range msgs { - err := validateOrder(ctx, pairMapper, nil, msg) + err := keeper.validateOrder(ctx, pairMapper, nil, msg) require.Error(t, err) require.Equal(t, fmt.Sprintf("Failed to parse trading pair symbol:%s into assets", msg.Symbol), err.Error()) } } func TestHandler_ValidateOrder_WrongPrice(t *testing.T) { - pairMapper, accMapper, ctx := setupMappers() + pairMapper, accMapper, ctx, keeper := setupMappers() pair := types.NewTradingPair("AAA-000", "BNB", 1e8) err := pairMapper.AddTradingPair(ctx, pair) require.NoError(t, err) @@ -125,13 +129,13 @@ func TestHandler_ValidateOrder_WrongPrice(t *testing.T) { Id: fmt.Sprintf("%X-0", acc.GetAddress()), } - err = validateOrder(ctx, pairMapper, acc, msg) + err = keeper.validateOrder(ctx, acc, msg) require.Error(t, err) require.Equal(t, fmt.Sprintf("price(%v) is not rounded to tickSize(%v)", msg.Price, pair.TickSize.ToInt64()), err.Error()) } func TestHandler_ValidateOrder_WrongQuantity(t *testing.T) { - pairMapper, accMapper, ctx := setupMappers() + pairMapper, accMapper, ctx, keeper := setupMappers() pair := types.NewTradingPair("AAA-000", "BNB", 1e8) err := pairMapper.AddTradingPair(ctx, pair) require.NoError(t, err) @@ -146,13 +150,13 @@ func TestHandler_ValidateOrder_WrongQuantity(t *testing.T) { Id: fmt.Sprintf("%X-0", acc.GetAddress()), } - err = validateOrder(ctx, pairMapper, acc, msg) + err = keeper.validateOrder(ctx, acc, msg) require.Error(t, err) require.Equal(t, fmt.Sprintf("quantity(%v) is not rounded to lotSize(%v)", msg.Quantity, pair.LotSize.ToInt64()), err.Error()) } func TestHandler_ValidateOrder_Normal(t *testing.T) { - pairMapper, accMapper, ctx := setupMappers() + pairMapper, accMapper, ctx, keeper:= setupMappers() err := pairMapper.AddTradingPair(ctx, types.NewTradingPair("AAA-000", "BNB", 1e8)) require.NoError(t, err) @@ -166,12 +170,12 @@ func TestHandler_ValidateOrder_Normal(t *testing.T) { Id: fmt.Sprintf("%X-0", acc.GetAddress()), } - err = validateOrder(ctx, pairMapper, acc, msg) + err = keeper.validateOrder(ctx, acc, msg) require.NoError(t, err) } func TestHandler_ValidateOrder_MaxNotional(t *testing.T) { - pairMapper, accMapper, ctx := setupMappers() + pairMapper, accMapper, ctx, keeper := setupMappers() err := pairMapper.AddTradingPair(ctx, types.NewTradingPair("AAA-000", "BNB", 1e8)) require.NoError(t, err) @@ -185,7 +189,7 @@ func TestHandler_ValidateOrder_MaxNotional(t *testing.T) { Id: fmt.Sprintf("%X-0", acc.GetAddress()), } - err = validateOrder(ctx, pairMapper, acc, msg) + err = keeper.validateOrder(ctx, acc, msg) require.Error(t, err) require.Equal(t, "notional value of the order is too large(cannot fit in int64)", err.Error()) } diff --git a/plugins/dex/order/keeper.go b/plugins/dex/order/keeper.go index 6796fccbc..894613156 100644 --- a/plugins/dex/order/keeper.go +++ b/plugins/dex/order/keeper.go @@ -705,7 +705,7 @@ func (kp *Keeper) CanDelistTradingPair(ctx sdk.Context, baseAsset, quoteAsset st } tradingPairs := kp.PairMapper.ListAllTradingPairs(ctx) - for _, pair := range tradingPairs { + for _, pair := range tradingPairs { //TODO if (pair.BaseAssetSymbol == symbolToCheck && pair.QuoteAssetSymbol != types.NativeTokenSymbol) || (pair.QuoteAssetSymbol == symbolToCheck && pair.BaseAssetSymbol != types.NativeTokenSymbol) { return fmt.Errorf("trading pair %s_%s should not exist before delisting %s_%s", diff --git a/plugins/dex/order/keeper_recovery.go b/plugins/dex/order/keeper_recovery.go index a1c70b416..fe5e13521 100644 --- a/plugins/dex/order/keeper_recovery.go +++ b/plugins/dex/order/keeper_recovery.go @@ -152,6 +152,7 @@ func (kp *Keeper) LoadOrderBookSnapshot(ctx sdk.Context, latestBlockHeight int64 eng.Book.InsertPriceLevel(&pl, me.SELLSIDE) } eng.LastTradePrice = ob.LastTradePrice + eng.LastMatchHeight = height ctx.Logger().Info("Successfully Loaded order snapshot", "pair", pair) } key := genActiveOrdersSnapshotKey(height) diff --git a/plugins/dex/order/keeper_test.go b/plugins/dex/order/keeper_test.go index dda2c32e7..395496a96 100644 --- a/plugins/dex/order/keeper_test.go +++ b/plugins/dex/order/keeper_test.go @@ -56,8 +56,19 @@ func MakeKeeper(cdc *wire.Codec) *Keeper { accKeeper := auth.NewAccountKeeper(cdc, common.AccountStoreKey, types.ProtoAppAccount) codespacer := sdk.NewCodespacer() pairMapper := store.NewTradingPairMapper(cdc, common.PairStoreKey, false) - keeper := NewKeeper(common.DexStoreKey, accKeeper, pairMapper, - codespacer.RegisterNext(dextypes.DefaultCodespace), 2, cdc, true) + globalKeeper := NewGlobalKeeper(cdc, accKeeper, true) + keeper := NewKeeper(common.DexStoreKey, pairMapper, + codespacer.RegisterNext(dextypes.DefaultCodespace), 2, cdc, globalKeeper) + return keeper +} + +func MakeMiniKeeper(cdc *wire.Codec) *MiniKeeper { + accKeeper := auth.NewAccountKeeper(cdc, common.AccountStoreKey, types.ProtoAppAccount) + codespacer := sdk.NewCodespacer() + pairMapper := store.NewTradingPairMapper(cdc, common.MiniTokenPairStoreKey, true) + globalKeeper := NewGlobalKeeper(cdc, accKeeper, true) + keeper := NewMiniKeeper(common.DexMiniStoreKey, pairMapper, + codespacer.RegisterNext(dextypes.DefaultCodespace), 2, cdc, globalKeeper) return keeper } @@ -77,6 +88,7 @@ func TestKeeper_MatchFailure(t *testing.T) { assert := assert.New(t) cdc := MakeCodec() keeper := MakeKeeper(cdc) + miniKeeper := MakeMiniKeeper(cdc) cms := MakeCMS(nil) logger := log.NewTMLogger(os.Stdout) ctx := sdk.NewContext(cms, abci.Header{}, sdk.RunTxModeCheck, logger) @@ -107,7 +119,8 @@ func TestKeeper_MatchFailure(t *testing.T) { msg = NewNewOrderMsg(accAdd, "123462", Side.BUY, "XYZ-000_BNB", 99000, 15000000) ord = OrderInfo{msg, 42, 0, 42, 0, 0, "", 0} keeper.AddOrder(ord, false) - tradeOuts := keeper.matchAndDistributeTrades(true, 42, 0, false) + symbolsToMatch := keeper.symbolSelector.SelectSymbolsToMatch(keeper.roundOrders, ctx.BlockHeader().Height, 0, false) + tradeOuts := matchAndDistributeTrades(keeper, miniKeeper, true, 42, 0, symbolsToMatch, logger) c := channelHash(accAdd, 4) i := 0 for tr := range tradeOuts[c] { @@ -255,6 +268,7 @@ func TestKeeper_SnapShotAndLoadAfterMatch(t *testing.T) { assert := assert.New(t) cdc := MakeCodec() keeper := MakeKeeper(cdc) + miniKeeper := MakeMiniKeeper(cdc) cms := MakeCMS(nil) logger := log.NewTMLogger(os.Stdout) ctx := sdk.NewContext(cms, abci.Header{}, sdk.RunTxModeCheck, logger) @@ -275,7 +289,7 @@ func TestKeeper_SnapShotAndLoadAfterMatch(t *testing.T) { assert.Equal(3, len(keeper.allOrders["XYZ-000_BNB"])) assert.Equal(1, len(keeper.engines)) - keeper.MatchSymbols(42, 0) + MatchSymbols(42, 0, keeper, miniKeeper,false,logger) _, err := keeper.SnapShotOrderBook(ctx, 43) assert.Nil(err) keeper.MarkBreatheBlock(ctx, 43, time.Now()) @@ -547,7 +561,9 @@ func setup() (ctx sdk.Context, mapper auth.AccountKeeper, keeper *Keeper) { accountCache := getAccountCache(cdc, ms, capKey) pairMapper := store.NewTradingPairMapper(cdc, common.PairStoreKey, false) ctx = sdk.NewContext(ms, abci.Header{ChainID: "mychainid"}, sdk.RunTxModeDeliver, log.NewNopLogger()).WithAccountCache(accountCache) - keeper = NewKeeper(capKey2, mapper, pairMapper, sdk.NewCodespacer().RegisterNext(dextypes.DefaultCodespace), 2, cdc, false) + + globalKeeper := NewGlobalKeeper(cdc, mapper, false) + keeper = NewKeeper(capKey2, pairMapper, sdk.NewCodespacer().RegisterNext(dextypes.DefaultCodespace), 2, cdc, globalKeeper) return } diff --git a/plugins/dex/order/mini_keeper.go b/plugins/dex/order/mini_keeper.go index 1a01456f8..d87b2c92b 100644 --- a/plugins/dex/order/mini_keeper.go +++ b/plugins/dex/order/mini_keeper.go @@ -132,15 +132,5 @@ func (kp *MiniKeeper) validateOrder(ctx sdk.Context, acc sdk.Account, msg NewOrd // override func (kp *MiniKeeper) LoadOrderBookSnapshot(ctx sdk.Context, latestBlockHeight int64, timeOfLatestBlock time.Time, blockInterval, daysBack int) (int64, error) { - //TODO review - //lastBreatheBlockHeight := kp.GetLastBreatheBlockHeight(ctx, latestBlockHeight, timeOfLatestBlock, blockInterval, daysBack) - //upgradeHeight := sdk.UpgradeMgr.GetUpgradeHeight(upgrade.BEP8) - //kp.logger.Info("Loaded MiniKeeper orderbook ", "lastBreatheBlockHeight", lastBreatheBlockHeight, "upgradeHeight", upgradeHeight) - //if lastBreatheBlockHeight < upgradeHeight { - // return lastBreatheBlockHeight, nil - //} else { - // return kp.Keeper.LoadOrderBookSnapshot(ctx, latestBlockHeight, timeOfLatestBlock, blockInterval, daysBack) - //} - return kp.Keeper.LoadOrderBookSnapshot(ctx, latestBlockHeight, timeOfLatestBlock, blockInterval, daysBack) }