From 40a0c6ff20683c87cf19cc9679419c8972893a4d Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sat, 6 Feb 2021 12:24:26 +0000 Subject: [PATCH] bank: don't ignore errors returned by Balance.GetAddress() Closes: #8530 --- x/bank/types/balance.go | 6 +++++- x/bank/types/balance_test.go | 41 ++++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/x/bank/types/balance.go b/x/bank/types/balance.go index 93517a1d925b..49330bc06af1 100644 --- a/x/bank/types/balance.go +++ b/x/bank/types/balance.go @@ -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 } diff --git a/x/bank/types/balance_test.go b/x/bank/types/balance_test.go index 73e4d8eff387..6282b2478b19 100644 --- a/x/bank/types/balance_test.go +++ b/x/bank/types/balance_test.go @@ -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()) + } + }) + } +}