-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributor.go
301 lines (271 loc) · 9 KB
/
distributor.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
package main
import (
"context"
"errors"
"fmt"
"math/big"
"net"
"net/netip"
"time"
"github.com/rhansen/go-kairos/kairos"
"github.com/wavesplatform/gowaves/pkg/crypto"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"github.com/wavesplatform/gowaves/pkg/p2p/peer"
"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/alexeykiselev/waves-fork-detector/chains"
"github.com/alexeykiselev/waves-fork-detector/loading"
"github.com/alexeykiselev/waves-fork-detector/peers"
)
const pingInterval = 1 * time.Minute
type Distributor struct {
ctx context.Context
wait func() error
scheme proto.Scheme
registry *peers.Registry
linkage *chains.Linkage
parent peer.Parent
idsCh chan loading.IDsPackage
blockCh chan loading.BlockPackage
timer *kairos.Timer
}
func NewDistributor(
scheme proto.Scheme, linkage *chains.Linkage, registry *peers.Registry, parent peer.Parent,
) *Distributor {
idsCh := make(chan loading.IDsPackage)
blockCh := make(chan loading.BlockPackage)
return &Distributor{
scheme: scheme,
linkage: linkage,
registry: registry,
parent: parent,
idsCh: idsCh,
blockCh: blockCh,
timer: kairos.NewStoppedTimer(),
}
}
func (d *Distributor) Run(ctx context.Context) {
g, gc := errgroup.WithContext(ctx)
d.ctx = gc
d.wait = g.Wait
g.Go(d.runLoop)
d.timer.Reset(pingInterval)
}
func (d *Distributor) Shutdown() {
if err := d.wait(); err != nil {
zap.S().Warnf("Failed to shutdown Distributor: %v", err)
}
close(d.idsCh)
close(d.blockCh)
zap.S().Info("Distributor shutdown successfully")
}
func (d *Distributor) IDsCh() <-chan loading.IDsPackage {
return d.idsCh
}
func (d *Distributor) BlockCh() <-chan loading.BlockPackage {
return d.blockCh
}
func (d *Distributor) runLoop() error {
for {
select {
case <-d.ctx.Done():
zap.S().Debugf("[DTR] Distributor shutdown in progress...")
return nil
case <-d.timer.C:
zap.S().Info("Pinging connections")
d.pingConnections()
d.timer.Reset(pingInterval)
case infoMessage := <-d.parent.InfoCh:
d.handleInfoMessage(infoMessage)
case message := <-d.parent.MessageCh:
d.handleMessage(message)
}
}
}
func (d *Distributor) pingConnections() {
d.registry.Broadcast(&proto.GetPeersMessage{})
}
func (d *Distributor) handleInfoMessage(msg peer.InfoMessage) {
switch v := msg.Value.(type) {
case *peer.Connected:
d.handleConnected(v)
case *peer.InternalErr:
d.handleInternalError(msg.Peer, v)
}
}
func (d *Distributor) handleConnected(cm *peer.Connected) {
ap, err := netip.ParseAddrPort(cm.Peer.RemoteAddr().String())
if err != nil {
zap.S().Warnf("Failed to parse address: %v", err)
return
}
if rpErr := d.registry.RegisterPeer(ap.Addr(), cm.Peer, cm.Peer.Handshake()); rpErr != nil {
zap.S().Warnf("Failed to check peer: %v", rpErr)
return
}
}
func (d *Distributor) handleInternalError(peer peer.Peer, ie *peer.InternalErr) {
ap, err := netip.ParseAddrPort(peer.RemoteAddr().String())
if err != nil {
zap.S().Warnf("Failed to parse address: %v", err)
return
}
zap.S().Infof("[DTR] Closing connection with peer %s", ap.Addr().String())
zap.S().Debugf("[DTR] Peer %s failed with error: %v", ap.Addr().String(), ie.Err)
if clErr := peer.Close(); clErr != nil {
zap.S().Warnf("Failed to close peer '%s' connection: %v", ap.Addr().String(), clErr)
}
if urErr := d.registry.UnregisterPeer(ap.Addr()); urErr != nil {
zap.S().Warnf("Failed to unregister peer '%s': %v", ap.Addr().String(), urErr)
}
}
func (d *Distributor) handleMessage(msg peer.ProtoMessage) {
switch v := msg.Message.(type) {
case *proto.GetPeersMessage:
d.handleGetPeersMessage(msg.ID)
case *proto.PeersMessage:
d.handlePeersMessage(v)
case *proto.SignaturesMessage:
d.handleSignaturesMessage(msg.ID, v.Signatures)
case *proto.BlockMessage:
d.handleBlockMessage(msg.ID, v)
case *proto.ScoreMessage:
d.handleScoreMessage(msg.ID, v.Score)
case *proto.MicroBlockInvMessage:
d.handleMicroBlockInvMessage(msg.ID, v)
case *proto.PBBlockMessage:
d.handleProtoBlockMessage(msg.ID, v)
case *proto.BlockIdsMessage:
d.handleBlockIDsMessage(msg.ID, v.Blocks)
}
}
func (d *Distributor) handleScoreMessage(peer peer.Peer, score []byte) {
ap, err := netip.ParseAddrPort(peer.RemoteAddr().String())
if err != nil {
zap.S().Debugf("[DTR] Failed to parse peer address: %v", err)
return
}
s := big.NewInt(0).SetBytes(score)
zap.S().Debugf("[DTR] New score %s recevied from %s", s.String(), ap.Addr().String())
err = d.registry.UpdatePeerScore(ap.Addr(), s)
if err != nil {
zap.S().Debugf("[DTR] Failed to update score of peer '%s': %v", ap.Addr().String(), err)
return
}
}
func (d *Distributor) handleBlockMessage(peer peer.Peer, bm *proto.BlockMessage) {
b := &proto.Block{}
if err := b.UnmarshalBinary(bm.BlockBytes, d.scheme); err != nil {
zap.S().Warnf("Failed to unmarshal block from peer '%s': %v", peer.RemoteAddr().String(), err)
return
}
zap.S().Infof("Block '%s' received from peer '%s'", b.BlockID().String(), peer.RemoteAddr().String())
if err := d.handleBlock(b, peer.RemoteAddr()); err != nil {
zap.S().Warnf("Failed to handle block from peer '%s': %v", peer.RemoteAddr().String(), err)
}
}
func (d *Distributor) handleProtoBlockMessage(peer peer.Peer, bm *proto.PBBlockMessage) {
b := &proto.Block{}
if err := b.UnmarshalFromProtobuf(bm.PBBlockBytes); err != nil {
zap.S().Warnf("Failed to unmarshal protobuf block from peer '%s': %v",
peer.RemoteAddr().String(), err)
return
}
zap.S().Infof("Block '%s' received from peer '%s'", b.BlockID().String(), peer.RemoteAddr().String())
if err := d.handleBlock(b, peer.RemoteAddr()); err != nil {
zap.S().Warnf("Failed to handle block from peer '%s': %v", peer.RemoteAddr().String(), err)
}
}
func (d *Distributor) handleBlock(block *proto.Block, addr proto.TCPAddr) error {
ap, err := netip.ParseAddrPort(addr.String())
if err != nil {
return fmt.Errorf("failed to parse peer remote address: %w", err)
}
if putErr := d.linkage.PutBlock(block, ap.Addr()); putErr != nil && !errors.Is(putErr, chains.ErrParentNotFound) {
return fmt.Errorf("failed to append block: %w", err)
}
zap.S().Debugf("[DTR] Block '%s' from '%s' was appended", block.BlockID().String(), ap.Addr().String())
d.blockCh <- loading.BlockPackage{Peer: ap.Addr(), Block: block}
return nil
}
func (d *Distributor) handlePeersMessage(pm *proto.PeersMessage) {
zap.S().Debugf("[DTR] Received %d peers", len(pm.Peers))
addresses := make([]*net.TCPAddr, 0, len(pm.Peers))
for _, pi := range pm.Peers {
ap, err := netip.ParseAddrPort(pi.String())
if err != nil {
zap.S().Debugf("[DTR] Failed to parse peer address: %v", err)
continue
}
addresses = append(addresses, net.TCPAddrFromAddrPort(ap))
}
cnt := d.registry.AppendAddresses(addresses)
if cnt > 0 {
zap.S().Infof("[DTR] Added %d new peers", cnt)
}
}
func (d *Distributor) handleGetPeersMessage(peer peer.Peer) {
zap.S().Debugf("[DTR] Get peers from %s", peer.RemoteAddr().String())
friendlyPeers, err := d.registry.FriendlyPeers()
if err != nil {
zap.S().Warnf("Failed to get peers: %v", err)
return
}
infos := make([]proto.PeerInfo, 0, len(friendlyPeers))
for _, p := range friendlyPeers {
pi := proto.PeerInfo{
Addr: p.TCPAddr().IP,
Port: p.AddressPort.Port(),
}
infos = append(infos, pi)
}
peersMessage := &proto.PeersMessage{
Peers: infos,
}
peer.SendMessage(peersMessage)
}
func (d *Distributor) handleSignaturesMessage(peer peer.Peer, signatures []crypto.Signature) {
ap, err := netip.ParseAddrPort(peer.RemoteAddr().String())
if err != nil {
zap.S().Warnf("Failed to parse peer address: %v", err)
return
}
ids := make([]proto.BlockID, len(signatures))
for i, s := range signatures {
ids[i] = proto.NewBlockIDFromSignature(s)
}
zap.S().Debugf("[DTR] Signatures received from '%s'", ap.Addr().String())
d.idsCh <- loading.IDsPackage{Peer: ap.Addr(), IDs: ids}
}
func (d *Distributor) handleBlockIDsMessage(peer peer.Peer, ids []proto.BlockID) {
ap, err := netip.ParseAddrPort(peer.RemoteAddr().String())
if err != nil {
zap.S().Warnf("Failed to parse peer address: %v", err)
return
}
if len(ids) == 0 {
zap.S().Warnf("Empty IDs list received from '%s'", ap.Addr().String())
return
}
zap.S().Debugf("[DTR] Block IDs [%s..%s] received from %s",
ids[0].ShortString(), ids[len(ids)-1].ShortString(), ap.Addr().String())
d.idsCh <- loading.IDsPackage{Peer: ap.Addr(), IDs: ids}
}
func (d *Distributor) handleMicroBlockInvMessage(peer peer.Peer, msg *proto.MicroBlockInvMessage) {
ap, err := netip.ParseAddrPort(peer.RemoteAddr().String())
if err != nil {
zap.S().Warnf("Failed to parse peer address: %v", err)
return
}
inv := &proto.MicroBlockInv{}
if umErr := inv.UnmarshalBinary(msg.Body); umErr != nil {
zap.S().Warnf("Failed to unmarshal MicroBlockInv message: %v", umErr)
return
}
if putErr := d.linkage.PutMicroBlock(inv, ap.Addr()); putErr != nil {
zap.S().Warnf("Failed to append micro-block '%s' received form '%s': %v",
inv.TotalBlockID.String(), ap.Addr().String(), putErr)
return
}
zap.S().Infof("Micro-block '%s' received from peer '%s'", inv.TotalBlockID.String(), ap.Addr().String())
}