Skip to content

Commit

Permalink
Add helpful debug info to VerifyAddressFormat (cosmos#7016)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
webmaster128 and mergify[bot] authored Aug 12, 2020
1 parent 3a4e608 commit 034b478
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func VerifyAddressFormat(bz []byte) error {
return verifier(bz)
}
if len(bz) != AddrLen {
return errors.New("incorrect address length")
return fmt.Errorf("incorrect address length (expected: %d, actual: %d)", AddrLen, len(bz))
}
return nil
}
Expand Down
16 changes: 16 additions & 0 deletions types/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,22 @@ func TestAddressInterface(t *testing.T) {

}

func TestVerifyAddressFormat(t *testing.T) {
addr0 := make([]byte, 0)
addr5 := make([]byte, 5)
addr20 := make([]byte, 20)
addr32 := make([]byte, 32)

err := types.VerifyAddressFormat(addr0)
require.EqualError(t, err, "incorrect address length (expected: 20, actual: 0)")
err = types.VerifyAddressFormat(addr5)
require.EqualError(t, err, "incorrect address length (expected: 20, actual: 5)")
err = types.VerifyAddressFormat(addr20)
require.Nil(t, err)
err = types.VerifyAddressFormat(addr32)
require.EqualError(t, err, "incorrect address length (expected: 20, actual: 32)")
}

func TestCustomAddressVerifier(t *testing.T) {
// Create a 10 byte address
addr := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Expand Down

0 comments on commit 034b478

Please sign in to comment.