Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start submitting misbehavior reports #414

Open
wants to merge 1 commit into
base: 01-15-add_loggingmisbehaviorservice
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions pkg/sync/syncWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/xmtp/xmtpd/pkg/db/queries"
envUtils "github.com/xmtp/xmtpd/pkg/envelopes"
clientInterceptors "github.com/xmtp/xmtpd/pkg/interceptors/client"
"github.com/xmtp/xmtpd/pkg/misbehavior"
"github.com/xmtp/xmtpd/pkg/proto/xmtpv4/envelopes"
"github.com/xmtp/xmtpd/pkg/proto/xmtpv4/message_api"
"github.com/xmtp/xmtpd/pkg/registrant"
Expand All @@ -31,6 +32,7 @@ type syncWorker struct {
subscriptions map[uint32]struct{}
subscriptionsMutex sync.RWMutex
cancel context.CancelFunc
misbehaviorService misbehavior.MisbehaviorService
}

type originatorStream struct {
Expand Down Expand Up @@ -58,14 +60,15 @@ func startSyncWorker(
ctx, cancel := context.WithCancel(ctx)

s := &syncWorker{
ctx: ctx,
log: log.Named("syncWorker"),
nodeRegistry: nodeRegistry,
registrant: registrant,
store: store,
wg: sync.WaitGroup{},
subscriptions: make(map[uint32]struct{}),
cancel: cancel,
ctx: ctx,
log: log.Named("syncWorker"),
nodeRegistry: nodeRegistry,
registrant: registrant,
store: store,
wg: sync.WaitGroup{},
subscriptions: make(map[uint32]struct{}),
cancel: cancel,
misbehaviorService: misbehavior.NewLoggingMisbehaviorService(log),
}
if err := s.start(); err != nil {
return nil, err
Expand Down Expand Up @@ -362,8 +365,19 @@ func (s *syncWorker) validateAndInsertEnvelope(
lastNs = stream.lastEnvelope.OriginatorNs()
}
if env.OriginatorSequenceID() != lastSequenceID+1 || env.OriginatorNs() < lastNs {
// TODO(rich) Submit misbehavior report and continue
s.log.Error("Received out of order envelope")
if report, err := misbehavior.NewSafetyFailureReport(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really struggle to read this inlined error handling. We generally don't use the format.

The explicit formatting is much more readable:

report, err := misbehavior.NewSafetyFailureReport(
    stream.nodeID,
    message_api.Misbehavior_MISBEHAVIOR_OUT_OF_ORDER,
    true,
    []*envUtils.OriginatorEnvelope{stream.lastEnvelope, env},
)
if err != nil {
    // Log the error if the report creation fails
    s.log.Debug("Failed to create misbehavior report", zap.Error(err))
    return // Exit early or handle it as appropriate
}

// Submit the misbehavior report
err = s.misbehaviorService.SafetyFailure(report)
if err != nil {
    // Log the error if the report submission fails
    s.log.Debug("Failed to submit misbehavior report", zap.Error(err))
}

stream.nodeID,
message_api.Misbehavior_MISBEHAVIOR_OUT_OF_ORDER,
true,
[]*envUtils.OriginatorEnvelope{stream.lastEnvelope, env},
); err == nil {
if err = s.misbehaviorService.SafetyFailure(report); err != nil {
s.log.Debug("Failed to submit misbehavior report", zap.Error(err))
}
} else {
s.log.Debug("Failed to create misbehavior report", zap.Error(err))
}

}

if env.OriginatorSequenceID() > lastSequenceID {
Expand Down
Loading