Skip to content

Commit

Permalink
bank: don't ignore errors returned by Balance.GetAddress()
Browse files Browse the repository at this point in the history
Closes: #8530
From: #8531
Thanks: @odeke-em for pointing this out.
  • Loading branch information
Alessio Treglia committed Feb 7, 2021
1 parent 8a79ec1 commit 436a2ab
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
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())
}
})
}
}

0 comments on commit 436a2ab

Please sign in to comment.