Skip to content

Commit

Permalink
Merge PR #4281: Fix addGenesisAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored May 7, 2019
1 parent 99df748 commit 1bd70a2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions .pending/bugfixes/sdk/4271-Fix-addGenesisA
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4271 Fix addGenesisAccount by using Coins#IsAnyGT for vesting amount validation.
1 change: 1 addition & 0 deletions .pending/features/sdk/4271-Implement-Coins
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4271 Implement Coins#IsAnyGT
2 changes: 1 addition & 1 deletion cmd/gaia/init/genesis_accts.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func addGenesisAccount(
EndTime: vestingEnd,
}

if bvacc.OriginalVesting.IsAllGT(acc.Coins) {
if bvacc.OriginalVesting.IsAnyGT(acc.Coins) {
return appState, fmt.Errorf("vesting amount cannot be greater than total amount")
}
if vestingStart >= vestingEnd {
Expand Down
15 changes: 14 additions & 1 deletion cmd/gaia/init/genesis_accts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,27 @@ func TestAddGenesisAccount(t *testing.T) {
},
true,
},
{
"invalid vesting amount with multi coins",
args{
app.GenesisState{},
addr1,
sdk.NewCoins(sdk.NewInt64Coin("uatom", 50), sdk.NewInt64Coin("eth", 50)),
sdk.NewCoins(sdk.NewInt64Coin("uatom", 100), sdk.NewInt64Coin("eth", 20)),
0,
1,
},
true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := addGenesisAccount(
cdc, tt.args.appState, tt.args.addr, tt.args.coins,
tt.args.vestingAmt, tt.args.vestingStart, tt.args.vestingEnd,
)
require.Equal(t, tt.wantErr, (err != nil))
require.Equal(t, tt.wantErr, err != nil)
})
}
}
23 changes: 23 additions & 0 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,29 @@ func (coins Coins) IsAllLTE(coinsB Coins) bool {
return coinsB.IsAllGTE(coins)
}

// IsAnyGT returns true iff for any denom in coins, the denom is present at a
// greater amount in coinsB.
//
// e.g.
// {2A, 3B}.IsAnyGT{A} = true
// {2A, 3B}.IsAnyGT{5C} = false
// {}.IsAnyGT{5C} = false
// {2A, 3B}.IsAnyGT{} = false
func (coins Coins) IsAnyGT(coinsB Coins) bool {
if len(coinsB) == 0 {
return false
}

for _, coin := range coins {
amt := coinsB.AmountOf(coin.Denom)
if coin.Amount.GT(amt) && !amt.IsZero() {
return true
}
}

return false
}

// IsAnyGTE returns true iff coins contains at least one denom that is present
// at a greater or equal amount in coinsB; it returns false otherwise.
//
Expand Down
19 changes: 19 additions & 0 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,22 @@ func TestNewCoins(t *testing.T) {
})
}
}

func TestCoinsIsAnyGT(t *testing.T) {
twoAtom := NewInt64Coin("atom", 2)
fiveAtom := NewInt64Coin("atom", 5)
threeEth := NewInt64Coin("eth", 3)
sixEth := NewInt64Coin("eth", 6)
twoBtc := NewInt64Coin("btc", 2)

require.False(t, Coins{}.IsAnyGT(Coins{}))

require.False(t, Coins{fiveAtom}.IsAnyGT(Coins{}))
require.False(t, Coins{}.IsAnyGT(Coins{fiveAtom}))
require.True(t, Coins{fiveAtom}.IsAnyGT(Coins{twoAtom}))
require.False(t, Coins{twoAtom}.IsAnyGT(Coins{fiveAtom}))

require.True(t, Coins{twoAtom, sixEth}.IsAnyGT(Coins{twoBtc, fiveAtom, threeEth}))
require.False(t, Coins{twoBtc, twoAtom, threeEth}.IsAnyGT(Coins{fiveAtom, sixEth}))
require.False(t, Coins{twoAtom, sixEth}.IsAnyGT(Coins{twoBtc, fiveAtom}))
}

0 comments on commit 1bd70a2

Please sign in to comment.