bank: don't ignore errors returned by Balance.GetAddress() (#8531)

Closes: #8530
This commit is contained in:
Alessio Treglia 2021-02-06 21:51:26 +00:00 committed by GitHub
parent c5c049a045
commit 8001e3ca3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 10 deletions

View File

@ -15,7 +15,11 @@ var _ exported.GenesisBalance = (*Balance)(nil)
// GetAddress returns the account address of the Balance object.
func (b Balance) GetAddress() sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(b.Address)
addr, err := sdk.AccAddressFromBech32(b.Address)
if err != nil {
panic(fmt.Errorf("couldn't convert %q to account address: %v", b.Address, err))
}
return addr
}

View File

@ -1,4 +1,4 @@
package types
package types_test
import (
"testing"
@ -6,34 +6,34 @@ import (
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
)
func TestBalanceValidate(t *testing.T) {
testCases := []struct {
name string
balance Balance
balance bank.Balance
expErr bool
}{
{
"valid balance",
Balance{
bank.Balance{
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t",
Coins: sdk.Coins{sdk.NewInt64Coin("uatom", 1)},
},
false,
},
{"empty balance", Balance{}, true},
{"empty balance", bank.Balance{}, true},
{
"nil balance coins",
Balance{
bank.Balance{
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t",
},
true,
},
{
"dup coins",
Balance{
bank.Balance{
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t",
Coins: sdk.Coins{
sdk.NewInt64Coin("uatom", 1),
@ -44,7 +44,7 @@ func TestBalanceValidate(t *testing.T) {
},
{
"invalid coin denom",
Balance{
bank.Balance{
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t",
Coins: sdk.Coins{
sdk.Coin{Denom: "", Amount: sdk.OneInt()},
@ -54,7 +54,7 @@ func TestBalanceValidate(t *testing.T) {
},
{
"negative coin",
Balance{
bank.Balance{
Address: "cosmos1yq8lgssgxlx9smjhes6ryjasmqmd3ts2559g0t",
Coins: sdk.Coins{
sdk.Coin{Denom: "uatom", Amount: sdk.NewInt(-1)},
@ -78,3 +78,26 @@ func TestBalanceValidate(t *testing.T) {
})
}
}
func TestBalance_GetAddress(t *testing.T) {
tests := []struct {
name string
Address string
wantPanic bool
}{
{"empty address", "", true},
{"malformed address", "invalid", true},
{"valid address", "cosmos1vy0ga0klndqy92ceqehfkvgmn4t94eteq4hmqv", false},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
b := bank.Balance{Address: tt.Address}
if tt.wantPanic {
require.Panics(t, func() { b.GetAddress() })
} else {
require.False(t, b.GetAddress().Empty())
}
})
}
}