From f21fce1cfd80dff905580041444ffd59afd60e07 Mon Sep 17 00:00:00 2001 From: zjubfd <296179868@qq.com> Date: Wed, 30 Oct 2019 16:38:51 +0800 Subject: [PATCH] [R4R] fix account may have currency with zero balance (#679) * hardfork: fix possible zero currency balance * add change log * fix review suggest --- CHANGELOG_PENDING.md | 1 + app/app.go | 1 + app/config/config.go | 4 ++++ common/upgrade/upgrade.go | 1 + plugins/dex/order/keeper.go | 9 ++++++--- 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index b20233e2b..691db3459 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -2,6 +2,7 @@ ## Develop BUG FIXES +* [\#677](https://github.com/binance-chain/node/pull/677) [Dex] fix account may have currency with zero balance IMPROVEMENTS * [\#672](https://github.com/binance-chain/node/pull/672) [DEX] Change listing rule diff --git a/app/app.go b/app/app.go index b95f30f4e..d76a29dfe 100644 --- a/app/app.go +++ b/app/app.go @@ -260,6 +260,7 @@ func SetUpgradeConfig(upgradeConfig *config.UpgradeConfig) { upgrade.Mgr.AddUpgradeHeight(upgrade.FixSignBytesOverflow, upgradeConfig.FixSignBytesOverflowHeight) upgrade.Mgr.AddUpgradeHeight(upgrade.LotSizeOptimization, upgradeConfig.LotSizeUpgradeHeight) upgrade.Mgr.AddUpgradeHeight(upgrade.ListingRuleUpgrade, upgradeConfig.ListingRuleUpgradeHeight) + upgrade.Mgr.AddUpgradeHeight(upgrade.FixZeroBalance, upgradeConfig.FixZeroBalanceHeight) // register store keys of upgrade upgrade.Mgr.RegisterStoreKeys(upgrade.BEP9, common.TimeLockStoreKey.Name()) diff --git a/app/config/config.go b/app/config/config.go index d9389f355..f081c0b5c 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -65,6 +65,8 @@ FixSignBytesOverflowHeight = {{ .UpgradeConfig.FixSignBytesOverflowHeight }} LotSizeUpgradeHeight = {{ .UpgradeConfig.LotSizeUpgradeHeight }} # Block height of changing listing rule upgrade ListingRuleUpgradeHeight = {{ .UpgradeConfig.ListingRuleUpgradeHeight }} +# Block height of FixZeroBalanceHeight upgrade +FixZeroBalanceHeight = {{ .UpgradeConfig.FixZeroBalanceHeight }} [query] # ABCI query interface black list, suggested value: ["custom/gov/proposals", "custom/timelock/timelocks", "custom/atomicSwap/swapcreator", "custom/atomicSwap/swaprecipient"] @@ -353,6 +355,7 @@ type UpgradeConfig struct { FixSignBytesOverflowHeight int64 `mapstructure:"FixSignBytesOverflowHeight"` LotSizeUpgradeHeight int64 `mapstructure:"LotSizeUpgradeHeight"` ListingRuleUpgradeHeight int64 `mapstructure:"ListingRuleUpgradeHeight"` + FixZeroBalanceHeight int64 `mapstructure:"FixZeroBalanceHeight"` } func defaultUpgradeConfig() *UpgradeConfig { @@ -367,6 +370,7 @@ func defaultUpgradeConfig() *UpgradeConfig { FixSignBytesOverflowHeight: math.MaxInt64, LotSizeUpgradeHeight: math.MaxInt64, ListingRuleUpgradeHeight: math.MaxInt64, + FixZeroBalanceHeight: math.MaxInt64, } } diff --git a/common/upgrade/upgrade.go b/common/upgrade/upgrade.go index 059219b8c..c1ecba7de 100644 --- a/common/upgrade/upgrade.go +++ b/common/upgrade/upgrade.go @@ -21,6 +21,7 @@ const ( FixSignBytesOverflow = sdk.FixSignBytesOverflow LotSizeOptimization = "LotSizeOptimization" ListingRuleUpgrade = "ListingRuleUpgrade" // Remove restriction that only the owner of base asset can list trading pair + FixZeroBalance = "FixZeroBalance" ) func UpgradeBEP10(before func(), after func()) { diff --git a/plugins/dex/order/keeper.go b/plugins/dex/order/keeper.go index 86a6e318a..31cb17e62 100644 --- a/plugins/dex/order/keeper.go +++ b/plugins/dex/order/keeper.go @@ -609,9 +609,12 @@ func (kp *Keeper) doTransfer(ctx sdk.Context, tran *Transfer) sdk.Error { panic(errors.New("unlocked tokens cannot cover the expense")) } account.SetLockedCoins(newLocked) - account.SetCoins(account.GetCoins(). - Plus(sdk.Coins{sdk.NewCoin(tran.inAsset, tran.in)}). - Plus(sdk.Coins{sdk.NewCoin(tran.outAsset, tran.unlock-tran.out)})) + accountCoin := account.GetCoins(). + Plus(sdk.Coins{sdk.NewCoin(tran.inAsset, tran.in)}) + if remain := tran.unlock-tran.out; remain > 0 || !sdk.IsUpgrade(upgrade.FixZeroBalance) { + accountCoin = accountCoin.Plus(sdk.Coins{sdk.NewCoin(tran.outAsset, remain)}) + } + account.SetCoins(accountCoin) kp.am.SetAccount(ctx, account) kp.logger.Debug("Performed Trade Allocation", "account", account, "allocation", tran.String())