From 8b9782110ac19c30b29d175ae35942d58f6b5918 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 6 Feb 2019 12:14:49 -0800 Subject: [PATCH 1/3] IsAllGTEZero -> IsAnyNegative --- PENDING.md | 1 + docs/_attic/sdk/core/examples/app1.go | 2 +- types/coin.go | 18 +++++++++--------- x/auth/stdtx.go | 2 +- x/bank/keeper.go | 2 +- x/bank/simulation/invariants.go | 2 +- x/gov/msgs.go | 4 ++-- 7 files changed, 16 insertions(+), 15 deletions(-) diff --git a/PENDING.md b/PENDING.md index de670bea2358..4d89ae50887f 100644 --- a/PENDING.md +++ b/PENDING.md @@ -30,6 +30,7 @@ BREAKING CHANGES * [\#3514](https://github.com/cosmos/cosmos-sdk/pull/3514) Various clean ups: - Replace all GetKeyBase* functions family in favor of NewKeyBaseFromDir and NewKeyBaseFromHomeFlag. - Remove Get prefix from all TxBuilder's getters. + * [\#3522](https://github.com/cosmos/cosmos-sdk/pull/3522) Get rid of double negatives: Coins.IsNotNegative() -> Coins.IsAnyNegative(). * Tendermint diff --git a/docs/_attic/sdk/core/examples/app1.go b/docs/_attic/sdk/core/examples/app1.go index 715a88bc72b6..8cf30f4bb2c8 100644 --- a/docs/_attic/sdk/core/examples/app1.go +++ b/docs/_attic/sdk/core/examples/app1.go @@ -154,7 +154,7 @@ func handleFrom(store sdk.KVStore, from sdk.AccAddress, amt sdk.Coins) sdk.Resul senderCoins := acc.Coins.Minus(amt) // If any coin has negative amount, return insufficient coins error. - if !senderCoins.IsNotNegative() { + if senderCoins.IsAnyNegative() { return sdk.ErrInsufficientCoins("Insufficient coins in account").Result() } diff --git a/types/coin.go b/types/coin.go index 6fc0e8c5a3cc..d3d63f2aa359 100644 --- a/types/coin.go +++ b/types/coin.go @@ -262,7 +262,7 @@ func (coins Coins) Minus(coinsB Coins) Coins { // negative coin amount was returned. func (coins Coins) SafeMinus(coinsB Coins) (Coins, bool) { diff := coins.safePlus(coinsB.negative()) - return diff, !diff.IsNotNegative() + return diff, diff.IsAnyNegative() } // IsAllGT returns true if for every denom in coins, the denom is present at a @@ -284,7 +284,7 @@ func (coins Coins) IsAllGTE(coinsB Coins) bool { return true } - return diff.IsNotNegative() + return !diff.IsAnyNegative() } // IsAllLT returns True iff for every denom in coins, the denom is present at @@ -380,22 +380,22 @@ func (coins Coins) IsPositive() bool { return true } -// IsNotNegative returns true if there is no coin amount with a negative value -// (even no coins is true here). -// +// IsAnyNegative returns true if there is at least one coin whose amount +// is negative; returns false otherwise. It returns false if the coin set +// is empty too. // TODO: Remove once unsigned integers are used. -func (coins Coins) IsNotNegative() bool { +func (coins Coins) IsAnyNegative() bool { if len(coins) == 0 { - return true + return false } for _, coin := range coins { if coin.IsNegative() { - return false + return true } } - return true + return false } // negative returns a set of coins with all amount negative. diff --git a/x/auth/stdtx.go b/x/auth/stdtx.go index f13e77457e0f..c3bc7b703e7f 100644 --- a/x/auth/stdtx.go +++ b/x/auth/stdtx.go @@ -46,7 +46,7 @@ func (tx StdTx) ValidateBasic() sdk.Error { if tx.Fee.Gas > maxGasWanted { return sdk.ErrGasOverflow(fmt.Sprintf("invalid gas supplied; %d > %d", tx.Fee.Gas, maxGasWanted)) } - if !tx.Fee.Amount.IsNotNegative() { + if tx.Fee.Amount.IsAnyNegative() { return sdk.ErrInsufficientFee(fmt.Sprintf("invalid fee %s amount provided", tx.Fee.Amount)) } if len(stdSigs) == 0 { diff --git a/x/bank/keeper.go b/x/bank/keeper.go index 534facec5a97..b4ae07a60dc2 100644 --- a/x/bank/keeper.go +++ b/x/bank/keeper.go @@ -258,7 +258,7 @@ func addCoins(ctx sdk.Context, am auth.AccountKeeper, addr sdk.AccAddress, amt s oldCoins := getCoins(ctx, am, addr) newCoins := oldCoins.Plus(amt) - if !newCoins.IsNotNegative() { + if newCoins.IsAnyNegative() { return amt, nil, sdk.ErrInsufficientCoins(fmt.Sprintf("%s < %s", oldCoins, amt)) } diff --git a/x/bank/simulation/invariants.go b/x/bank/simulation/invariants.go index ee826c0e36a3..34778b2758c2 100644 --- a/x/bank/simulation/invariants.go +++ b/x/bank/simulation/invariants.go @@ -16,7 +16,7 @@ func NonnegativeBalanceInvariant(mapper auth.AccountKeeper) simulation.Invariant accts := mock.GetAllAccounts(mapper, ctx) for _, acc := range accts { coins := acc.GetCoins() - if !coins.IsNotNegative() { + if coins.IsAnyNegative() { return fmt.Errorf("%s has a negative denomination of %s", acc.GetAddress().String(), coins.String()) diff --git a/x/gov/msgs.go b/x/gov/msgs.go index 534316e4fb44..ef671a48b9b9 100644 --- a/x/gov/msgs.go +++ b/x/gov/msgs.go @@ -65,7 +65,7 @@ func (msg MsgSubmitProposal) ValidateBasic() sdk.Error { if !msg.InitialDeposit.IsValid() { return sdk.ErrInvalidCoins(msg.InitialDeposit.String()) } - if !msg.InitialDeposit.IsNotNegative() { + if msg.InitialDeposit.IsAnyNegative() { return sdk.ErrInvalidCoins(msg.InitialDeposit.String()) } return nil @@ -120,7 +120,7 @@ func (msg MsgDeposit) ValidateBasic() sdk.Error { if !msg.Amount.IsValid() { return sdk.ErrInvalidCoins(msg.Amount.String()) } - if !msg.Amount.IsNotNegative() { + if msg.Amount.IsAnyNegative() { return sdk.ErrInvalidCoins(msg.Amount.String()) } if msg.ProposalID < 0 { From cbbab1771a9e79b5371850a36fe3a278ce6a68a2 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 6 Feb 2019 13:54:11 -0800 Subject: [PATCH 2/3] expand PKGS string, no need for array expansion --- tests/test_cover.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cover.sh b/tests/test_cover.sh index 3fb0ab69cac3..3d3a6097e546 100644 --- a/tests/test_cover.sh +++ b/tests/test_cover.sh @@ -5,7 +5,7 @@ PKGS=$(go list ./... | grep -v /vendor/ | grep -v github.com/cosmos/cosmos-sdk/c set -e echo "mode: atomic" > coverage.txt -for pkg in ${PKGS[@]}; do +for pkg in ${PKGS}; do go test -v -timeout 30m -race -coverprofile=profile.out -covermode=atomic "$pkg" if [ -f profile.out ]; then tail -n +2 profile.out >> coverage.txt; From f83f6d16fe20a2effbf1034592fb0ce927960845 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Wed, 6 Feb 2019 14:18:24 -0800 Subject: [PATCH 3/3] Run test_cover.sh with bash -x --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0b2c2c76be62..91c6fa529546 100644 --- a/Makefile +++ b/Makefile @@ -198,7 +198,7 @@ test_sim_gaia_profile: -SimulationEnabled=true -SimulationNumBlocks=$(SIM_NUM_BLOCKS) -SimulationBlockSize=$(SIM_BLOCK_SIZE) -SimulationCommit=$(SIM_COMMIT) -timeout 24h -cpuprofile cpu.out -memprofile mem.out test_cover: - @export VERSION=$(VERSION); bash tests/test_cover.sh + @export VERSION=$(VERSION); bash -x tests/test_cover.sh test_lint: gometalinter --config=tools/gometalinter.json ./...