-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
iface.go
138 lines (114 loc) · 4.45 KB
/
iface.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package store
import (
"errors"
"math"
"net"
"time"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
)
type TopicScores struct {
TimeInMesh float64 `json:"timeInMesh"` // in seconds
FirstMessageDeliveries float64 `json:"firstMessageDeliveries"`
MeshMessageDeliveries float64 `json:"meshMessageDeliveries"`
InvalidMessageDeliveries float64 `json:"invalidMessageDeliveries"`
}
type GossipScores struct {
Total float64 `json:"total"`
Blocks TopicScores `json:"blocks"` // fully zeroed if the peer has not been in the mesh on the topic
IPColocationFactor float64 `json:"IPColocationFactor"`
BehavioralPenalty float64 `json:"behavioralPenalty"`
}
func (g GossipScores) Apply(rec *scoreRecord) {
rec.PeerScores.Gossip = g
}
type ReqRespScores struct {
ValidResponses float64 `json:"validResponses"`
ErrorResponses float64 `json:"errorResponses"`
RejectedPayloads float64 `json:"rejectedPayloads"`
}
type IncrementValidResponses struct {
Cap float64
}
func (i IncrementValidResponses) Apply(rec *scoreRecord) {
rec.PeerScores.ReqResp.ValidResponses = math.Min(rec.PeerScores.ReqResp.ValidResponses+1, i.Cap)
}
type IncrementErrorResponses struct {
Cap float64
}
func (i IncrementErrorResponses) Apply(rec *scoreRecord) {
rec.PeerScores.ReqResp.ErrorResponses = math.Min(rec.PeerScores.ReqResp.ErrorResponses+1, i.Cap)
}
type IncrementRejectedPayloads struct {
Cap float64
}
func (i IncrementRejectedPayloads) Apply(rec *scoreRecord) {
rec.PeerScores.ReqResp.RejectedPayloads = math.Min(rec.PeerScores.ReqResp.RejectedPayloads+1, i.Cap)
}
type DecayApplicationScores struct {
ValidResponseDecay float64
ErrorResponseDecay float64
RejectedPayloadDecay float64
DecayToZero float64
}
func (d *DecayApplicationScores) Apply(rec *scoreRecord) {
decay := func(value float64, decay float64) float64 {
value *= decay
if value < d.DecayToZero {
return 0
}
return value
}
rec.PeerScores.ReqResp.ValidResponses = decay(rec.PeerScores.ReqResp.ValidResponses, d.ValidResponseDecay)
rec.PeerScores.ReqResp.ErrorResponses = decay(rec.PeerScores.ReqResp.ErrorResponses, d.ErrorResponseDecay)
rec.PeerScores.ReqResp.RejectedPayloads = decay(rec.PeerScores.ReqResp.RejectedPayloads, d.RejectedPayloadDecay)
}
type PeerScores struct {
Gossip GossipScores `json:"gossip"`
ReqResp ReqRespScores `json:"reqResp"`
}
// ScoreDatastore defines a type-safe API for getting and setting libp2p peer score information
type ScoreDatastore interface {
// GetPeerScores returns the current scores for the specified peer
GetPeerScores(id peer.ID) (PeerScores, error)
// GetPeerScore returns the current combined score for the specified peer
GetPeerScore(id peer.ID) (float64, error)
// SetScore applies the given store diff to the specified peer
SetScore(id peer.ID, diff ScoreDiff) (PeerScores, error)
}
// ScoreDiff defines a type-safe batch of changes to apply to the peer-scoring record of the peer.
// The scoreRecord the diff is applied to is private: diffs can only be defined in this package,
// to ensure changes to the record are non-breaking.
type ScoreDiff interface {
Apply(score *scoreRecord)
}
var UnknownBanErr = errors.New("unknown ban")
type PeerBanStore interface {
// SetPeerBanExpiration create the peer ban with expiration time.
// If expiry == time.Time{} then the ban is deleted.
SetPeerBanExpiration(id peer.ID, expiry time.Time) error
// GetPeerBanExpiration gets the peer ban expiration time, or UnknownBanErr error if none exists.
GetPeerBanExpiration(id peer.ID) (time.Time, error)
}
type IPBanStore interface {
// SetIPBanExpiration create the IP ban with expiration time.
// If expiry == time.Time{} then the ban is deleted.
SetIPBanExpiration(ip net.IP, expiry time.Time) error
// GetIPBanExpiration gets the IP ban expiration time, or UnknownBanErr error if none exists.
GetIPBanExpiration(ip net.IP) (time.Time, error)
}
type MetadataStore interface {
// SetPeerMetadata sets the metadata for the specified peer
SetPeerMetadata(id peer.ID, md PeerMetadata) (PeerMetadata, error)
// GetPeerMetadata returns the metadata for the specified peer
GetPeerMetadata(id peer.ID) (PeerMetadata, error)
}
// ExtendedPeerstore defines a type-safe API to work with additional peer metadata based on a libp2p peerstore.Peerstore
type ExtendedPeerstore interface {
peerstore.Peerstore
ScoreDatastore
peerstore.CertifiedAddrBook
PeerBanStore
IPBanStore
MetadataStore
}