Skip to content

Commit

Permalink
[R4R] fix account may have currency with zero balance (#679)
Browse files Browse the repository at this point in the history
* hardfork: fix possible zero currency balance

* add change log

* fix review suggest
  • Loading branch information
unclezoro authored and forcodedancing committed May 19, 2022
1 parent 904bda9 commit f21fce1
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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 {
Expand All @@ -367,6 +370,7 @@ func defaultUpgradeConfig() *UpgradeConfig {
FixSignBytesOverflowHeight: math.MaxInt64,
LotSizeUpgradeHeight: math.MaxInt64,
ListingRuleUpgradeHeight: math.MaxInt64,
FixZeroBalanceHeight: math.MaxInt64,
}
}

Expand Down
1 change: 1 addition & 0 deletions common/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
9 changes: 6 additions & 3 deletions plugins/dex/order/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit f21fce1

Please sign in to comment.