Skip to content

Commit

Permalink
BEP8 Mini token features (#725)
Browse files Browse the repository at this point in the history
*support mini-token
  • Loading branch information
EnderCrypto authored and forcodedancing committed May 19, 2022
1 parent 2efcc2c commit d90c200
Show file tree
Hide file tree
Showing 116 changed files with 4,773 additions and 1,120 deletions.
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
52 changes: 33 additions & 19 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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 @@ -79,7 +80,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 Down Expand Up @@ -129,7 +130,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 @@ -265,6 +266,8 @@ func SetUpgradeConfig(upgradeConfig *config.UpgradeConfig) {
upgrade.Mgr.AddUpgradeHeight(upgrade.LotSizeOptimization, upgradeConfig.LotSizeUpgradeHeight)
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)

Expand All @@ -287,6 +290,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 @@ -305,9 +315,8 @@ 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)

Expand All @@ -330,11 +339,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 @@ -525,12 +535,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 @@ -550,6 +559,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 @@ -768,10 +778,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 @@ -800,33 +813,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
2 changes: 2 additions & 0 deletions 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
12 changes: 10 additions & 2 deletions app/apptest/ordertx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +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 Down Expand Up @@ -159,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 @@ -209,9 +213,11 @@ func Test_Match(t *testing.T) {
buys, sells, pendingMatch := getOrderBook("BTC-000_BNB")
assert.Equal(4, len(buys))
assert.Equal(3, len(sells))

assert.Equal(true, pendingMatch)
testApp.DexKeeper.MatchAndAllocateAll(ctx, nil)
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)
Expand Down Expand Up @@ -266,8 +272,9 @@ func Test_Match(t *testing.T) {
assert.Equal(4, len(buys))
assert.Equal(3, len(sells))

testApp.DexKeeper.MatchAndAllocateAll(ctx, nil)
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))
Expand Down Expand Up @@ -313,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
12 changes: 9 additions & 3 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ LotSizeUpgradeHeight = {{ .UpgradeConfig.LotSizeUpgradeHeight }}
ListingRuleUpgradeHeight = {{ .UpgradeConfig.ListingRuleUpgradeHeight }}
# Block height of FixZeroBalanceHeight upgrade
FixZeroBalanceHeight = {{ .UpgradeConfig.FixZeroBalanceHeight }}
# Block height of BEP8 upgrade
BEP8Height = {{ .UpgradeConfig.BEP8Height }}
# Block height of BEP67 upgrade
BEP67Height = {{ .UpgradeConfig.BEP67Height }}
# Block height of BEP70 upgrade
Expand Down Expand Up @@ -360,6 +362,7 @@ func defaultBaseConfig() *BaseConfig {
}

type UpgradeConfig struct {

// Galileo Upgrade
BEP6Height int64 `mapstructure:"BEP6Height"`
BEP9Height int64 `mapstructure:"BEP9Height"`
Expand All @@ -368,14 +371,16 @@ type UpgradeConfig struct {
// Hubble Upgrade
BEP12Height int64 `mapstructure:"BEP12Height"`
// Archimedes Upgrade
BEP3Height int64 `mapstructure:"BEP3Height"`
BEP3Height int64 `mapstructure:"BEP3Height"`

// TODO: add upgrade name
FixSignBytesOverflowHeight int64 `mapstructure:"FixSignBytesOverflowHeight"`
LotSizeUpgradeHeight int64 `mapstructure:"LotSizeUpgradeHeight"`
ListingRuleUpgradeHeight int64 `mapstructure:"ListingRuleUpgradeHeight"`
FixZeroBalanceHeight int64 `mapstructure:"FixZeroBalanceHeight"`

// TODO: add upgrade name
BEP8Height int64 `mapstructure:"BEP8Height"`
BEP67Height int64 `mapstructure:"BEP67Height"`
BEP70Height int64 `mapstructure:"BEP70Height"`
}
Expand All @@ -393,8 +398,9 @@ func defaultUpgradeConfig() *UpgradeConfig {
LotSizeUpgradeHeight: math.MaxInt64,
ListingRuleUpgradeHeight: math.MaxInt64,
FixZeroBalanceHeight: math.MaxInt64,
BEP67Height: 1,
BEP70Height: 1,
BEP8Height: math.MaxInt64,
BEP67Height: math.MaxInt64,
BEP70Height: math.MaxInt64,
}
}

Expand Down
11 changes: 4 additions & 7 deletions app/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,12 @@ func (app *BinanceChain) processErrAbciResponseForPub(txBytes []byte) {
case order.NewOrderMsg:
app.Logger.Info("failed to process NewOrderMsg", "oid", msg.Id)
// The error on deliver should be rare and only impact witness publisher's performance
app.DexKeeper.OrderChangesMtx.Lock()
app.DexKeeper.OrderChanges = append(app.DexKeeper.OrderChanges, order.OrderChange{msg.Id, order.FailedBlocking, "", msg})
app.DexKeeper.OrderChangesMtx.Unlock()
app.DexKeeper.UpdateOrderChangeSync(order.OrderChange{msg.Id, order.FailedBlocking, "", msg}, msg.Symbol)
case order.CancelOrderMsg:
app.Logger.Info("failed to process CancelOrderMsg", "oid", msg.RefId)
// The error on deliver should be rare and only impact witness publisher's performance
app.DexKeeper.OrderChangesMtx.Lock()
// OrderInfo must has been in keeper.OrderInfosForPub
app.DexKeeper.OrderChanges = append(app.DexKeeper.OrderChanges, order.OrderChange{msg.RefId, order.FailedBlocking, "", msg})
app.DexKeeper.OrderChangesMtx.Unlock()
// OrderInfo must has been in keeper.orderInfosForPub
app.DexKeeper.UpdateOrderChangeSync(order.OrderChange{msg.RefId, order.FailedBlocking, "", msg}, msg.Symbol)
default:
// deliberately do nothing for message other than NewOrderMsg
// in future, we may publish fail status of send msg
Expand Down Expand Up @@ -193,6 +189,7 @@ func (app *BinanceChain) reInitChain() error {
snapshot.Manager().GetStateDB(),
app.LastBlockHeight(),
app.TxDecoder)

app.initParams()

// init app cache
Expand Down
Loading

0 comments on commit d90c200

Please sign in to comment.