/
interface.go
283 lines (246 loc) · 9.44 KB
/
interface.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package p2p
import (
"context"
"encoding/hex"
"io"
"time"
"github.com/bhagyaraj1208117/andes-abc-1/core"
crypto "github.com/bhagyaraj1208117/andes-crypto-go"
logger "github.com/bhagyaraj1208117/andes-logger-xyz"
)
// MessageProcessor is the interface used to describe what a receive message processor should do
// All implementations that will be called from Messenger implementation will need to satisfy this interface
// If the function returns a non nil value, the received message will not be propagated to its connected peers
type MessageProcessor interface {
ProcessReceivedMessage(message MessageP2P, fromConnectedPeer core.PeerID, source MessageHandler) error
IsInterfaceNil() bool
}
// PeerDiscoverer defines the behaviour of a peer discovery mechanism
type PeerDiscoverer interface {
Bootstrap() error
Name() string
IsInterfaceNil() bool
}
// Reconnecter defines the behaviour of a network reconnection mechanism
type Reconnecter interface {
ReconnectToNetwork(ctx context.Context)
IsInterfaceNil() bool
}
// MessageHandler defines the behaviour of a component able to send and process messages
type MessageHandler interface {
io.Closer
MessageProcessor
CreateTopic(name string, createChannelForTopic bool) error
HasTopic(name string) bool
RegisterMessageProcessor(topic string, identifier string, handler MessageProcessor) error
UnregisterAllMessageProcessors() error
UnregisterMessageProcessor(topic string, identifier string) error
Broadcast(topic string, buff []byte)
BroadcastOnChannel(channel string, topic string, buff []byte)
BroadcastUsingPrivateKey(topic string, buff []byte, pid core.PeerID, skBytes []byte)
BroadcastOnChannelUsingPrivateKey(channel string, topic string, buff []byte, pid core.PeerID, skBytes []byte)
SendToConnectedPeer(topic string, buff []byte, peerID core.PeerID) error
UnJoinAllTopics() error
SetDebugger(debugger Debugger) error
IsInterfaceNil() bool
}
// ConnectionsHandler defines the behaviour of a component able to handle connections
type ConnectionsHandler interface {
io.Closer
Bootstrap() error
Peers() []core.PeerID
Addresses() []string
ConnectToPeer(address string) error
IsConnected(peerID core.PeerID) bool
ConnectedPeers() []core.PeerID
ConnectedAddresses() []string
PeerAddresses(pid core.PeerID) []string
ConnectedPeersOnTopic(topic string) []core.PeerID
SetPeerShardResolver(peerShardResolver PeerShardResolver) error
GetConnectedPeersInfo() *ConnectedPeersInfo
WaitForConnections(maxWaitingTime time.Duration, minNumOfPeers uint32)
IsConnectedToTheNetwork() bool
ThresholdMinConnectedPeers() int
SetThresholdMinConnectedPeers(minConnectedPeers int) error
SetPeerDenialEvaluator(handler PeerDenialEvaluator) error
IsInterfaceNil() bool
}
// Messenger is the main interface used for communication with other peers
type Messenger interface {
MessageHandler
ConnectionsHandler
ID() core.PeerID
Port() int
Sign(payload []byte) ([]byte, error)
Verify(payload []byte, pid core.PeerID, signature []byte) error
SignUsingPrivateKey(skBytes []byte, payload []byte) ([]byte, error)
AddPeerTopicNotifier(notifier PeerTopicNotifier) error
IsInterfaceNil() bool
}
// MessageP2P defines what a p2p message can do (should return)
type MessageP2P interface {
From() []byte
Data() []byte
Payload() []byte
SeqNo() []byte
Topic() string
Signature() []byte
Key() []byte
Peer() core.PeerID
Timestamp() int64
BroadcastMethod() BroadcastMethod
IsInterfaceNil() bool
}
// DirectSender defines a component that can send direct messages to connected peers
type DirectSender interface {
NextSequenceNumber() []byte
Send(topic string, buff []byte, peer core.PeerID) error
RegisterDirectMessageProcessor(handler MessageHandler) error
IsInterfaceNil() bool
}
// PeerDiscoveryFactory defines the factory for peer discoverer implementation
type PeerDiscoveryFactory interface {
CreatePeerDiscoverer() (PeerDiscoverer, error)
IsInterfaceNil() bool
}
// MessageOriginatorPid will output the message peer id in a pretty format
// If it can, it will display the last displayLastPidChars (12) characters from the pid
func MessageOriginatorPid(msg MessageP2P) string {
return PeerIdToShortString(msg.Peer())
}
// PeerIdToShortString trims the first displayLastPidChars characters of the provided peer ID after
// converting the peer ID to string using the Pretty functionality
func PeerIdToShortString(pid core.PeerID) string {
prettyPid := pid.Pretty()
lenPrettyPid := len(prettyPid)
if lenPrettyPid > displayLastPidChars {
return "..." + prettyPid[lenPrettyPid-displayLastPidChars:]
}
return prettyPid
}
// MessageOriginatorSeq will output the sequence number as hex
func MessageOriginatorSeq(msg MessageP2P) string {
return hex.EncodeToString(msg.SeqNo())
}
// PeerShardResolver is able to resolve the link between the provided PeerID and the shardID
type PeerShardResolver interface {
GetPeerInfo(pid core.PeerID) core.P2PPeerInfo
IsInterfaceNil() bool
}
// ConnectedPeersInfo represents the DTO structure used to output the metrics for connected peers
type ConnectedPeersInfo struct {
SelfShardID uint32
UnknownPeers []string
Seeders []string
IntraShardValidators map[uint32][]string
IntraShardObservers map[uint32][]string
CrossShardValidators map[uint32][]string
CrossShardObservers map[uint32][]string
NumValidatorsOnShard map[uint32]int
NumObserversOnShard map[uint32]int
NumPreferredPeersOnShard map[uint32]int
NumIntraShardValidators int
NumIntraShardObservers int
NumCrossShardValidators int
NumCrossShardObservers int
}
// NetworkShardingCollector defines the updating methods used by the network sharding component
// The interface assures that the collected data will be used by the p2p network sharding components
type NetworkShardingCollector interface {
UpdatePeerIDInfo(pid core.PeerID, pk []byte, shardID uint32)
IsInterfaceNil() bool
}
// SignerVerifier is used in higher level protocol authentication of 2 peers after the basic p2p connection has been made
type SignerVerifier interface {
Sign(payload []byte) ([]byte, error)
Verify(payload []byte, pid core.PeerID, signature []byte) error
IsInterfaceNil() bool
}
// Marshaller defines the 2 basic operations: serialize (marshal) and deserialize (unmarshal)
type Marshaller interface {
Marshal(obj interface{}) ([]byte, error)
Unmarshal(obj interface{}, buff []byte) error
IsInterfaceNil() bool
}
// PreferredPeersHolderHandler defines the behavior of a component able to handle preferred peers operations
type PreferredPeersHolderHandler interface {
PutConnectionAddress(peerID core.PeerID, address string)
PutShardID(peerID core.PeerID, shardID uint32)
Get() map[uint32][]core.PeerID
Contains(peerID core.PeerID) bool
Remove(peerID core.PeerID)
Clear()
IsInterfaceNil() bool
}
// PeerCounts represents the DTO structure used to output the count metrics for connected peers
type PeerCounts struct {
UnknownPeers int
IntraShardPeers int
CrossShardPeers int
}
// Sharder defines the eviction computing process of unwanted peers
type Sharder interface {
SetSeeders(addresses []string)
IsSeeder(pid core.PeerID) bool
SetPeerShardResolver(psp PeerShardResolver) error
IsInterfaceNil() bool
}
// PeerDenialEvaluator defines the behavior of a component that is able to decide if a peer ID is black listed or not
// TODO move antiflooding inside network messenger
type PeerDenialEvaluator interface {
IsDenied(pid core.PeerID) bool
UpsertPeerID(pid core.PeerID, duration time.Duration) error
IsInterfaceNil() bool
}
// Debugger represent a p2p debugger able to print p2p statistics (messages received/sent per topic)
type Debugger interface {
AddIncomingMessage(topic string, size uint64, isRejected bool)
AddOutgoingMessage(topic string, size uint64, isRejected bool)
Close() error
IsInterfaceNil() bool
}
// SyncTimer represent an entity able to tell the current time
type SyncTimer interface {
CurrentTime() time.Time
IsInterfaceNil() bool
}
// ConnectionsWatcher represent an entity able to watch new connections
type ConnectionsWatcher interface {
NewKnownConnection(pid core.PeerID, connection string)
Close() error
IsInterfaceNil() bool
}
// PeersRatingHandler represent an entity able to handle peers ratings
type PeersRatingHandler interface {
IncreaseRating(pid core.PeerID)
DecreaseRating(pid core.PeerID)
GetTopRatedPeersFromList(peers []core.PeerID, minNumOfPeersExpected int) []core.PeerID
IsInterfaceNil() bool
}
// PeersRatingMonitor represent an entity able to provide peers ratings
type PeersRatingMonitor interface {
GetConnectedPeersRatings(connectionsHandler ConnectionsHandler) (string, error)
IsInterfaceNil() bool
}
// PeerTopicNotifier represent an entity able to handle new notifications on a new peer on a topic
type PeerTopicNotifier interface {
NewPeerFound(pid core.PeerID, topic string)
IsInterfaceNil() bool
}
// P2PKeyConverter defines what a p2p key converter can do
type P2PKeyConverter interface {
ConvertPeerIDToPublicKey(keyGen crypto.KeyGenerator, pid core.PeerID) (crypto.PublicKey, error)
ConvertPublicKeyToPeerID(pk crypto.PublicKey) (core.PeerID, error)
IsInterfaceNil() bool
}
// Logger defines the behavior of a data logger component
type Logger interface {
Trace(message string, args ...interface{})
Debug(message string, args ...interface{})
Info(message string, args ...interface{})
Warn(message string, args ...interface{})
Error(message string, args ...interface{})
LogIfError(err error, args ...interface{})
GetLevel() logger.LogLevel
IsInterfaceNil() bool
}