Add helpful debug info to VerifyAddressFormat (#7016)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Simon Warta 2020-08-12 15:03:28 +02:00 committed by GitHub
parent 3a4e608930
commit 034b478a60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -111,7 +111,7 @@ func VerifyAddressFormat(bz []byte) error {
return verifier(bz) return verifier(bz)
} }
if len(bz) != AddrLen { 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 return nil
} }

View File

@ -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) { func TestCustomAddressVerifier(t *testing.T) {
// Create a 10 byte address // Create a 10 byte address
addr := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} addr := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}