Skip to content

Commit

Permalink
feat(dot/parachains) implement ReputationAggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielDDHM committed Dec 11, 2024
1 parent 6b42c5b commit c7ea3f0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
2 changes: 0 additions & 2 deletions dot/parachain/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func (r *ReputationAggregator) Modify(overseerCh chan<- NetworkBridgeTxMessage,
}

r.byPeer[peerID] = primitives.SaturatingAdd(r.byPeer[peerID], rep.CostOrBenefit())
fmt.Printf("Accumulated reputation change for peer %s: %+v\n", peerID, rep)
}

// singleSend sends a single reputation change directly.
Expand All @@ -144,7 +143,6 @@ func (r *ReputationAggregator) singleSend(overseerCh chan<- NetworkBridgeTxMessa
},
}
overseerCh <- message
fmt.Printf("Sent immediate reputation change for peer %s: %+v\n", peerID, rep)
}

// SigningKeyAndIndex finds the first key we can sign with from the given set of validators,
Expand Down
20 changes: 10 additions & 10 deletions dot/parachain/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestReputationAggregator_SendImmediately(t *testing.T) {
// Mock channel

overseerCh := make(chan NetworkBridgeTxMessage, 1)

// Create a new aggregator with immediate send logic for Malicious type
Expand All @@ -20,7 +20,7 @@ func TestReputationAggregator_SendImmediately(t *testing.T) {
peerID := peer.ID("peer1")
repChange := UnifiedReputationChange{
Type: Malicious,
Reason: "Detected malicious behavior",
Reason: "Detected malicious behaviour",
}

// Modify the aggregator
Expand All @@ -37,7 +37,7 @@ func TestReputationAggregator_SendImmediately(t *testing.T) {
}

func TestReputationAggregator_BatchSend(t *testing.T) {
// Mock channel

overseerCh := make(chan NetworkBridgeTxMessage, 1)

// Create a new aggregator with no immediate send logic
Expand All @@ -48,8 +48,8 @@ func TestReputationAggregator_BatchSend(t *testing.T) {
// Add multiple reputation changes
peerID1 := peer.ID("peer1")
peerID2 := peer.ID("peer2")
aggregator.Modify(overseerCh, peerID1, UnifiedReputationChange{Type: BenefitMinor, Reason: "Good behavior"})
aggregator.Modify(overseerCh, peerID2, UnifiedReputationChange{Type: BenefitMajor, Reason: "Excellent behavior"})
aggregator.Modify(overseerCh, peerID1, UnifiedReputationChange{Type: BenefitMinor, Reason: "Good behaviour"})
aggregator.Modify(overseerCh, peerID2, UnifiedReputationChange{Type: BenefitMajor, Reason: "Excellent behaviour"})

// Verify no messages were sent yet
select {
Expand All @@ -73,7 +73,7 @@ func TestReputationAggregator_BatchSend(t *testing.T) {
}

func TestReputationAggregator_ClearAfterSend(t *testing.T) {
// Mock channel

overseerCh := make(chan NetworkBridgeTxMessage, 1)

// Create a new aggregator
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestReputationAggregator_ClearAfterSend(t *testing.T) {
}

func TestReputationAggregator_ConflictResolution(t *testing.T) {
// Mock channel

overseerCh := make(chan NetworkBridgeTxMessage, 1)

// Create a new aggregator
Expand All @@ -111,7 +111,7 @@ func TestReputationAggregator_ConflictResolution(t *testing.T) {

// Add multiple reputation changes for the same peer
peerID := peer.ID("peer1")
aggregator.Modify(overseerCh, peerID, UnifiedReputationChange{Type: BenefitMajor, Reason: "Helpful behavior"})
aggregator.Modify(overseerCh, peerID, UnifiedReputationChange{Type: BenefitMajor, Reason: "Helpful behaviour"})
aggregator.Modify(overseerCh, peerID, UnifiedReputationChange{Type: CostMinor, Reason: "Minor issue"})

// Call Send to flush changes
Expand All @@ -128,7 +128,7 @@ func TestReputationAggregator_ConflictResolution(t *testing.T) {
}

func TestReputationAggregator_NoActionWithoutChanges(t *testing.T) {
// Mock channel

overseerCh := make(chan NetworkBridgeTxMessage, 1)

// Create a new aggregator
Expand All @@ -144,6 +144,6 @@ func TestReputationAggregator_NoActionWithoutChanges(t *testing.T) {
case <-overseerCh:
t.Error("Expected no message, but one was sent")
default:
// Expected behavior
// Expected behaviour
}
}

0 comments on commit c7ea3f0

Please sign in to comment.