Skip to content

Commit

Permalink
fix: replace IsEqual with Equal (#1314)
Browse files Browse the repository at this point in the history
* Replace IsEqual with Equal

* Update CHANGELOG
  • Loading branch information
ulbqb committed Mar 27, 2024
1 parent e5709f3 commit 143a763
Show file tree
Hide file tree
Showing 27 changed files with 62 additions and 79 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (chore) [\#1168](https://github.com/Finschia/finschia-sdk/pull/1168) Replace `ExactArgs(0)` with `NoArgs()` in `x/upgrade` module
* (server) [\#1175](https://github.com/Finschia/finschia-sdk/pull/1175) Use go embed for swagger
* (x/collection) [\#1287](https://github.com/Finschia/finschia-sdk/pull/1287) add nft id validation to MsgSendNFT
* (types) [\#1314](https://github.com/Finschia/finschia-sdk/pull/1314) replace IsEqual with Equal

### Bug Fixes
* chore(deps) [\#1141](https://github.com/Finschia/finschia-sdk/pull/1141) Bump github.com/cosmos/ledger-cosmos-go from 0.12.2 to 0.13.2 to fix ledger signing issue
Expand Down
2 changes: 1 addition & 1 deletion simapp/test_helpers.go
Expand Up @@ -370,7 +370,7 @@ func TestAddr(addr, bech string) (sdk.AccAddress, error) {
func CheckBalance(t *testing.T, app *SimApp, addr sdk.AccAddress, balances sdk.Coins) {
t.Helper()
ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{})
require.True(t, balances.IsEqual(app.BankKeeper.GetAllBalances(ctxCheck, addr)))
require.True(t, balances.Equal(app.BankKeeper.GetAllBalances(ctxCheck, addr)))
}

// SignCheckDeliver checks a generated signed transaction and simulates a
Expand Down
17 changes: 7 additions & 10 deletions types/coin.go
Expand Up @@ -87,12 +87,9 @@ func (coin Coin) IsLT(other Coin) bool {
}

// IsEqual returns true if the two sets of Coins have the same value
// Deprecated: Use Coin.Equal instead.
func (coin Coin) IsEqual(other Coin) bool {
if coin.Denom != other.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
}

return coin.Amount.Equal(other.Amount)
return coin.Equal(other)
}

// Add adds amounts of two coins with same denom. If the coins differ in denom then
Expand Down Expand Up @@ -404,7 +401,7 @@ func (coins Coins) SafeSub(coinsB Coins) (Coins, bool) {
// a.IsAllLTE(a.Max(b))
// b.IsAllLTE(a.Max(b))
// a.IsAllLTE(c) && b.IsAllLTE(c) == a.Max(b).IsAllLTE(c)
// a.Add(b...).IsEqual(a.Min(b).Add(a.Max(b)...))
// a.Add(b...).Equal(a.Min(b).Add(a.Max(b)...))
//
// E.g.
// {1A, 3B, 2C}.Max({4A, 2B, 2C} == {4A, 3B, 2C})
Expand Down Expand Up @@ -450,7 +447,7 @@ func (coins Coins) Max(coinsB Coins) Coins {
// a.Min(b).IsAllLTE(a)
// a.Min(b).IsAllLTE(b)
// c.IsAllLTE(a) && c.IsAllLTE(b) == c.IsAllLTE(a.Min(b))
// a.Add(b...).IsEqual(a.Min(b).Add(a.Max(b)...))
// a.Add(b...).Equal(a.Min(b).Add(a.Max(b)...))
//
// E.g.
// {1A, 3B, 2C}.Min({4A, 2B, 2C} == {1A, 2B, 2C})
Expand Down Expand Up @@ -593,8 +590,8 @@ func (coins Coins) IsZero() bool {
return true
}

// IsEqual returns true if the two sets of Coins have the same value
func (coins Coins) IsEqual(coinsB Coins) bool {
// Equal returns true if the two sets of Coins have the same value
func (coins Coins) Equal(coinsB Coins) bool {
if len(coins) != len(coinsB) {
return false
}
Expand All @@ -603,7 +600,7 @@ func (coins Coins) IsEqual(coinsB Coins) bool {
coinsB = coinsB.Sort()

for i := 0; i < len(coins); i++ {
if !coins[i].IsEqual(coinsB[i]) {
if !coins[i].Equal(coinsB[i]) {
return false
}
}
Expand Down
46 changes: 17 additions & 29 deletions types/coin_test.go
Expand Up @@ -56,21 +56,15 @@ func (s *coinTestSuite) TestIsEqualCoin() {
inputOne sdk.Coin
inputTwo sdk.Coin
expected bool
panics bool
}{
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 1), true, false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom2, 1), false, true},
{sdk.NewInt64Coin("stake", 1), sdk.NewInt64Coin("stake", 10), false, false},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom1, 1), true},
{sdk.NewInt64Coin(testDenom1, 1), sdk.NewInt64Coin(testDenom2, 1), false},
{sdk.NewInt64Coin("stake", 1), sdk.NewInt64Coin("stake", 10), false},
}

for tcIndex, tc := range cases {
tc := tc
if tc.panics {
s.Require().Panics(func() { tc.inputOne.IsEqual(tc.inputTwo) })
} else {
res := tc.inputOne.IsEqual(tc.inputTwo)
s.Require().Equal(tc.expected, res, "coin equality relation is incorrect, tc #%d", tcIndex)
}
res := tc.inputOne.IsEqual(tc.inputTwo)
s.Require().Equal(tc.expected, res, "coin equality relation is incorrect, tc #%d", tcIndex)
}
}

Expand Down Expand Up @@ -405,25 +399,19 @@ func (s *coinTestSuite) TestEqualCoins() {
inputOne sdk.Coins
inputTwo sdk.Coins
expected bool
panics bool
}{
{sdk.Coins{}, sdk.Coins{}, true, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, true, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, true, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom2, 0)}, false, true},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 1)}, false, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, false, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, true, false},
{sdk.Coins{}, sdk.Coins{}, true},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, true},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, true},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom2, 0)}, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 1)}, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, false},
{sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, sdk.Coins{sdk.NewInt64Coin(testDenom1, 0), sdk.NewInt64Coin(testDenom2, 1)}, true},
}

for tcnum, tc := range cases {
tc := tc
if tc.panics {
s.Require().Panics(func() { tc.inputOne.IsEqual(tc.inputTwo) })
} else {
res := tc.inputOne.IsEqual(tc.inputTwo)
s.Require().Equal(tc.expected, res, "Equality is differed from exported. tc #%d, expected %b, actual %b.", tcnum, tc.expected, res)
}
res := tc.inputOne.Equal(tc.inputTwo)
s.Require().Equal(tc.expected, res, "Equality is differed from exported. tc #%d, expected %b, actual %b.", tcnum, tc.expected, res)
}
}

Expand Down Expand Up @@ -684,8 +672,8 @@ func (s *coinTestSuite) TestMinMax() {
for _, tc := range cases {
min := tc.input1.Min(tc.input2)
max := tc.input1.Max(tc.input2)
s.Require().True(min.IsEqual(tc.min), tc.name)
s.Require().True(max.IsEqual(tc.max), tc.name)
s.Require().True(min.Equal(tc.min), tc.name)
s.Require().True(max.Equal(tc.max), tc.name)
}
}

Expand Down Expand Up @@ -961,7 +949,7 @@ func (s *coinTestSuite) TestNewCoins() {
continue
}
got := sdk.NewCoins(tt.coins...)
s.Require().True(got.IsEqual(tt.want))
s.Require().True(got.Equal(tt.want))
}
}

Expand Down
13 changes: 5 additions & 8 deletions types/dec_coin.go
Expand Up @@ -79,12 +79,9 @@ func (coin DecCoin) IsLT(other DecCoin) bool {
}

// IsEqual returns true if the two sets of Coins have the same value.
// Deprecated: Use DecCoin.Equal instead.
func (coin DecCoin) IsEqual(other DecCoin) bool {
if coin.Denom != other.Denom {
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
}

return coin.Amount.Equal(other.Amount)
return coin.Equal(other)
}

// Add adds amounts of two decimal coins with same denom.
Expand Down Expand Up @@ -480,8 +477,8 @@ func (coins DecCoins) AmountOf(denom string) Dec {
}
}

// IsEqual returns true if the two sets of DecCoins have the same value.
func (coins DecCoins) IsEqual(coinsB DecCoins) bool {
// Equal returns true if the two sets of DecCoins have the same value.
func (coins DecCoins) Equal(coinsB DecCoins) bool {
if len(coins) != len(coinsB) {
return false
}
Expand All @@ -490,7 +487,7 @@ func (coins DecCoins) IsEqual(coinsB DecCoins) bool {
coinsB = coinsB.Sort()

for i := 0; i < len(coins); i++ {
if !coins[i].IsEqual(coinsB[i]) {
if !coins[i].Equal(coinsB[i]) {
return false
}
}
Expand Down
2 changes: 1 addition & 1 deletion types/dec_coin_test.go
Expand Up @@ -445,7 +445,7 @@ func (s *decCoinTestSuite) TestDecCoinsIntersect() {
s.Require().NoError(err, "unexpected parse error in %v", i)
exr, err := sdk.ParseDecCoins(tc.expectedResult)
s.Require().NoError(err, "unexpected parse error in %v", i)
s.Require().True(in1.Intersect(in2).IsEqual(exr), "in1.cap(in2) != exr in %v", i)
s.Require().True(in1.Intersect(in2).Equal(exr), "in1.cap(in2) != exr in %v", i)
}
}

Expand Down
4 changes: 2 additions & 2 deletions x/auth/legacy/v043/store_test.go
Expand Up @@ -595,8 +595,8 @@ func trackingCorrected(t *testing.T, ctx sdk.Context, ak authkeeper.AccountKeepe
vDA, ok := baseAccount.(exported.VestingAccount)
require.True(t, ok)

vestedOk := expDelVesting.IsEqual(vDA.GetDelegatedVesting())
freeOk := expDelFree.IsEqual(vDA.GetDelegatedFree())
vestedOk := expDelVesting.Equal(vDA.GetDelegatedVesting())
freeOk := expDelFree.Equal(vDA.GetDelegatedFree())
require.True(t, vestedOk, vDA.GetDelegatedVesting().String())
require.True(t, freeOk, vDA.GetDelegatedFree().String())
}
Expand Down
2 changes: 1 addition & 1 deletion x/auth/vesting/types/vesting_account.go
Expand Up @@ -419,7 +419,7 @@ func (pva PeriodicVestingAccount) Validate() error {
if endTime != pva.EndTime {
return errors.New("vesting end time does not match length of all vesting periods")
}
if !originalVesting.IsEqual(pva.OriginalVesting) {
if !originalVesting.Equal(pva.OriginalVesting) {
return errors.New("original vesting coins does not match the sum of all coins in vesting periods")
}

Expand Down
12 changes: 6 additions & 6 deletions x/auth/vesting/types/vesting_account_test.go
Expand Up @@ -209,7 +209,7 @@ func TestSpendableCoinsDelVestingAcc(t *testing.T) {
// schedule
dva := types.NewDelayedVestingAccount(bacc, origCoins, endTime.Unix())
lockedCoins := dva.LockedCoins(now)
require.True(t, lockedCoins.IsEqual(origCoins))
require.True(t, lockedCoins.Equal(origCoins))

// require that all coins are spendable after the maturation of the vesting
// schedule
Expand All @@ -218,14 +218,14 @@ func TestSpendableCoinsDelVestingAcc(t *testing.T) {

// require that all coins are still vesting after some time
lockedCoins = dva.LockedCoins(now.Add(12 * time.Hour))
require.True(t, lockedCoins.IsEqual(origCoins))
require.True(t, lockedCoins.Equal(origCoins))

// delegate some locked coins
// require that locked is reduced
delegatedAmount := sdk.NewCoins(sdk.NewInt64Coin(stakeDenom, 50))
dva.TrackDelegation(now.Add(12*time.Hour), origCoins, delegatedAmount)
lockedCoins = dva.LockedCoins(now.Add(12 * time.Hour))
require.True(t, lockedCoins.IsEqual(origCoins.Sub(delegatedAmount)))
require.True(t, lockedCoins.Equal(origCoins.Sub(delegatedAmount)))
}

func TestTrackDelegationDelVestingAcc(t *testing.T) {
Expand Down Expand Up @@ -573,18 +573,18 @@ func TestSpendableCoinsPermLockedVestingAcc(t *testing.T) {
// schedule
plva := types.NewPermanentLockedAccount(bacc, origCoins)
lockedCoins := plva.LockedCoins(now)
require.True(t, lockedCoins.IsEqual(origCoins))
require.True(t, lockedCoins.Equal(origCoins))

// require that all coins are still locked at end time
lockedCoins = plva.LockedCoins(endTime)
require.True(t, lockedCoins.IsEqual(origCoins))
require.True(t, lockedCoins.Equal(origCoins))

// delegate some locked coins
// require that locked is reduced
delegatedAmount := sdk.NewCoins(sdk.NewInt64Coin(stakeDenom, 50))
plva.TrackDelegation(now.Add(12*time.Hour), origCoins, delegatedAmount)
lockedCoins = plva.LockedCoins(now.Add(12 * time.Hour))
require.True(t, lockedCoins.IsEqual(origCoins.Sub(delegatedAmount)))
require.True(t, lockedCoins.Equal(origCoins.Sub(delegatedAmount)))
}

func TestTrackDelegationPermLockedVestingAcc(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/bank/keeper/genesis.go
Expand Up @@ -25,7 +25,7 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
totalSupply = totalSupply.Add(balance.Coins...)
}

if !genState.Supply.Empty() && !genState.Supply.IsEqual(totalSupply) {
if !genState.Supply.Empty() && !genState.Supply.Equal(totalSupply) {
panic(fmt.Errorf("genesis supply is incorrect, expected %v, got %v", genState.Supply, totalSupply))
}

Expand Down
2 changes: 1 addition & 1 deletion x/bank/keeper/grpc_query_test.go
Expand Up @@ -41,7 +41,7 @@ func (suite *IntegrationTestSuite) TestQueryBalance() {
res, err = queryClient.Balance(gocontext.Background(), req)
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.True(res.Balance.IsEqual(newFooCoin(50)))
suite.True(res.Balance.Equal(newFooCoin(50)))
}

func (suite *IntegrationTestSuite) TestQueryAllBalances() {
Expand Down
2 changes: 1 addition & 1 deletion x/bank/keeper/invariants.go
Expand Up @@ -62,7 +62,7 @@ func TotalSupply(k Keeper) sdk.Invariant {
return false
})

broken := !expectedTotal.IsEqual(supply)
broken := !expectedTotal.Equal(supply)

return sdk.FormatInvariant(types.ModuleName, "total supply",
fmt.Sprintf(
Expand Down
4 changes: 2 additions & 2 deletions x/bank/keeper/querier_test.go
Expand Up @@ -48,7 +48,7 @@ func (suite *IntegrationTestSuite) TestQuerier_QueryBalance() {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &balance))
suite.True(balance.IsEqual(newFooCoin(50)))
suite.True(balance.Equal(newFooCoin(50)))
}

func (suite *IntegrationTestSuite) TestQuerier_QueryAllBalances() {
Expand Down Expand Up @@ -84,7 +84,7 @@ func (suite *IntegrationTestSuite) TestQuerier_QueryAllBalances() {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &balances))
suite.True(balances.IsEqual(origCoins))
suite.True(balances.Equal(origCoins))
}

func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupply() {
Expand Down
2 changes: 1 addition & 1 deletion x/bank/types/genesis.go
Expand Up @@ -53,7 +53,7 @@ func (gs GenesisState) Validate() error {
return err
}

if !gs.Supply.IsEqual(totalSupply) {
if !gs.Supply.Equal(totalSupply) {
return fmt.Errorf("genesis supply is incorrect, expected %v, got %v", gs.Supply, totalSupply)
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/bank/types/msgs.go
Expand Up @@ -185,7 +185,7 @@ func ValidateInputsOutputs(inputs []Input, outputs []Output) error {
}

// make sure inputs and outputs match
if !totalIn.IsEqual(totalOut) {
if !totalIn.Equal(totalOut) {
return ErrInputOutputMismatch
}

Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/delegation.go
Expand Up @@ -150,7 +150,7 @@ func (k Keeper) withdrawDelegationRewards(ctx sdk.Context, val stakingtypes.Vali
// defensive edge case may happen on the very final digits
// of the decCoins due to operation order of the distribution mechanism.
rewards := rewardsRaw.Intersect(outstanding)
if !rewards.IsEqual(rewardsRaw) {
if !rewards.Equal(rewardsRaw) {
logger := k.Logger(ctx)
logger.Info(
"rounding error withdrawing rewards from validator",
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/genesis.go
Expand Up @@ -90,7 +90,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
if balances.IsZero() {
k.authKeeper.SetModuleAccount(ctx, moduleAcc)
}
if !balances.IsEqual(moduleHoldingsInt) {
if !balances.Equal(moduleHoldingsInt) {
panic(fmt.Sprintf("distribution module balance does not match the module holdings: %s <-> %s", balances, moduleHoldingsInt))
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/invariants.go
Expand Up @@ -147,7 +147,7 @@ func ModuleAccountInvariant(k Keeper) sdk.Invariant {
macc := k.GetDistributionAccount(ctx)
balances := k.bankKeeper.GetAllBalances(ctx, macc.GetAddress())

broken := !balances.IsEqual(expectedInt)
broken := !balances.Equal(expectedInt)
return sdk.FormatInvariant(
types.ModuleName, "ModuleAccount coins",
fmt.Sprintf("\texpected ModuleAccount coins: %s\n"+
Expand Down
2 changes: 1 addition & 1 deletion x/foundation/keeper/internal/invariants.go
Expand Up @@ -30,7 +30,7 @@ func ModuleAccountInvariant(k Keeper) sdk.Invariant {

treasury := k.GetTreasury(ctx)
msg := fmt.Sprintf("coins in the treasury; expected %s, got %s\n", treasury, balance)
broken := !treasury.IsEqual(sdk.NewDecCoinsFromCoins(balance...))
broken := !treasury.Equal(sdk.NewDecCoinsFromCoins(balance...))

return sdk.FormatInvariant(foundation.ModuleName, moduleAccountInvariant, msg), broken
}
Expand Down
4 changes: 2 additions & 2 deletions x/gov/abci_test.go
Expand Up @@ -305,7 +305,7 @@ func TestProposalPassedEndblocker(t *testing.T) {
moduleAccCoins := app.BankKeeper.GetAllBalances(ctx, macc.GetAddress())

deposits := initialModuleAccCoins.Add(proposal.TotalDeposit...).Add(proposalCoins...)
require.True(t, moduleAccCoins.IsEqual(deposits))
require.True(t, moduleAccCoins.Equal(deposits))

err = app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[0], types.NewNonSplitVoteOption(types.OptionYes))
require.NoError(t, err)
Expand All @@ -318,7 +318,7 @@ func TestProposalPassedEndblocker(t *testing.T) {

macc = app.GovKeeper.GetGovernanceAccount(ctx)
require.NotNil(t, macc)
require.True(t, app.BankKeeper.GetAllBalances(ctx, macc.GetAddress()).IsEqual(initialModuleAccCoins))
require.True(t, app.BankKeeper.GetAllBalances(ctx, macc.GetAddress()).Equal(initialModuleAccCoins))
}

func TestEndBlockerProposalHandlerFailed(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/gov/genesis.go
Expand Up @@ -48,7 +48,7 @@ func InitGenesis(ctx sdk.Context, ak types.AccountKeeper, bk types.BankKeeper, k
}

// check if total deposits equals balance, if it doesn't panic because there were export/import errors
if !balance.IsEqual(totalDeposits) {
if !balance.Equal(totalDeposits) {
panic(fmt.Sprintf("expected module account was %s but we got %s", balance.String(), totalDeposits.String()))
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/gov/keeper/deposit_test.go
Expand Up @@ -28,7 +28,7 @@ func TestDeposits(t *testing.T) {
addr0Initial := app.BankKeeper.GetAllBalances(ctx, TestAddrs[0])
addr1Initial := app.BankKeeper.GetAllBalances(ctx, TestAddrs[1])

require.True(t, proposal.TotalDeposit.IsEqual(sdk.NewCoins()))
require.True(t, proposal.TotalDeposit.Equal(sdk.NewCoins()))

// Check no deposits at beginning
_, found := app.GovKeeper.GetDeposit(ctx, proposalID, TestAddrs[1])
Expand Down

0 comments on commit 143a763

Please sign in to comment.