Merge PR #4281: Fix addGenesisAccount

This commit is contained in:
Alexander Bezobchuk 2019-05-07 13:10:35 -04:00 committed by GitHub
parent 99df748dda
commit 1bd70a238d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 2 deletions

View File

@ -0,0 +1 @@
#4271 Fix addGenesisAccount by using Coins#IsAnyGT for vesting amount validation.

View File

@ -0,0 +1 @@
#4271 Implement Coins#IsAnyGT

View File

@ -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 {

View File

@ -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)
})
}
}

View File

@ -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.
//

View File

@ -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}))
}