Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/golang.org/x/sync-0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ramin authored Jan 9, 2024
2 parents f2b5cdc + 73336ed commit 1e33c75
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (ce *Exchange) GetRangeByHeight(
ce.metrics.requestDurationPerHeader(ctx, time.Since(start), amount)

for _, h := range headers {
err := from.Verify(h)
err := libhead.Verify[*header.ExtendedHeader](from, h, libhead.DefaultHeightThreshold)
if err != nil {
return nil, fmt.Errorf("verifying next header against last verified height: %d: %w",
from.Height(), err)
Expand Down
5 changes: 4 additions & 1 deletion libs/utils/address.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package utils

import (
"errors"
"fmt"
"net"
"strings"
)

var ErrInvalidIP = errors.New("invalid IP address or hostname given")

// SanitizeAddr trims leading protocol scheme and port from the given
// IP address or hostname if present.
func SanitizeAddr(addr string) (string, error) {
Expand All @@ -16,7 +19,7 @@ func SanitizeAddr(addr string) (string, error) {
addr = strings.TrimSuffix(addr, "/")
addr = strings.Split(addr, ":")[0]
if addr == "" {
return "", fmt.Errorf("invalid IP address or hostname given: %s", original)
return "", fmt.Errorf("%w: %s", ErrInvalidIP, original)
}
return addr, nil
}
Expand Down
5 changes: 4 additions & 1 deletion libs/utils/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func TestSanitizeAddr(t *testing.T) {
var tests = []struct {
addr string
want string
err error
}{
// Testcase: trims protocol prefix
{addr: "http://celestia.org", want: "celestia.org"},
Expand All @@ -20,13 +21,15 @@ func TestSanitizeAddr(t *testing.T) {
{addr: "tcp://192.168.42.42:5050/", want: "192.168.42.42"},
// Testcase: invariant ip
{addr: "192.168.42.42", want: "192.168.42.42"},
// Testcase: empty addr
{addr: "", want: "", err: ErrInvalidIP},
}

for _, tt := range tests {
t.Run(tt.addr, func(t *testing.T) {
got, err := SanitizeAddr(tt.addr)
require.NoError(t, err)
require.Equal(t, tt.want, got)
require.ErrorIs(t, err, tt.err)
})
}
}
Expand Down

0 comments on commit 1e33c75

Please sign in to comment.