-
Notifications
You must be signed in to change notification settings - Fork 204
/
interface.go
359 lines (319 loc) · 12.4 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package dataRetriever
import (
"time"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/counting"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-go/p2p"
"github.com/multiversx/mx-chain-go/state"
"github.com/multiversx/mx-chain-go/storage"
)
// ResolverThrottler can monitor the number of the currently running resolver go routines
type ResolverThrottler interface {
CanProcess() bool
StartProcessing()
EndProcessing()
IsInterfaceNil() bool
}
// Resolver defines what a data resolver should do
type Resolver interface {
ProcessReceivedMessage(message p2p.MessageP2P, fromConnectedPeer core.PeerID, source p2p.MessageHandler) error
SetDebugHandler(handler DebugHandler) error
Close() error
IsInterfaceNil() bool
}
// Requester defines what a data requester should do
type Requester interface {
RequestDataFromHash(hash []byte, epoch uint32) error
SetNumPeersToQuery(intra int, cross int)
NumPeersToQuery() (int, int)
SetDebugHandler(handler DebugHandler) error
IsInterfaceNil() bool
}
// HeaderResolver defines what a block header resolver should do
type HeaderResolver interface {
Resolver
SetEpochHandler(epochHandler EpochHandler) error
}
// HeaderRequester defines what a block header requester should do
type HeaderRequester interface {
Requester
SetEpochHandler(epochHandler EpochHandler) error
}
// TopicResolverSender defines what sending operations are allowed for a topic resolver
type TopicResolverSender interface {
Send(buff []byte, peer core.PeerID, destination p2p.MessageHandler) error
RequestTopic() string
TargetShardID() uint32
SetDebugHandler(handler DebugHandler) error
DebugHandler() DebugHandler
IsInterfaceNil() bool
}
// ResolversContainer defines a resolvers holder data type with basic functionality
type ResolversContainer interface {
Get(key string) (Resolver, error)
Add(key string, val Resolver) error
AddMultiple(keys []string, resolvers []Resolver) error
Replace(key string, val Resolver) error
Remove(key string)
Len() int
ResolverKeys() string
Iterate(handler func(key string, resolver Resolver) bool)
Close() error
IsInterfaceNil() bool
}
// RequestersFinder extends a requesters container
type RequestersFinder interface {
RequestersContainer
IntraShardRequester(baseTopic string) (Requester, error)
MetaChainRequester(baseTopic string) (Requester, error)
CrossShardRequester(baseTopic string, crossShard uint32) (Requester, error)
MetaCrossShardRequester(baseTopic string, crossShard uint32) (Requester, error)
}
// ResolversContainerFactory defines the functionality to create a resolvers container
type ResolversContainerFactory interface {
Create() (ResolversContainer, error)
IsInterfaceNil() bool
}
// TopicRequestSender defines what sending operations are allowed for a topic requester
type TopicRequestSender interface {
SendOnRequestTopic(rd *RequestData, originalHashes [][]byte) error
SetNumPeersToQuery(intra int, cross int)
NumPeersToQuery() (int, int)
RequestTopic() string
TargetShardID() uint32
SetDebugHandler(handler DebugHandler) error
DebugHandler() DebugHandler
IsInterfaceNil() bool
}
// RequestersContainer defines a requesters holder data type with basic functionality
type RequestersContainer interface {
Get(key string) (Requester, error)
Add(key string, val Requester) error
AddMultiple(keys []string, requesters []Requester) error
Replace(key string, val Requester) error
Remove(key string)
Len() int
RequesterKeys() string
Iterate(handler func(key string, requester Requester) bool)
Close() error
IsInterfaceNil() bool
}
// RequestersContainerFactory defines the functionality to create a requesters container
type RequestersContainerFactory interface {
Create() (RequestersContainer, error)
IsInterfaceNil() bool
}
// EpochHandler defines the functionality to get the current epoch
type EpochHandler interface {
MetaEpoch() uint32
IsInterfaceNil() bool
}
// ManualEpochStartNotifier can manually notify an epoch change
type ManualEpochStartNotifier interface {
NewEpoch(epoch uint32)
CurrentEpoch() uint32
IsInterfaceNil() bool
}
// MessageHandler defines the functionality needed by structs to send data to other peers
type MessageHandler interface {
ConnectedPeersOnTopic(topic string) []core.PeerID
SendToConnectedPeer(topic string, buff []byte, peerID core.PeerID) error
ID() core.PeerID
ConnectedPeers() []core.PeerID
IsConnected(peerID core.PeerID) bool
IsInterfaceNil() bool
}
// TopicHandler defines the functionality needed by structs to manage topics and message processors
type TopicHandler interface {
HasTopic(name string) bool
CreateTopic(name string, createChannelForTopic bool) error
RegisterMessageProcessor(topic string, identifier string, handler p2p.MessageProcessor) error
}
// IntRandomizer interface provides functionality over generating integer numbers
type IntRandomizer interface {
Intn(n int) int
IsInterfaceNil() bool
}
// StorageType defines the storage levels on a node
type StorageType uint8
// PeerListCreator is used to create a peer list
type PeerListCreator interface {
CrossShardPeerList() []core.PeerID
IntraShardPeerList() []core.PeerID
IsInterfaceNil() bool
}
// ShardedDataCacherNotifier defines what a sharded-data structure can perform
type ShardedDataCacherNotifier interface {
RegisterOnAdded(func(key []byte, value interface{}))
ShardDataStore(cacheId string) (c storage.Cacher)
AddData(key []byte, data interface{}, sizeInBytes int, cacheId string)
SearchFirstData(key []byte) (value interface{}, ok bool)
RemoveData(key []byte, cacheId string)
RemoveSetOfDataFromPool(keys [][]byte, cacheId string)
ImmunizeSetOfDataAgainstEviction(keys [][]byte, cacheId string)
RemoveDataFromAllShards(key []byte)
MergeShardStores(sourceCacheID, destCacheID string)
Clear()
ClearShardStore(cacheId string)
GetCounts() counting.CountsWithSize
Keys() [][]byte
IsInterfaceNil() bool
}
// ShardIdHashMap represents a map for shardId and hash
type ShardIdHashMap interface {
Load(shardId uint32) ([]byte, bool)
Store(shardId uint32, hash []byte)
Range(f func(shardId uint32, hash []byte) bool)
Delete(shardId uint32)
IsInterfaceNil() bool
}
// HeadersPool defines what a headers pool structure can perform
type HeadersPool interface {
Clear()
AddHeader(headerHash []byte, header data.HeaderHandler)
RemoveHeaderByHash(headerHash []byte)
RemoveHeaderByNonceAndShardId(headerNonce uint64, shardId uint32)
GetHeadersByNonceAndShardId(headerNonce uint64, shardId uint32) ([]data.HeaderHandler, [][]byte, error)
GetHeaderByHash(hash []byte) (data.HeaderHandler, error)
RegisterHandler(handler func(headerHandler data.HeaderHandler, headerHash []byte))
Nonces(shardId uint32) []uint64
Len() int
MaxSize() int
IsInterfaceNil() bool
GetNumHeaders(shardId uint32) int
}
// TransactionCacher defines the methods for the local transaction cacher, needed for the current block
type TransactionCacher interface {
Clean()
GetTx(txHash []byte) (data.TransactionHandler, error)
AddTx(txHash []byte, tx data.TransactionHandler)
IsInterfaceNil() bool
}
// ValidatorInfoCacher defines the methods for the local validator info cacher, needed for the current epoch
type ValidatorInfoCacher interface {
Clean()
GetValidatorInfo(validatorInfoHash []byte) (*state.ShardValidatorInfo, error)
AddValidatorInfo(validatorInfoHash []byte, validatorInfo *state.ShardValidatorInfo)
IsInterfaceNil() bool
}
// PoolsHolder defines getters for data pools
type PoolsHolder interface {
Transactions() ShardedDataCacherNotifier
UnsignedTransactions() ShardedDataCacherNotifier
RewardTransactions() ShardedDataCacherNotifier
Headers() HeadersPool
MiniBlocks() storage.Cacher
PeerChangesBlocks() storage.Cacher
TrieNodes() storage.Cacher
TrieNodesChunks() storage.Cacher
SmartContracts() storage.Cacher
CurrentBlockTxs() TransactionCacher
CurrentEpochValidatorInfo() ValidatorInfoCacher
PeerAuthentications() storage.Cacher
Heartbeats() storage.Cacher
ValidatorsInfo() ShardedDataCacherNotifier
Close() error
IsInterfaceNil() bool
}
// StorageService is the interface for data storage unit provided services
type StorageService interface {
// GetStorer returns the storer from the chain map
// If the unit is missing, it returns an error
GetStorer(unitType UnitType) (storage.Storer, error)
// AddStorer will add a new storer to the chain map
AddStorer(key UnitType, s storage.Storer)
// Has returns true if the key is found in the selected Unit or false otherwise
Has(unitType UnitType, key []byte) error
// Get returns the value for the given key if found in the selected storage unit, nil otherwise
Get(unitType UnitType, key []byte) ([]byte, error)
// Put stores the key, value pair in the selected storage unit
Put(unitType UnitType, key []byte, value []byte) error
// SetEpochForPutOperation will set the epoch which will be used for the put operation
SetEpochForPutOperation(epoch uint32)
// GetAll gets all the elements with keys in the keys array, from the selected storage unit
// If there is a missing key in the unit, it returns an error
GetAll(unitType UnitType, keys [][]byte) (map[string][]byte, error)
// GetAllStorers returns all the storers
GetAllStorers() map[UnitType]storage.Storer
// Destroy removes the underlying files/resources used by the storage service
Destroy() error
// CloseAll will close all the units
CloseAll() error
// IsInterfaceNil returns true if there is no value under the interface
IsInterfaceNil() bool
}
// DataPacker can split a large slice of byte slices in smaller packets
type DataPacker interface {
PackDataInChunks(data [][]byte, limit int) ([][]byte, error)
IsInterfaceNil() bool
}
// TrieDataGetter returns requested data from the trie
type TrieDataGetter interface {
GetSerializedNodes([]byte, uint64) ([][]byte, uint64, error)
GetSerializedNode([]byte) ([]byte, error)
IsInterfaceNil() bool
}
// RequestedItemsHandler can determine if a certain key has or not been requested
type RequestedItemsHandler interface {
Add(key string) error
Has(key string) bool
Sweep()
IsInterfaceNil() bool
}
// P2PAntifloodHandler defines the behavior of a component able to signal that the system is too busy (or flooded) processing
// p2p messages
type P2PAntifloodHandler interface {
CanProcessMessage(message p2p.MessageP2P, fromConnectedPeer core.PeerID) error
CanProcessMessagesOnTopic(peer core.PeerID, topic string, numMessages uint32, _ uint64, sequence []byte) error
BlacklistPeer(peer core.PeerID, reason string, duration time.Duration)
IsInterfaceNil() bool
}
// WhiteListHandler is the interface needed to add whitelisted data
type WhiteListHandler interface {
Remove(keys [][]byte)
Add(keys [][]byte)
IsInterfaceNil() bool
}
// DebugHandler defines an interface for debugging the requested-resolved data
type DebugHandler interface {
LogRequestedData(topic string, hashes [][]byte, numReqIntra int, numReqCross int)
LogFailedToResolveData(topic string, hash []byte, err error)
LogSucceededToResolveData(topic string, hash []byte)
IsInterfaceNil() bool
}
// CurrentNetworkEpochProviderHandler is an interface able to compute if the provided epoch is active on the network or not
type CurrentNetworkEpochProviderHandler interface {
EpochIsActiveInNetwork(epoch uint32) bool
EpochConfirmed(newEpoch uint32, newTimestamp uint64)
IsInterfaceNil() bool
}
// PreferredPeersHolderHandler defines the behavior of a component able to handle preferred peers operations
type PreferredPeersHolderHandler interface {
Get() map[uint32][]core.PeerID
Contains(peerID core.PeerID) bool
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
}
// SelfShardIDProvider defines the behavior of a component able to provide the self shard ID
type SelfShardIDProvider interface {
SelfId() uint32
IsInterfaceNil() bool
}
// NodesCoordinator provides Validator methods needed for the peer processing
type NodesCoordinator interface {
GetAllEligibleValidatorsPublicKeys(epoch uint32) (map[uint32][][]byte, error)
IsInterfaceNil() bool
}
// PeerAuthenticationPayloadValidator defines the operations supported by an entity able to validate timestamps
// found in peer authentication messages
type PeerAuthenticationPayloadValidator interface {
ValidateTimestamp(payloadTimestamp int64) error
IsInterfaceNil() bool
}