Skip to content

Commit

Permalink
Merge PR cosmos#3679: Consistent Operators
Browse files Browse the repository at this point in the history
* Minus->Sub Plus->Add Div->Quo

* pending

* Update PENDING.md

Co-Authored-By: rigelrozanski <[email protected]>

* fix

* typo
  • Loading branch information
rigelrozanski authored Feb 21, 2019
1 parent 992dc8b commit a814e5c
Show file tree
Hide file tree
Showing 37 changed files with 188 additions and 186 deletions.
2 changes: 2 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

### SDK

* \#3679 Consistent operators across Coins, DecCoins, Int, Dec
replaced: Minus->Sub Plus->Add Div->Quo
* [\#3665] Overhaul sdk.Uint type in preparation for Coins Int -> Uint migration.
* \#3691 Cleanup error messages
* \#3456 Integrate in the Int.ToDec() convenience function
Expand Down
30 changes: 15 additions & 15 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestCoinSend(t *testing.T) {
// query sender
acc = getAccount(t, port, addr)
coins := acc.GetCoins()
expectedBalance := initialBalance[0].Minus(fees[0])
expectedBalance := initialBalance[0].Sub(fees[0])

require.Equal(t, sdk.DefaultBondDenom, coins[0].Denom)
require.Equal(t, expectedBalance.Amount.SubRaw(1), coins[0].Amount)
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestCoinSend(t *testing.T) {
require.Equal(t, uint32(0), resultTx.Code)

acc = getAccount(t, port, addr)
expectedBalance = expectedBalance.Minus(fees[0])
expectedBalance = expectedBalance.Sub(fees[0])
require.Equal(t, expectedBalance.Amount.SubRaw(1), acc.GetCoins().AmountOf(sdk.DefaultBondDenom))
}

Expand All @@ -209,7 +209,7 @@ func TestCoinSendAccAuto(t *testing.T) {
// query sender
acc = getAccount(t, port, addr)
coins := acc.GetCoins()
expectedBalance := initialBalance[0].Minus(fees[0])
expectedBalance := initialBalance[0].Sub(fees[0])

require.Equal(t, sdk.DefaultBondDenom, coins[0].Denom)
require.Equal(t, expectedBalance.Amount.SubRaw(1), coins[0].Amount)
Expand Down Expand Up @@ -465,7 +465,7 @@ func TestBonding(t *testing.T) {
// verify balance
acc = getAccount(t, port, addr)
coins := acc.GetCoins()
expectedBalance := initialBalance[0].Minus(fees[0])
expectedBalance := initialBalance[0].Sub(fees[0])
require.Equal(t, expectedBalance.Amount.Sub(delTokens), coins.AmountOf(sdk.DefaultBondDenom))
expectedBalance = coins[0]

Expand Down Expand Up @@ -499,7 +499,7 @@ func TestBonding(t *testing.T) {
// sender should have not received any coins as the unbonding has only just begun
acc = getAccount(t, port, addr)
coins = acc.GetCoins()
expectedBalance = expectedBalance.Minus(fees[0])
expectedBalance = expectedBalance.Sub(fees[0])
require.True(t,
expectedBalance.Amount.LT(coins.AmountOf(sdk.DefaultBondDenom)) ||
expectedBalance.Amount.Equal(coins.AmountOf(sdk.DefaultBondDenom)),
Expand All @@ -517,7 +517,7 @@ func TestBonding(t *testing.T) {

ubd := getUnbondingDelegation(t, port, addr, operAddrs[0])
require.Len(t, ubd.Entries, 1)
require.Equal(t, delTokens.DivRaw(2), ubd.Entries[0].Balance)
require.Equal(t, delTokens.QuoRaw(2), ubd.Entries[0].Balance)

// test redelegation
rdTokens := sdk.TokensFromTendermintPower(30)
Expand All @@ -528,7 +528,7 @@ func TestBonding(t *testing.T) {

// verify balance after paying fees
acc = getAccount(t, port, addr)
expectedBalance = expectedBalance.Minus(fees[0])
expectedBalance = expectedBalance.Sub(fees[0])
require.True(t,
expectedBalance.Amount.LT(coins.AmountOf(sdk.DefaultBondDenom)) ||
expectedBalance.Amount.Equal(coins.AmountOf(sdk.DefaultBondDenom)),
Expand Down Expand Up @@ -612,7 +612,7 @@ func TestSubmitProposal(t *testing.T) {

// verify balance
acc = getAccount(t, port, addr)
expectedBalance := initialBalance[0].Minus(fees[0])
expectedBalance := initialBalance[0].Sub(fees[0])
require.Equal(t, expectedBalance.Amount.Sub(proposalTokens), acc.GetCoins().AmountOf(sdk.DefaultBondDenom))

// query proposal
Expand Down Expand Up @@ -648,7 +648,7 @@ func TestDeposit(t *testing.T) {
// verify balance
acc = getAccount(t, port, addr)
coins := acc.GetCoins()
expectedBalance := initialBalance[0].Minus(fees[0])
expectedBalance := initialBalance[0].Sub(fees[0])
require.Equal(t, expectedBalance.Amount.Sub(proposalTokens), coins.AmountOf(sdk.DefaultBondDenom))
expectedBalance = coins[0]

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

// verify balance after deposit and fee
acc = getAccount(t, port, addr)
expectedBalance = expectedBalance.Minus(fees[0])
expectedBalance = expectedBalance.Sub(fees[0])
require.Equal(t, expectedBalance.Amount.Sub(depositTokens), acc.GetCoins().AmountOf(sdk.DefaultBondDenom))

// query tx
Expand Down Expand Up @@ -705,7 +705,7 @@ func TestVote(t *testing.T) {
// verify balance
acc = getAccount(t, port, addr)
coins := acc.GetCoins()
expectedBalance := initialBalance[0].Minus(fees[0])
expectedBalance := initialBalance[0].Sub(fees[0])
require.Equal(t, expectedBalance.Amount.Sub(proposalTokens), coins.AmountOf(sdk.DefaultBondDenom))
expectedBalance = coins[0]

Expand All @@ -721,7 +721,7 @@ func TestVote(t *testing.T) {
// verify balance after vote and fee
acc = getAccount(t, port, addr)
coins = acc.GetCoins()
expectedBalance = expectedBalance.Minus(fees[0])
expectedBalance = expectedBalance.Sub(fees[0])
require.Equal(t, expectedBalance.Amount, coins.AmountOf(sdk.DefaultBondDenom))
expectedBalance = coins[0]

Expand All @@ -745,7 +745,7 @@ func TestVote(t *testing.T) {
// verify balance
acc = getAccount(t, port, addr)
coins = acc.GetCoins()
expectedBalance = expectedBalance.Minus(fees[0])
expectedBalance = expectedBalance.Sub(fees[0])
require.Equal(t, expectedBalance.Amount.Sub(delTokens), coins.AmountOf(sdk.DefaultBondDenom))
expectedBalance = coins[0]

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

// verify balance
acc = getAccount(t, port, addr)
expectedBalance = expectedBalance.Minus(fees[0])
expectedBalance = expectedBalance.Sub(fees[0])
require.Equal(t, expectedBalance.Amount, acc.GetCoins().AmountOf(sdk.DefaultBondDenom))

tally = getTally(t, port, proposalID)
Expand Down Expand Up @@ -792,7 +792,7 @@ func TestProposalsQuery(t *testing.T) {
defer cleanup()

depositParam := getDepositParam(t, port)
halfMinDeposit := depositParam.MinDeposit.AmountOf(sdk.DefaultBondDenom).DivRaw(2)
halfMinDeposit := depositParam.MinDeposit.AmountOf(sdk.DefaultBondDenom).QuoRaw(2)
getVotingParam(t, port)
getTallyingParam(t, port)

Expand Down
4 changes: 2 additions & 2 deletions docs/_attic/sdk/core/examples/app1.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func handleFrom(store sdk.KVStore, from sdk.AccAddress, amt sdk.Coins) sdk.Resul
}

// Deduct msg amount from sender account.
senderCoins := acc.Coins.Minus(amt)
senderCoins := acc.Coins.Sub(amt)

// If any coin has negative amount, return insufficient coins error.
if senderCoins.IsAnyNegative() {
Expand Down Expand Up @@ -188,7 +188,7 @@ func handleTo(store sdk.KVStore, to sdk.AccAddress, amt sdk.Coins) sdk.Result {
}

// Add amount to receiver's old coins
receiverCoins := acc.Coins.Plus(amt)
receiverCoins := acc.Coins.Add(amt)

// Update receiver account
acc.Coins = receiverCoins
Expand Down
38 changes: 19 additions & 19 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (coin Coin) IsEqual(other Coin) bool {

// Adds amounts of two coins with same denom. If the coins differ in denom then
// it panics.
func (coin Coin) Plus(coinB Coin) Coin {
func (coin Coin) Add(coinB Coin) Coin {
if coin.Denom != coinB.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom))
}
Expand All @@ -96,7 +96,7 @@ func (coin Coin) Plus(coinB Coin) Coin {

// Subtracts amounts of two coins with same denom. If the coins differ in denom
// then it panics.
func (coin Coin) Minus(coinB Coin) Coin {
func (coin Coin) Sub(coinB Coin) Coin {
if coin.Denom != coinB.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom))
}
Expand Down Expand Up @@ -178,27 +178,27 @@ func (coins Coins) IsValid() bool {
}
}

// Plus adds two sets of coins.
// Add adds two sets of coins.
//
// e.g.
// {2A} + {A, 2B} = {3A, 2B}
// {2A} + {0B} = {2A}
//
// NOTE: Plus operates under the invariant that coins are sorted by
// NOTE: Add operates under the invariant that coins are sorted by
// denominations.
//
// CONTRACT: Plus will never return Coins where one Coin has a non-positive
// CONTRACT: Add will never return Coins where one Coin has a non-positive
// amount. In otherwords, IsValid will always return true.
func (coins Coins) Plus(coinsB Coins) Coins {
return coins.safePlus(coinsB)
func (coins Coins) Add(coinsB Coins) Coins {
return coins.safeAdd(coinsB)
}

// safePlus will perform addition of two coins sets. If both coin sets are
// safeAdd will perform addition of two coins sets. If both coin sets are
// empty, then an empty set is returned. If only a single set is empty, the
// other set is returned. Otherwise, the coins are compared in order of their
// denomination and addition only occurs when the denominations match, otherwise
// the coin is simply added to the sum assuming it's not zero.
func (coins Coins) safePlus(coinsB Coins) Coins {
func (coins Coins) safeAdd(coinsB Coins) Coins {
sum := ([]Coin)(nil)
indexA, indexB := 0, 0
lenA, lenB := len(coins), len(coinsB)
Expand Down Expand Up @@ -228,7 +228,7 @@ func (coins Coins) safePlus(coinsB Coins) Coins {
indexA++

case 0: // coin A denom == coin B denom
res := coinA.Plus(coinB)
res := coinA.Add(coinB)
if !res.IsZero() {
sum = append(sum, res)
}
Expand All @@ -246,35 +246,35 @@ func (coins Coins) safePlus(coinsB Coins) Coins {
}
}

// Minus subtracts a set of coins from another.
// Sub subtracts a set of coins from another.
//
// e.g.
// {2A, 3B} - {A} = {A, 3B}
// {2A} - {0B} = {2A}
// {A, B} - {A} = {B}
//
// CONTRACT: Minus will never return Coins where one Coin has a non-positive
// CONTRACT: Sub will never return Coins where one Coin has a non-positive
// amount. In otherwords, IsValid will always return true.
func (coins Coins) Minus(coinsB Coins) Coins {
diff, hasNeg := coins.SafeMinus(coinsB)
func (coins Coins) Sub(coinsB Coins) Coins {
diff, hasNeg := coins.SafeSub(coinsB)
if hasNeg {
panic("negative coin amount")
}

return diff
}

// SafeMinus performs the same arithmetic as Minus but returns a boolean if any
// SafeSub performs the same arithmetic as Sub but returns a boolean if any
// negative coin amount was returned.
func (coins Coins) SafeMinus(coinsB Coins) (Coins, bool) {
diff := coins.safePlus(coinsB.negative())
func (coins Coins) SafeSub(coinsB Coins) (Coins, bool) {
diff := coins.safeAdd(coinsB.negative())
return diff, diff.IsAnyNegative()
}

// IsAllGT returns true if for every denom in coins, the denom is present at a
// greater amount in coinsB.
func (coins Coins) IsAllGT(coinsB Coins) bool {
diff, _ := coins.SafeMinus(coinsB)
diff, _ := coins.SafeSub(coinsB)
if len(diff) == 0 {
return false
}
Expand All @@ -285,7 +285,7 @@ func (coins Coins) IsAllGT(coinsB Coins) bool {
// IsAllGTE returns true iff for every denom in coins, the denom is present at
// an equal or greater amount in coinsB.
func (coins Coins) IsAllGTE(coinsB Coins) bool {
diff, _ := coins.SafeMinus(coinsB)
diff, _ := coins.SafeSub(coinsB)
if len(diff) == 0 {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions types/coin_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func BenchmarkCoinsAdditionIntersect(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
coinsA.Plus(coinsB)
coinsA.Add(coinsB)
}
}
}
Expand Down Expand Up @@ -50,7 +50,7 @@ func BenchmarkCoinsAdditionNoIntersect(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
coinsA.Plus(coinsB)
coinsA.Add(coinsB)
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestIsEqualCoin(t *testing.T) {
}
}

func TestPlusCoin(t *testing.T) {
func TestAddCoin(t *testing.T) {
cases := []struct {
inputOne Coin
inputTwo Coin
Expand All @@ -61,15 +61,15 @@ func TestPlusCoin(t *testing.T) {

for tcIndex, tc := range cases {
if tc.shouldPanic {
require.Panics(t, func() { tc.inputOne.Plus(tc.inputTwo) })
require.Panics(t, func() { tc.inputOne.Add(tc.inputTwo) })
} else {
res := tc.inputOne.Plus(tc.inputTwo)
res := tc.inputOne.Add(tc.inputTwo)
require.Equal(t, tc.expected, res, "sum of coins is incorrect, tc #%d", tcIndex)
}
}
}

func TestMinusCoin(t *testing.T) {
func TestSubCoin(t *testing.T) {
cases := []struct {
inputOne Coin
inputTwo Coin
Expand All @@ -85,9 +85,9 @@ func TestMinusCoin(t *testing.T) {

for tcIndex, tc := range cases {
if tc.shouldPanic {
require.Panics(t, func() { tc.inputOne.Minus(tc.inputTwo) })
require.Panics(t, func() { tc.inputOne.Sub(tc.inputTwo) })
} else {
res := tc.inputOne.Minus(tc.inputTwo)
res := tc.inputOne.Sub(tc.inputTwo)
require.Equal(t, tc.expected, res, "difference of coins is incorrect, tc #%d", tcIndex)
}
}
Expand All @@ -97,7 +97,7 @@ func TestMinusCoin(t *testing.T) {
inputTwo Coin
expected int64
}{NewInt64Coin(testDenom1, 1), NewInt64Coin(testDenom1, 1), 0}
res := tc.inputOne.Minus(tc.inputTwo)
res := tc.inputOne.Sub(tc.inputTwo)
require.Equal(t, tc.expected, res.Amount.Int64())
}

Expand Down Expand Up @@ -205,7 +205,7 @@ func TestEqualCoins(t *testing.T) {
}
}

func TestPlusCoins(t *testing.T) {
func TestAddCoins(t *testing.T) {
zero := NewInt(0)
one := NewInt(1)
two := NewInt(2)
Expand All @@ -223,13 +223,13 @@ func TestPlusCoins(t *testing.T) {
}

for tcIndex, tc := range cases {
res := tc.inputOne.Plus(tc.inputTwo)
res := tc.inputOne.Add(tc.inputTwo)
assert.True(t, res.IsValid())
require.Equal(t, tc.expected, res, "sum of coins is incorrect, tc #%d", tcIndex)
}
}

func TestMinusCoins(t *testing.T) {
func TestSubCoins(t *testing.T) {
zero := NewInt(0)
one := NewInt(1)
two := NewInt(2)
Expand All @@ -249,9 +249,9 @@ func TestMinusCoins(t *testing.T) {

for i, tc := range testCases {
if tc.shouldPanic {
require.Panics(t, func() { tc.inputOne.Minus(tc.inputTwo) })
require.Panics(t, func() { tc.inputOne.Sub(tc.inputTwo) })
} else {
res := tc.inputOne.Minus(tc.inputTwo)
res := tc.inputOne.Sub(tc.inputTwo)
assert.True(t, res.IsValid())
require.Equal(t, tc.expected, res, "sum of coins is incorrect, tc #%d", i)
}
Expand Down
Loading

0 comments on commit a814e5c

Please sign in to comment.