Skip to content

Commit

Permalink
Don't blacklist nodes on EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed May 10, 2024
1 parent edb6aad commit 696b0f9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
12 changes: 9 additions & 3 deletions pkg/api/v3/message/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package message
import (
"context"
"encoding/json"
"io"

"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -101,10 +102,14 @@ func (c *RoutedTransport) RoundTrip(ctx context.Context, requests []Message, cal
res, err := s.Read()
if err != nil {
// Add peer ID
p, ok := err.(interface{ Peer() peer.ID })
var perr interface{ Peer() peer.ID }
ok := errors.As(err, &perr)
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
err = errors.StreamAborted.WithFormat("%v", err)
}
err := errors.PeerMisbehaved.WithFormat("read request: %w", err)
if ok {
err.Data, _ = json.Marshal(struct{ Peer peer.ID }{Peer: p.Peer()})
err.Data, _ = json.Marshal(struct{ Peer peer.ID }{Peer: perr.Peer()})
}
return err
}
Expand Down Expand Up @@ -297,7 +302,8 @@ func (c *RoutedTransport) dial(ctx context.Context, addr multiaddr.Multiaddr, st
// Return the error if it's a client error (e.g. misdial)
return nil, errors.UnknownError.Wrap(err)

case errors.EncodingError.ErrorAs(err, &err2):
case errors.EncodingError.ErrorAs(err, &err2),
errors.StreamAborted.ErrorAs(err, &err2):
// If the error is an encoding issue, log it and return "internal error"
if isMulti {
multi.BadDial(ctx, addr, s, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v3/p2p/dial/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (d *dialer) BadDial(ctx context.Context, addr multiaddr.Multiaddr, s messag

slog.InfoCtx(ctx, "Bad dial", "peer", ss.peer, "address", addr, "error", err)

if errors.Is(err, errors.EncodingError) {
if errors.Is(err, errors.EncodingError) || errors.Is(err, errors.StreamAborted) {
// Don't mark a peer bad if there's an encoding failure. Is this a good
// idea?
return false
Expand Down
9 changes: 8 additions & 1 deletion pkg/errors/enums_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,17 @@ const PeerMisbehaved Status = 507
// InvalidRecord means the database has one or more invalid records.
const InvalidRecord Status = 508

// StreamAborted is equivalent to [io.UnexpectedEOF].
const StreamAborted Status = 509

// GetEnumValue returns the value of the Status
func (v Status) GetEnumValue() uint64 { return uint64(v) }

// SetEnumValue sets the value. SetEnumValue returns false if the value is invalid.
func (v *Status) SetEnumValue(id uint64) bool {
u := Status(id)
switch u {
case OK, Delivered, Pending, Remote, WrongPartition, BadRequest, Unauthenticated, InsufficientCredits, Unauthorized, NotFound, NotAllowed, Rejected, Expired, Conflict, BadSignerVersion, BadTimestamp, BadUrlLength, IncompleteChain, InsufficientBalance, InternalError, UnknownError, EncodingError, FatalError, NotReady, WrongType, NoPeer, PeerMisbehaved, InvalidRecord:
case OK, Delivered, Pending, Remote, WrongPartition, BadRequest, Unauthenticated, InsufficientCredits, Unauthorized, NotFound, NotAllowed, Rejected, Expired, Conflict, BadSignerVersion, BadTimestamp, BadUrlLength, IncompleteChain, InsufficientBalance, InternalError, UnknownError, EncodingError, FatalError, NotReady, WrongType, NoPeer, PeerMisbehaved, InvalidRecord, StreamAborted:
*v = u
return true
}
Expand Down Expand Up @@ -171,6 +174,8 @@ func (v Status) String() string {
return "peerMisbehaved"
case InvalidRecord:
return "invalidRecord"
case StreamAborted:
return "streamAborted"
}
return fmt.Sprintf("Status:%d", v)
}
Expand Down Expand Up @@ -234,6 +239,8 @@ func StatusByName(name string) (Status, bool) {
return PeerMisbehaved, true
case "invalidrecord":
return InvalidRecord, true
case "streamaborted":
return StreamAborted, true
}
return 0, false
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/errors/status.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ Status:
InvalidRecord:
value: 508
description: means the database has one or more invalid records
StreamAborted:
value: 509
description: is equivalent to [io.ErrUnexpectedEOF]

0 comments on commit 696b0f9

Please sign in to comment.