Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backport/v0.41.x]: bank: don't ignore errors returned by Balance.GetAddress() #8534

Merged
merged 1 commit into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion x/bank/types/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
41 changes: 32 additions & 9 deletions x/bank/types/balance_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package types
package types_test

import (
"testing"

"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),
Expand All @@ -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()},
Expand All @@ -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)},
Expand All @@ -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())
}
})
}
}