Skip to content

Commit 07fe262

Browse files
committed
Updates
1 parent 524a754 commit 07fe262

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

services/p2p/HandleCatchupMetrics.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,20 @@ func (s *Server) GetPeersForCatchup(ctx context.Context, req *p2p_api.GetPeersFo
102102
// Convert to proto format
103103
protoPeers := make([]*p2p_api.PeerInfoForCatchup, 0, len(peers))
104104
for _, p := range peers {
105+
// Calculate total attempts as sum of successes and failures
106+
// InteractionAttempts is a separate counter that may not match
107+
totalAttempts := p.InteractionSuccesses + p.InteractionFailures
108+
105109
protoPeers = append(protoPeers, &p2p_api.PeerInfoForCatchup{
106110
Id: p.ID.String(),
107111
Height: p.Height,
108112
BlockHash: p.BlockHash,
109113
DataHubUrl: p.DataHubURL,
110114
IsHealthy: p.IsHealthy,
111-
CatchupReputationScore: p.ReputationScore, // Map new field to API field
112-
CatchupAttempts: p.InteractionAttempts, // Map new field to API field
113-
CatchupSuccesses: p.InteractionSuccesses, // Map new field to API field
114-
CatchupFailures: p.InteractionFailures, // Map new field to API field
115+
CatchupReputationScore: p.ReputationScore,
116+
CatchupAttempts: totalAttempts, // Use calculated total, not InteractionAttempts
117+
CatchupSuccesses: p.InteractionSuccesses, // Number of successful interactions
118+
CatchupFailures: p.InteractionFailures, // Number of failed interactions
115119
})
116120
}
117121

@@ -300,9 +304,10 @@ func (s *Server) IsPeerUnhealthy(ctx context.Context, req *p2p_api.IsPeerUnhealt
300304
}, nil
301305
}
302306

303-
// Check success rate based on attempts and successes
304-
if peerInfo.InteractionAttempts > 10 && peerInfo.InteractionSuccesses < peerInfo.InteractionAttempts/2 {
305-
successRate := float64(peerInfo.InteractionSuccesses) / float64(peerInfo.InteractionAttempts)
307+
// Check success rate based on total interactions (successes + failures)
308+
totalInteractions := peerInfo.InteractionSuccesses + peerInfo.InteractionFailures
309+
if totalInteractions > 10 && peerInfo.InteractionSuccesses < totalInteractions/2 {
310+
successRate := float64(peerInfo.InteractionSuccesses) / float64(totalInteractions)
306311
return &p2p_api.IsPeerUnhealthyResponse{
307312
IsUnhealthy: true,
308313
Reason: fmt.Sprintf("low success rate: %.2f%%", successRate*100),

services/p2p/sync_coordinator.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ func (sc *SyncCoordinator) isCaughtUp() bool {
9696
// Get all peers
9797
peers := sc.registry.GetAllPeers()
9898

99-
// Check if any peer is significantly ahead of us
99+
// Check if any peer is significantly ahead of us and has a good reputation
100100
for _, p := range peers {
101-
if p.Height > localHeight {
101+
if p.Height > localHeight && p.ReputationScore > 20 {
102102
return false // At least one peer is ahead
103103
}
104104
}
@@ -359,22 +359,22 @@ func (sc *SyncCoordinator) handleFSMTransition(currentState *blockchain_api.FSMS
359359
currentPeer, localHeight, peerInfo.Height)
360360

361361
// Record catchup failure for reputation tracking
362-
if sc.registry != nil {
362+
/* if sc.registry != nil {
363363
// Get peer info to check failure count
364364
peerInfo, _ := sc.registry.GetPeer(currentPeer)
365365
366366
// If this peer has failed multiple times recently, treat as malicious
367367
// (likely on an invalid chain)
368368
if peerInfo.InteractionFailures > 2 &&
369-
time.Since(peerInfo.LastInteractionFailure) < 5*time.Minute {
369+
time.Since(peerInfo.LastInteractionFailure) < 5*time.Minute {
370370
sc.registry.RecordMaliciousInteraction(currentPeer)
371371
sc.logger.Warnf("[SyncCoordinator] Peer %s has failed %d times recently, marking as potentially malicious",
372372
currentPeer, peerInfo.InteractionFailures)
373373
} else {
374374
sc.registry.RecordCatchupFailure(currentPeer)
375375
sc.logger.Infof("[SyncCoordinator] Recorded catchup failure for peer %s (reputation will decrease)", currentPeer)
376376
}
377-
}
377+
}*/
378378

379379
sc.ClearSyncPeer()
380380
_ = sc.TriggerSync()
@@ -724,12 +724,12 @@ func (sc *SyncCoordinator) checkAllPeersAttempted() {
724724
for _, p := range peers {
725725
// Count peers that would normally be eligible
726726
if p.Height > localHeight && p.IsHealthy && !p.IsBanned &&
727-
p.DataHubURL != "" && p.URLResponsive && p.ReputationScore >= 20 {
727+
p.DataHubURL != "" && p.URLResponsive && p.ReputationScore >= 20 {
728728
eligibleCount++
729729

730730
// Check if attempted recently
731731
if !p.LastSyncAttempt.IsZero() &&
732-
time.Since(p.LastSyncAttempt) < syncAttemptCooldown {
732+
time.Since(p.LastSyncAttempt) < syncAttemptCooldown {
733733
recentlyAttemptedCount++
734734
}
735735
}

0 commit comments

Comments
 (0)