-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathcache.go
More file actions
85 lines (74 loc) · 3.9 KB
/
cache.go
File metadata and controls
85 lines (74 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package p2p
import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
)
// ProtocolPeerCache is an interface that stores a mapping from protocol ID to peers who support that protocol.
type ProtocolPeerCache interface {
// RemovePeer removes the specified peer from the protocol cache.
RemovePeer(peerID peer.ID)
// AddProtocols adds the specified protocols for the given peer to the protocol cache.
AddProtocols(peerID peer.ID, protocols []protocol.ID)
// RemoveProtocols removes the specified protocols for the given peer from the protocol cache.
RemoveProtocols(peerID peer.ID, protocols []protocol.ID)
// GetPeers returns a copy of the set of peers that support the given protocol.
GetPeers(pid protocol.ID) map[peer.ID]struct{}
}
// UpdateFunction is a function that adjusts the GossipSub spam record of a peer.
// Args:
// - record: the GossipSubSpamRecord of the peer.
// Returns:
// - *GossipSubSpamRecord: the adjusted GossipSubSpamRecord of the peer.
type UpdateFunction func(record GossipSubSpamRecord) GossipSubSpamRecord
// GossipSubSpamRecordCache is a cache for storing the GossipSub spam records of peers.
// The spam records of peers is used to calculate the application specific score, which is part of the GossipSub score of a peer.
// Note that none of the spam records, application specific score, and GossipSub score are shared publicly with other peers.
// Rather they are solely used by the current peer to select the peers to which it will connect on a topic mesh.
//
// Implementation must be thread-safe.
type GossipSubSpamRecordCache interface {
// Add adds the GossipSubSpamRecord of a peer to the cache.
// Args:
// - peerID: the peer ID of the peer in the GossipSub protocol.
// - record: the GossipSubSpamRecord of the peer.
//
// Returns:
// - bool: true if the record was added successfully, false otherwise.
Add(peerId peer.ID, record GossipSubSpamRecord) bool
// Get returns the GossipSubSpamRecord of a peer from the cache.
// Args:
// - peerID: the peer ID of the peer in the GossipSub protocol.
// Returns:
// - *GossipSubSpamRecord: the GossipSubSpamRecord of the peer.
// - error on failure to retrieve the record. The returned error is irrecoverable and indicates an exception.
// - bool: true if the record was retrieved successfully, false otherwise.
Get(peerID peer.ID) (*GossipSubSpamRecord, error, bool)
// Update updates the GossipSub spam penalty of a peer in the cache using the given adjust function.
// Args:
// - peerID: the peer ID of the peer in the GossipSub protocol.
// - adjustFn: the adjust function to be applied to the record.
// Returns:
// - *GossipSubSpamRecord: the updated record.
// - error on failure to update the record. The returned error is irrecoverable and indicates an exception.
Update(peerID peer.ID, updateFunc UpdateFunction) (*GossipSubSpamRecord, error)
// Has returns true if the cache contains the GossipSubSpamRecord of the given peer.
// Args:
// - peerID: the peer ID of the peer in the GossipSub protocol.
// Returns:
// - bool: true if the cache contains the GossipSubSpamRecord of the given peer, false otherwise.
Has(peerID peer.ID) bool
}
// GossipSubSpamRecord represents spam record of a peer in the GossipSub protocol.
// It acts as a penalty card for a peer in the GossipSub protocol that keeps the
// spam penalty of the peer as well as its decay factor.
// GossipSubSpam record is used to calculate the application specific score of a peer in the GossipSub protocol.
type GossipSubSpamRecord struct {
// Decay factor of gossipsub spam penalty.
// The Penalty is multiplied by the Decay factor every time the Penalty is updated.
// This is to prevent the Penalty from being stuck at a negative value.
// Each peer has its own Decay factor based on its behavior.
// Valid decay value is in the range [0, 1].
Decay float64
// Penalty is the application specific Penalty of the peer.
Penalty float64
}