Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Erheng Lu committed May 6, 2020
1 parent 3de38ca commit d92a2ca
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 44 deletions.
3 changes: 2 additions & 1 deletion app/pub/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
18 changes: 12 additions & 6 deletions plugins/dex/matcheng/engine_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/binance-chain/node/common/utils"
)

func (me *MatchEng) Match(height int64) bool {
func (me *MatchEng) Match(height int64, isMini bool) bool {
if !sdk.IsUpgrade(upgrade.BEP19) {
return me.MatchBeforeGalileo(height)
}
Expand All @@ -32,9 +32,15 @@ func (me *MatchEng) Match(height int64) bool {
me.logger.Error("dropRedundantQty failed", "error", err)
return false
}
//If order height >= the last Match height, then it's maker.
//If order height > the last Match height, then it's maker.
// Block Height cannot be used here since mini-token is not matched in every block
takerSide, err := me.determineTakerSide(me.LastMatchHeight, index)
var lastMatchHeight int64
if isMini {
lastMatchHeight = me.LastMatchHeight
}else{
lastMatchHeight = height -1 //Every block is deemed as performed matching for all BEP2 symbols
}
takerSide, err := me.determineTakerSide(lastMatchHeight, index)
if err != nil {
me.logger.Error("determineTakerSide failed", "error", err)
return false
Expand Down Expand Up @@ -120,10 +126,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
Expand Down
4 changes: 2 additions & 2 deletions plugins/dex/matcheng/match_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ func TestMatchEng_Match(t *testing.T) {
me.Book.InsertOrder("16", BUYSIDE, 100, 100, 20)

upgrade.Mgr.SetHeight(100)
assert.True(me.Match(100))
assert.True(me.Match(100, false))
assert.Equal(4, len(me.overLappedLevel))
assert.Equal(int64(100), me.LastTradePrice)
assert.Equal([]Trade{
Expand Down Expand Up @@ -979,7 +979,7 @@ func TestMatchEng_Match(t *testing.T) {
me.Book.InsertOrder("6", BUYSIDE, 100, 110, 40)
me.Book.InsertOrder("8", BUYSIDE, 100, 110, 100)

assert.True(me.Match(100))
assert.True(me.Match(100, false))
assert.Equal(4, len(me.overLappedLevel))
assert.Equal(int64(104), me.LastTradePrice)
assert.Equal([]Trade{
Expand Down
38 changes: 21 additions & 17 deletions plugins/dex/order/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,34 @@ 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)
pairMapper := store.NewTradingPairMapper(cdc, key, false)
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) {
Expand All @@ -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)
Expand All @@ -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{
{
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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())
}
2 changes: 1 addition & 1 deletion plugins/dex/order/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions plugins/dex/order/keeper_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func matchAndDistributeTrades(dexKeeper *Keeper, dexMiniKeeper *MiniKeeper, dist

func MatchSymbols(height, timestamp int64, dexKeeper *Keeper, dexMiniKeeper *MiniKeeper, matchAllSymbols bool, logger tmlog.Logger) {
symbolsToMatch := dexKeeper.symbolSelector.SelectSymbolsToMatch(dexKeeper.roundOrders, height, timestamp, matchAllSymbols)
symbolsToMatch = append(symbolsToMatch, dexKeeper.symbolSelector.SelectSymbolsToMatch(dexKeeper.roundOrders, height, timestamp, matchAllSymbols)...)
symbolsToMatch = append(symbolsToMatch, dexMiniKeeper.symbolSelector.SelectSymbolsToMatch(dexMiniKeeper.roundOrders, height, timestamp, matchAllSymbols)...)
logger.Debug("symbols to match", "symbols", symbolsToMatch)

tradeOuts := matchAndDistributeTrades(dexKeeper, dexMiniKeeper, true, height, timestamp, symbolsToMatch, logger)
Expand All @@ -130,7 +130,7 @@ func (kp *Keeper) matchAndDistributeTradesForSymbol(symbol string, height, times
concurrency := len(tradeOuts)
// please note there is no logging in matching, expecting to see the order book details
// from the exchange's order book stream.
if engine.Match(height) {
if engine.Match(height, dexUtils.IsMiniTokenTradingPair(symbol)) {
kp.logger.Debug("Match finish:", "symbol", symbol, "lastTradePrice", engine.LastTradePrice)
for i := range engine.Trades {
t := &engine.Trades[i]
Expand Down
1 change: 1 addition & 0 deletions plugins/dex/order/keeper_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 21 additions & 5 deletions plugins/dex/order/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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] {
Expand Down Expand Up @@ -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)
Expand All @@ -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())
Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 0 additions & 10 deletions plugins/dex/order/mini_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit d92a2ca

Please sign in to comment.