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
47 changes: 37 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,10 @@ 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")
err = s.submitOutOfOrderReport(stream.nodeID, stream.lastEnvelope, env)
if err != nil {
s.log.Error("Failed to submit out of order report", zap.Error(err))
}
}

if env.OriginatorSequenceID() > lastSequenceID {
Expand All @@ -375,6 +380,28 @@ func (s *syncWorker) validateAndInsertEnvelope(
s.insertEnvelope(env)
}

func (s *syncWorker) submitOutOfOrderReport(
nodeID uint32,
lastEnvelope *envUtils.OriginatorEnvelope,
currentEnvelope *envUtils.OriginatorEnvelope,
) error {
report, err := misbehavior.NewSafetyFailureReport(
nodeID,
message_api.Misbehavior_MISBEHAVIOR_OUT_OF_ORDER,
true,
[]*envUtils.OriginatorEnvelope{lastEnvelope, currentEnvelope},
)
if err != nil {
return err
}

err = s.misbehaviorService.SafetyFailure(report)
if err != nil {
return err
}
return nil
}
neekolas marked this conversation as resolved.
Show resolved Hide resolved

func (s *syncWorker) insertEnvelope(env *envUtils.OriginatorEnvelope) {
s.log.Debug("Replication server received envelope", zap.Any("envelope", env))
originatorBytes, err := env.Bytes()
Expand Down
Loading