From 131f6e079c9689c017330f344164dd0df327e5b7 Mon Sep 17 00:00:00 2001 From: Daniil Lashin Date: Sun, 18 Nov 2018 16:32:06 +0300 Subject: [PATCH] More fixes on issue with negative coin reserve --- CHANGELOG.md | 7 +++++++ core/transaction/buy_coin.go | 18 ++++++++++++++++++ core/transaction/create_coin.go | 6 ++++++ core/transaction/create_multisig.go | 6 ++++++ core/transaction/declare_candidacy.go | 6 ++++++ core/transaction/delegate.go | 6 ++++++ core/transaction/multisend.go | 6 ++++++ core/transaction/redeem_check.go | 6 ++++++ core/transaction/sell_all_coin.go | 6 ++++++ core/transaction/sell_coin.go | 6 ++++++ core/transaction/send.go | 6 ++++++ core/transaction/switch_candidate_status.go | 12 ++++++++++++ core/transaction/unbond.go | 6 ++++++ formula/formula.go | 9 +++++++++ version/version.go | 4 ++-- 15 files changed, 108 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2425045a7..ae0871215 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.7.3 +*Nov 18th, 2018* + +BUG FIXES + +- [core] More fixes on issue with negative coin reserve + ## 0.7.2 *Nov 18th, 2018* diff --git a/core/transaction/buy_coin.go b/core/transaction/buy_coin.go index a46e3f2f2..12614008c 100644 --- a/core/transaction/buy_coin.go +++ b/core/transaction/buy_coin.go @@ -79,6 +79,12 @@ func (data BuyCoinData) Run(sender types.Address, tx *Transaction, context *stat } commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } if context.GetBalance(sender, tx.GasCoin).Cmp(commission) < 0 { @@ -122,6 +128,12 @@ func (data BuyCoinData) Run(sender types.Address, tx *Transaction, context *stat value = formula.CalculateSaleAmount(coin.Volume, coin.ReserveBalance, coin.Crr, data.ValueToBuy) + if value == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } + if context.GetBalance(sender, data.CoinToSell).Cmp(value) < 0 { return Response{ Code: code.InsufficientFunds, @@ -159,6 +171,12 @@ func (data BuyCoinData) Run(sender types.Address, tx *Transaction, context *stat value = formula.CalculateSaleAmount(coinFrom.Volume, coinFrom.ReserveBalance, coinFrom.Crr, baseCoinNeeded) + if value == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } + if context.GetBalance(sender, data.CoinToSell).Cmp(value) < 0 { return Response{ Code: code.InsufficientFunds, diff --git a/core/transaction/create_coin.go b/core/transaction/create_coin.go index a34653193..c920008dd 100644 --- a/core/transaction/create_coin.go +++ b/core/transaction/create_coin.go @@ -102,6 +102,12 @@ func (data CreateCoinData) Run(sender types.Address, tx *Transaction, context *s } commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } if context.GetBalance(sender, tx.GasCoin).Cmp(commission) < 0 { diff --git a/core/transaction/create_multisig.go b/core/transaction/create_multisig.go index 77faea898..f64d871be 100644 --- a/core/transaction/create_multisig.go +++ b/core/transaction/create_multisig.go @@ -66,6 +66,12 @@ func (data CreateMultisigData) Run(sender types.Address, tx *Transaction, contex } commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } if context.GetBalance(sender, tx.GasCoin).Cmp(commission) < 0 { diff --git a/core/transaction/declare_candidacy.go b/core/transaction/declare_candidacy.go index efbe20b74..869ed7164 100644 --- a/core/transaction/declare_candidacy.go +++ b/core/transaction/declare_candidacy.go @@ -85,6 +85,12 @@ func (data DeclareCandidacyData) Run(sender types.Address, tx *Transaction, cont } commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } if context.GetBalance(sender, data.Coin).Cmp(data.Stake) < 0 { diff --git a/core/transaction/delegate.go b/core/transaction/delegate.go index d10d29809..4737ac462 100644 --- a/core/transaction/delegate.go +++ b/core/transaction/delegate.go @@ -67,6 +67,12 @@ func (data DelegateData) Run(sender types.Address, tx *Transaction, context *sta } commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } if context.GetBalance(sender, tx.GasCoin).Cmp(commission) < 0 { diff --git a/core/transaction/multisend.go b/core/transaction/multisend.go index 4b7d50fce..5d298e8ef 100644 --- a/core/transaction/multisend.go +++ b/core/transaction/multisend.go @@ -85,6 +85,12 @@ func (data MultisendData) Run(sender types.Address, tx *Transaction, context *st } commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } if err := checkBalances(context, sender, data.List, commission, tx.GasCoin); err != nil { diff --git a/core/transaction/redeem_check.go b/core/transaction/redeem_check.go index 0cd47cfc2..70163fb3d 100644 --- a/core/transaction/redeem_check.go +++ b/core/transaction/redeem_check.go @@ -125,6 +125,12 @@ func (data RedeemCheckData) Run(sender types.Address, tx *Transaction, context * if !decodedCheck.Coin.IsBaseCoin() { coin := context.GetStateCoin(decodedCheck.Coin) commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } totalTxCost := big.NewInt(0).Add(decodedCheck.Value, commission) diff --git a/core/transaction/sell_all_coin.go b/core/transaction/sell_all_coin.go index 2dd9a61ac..8e1850d3a 100644 --- a/core/transaction/sell_all_coin.go +++ b/core/transaction/sell_all_coin.go @@ -72,6 +72,12 @@ func (data SellAllCoinData) Run(sender types.Address, tx *Transaction, context * } commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } if context.GetBalance(sender, data.CoinToSell).Cmp(commission) < 0 { diff --git a/core/transaction/sell_coin.go b/core/transaction/sell_coin.go index 554a1d68f..a22fda3d3 100644 --- a/core/transaction/sell_coin.go +++ b/core/transaction/sell_coin.go @@ -79,6 +79,12 @@ func (data SellCoinData) Run(sender types.Address, tx *Transaction, context *sta } commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } if context.GetBalance(sender, data.CoinToSell).Cmp(data.ValueToSell) < 0 { diff --git a/core/transaction/send.go b/core/transaction/send.go index a58128e2c..0fe91d14a 100644 --- a/core/transaction/send.go +++ b/core/transaction/send.go @@ -67,6 +67,12 @@ func (data SendData) Run(sender types.Address, tx *Transaction, context *state.S } commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } if context.GetBalance(sender, data.Coin).Cmp(data.Value) < 0 { diff --git a/core/transaction/switch_candidate_status.go b/core/transaction/switch_candidate_status.go index 1ccbd4019..3e5e37633 100644 --- a/core/transaction/switch_candidate_status.go +++ b/core/transaction/switch_candidate_status.go @@ -54,6 +54,12 @@ func (data SetCandidateOnData) Run(sender types.Address, tx *Transaction, contex } commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } if context.GetBalance(sender, tx.GasCoin).Cmp(commission) < 0 { @@ -133,6 +139,12 @@ func (data SetCandidateOffData) Run(sender types.Address, tx *Transaction, conte } commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } if context.GetBalance(sender, tx.GasCoin).Cmp(commission) < 0 { diff --git a/core/transaction/unbond.go b/core/transaction/unbond.go index cdb04f253..a5f48a4f1 100644 --- a/core/transaction/unbond.go +++ b/core/transaction/unbond.go @@ -63,6 +63,12 @@ func (data UnbondData) Run(sender types.Address, tx *Transaction, context *state } commission = formula.CalculateSaleAmount(coin.Volume(), coin.ReserveBalance(), coin.Data().Crr, commissionInBaseCoin) + + if commission == nil { + return Response{ + Code: 999, + Log: "Unknown error"} + } } if context.GetBalance(sender, tx.GasCoin).Cmp(commission) < 0 { diff --git a/formula/formula.go b/formula/formula.go index 404ea0251..868d57263 100644 --- a/formula/formula.go +++ b/formula/formula.go @@ -38,6 +38,11 @@ func CalculatePurchaseReturn(supply *big.Int, reserve *big.Int, crr uint, deposi result, _ := res.Int(nil) + // TODO: delete + if result == nil { + return big.NewInt(0) + } + return result } @@ -117,6 +122,10 @@ func CalculateSaleAmount(supply *big.Int, reserve *big.Int, crr uint, wantReceiv return ret } + if reserve.Cmp(wantReceive) == -1 { + return nil + } + tSupply := newFloat(0).SetInt(supply) tReserve := newFloat(0).SetInt(reserve) tWantReceive := newFloat(0).SetInt(wantReceive) diff --git a/version/version.go b/version/version.go index 782d4ba60..cc09bc10f 100755 --- a/version/version.go +++ b/version/version.go @@ -4,12 +4,12 @@ package version const ( Maj = "0" Min = "7" - Fix = "2" + Fix = "3" ) var ( // Must be a string because scripts like dist.sh read this file. - Version = "0.7.2" + Version = "0.7.3" // GitCommit is the current HEAD set using ldflags. GitCommit string