-
Notifications
You must be signed in to change notification settings - Fork 33
/
router.go
357 lines (301 loc) · 10.6 KB
/
router.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
package raintree
import (
"fmt"
libp2pHost "github.com/libp2p/go-libp2p/core/host"
"google.golang.org/protobuf/proto"
"github.com/pokt-network/pocket/logger"
"github.com/pokt-network/pocket/p2p/config"
"github.com/pokt-network/pocket/p2p/protocol"
typesP2P "github.com/pokt-network/pocket/p2p/types"
"github.com/pokt-network/pocket/p2p/unicast"
"github.com/pokt-network/pocket/p2p/utils"
"github.com/pokt-network/pocket/shared/codec"
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
"github.com/pokt-network/pocket/shared/messaging"
"github.com/pokt-network/pocket/shared/modules"
"github.com/pokt-network/pocket/shared/modules/base_modules"
"github.com/pokt-network/pocket/telemetry"
)
var (
_ typesP2P.Router = &rainTreeRouter{}
_ modules.IntegrableModule = &rainTreeRouter{}
_ rainTreeFactory = &rainTreeRouter{}
)
type rainTreeFactory = modules.FactoryWithConfig[typesP2P.Router, *config.RainTreeConfig]
type rainTreeRouter struct {
base_modules.IntegrableModule
unicast.UnicastRouter
logger *modules.Logger
// handler is the function to call when a message is received.
handler typesP2P.MessageHandler
// host represents a libp2p libp2pNetwork node, it encapsulates a libp2p peerstore
// & connection manager. `libp2p.New` configures and starts listening
// according to options.
// (see: https://pkg.go.dev/github.com/libp2p/go-libp2p#section-readme)
host libp2pHost.Host
// selfAddr is the pocket address representing this host.
selfAddr cryptoPocket.Address
peersManager *rainTreePeersManager
}
func Create(bus modules.Bus, cfg *config.RainTreeConfig) (typesP2P.Router, error) {
return new(rainTreeRouter).Create(bus, cfg)
}
func (*rainTreeRouter) Create(
bus modules.Bus,
cfg *config.RainTreeConfig,
) (typesP2P.Router, error) {
rainTreeLogger := logger.Global.CreateLoggerForModule("rainTreeRouter")
if err := cfg.IsValid(); err != nil {
return nil, err
}
rtr := &rainTreeRouter{
host: cfg.Host,
selfAddr: cfg.Addr,
logger: rainTreeLogger,
handler: cfg.Handler,
}
bus.RegisterModule(rtr)
currentHeightProvider := bus.GetCurrentHeightProvider()
pstoreProvider, err := rtr.getPeerstoreProvider()
if err != nil {
return nil, err
}
height := currentHeightProvider.CurrentHeight()
pstore, err := pstoreProvider.GetStakedPeerstoreAtHeight(height)
if err != nil {
return nil, fmt.Errorf("getting staked peerstore at height %d: %w", height, err)
}
rainTreeLogger.Info().Fields(map[string]any{
"address": cfg.Addr,
"host_id": cfg.Host.ID(),
"protocol_id": protocol.BackgroundProtocolID,
"current_height": height,
"peerstore_size": pstore.Size(),
}).Msg("initializing raintree router")
if err := rtr.setupDependencies(pstore); err != nil {
return nil, err
}
return typesP2P.Router(rtr), nil
}
func (rtr *rainTreeRouter) Close() error {
return nil
}
func (rtr *rainTreeRouter) GetModuleName() string {
return typesP2P.StakedActorRouterSubmoduleName
}
// NetworkBroadcast implements the respective member of `typesP2P.Router`.
func (rtr *rainTreeRouter) Broadcast(data []byte) error {
return rtr.broadcastAtLevel(data, rtr.peersManager.GetMaxNumLevels())
}
// broadcastAtLevel recursively sends to both left and right target peers
// from the starting level, demoting until level == 0.
// (see: https://github.com/pokt-network/pocket-network-protocol/tree/main/p2p)
func (rtr *rainTreeRouter) broadcastAtLevel(data []byte, level uint32) error {
// This is handled either by the cleanup layer or redundancy layer
if level == 0 {
return nil
}
msg := &typesP2P.RainTreeMessage{
Level: level,
Data: data,
}
msgBz, err := codec.GetCodec().Marshal(msg)
if err != nil {
return err
}
// TECHDEBT(#810, #811): remove once `bus.GetPeerstoreProvider()` is available.
// Pre-handling the error from `rtr.getPeerstoreProvider()` before it is called
// downstream in a context without an error return value.
if _, err = rtr.getPeerstoreProvider(); err != nil {
return err
}
for _, target := range rtr.getTargetsAtLevel(level) {
if shouldSendToTarget(target) {
if err = rtr.sendInternal(msgBz, target.address); err != nil {
rtr.logger.Error().Err(err).Msg("sending to peer during broadcast")
}
}
}
if err = rtr.demote(msg); err != nil {
rtr.logger.Error().Err(err).Msg("demoting self during RainTree message propagation")
}
return nil
}
// demote broadcasts to the decremented level's targets.
// (see: https://github.com/pokt-network/pocket-network-protocol/tree/main/p2p)
func (rtr *rainTreeRouter) demote(rainTreeMsg *typesP2P.RainTreeMessage) error {
if rainTreeMsg.Level > 0 {
if err := rtr.broadcastAtLevel(rainTreeMsg.Data, rainTreeMsg.Level-1); err != nil {
return err
}
}
return nil
}
// NetworkSend implements the respective member of `typesP2P.Router`.
func (rtr *rainTreeRouter) Send(data []byte, address cryptoPocket.Address) error {
msg := &typesP2P.RainTreeMessage{
Level: 0, // Direct send that does not need to be propagated
Data: data,
}
bz, err := codec.GetCodec().Marshal(msg)
if err != nil {
return err
}
return rtr.sendInternal(bz, address)
}
// sendInternal sends `data` to the peer at pokt `address` if not self.
func (rtr *rainTreeRouter) sendInternal(data []byte, address cryptoPocket.Address) error {
// TODO: How should we handle this?
if rtr.selfAddr.Equals(address) {
rtr.logger.Debug().Str("pokt_addr", address.String()).Msg("attempted to send to self")
return nil
}
peer := rtr.peersManager.GetPeerstore().GetPeer(address)
if peer == nil {
return fmt.Errorf("%w: with pokt address %s", typesP2P.ErrUnknownPeer, address)
}
// debug logging
hostname := rtr.getHostname()
utils.LogOutgoingMsg(rtr.logger, hostname, peer)
if err := utils.Libp2pSendToPeer(rtr.host, protocol.RaintreeProtocolID, data, peer); err != nil {
return err
}
// A bus is not available In client debug mode
bus := rtr.GetBus()
if bus == nil {
return nil
}
bus.
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
telemetry.P2P_EVENT_METRICS_NAMESPACE,
telemetry.P2P_RAINTREE_MESSAGE_EVENT_METRIC_NAME,
telemetry.P2P_RAINTREE_MESSAGE_EVENT_METRIC_SEND_LABEL, "send",
)
return nil
}
// handleRainTreeMsg deserializes a RainTree message to extract the `PocketEnvelope`
// bytes contained within, continues broadcast propagation, if applicable, and
// passes them off to the application by calling the configured `rtr.handler`.
// Intended to be called in a go routine.
func (rtr *rainTreeRouter) handleRainTreeMsg(rainTreeMsgBz []byte) error {
blockHeightInt := rtr.GetBus().GetConsensusModule().CurrentHeight()
blockHeight := fmt.Sprintf("%d", blockHeightInt)
rtr.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
telemetry.P2P_EVENT_METRICS_NAMESPACE,
telemetry.P2P_RAINTREE_MESSAGE_EVENT_METRIC_NAME,
telemetry.P2P_RAINTREE_MESSAGE_EVENT_METRIC_HEIGHT_LABEL, blockHeight,
)
var rainTreeMsg typesP2P.RainTreeMessage
if err := proto.Unmarshal(rainTreeMsgBz, &rainTreeMsg); err != nil {
// TECHDEBT: add telemetry
return err
}
if err := rtr.validateRainTreeMsg(&rainTreeMsg); err != nil {
// TECHDEBT: add telemetry
return fmt.Errorf("validating raintree message: %w", err)
}
// Continue RainTree propagation
if rainTreeMsg.Level > 0 {
if err := rtr.broadcastAtLevel(rainTreeMsg.Data, rainTreeMsg.Level-1); err != nil {
return err
}
}
// There was no error, but we don't need to forward this to the app-specific bus.
// For example, the message has already been handled by the application.
if rainTreeMsg.Data == nil {
rtr.logger.Debug().Msg("no data in RainTree message")
return nil
}
// Call configured message handler with the serialized `PocketEnvelope`.
if err := rtr.handler(rainTreeMsg.Data); err != nil {
return fmt.Errorf("handling raintree message: %w", err)
}
return nil
}
// validateRainTreeMsg ensures that the `data` contained within the RainTree message
// is a valid `PocketEnvelope` by attempting to deserialize it.
func (rtr *rainTreeRouter) validateRainTreeMsg(rainTreeMsg *typesP2P.RainTreeMessage) error {
networkMessage := messaging.PocketEnvelope{}
return proto.Unmarshal(rainTreeMsg.Data, &networkMessage)
}
// GetPeerstore implements the respective member of `typesP2P.Router`.
func (rtr *rainTreeRouter) GetPeerstore() typesP2P.Peerstore {
return rtr.peersManager.GetPeerstore()
}
// AddPeer implements the respective member of `typesP2P.Router`.
func (rtr *rainTreeRouter) AddPeer(peer typesP2P.Peer) error {
// Noop if peer with the same pokt address exists in the peerstore.
// TECHDEBT: add method(s) to update peers.
if p := rtr.peersManager.GetPeerstore().GetPeer(peer.GetAddress()); p != nil {
return nil
}
if err := utils.AddPeerToLibp2pHost(rtr.host, peer); err != nil {
return err
}
rtr.peersManager.HandleEvent(
typesP2P.PeerManagerEvent{
EventType: typesP2P.AddPeerEventType,
Peer: peer,
},
)
return nil
}
// RemovePeer implements the respective member of `typesP2P.Router`.
func (rtr *rainTreeRouter) RemovePeer(peer typesP2P.Peer) error {
rtr.peersManager.HandleEvent(
typesP2P.PeerManagerEvent{
EventType: typesP2P.RemovePeerEventType,
Peer: peer,
},
)
return nil
}
// Size returns the number of peers the network is aware of and would attempt to
// broadcast to.
func (rtr *rainTreeRouter) Size() int {
return rtr.peersManager.GetPeerstore().Size()
}
// shouldSendToTarget returns false if target is self.
func shouldSendToTarget(target target) bool {
return !target.isSelf
}
// setupUnicastRouter configures and assigns `rtr.UnicastRouter`.
func (rtr *rainTreeRouter) setupUnicastRouter() error {
unicastRouterCfg := config.UnicastRouterConfig{
Logger: rtr.logger,
Host: rtr.host,
ProtocolID: protocol.RaintreeProtocolID,
MessageHandler: rtr.handleRainTreeMsg,
PeerHandler: rtr.AddPeer,
}
unicastRouter, err := unicast.Create(rtr.GetBus(), &unicastRouterCfg)
if err != nil {
return fmt.Errorf("setting up unicast router: %w", err)
}
rtr.UnicastRouter = *unicastRouter
return nil
}
func (rtr *rainTreeRouter) setupDependencies(pstore typesP2P.Peerstore) error {
if err := rtr.setupUnicastRouter(); err != nil {
return err
}
if err := rtr.setupPeerManager(pstore); err != nil {
return fmt.Errorf("setting up peer manager: %w", err)
}
if err := utils.PopulateLibp2pHost(rtr.host, pstore); err != nil {
return err
}
return nil
}
func (rtr *rainTreeRouter) setupPeerManager(pstore typesP2P.Peerstore) (err error) {
rtr.peersManager, err = newPeersManager(rtr.selfAddr, pstore, true)
return err
}
func (rtr *rainTreeRouter) getHostname() string {
return rtr.GetBus().GetRuntimeMgr().GetConfig().P2P.Hostname
}