Skip to content
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

0.7 Upgrade #744

Merged
merged 22 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ vendor
*.swp
.vscode/*.json

# Test generate tmp files
app/data
app/apptest/data
app_test/data
plugins/param/data
plugins/tokens/data
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 0.7
FEATURES
* [\#725](https://github.com/binance-chain/node/pull/725) [Token] [Dex] BEP8 - Mini-BEP2 token features
* [\#710](https://github.com/binance-chain/node/pull/710) [DEX] BEP70 - Support busd pair listing and trading

IMPROVEMENTS
* [\#704](https://github.com/binance-chain/node/pull/704) [DEX] BEP67 Price-based Order Expiration
* [\#714](https://github.com/binance-chain/node/pull/714) [DEX] Add pendingMatch flag to orderbook query response

## 0.6.3

BUG FIXES
Expand Down
2 changes: 2 additions & 0 deletions admin/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var transferOnlyModeBlackList = []string{
timelock.TimeLockMsg{}.Type(),
timelock.TimeUnlockMsg{}.Type(),
timelock.TimeRelockMsg{}.Type(),
issue.IssueMiniMsg{}.Type(),
issue.IssueTinyMsg{}.Type(),
}

var TxBlackList = map[runtime.Mode][]string{
Expand Down
58 changes: 39 additions & 19 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import (
"github.com/binance-chain/node/plugins/param"
"github.com/binance-chain/node/plugins/param/paramhub"
"github.com/binance-chain/node/plugins/tokens"
tkstore "github.com/binance-chain/node/plugins/tokens/store"
"github.com/binance-chain/node/plugins/tokens/issue"
"github.com/binance-chain/node/plugins/tokens/seturi"
"github.com/binance-chain/node/plugins/tokens/swap"
"github.com/binance-chain/node/plugins/tokens/timelock"
"github.com/binance-chain/node/wire"
Expand Down Expand Up @@ -80,7 +81,7 @@ type BinanceChain struct {
CoinKeeper bank.Keeper
DexKeeper *dex.DexKeeper
AccountKeeper auth.AccountKeeper
TokenMapper tkstore.Mapper
TokenMapper tokens.Mapper
ValAddrCache *ValAddrCache
stakeKeeper stake.Keeper
govKeeper gov.Keeper
Expand All @@ -95,6 +96,8 @@ type BinanceChain struct {
publicationConfig *config.PublicationConfig
publisher pub.MarketDataPublisher

dexConfig *config.DexConfig

// Unlike tendermint, we don't need implement a no-op metrics, usage of this field should
// check nil-ness to know whether metrics collection is turn on
// TODO(#246): make it an aggregated wrapper of all component metrics (i.e. DexKeeper, StakeKeeper)
Expand All @@ -119,6 +122,7 @@ func NewBinanceChain(logger log.Logger, db dbm.DB, traceStore io.Writer, baseApp
upgradeConfig: ServerContext.UpgradeConfig,
abciQueryBlackList: getABCIQueryBlackList(ServerContext.QueryConfig),
publicationConfig: ServerContext.PublicationConfig,
dexConfig: ServerContext.DexConfig,
}
// set upgrade config
SetUpgradeConfig(app.upgradeConfig)
Expand All @@ -127,7 +131,7 @@ func NewBinanceChain(logger log.Logger, db dbm.DB, traceStore io.Writer, baseApp

// mappers
app.AccountKeeper = auth.NewAccountKeeper(cdc, common.AccountStoreKey, types.ProtoAppAccount)
app.TokenMapper = tkstore.NewMapper(cdc, common.TokenStoreKey)
app.TokenMapper = tokens.NewMapper(cdc, common.TokenStoreKey)
app.CoinKeeper = bank.NewBaseKeeper(app.AccountKeeper)
app.ParamHub = paramhub.NewKeeper(cdc, common.ParamsStoreKey, common.TParamsStoreKey)
tradingPairMapper := dex.NewTradingPairMapper(app.Codec, common.PairStoreKey)
Expand Down Expand Up @@ -264,6 +268,10 @@ func SetUpgradeConfig(upgradeConfig *config.UpgradeConfig) {
upgrade.Mgr.AddUpgradeHeight(upgrade.ListingRuleUpgrade, upgradeConfig.ListingRuleUpgradeHeight)
upgrade.Mgr.AddUpgradeHeight(upgrade.FixZeroBalance, upgradeConfig.FixZeroBalanceHeight)

upgrade.Mgr.AddUpgradeHeight(upgrade.BEP8, upgradeConfig.BEP8Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP67, upgradeConfig.BEP67Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP70, upgradeConfig.BEP70Height)

// register store keys of upgrade
upgrade.Mgr.RegisterStoreKeys(upgrade.BEP9, common.TimeLockStoreKey.Name())
upgrade.Mgr.RegisterStoreKeys(upgrade.BEP3, common.AtomicSwapStoreKey.Name())
Expand All @@ -283,6 +291,13 @@ func SetUpgradeConfig(upgradeConfig *config.UpgradeConfig) {
swap.ClaimHTLTMsg{}.Type(),
swap.RefundHTLTMsg{}.Type(),
)
// register msg types of upgrade
upgrade.Mgr.RegisterMsgTypes(upgrade.BEP8,
issue.IssueMiniMsg{}.Type(),
issue.IssueTinyMsg{}.Type(),
seturi.SetURIMsg{}.Type(),
list.ListMiniMsg{}.Type(),
)
}

func getABCIQueryBlackList(queryConfig *config.QueryConfig) map[string]bool {
Expand All @@ -301,10 +316,10 @@ func (app *BinanceChain) initRunningMode() {
}

func (app *BinanceChain) initDex(pairMapper dex.TradingPairMapper) {
app.DexKeeper = dex.NewOrderKeeper(common.DexStoreKey, app.AccountKeeper, pairMapper,
app.RegisterCodespace(dex.DefaultCodespace), app.baseConfig.OrderKeeperConcurrency, app.Codec,
app.publicationConfig.ShouldPublishAny())

app.DexKeeper = dex.NewDexKeeper(common.DexStoreKey, app.AccountKeeper, pairMapper, app.RegisterCodespace(dex.DefaultCodespace), app.baseConfig.OrderKeeperConcurrency, app.Codec, app.publicationConfig.ShouldPublishAny())
app.DexKeeper.SubscribeParamChange(app.ParamHub)
app.DexKeeper.SetBUSDSymbol(app.dexConfig.BUSDSymbol)

// do not proceed if we are in a unit test and `CheckState` is unset.
if app.CheckState == nil {
Expand All @@ -325,11 +340,12 @@ func (app *BinanceChain) initDex(pairMapper dex.TradingPairMapper) {
stateDB,
app.LastBlockHeight(),
app.TxDecoder)

}

func (app *BinanceChain) initPlugins() {
tokens.InitPlugin(app, app.TokenMapper, app.AccountKeeper, app.CoinKeeper, app.timeLockKeeper, app.swapKeeper)
dex.InitPlugin(app, app.DexKeeper, app.TokenMapper, app.AccountKeeper, app.govKeeper)
dex.InitPlugin(app, app.DexKeeper, app.TokenMapper, app.govKeeper)
param.InitPlugin(app, app.ParamHub)
account.InitPlugin(app, app.AccountKeeper)
}
Expand Down Expand Up @@ -520,12 +536,11 @@ func (app *BinanceChain) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) a

isBreatheBlock := app.isBreatheBlock(height, lastBlockTime, blockTime)
var tradesToPublish []*pub.Trade

if sdk.IsUpgrade(upgrade.BEP19) || !isBreatheBlock {
if app.publicationConfig.ShouldPublishAny() && pub.IsLive {
tradesToPublish = pub.MatchAndAllocateAllForPublish(app.DexKeeper, ctx)
tradesToPublish = pub.MatchAndAllocateAllForPublish(app.DexKeeper, ctx, isBreatheBlock)
} else {
app.DexKeeper.MatchAndAllocateAll(ctx, nil)
app.DexKeeper.MatchAndAllocateSymbols(ctx, nil, isBreatheBlock)
}
}

Expand All @@ -545,6 +560,7 @@ func (app *BinanceChain) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) a
}

app.DexKeeper.StoreTradePrices(ctx)

blockFee := distributeFee(ctx, app.AccountKeeper, app.ValAddrCache, app.publicationConfig.PublishBlockFee)

tags, passed, failed := gov.EndBlocker(ctx, app.govKeeper)
Expand Down Expand Up @@ -763,10 +779,13 @@ func (app *BinanceChain) publish(tradesToPublish []*pub.Trade, proposalsToPublis
var blockToPublish *pub.Block
var latestPriceLevels order.ChangedPriceLevelsMap

orderChanges := app.DexKeeper.GetAllOrderChanges()
orderInfoForPublish := app.DexKeeper.GetAllOrderInfosForPub()

duration := pub.Timer(app.Logger, fmt.Sprintf("collect publish information, height=%d", height), func() {
if app.publicationConfig.PublishAccountBalance {
txRelatedAccounts := app.Pool.TxRelatedAddrs()
tradeRelatedAccounts := pub.GetTradeAndOrdersRelatedAccounts(app.DexKeeper, tradesToPublish)
tradeRelatedAccounts := pub.GetTradeAndOrdersRelatedAccounts(tradesToPublish, orderChanges, orderInfoForPublish)
accountsToPublish = pub.GetAccountBalances(
app.AccountKeeper,
ctx,
Expand Down Expand Up @@ -795,33 +814,34 @@ func (app *BinanceChain) publish(tradesToPublish []*pub.Trade, proposalsToPublis
pub.Logger.Info("start to publish", "height", height,
"blockTime", blockTime, "numOfTrades", len(tradesToPublish),
"numOfOrders", // the order num we collected here doesn't include trade related orders
len(app.DexKeeper.OrderChanges),
len(orderChanges),
"numOfProposals",
proposalsToPublish.NumOfMsgs,
"numOfStakeUpdates",
stakeUpdates.NumOfMsgs,
"numOfAccounts",
len(accountsToPublish))
pub.ToRemoveOrderIdCh = make(chan string, pub.ToRemoveOrderIdChannelSize)
pub.ToRemoveOrderIdCh = make(chan pub.OrderSymbolId, pub.ToRemoveOrderIdChannelSize)

pub.ToPublishCh <- pub.NewBlockInfoToPublish(
height,
blockTime,
tradesToPublish,
proposalsToPublish,
stakeUpdates,
app.DexKeeper.OrderChanges, // thread-safety is guarded by the signal from RemoveDoneCh
app.DexKeeper.OrderInfosForPub, // thread-safety is guarded by the signal from RemoveDoneCh
orderChanges, // thread-safety is guarded by the signal from RemoveDoneCh
orderInfoForPublish, // thread-safety is guarded by the signal from RemoveDoneCh
accountsToPublish,
latestPriceLevels,
blockFee,
app.DexKeeper.RoundOrderFees,
app.DexKeeper.RoundOrderFees, //only use DexKeeper RoundOrderFees
transferToPublish,
blockToPublish)

// remove item from OrderInfoForPublish when we published removed order (cancel, iocnofill, fullyfilled, expired)
for id := range pub.ToRemoveOrderIdCh {
pub.Logger.Debug("delete order from order changes map", "orderId", id)
delete(app.DexKeeper.OrderInfosForPub, id)
for o := range pub.ToRemoveOrderIdCh {
pub.Logger.Debug("delete order from order changes map", "symbol", o.Symbol, "orderId", o.Id)
app.DexKeeper.RemoveOrderInfosForPub(o.Symbol, o.Id)
}

pub.Logger.Debug("finish publish", "height", height)
Expand Down
10 changes: 5 additions & 5 deletions app/app_pub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func setupAppTest(t *testing.T) (*assert.Assertions, *require.Assertions, *Binan
pub.IsLive = true

keeper := app.DexKeeper
keeper.CollectOrderInfoForPublish = true
keeper.EnablePublish()
tradingPair := dextypes.NewTradingPair("XYZ-000", "BNB", 102000)
keeper.PairMapper.AddTradingPair(app.DeliverState.Ctx, tradingPair)
keeper.AddEngine(tradingPair)
Expand All @@ -112,8 +112,8 @@ func setupAppTest(t *testing.T) (*assert.Assertions, *require.Assertions, *Binan
keeper.FeeManager.FeeConfig.CancelFee = 12
keeper.FeeManager.FeeConfig.CancelFeeNative = 6

_, buyerAcc := testutils.NewAccountForPub(ctx, am, 100000000000, 0, 0) // give user enough coins to pay the fee
_, sellerAcc := testutils.NewAccountForPub(ctx, am, 100000000000, 0, 0)
_, buyerAcc := testutils.NewAccountForPub(ctx, am, 100000000000, 0, 0, "XYZ-000") // give user enough coins to pay the fee
_, sellerAcc := testutils.NewAccountForPub(ctx, am, 100000000000, 0, 0, "XYZ-000")
return assert.New(t), require.New(t), app, buyerAcc, sellerAcc
}

Expand All @@ -140,7 +140,7 @@ func TestAppPub_MatchOrder(t *testing.T) {

ctx := app.DeliverState.Ctx
msg := orderPkg.NewNewOrderMsg(buyerAcc.GetAddress(), orderPkg.GenerateOrderID(1, buyerAcc.GetAddress()), orderPkg.Side.BUY, "XYZ-000_BNB", 102000, 300000000)
handler := orderPkg.NewHandler(app.GetCodec(), app.DexKeeper, app.AccountKeeper)
handler := orderPkg.NewHandler(app.DexKeeper)
app.DeliverState.Ctx = app.DeliverState.Ctx.WithBlockHeight(41).WithBlockTime(time.Unix(0, 100))
buyerAcc.SetSequence(1)
app.AccountKeeper.SetAccount(ctx, buyerAcc)
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestAppPub_MatchOrder(t *testing.T) {

func TestAppPub_MatchAndCancelFee(t *testing.T) {
assert, require, app, buyerAcc, sellerAcc := setupAppTest(t)
handler := orderPkg.NewHandler(app.GetCodec(), app.DexKeeper, app.AccountKeeper)
handler := orderPkg.NewHandler(app.DexKeeper)
ctx := app.DeliverState.Ctx

// ==== Place a to-be-matched sell order and a to-be-cancelled buy order (in different symbol)
Expand Down
4 changes: 3 additions & 1 deletion app/apptest/match_allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func SetupTest(initPrices ...int64) (crypto.Address, sdk.Context, []sdk.Account)
func SetupTest_new(initPrices ...int64) (crypto.Address, sdk.Context, []sdk.Account) {
// for new match engine
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP19, -1)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP8, -1)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP70, -1)
addr := secp256k1.GenPrivKey().PubKey().Address()
accAddr := sdk.AccAddress(addr)
baseAcc := auth.BaseAccount{Address: accAddr}
Expand Down Expand Up @@ -230,7 +232,7 @@ func GetOrderId(add sdk.AccAddress, seq int64, ctx sdk.Context) string {
func GetOrderBook(pair string) ([]level, []level) {
buys := make([]level, 0)
sells := make([]level, 0)
orderbooks := testApp.DexKeeper.GetOrderBookLevels(pair, 25)
orderbooks, _ := testApp.DexKeeper.GetOrderBookLevels(pair, 25)
for _, l := range orderbooks {
if l.BuyPrice != 0 {
buys = append(buys, level{price: l.BuyPrice, qty: l.BuyQty})
Expand Down
44 changes: 32 additions & 12 deletions app/apptest/ordertx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ type level struct {
qty utils.Fixed8
}

func getOrderBook(pair string) ([]level, []level) {
func getOrderBook(pair string) ([]level, []level, bool) {
buys := make([]level, 0)
sells := make([]level, 0)
orderbooks := testApp.DexKeeper.GetOrderBookLevels(pair, 5)
orderbooks, pendingMatch := testApp.DexKeeper.GetOrderBookLevels(pair, 5)
for _, l := range orderbooks {
if l.BuyPrice != 0 {
buys = append(buys, level{price: l.BuyPrice, qty: l.BuyQty})
Expand All @@ -34,7 +34,7 @@ func getOrderBook(pair string) ([]level, []level) {
sells = append(sells, level{price: l.SellPrice, qty: l.SellQty})
}
}
return buys, sells
return buys, sells, pendingMatch
}

func genOrderID(add sdk.AccAddress, seq int64, ctx sdk.Context, am auth.AccountKeeper) string {
Expand Down Expand Up @@ -124,6 +124,12 @@ func Test_handleNewOrder_DeliverTx(t *testing.T) {
tradingPair := types.NewTradingPair("BTC-000", "BNB", 1e8)
testApp.DexKeeper.PairMapper.AddTradingPair(ctx, tradingPair)
testApp.DexKeeper.AddEngine(tradingPair)
testApp.DexKeeper.GetEngines()["BTC-000_BNB"].LastMatchHeight = -1

tradingPair2 := types.NewTradingPair("ETH-001", "BNB", 1e8)
testApp.DexKeeper.PairMapper.AddTradingPair(ctx, tradingPair2)
testApp.DexKeeper.AddEngine(tradingPair2)
testApp.DexKeeper.GetEngines()["ETH-001_BNB"].LastMatchHeight = -1

add := Account(0).GetAddress()
oid := fmt.Sprintf("%X-0", add)
Expand All @@ -133,11 +139,17 @@ func Test_handleNewOrder_DeliverTx(t *testing.T) {
t.Logf("res is %v and error is %v", res, e)
assert.Equal(uint32(0), res.Code)
assert.Nil(e)
buys, sells := getOrderBook("BTC-000_BNB")
buys, sells, pendingMatch := getOrderBook("BTC-000_BNB")
assert.Equal(1, len(buys))
assert.Equal(0, len(sells))
assert.Equal(true, pendingMatch)
assert.Equal(utils.Fixed8(355e8), buys[0].price)
assert.Equal(utils.Fixed8(1e8), buys[0].qty)

buys, sells, pendingMatch = getOrderBook("ETH-001_BNB")
assert.Equal(0, len(buys))
assert.Equal(0, len(sells))
assert.Equal(false, pendingMatch)
}

func Test_Match(t *testing.T) {
Expand All @@ -149,9 +161,11 @@ func Test_Match(t *testing.T) {
ethPair := types.NewTradingPair("ETH-000", "BNB", 97e8)
testApp.DexKeeper.PairMapper.AddTradingPair(ctx, ethPair)
testApp.DexKeeper.AddEngine(ethPair)
testApp.DexKeeper.GetEngines()["ETH-000_BNB"].LastMatchHeight = -1
btcPair := types.NewTradingPair("BTC-000", "BNB", 96e8)
testApp.DexKeeper.PairMapper.AddTradingPair(ctx, btcPair)
testApp.DexKeeper.AddEngine(btcPair)
testApp.DexKeeper.GetEngines()["BTC-000_BNB"].LastMatchHeight = -1
testApp.DexKeeper.FeeManager.UpdateConfig(newTestFeeConfig())

// setup accounts
Expand Down Expand Up @@ -196,13 +210,17 @@ func Test_Match(t *testing.T) {
t.Logf("res is %v and error is %v", res, e)
msg = o.NewNewOrderMsg(add, genOrderID(add, 3, ctx, am), 1, "BTC-000_BNB", 98e8, 300e8)
res, e = testClient.DeliverTxSync(msg, testApp.Codec)
buys, sells := getOrderBook("BTC-000_BNB")
buys, sells, pendingMatch := getOrderBook("BTC-000_BNB")
assert.Equal(4, len(buys))
assert.Equal(3, len(sells))
testApp.DexKeeper.MatchAndAllocateAll(ctx, nil)
buys, sells = getOrderBook("BTC-000_BNB")

assert.Equal(true, pendingMatch)
testApp.DexKeeper.MatchAndAllocateSymbols(ctx, nil, false)
buys, sells, pendingMatch = getOrderBook("BTC-000_BNB")

assert.Equal(0, len(buys))
assert.Equal(3, len(sells))
assert.Equal(false, pendingMatch)

trades, lastPx := testApp.DexKeeper.GetLastTradesForPair("BTC-000_BNB")
assert.Equal(int64(96e8), lastPx)
Expand Down Expand Up @@ -247,20 +265,21 @@ func Test_Match(t *testing.T) {
res, e = testClient.DeliverTxSync(msg, testApp.Codec)
t.Logf("res is %v and error is %v", res, e)

buys, sells = getOrderBook("BTC-000_BNB")
buys, sells, _ = getOrderBook("BTC-000_BNB")
assert.Equal(0, len(buys))
assert.Equal(3, len(sells))
buys, sells = getOrderBook("ETH-000_BNB")
buys, sells, _ = getOrderBook("ETH-000_BNB")
assert.Equal(4, len(buys))
assert.Equal(3, len(sells))

testApp.DexKeeper.MatchAndAllocateAll(ctx, nil)
buys, sells = getOrderBook("ETH-000_BNB")
testApp.DexKeeper.MatchAndAllocateSymbols(ctx, nil, false)
buys, sells, _ = getOrderBook("ETH-000_BNB")

t.Logf("buys: %v", buys)
t.Logf("sells: %v", sells)
assert.Equal(1, len(buys))
assert.Equal(2, len(sells))
buys, sells = getOrderBook("BTC-000_BNB")
buys, sells, _ = getOrderBook("BTC-000_BNB")
assert.Equal(0, len(buys))
assert.Equal(3, len(sells))
trades, lastPx = testApp.DexKeeper.GetLastTradesForPair("ETH-000_BNB")
Expand Down Expand Up @@ -301,6 +320,7 @@ func Test_handleCancelOrder_CheckTx(t *testing.T) {
tradingPair := types.NewTradingPair("BTC-000", "BNB", 1e8)
testApp.DexKeeper.PairMapper.AddTradingPair(ctx, tradingPair)
testApp.DexKeeper.AddEngine(tradingPair)
testApp.DexKeeper.GetEngines()["BTC-000_BNB"].LastMatchHeight = -1
testApp.DexKeeper.FeeManager.UpdateConfig(newTestFeeConfig())

// setup accounts
Expand Down
Loading