-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpeer.go
More file actions
347 lines (301 loc) · 8.62 KB
/
Copy pathpeer.go
File metadata and controls
347 lines (301 loc) · 8.62 KB
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
// Copyright 2020 The CortexTheseus Authors
// This file is part of the CortexTheseus library.
//
// The CortexTheseus library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The CortexTheseus library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the CortexTheseus library. If not, see <http://www.gnu.org/licenses/>.
package torrentfs
import (
"fmt"
"reflect"
"sync"
"time"
"github.com/CortexFoundation/CortexTheseus/common"
"github.com/CortexFoundation/CortexTheseus/log"
"github.com/CortexFoundation/CortexTheseus/p2p"
"github.com/CortexFoundation/CortexTheseus/rlp"
mapset "github.com/deckarep/golang-set/v2"
"github.com/CortexFoundation/torrentfs/params"
)
type Peer struct {
id string
host *TorrentFS
peer *p2p.Peer
ws p2p.MsgReadWriter
trusted bool
known mapset.Set[string]
quit chan any
wg sync.WaitGroup
version uint64
peerInfo *PeerInfo
msgChan chan any
seeding mapset.Set[string]
once sync.Once
}
type PeerInfo struct {
Listen uint64 `json:"listen"`
Root common.Hash `json:"root"` // SHA3 hash of the peer's best owned block
Files uint64 `json:"files"`
Leafs uint64 `json:"leafs"`
}
type MsgInfo struct {
Desc string `json:"desc"`
}
func newPeer(id string, host *TorrentFS, remote *p2p.Peer, rw p2p.MsgReadWriter) *Peer {
const (
msgChanBufSize = 1
defaultSetSize = 25
)
return &Peer{
id: id,
host: host,
peer: remote,
ws: rw,
known: mapset.NewSetWithSize[string](defaultSetSize),
quit: make(chan any),
msgChan: make(chan any, msgChanBufSize),
seeding: mapset.NewSetWithSize[string](defaultSetSize),
}
}
func (peer *Peer) Info() *PeerInfo {
return peer.peerInfo
}
func (peer *Peer) start() error {
peer.wg.Add(1)
go func() {
defer peer.wg.Done()
peer.update()
}()
/*peer.wg.Add(1)
go func() {
defer peer.wg.Done()
peer.calling()
}()*/
return nil
}
func (peer *Peer) expire() {
// Use a slice to store keys to be removed, which is safer if the map's Each method
// doesn't guarantee safe concurrent modification.
var toRemove []string
// Iterate over all known keys.
peer.known.Each(func(key string) bool {
if _, err := peer.host.Envelopes().Get(key); err != nil {
toRemove = append(toRemove, key)
}
return true // Continue the iteration.
})
// Remove all keys collected in the first step.
for _, key := range toRemove {
peer.known.Remove(key)
}
}
func (peer *Peer) update() {
// Defer statements to ensure resources are cleaned up on function exit.
stateTicker := time.NewTicker(params.PeerStateCycle)
defer stateTicker.Stop()
transmitTicker := time.NewTicker(params.TransmissionCycle)
defer transmitTicker.Stop()
expireTicker := time.NewTicker(params.ExpirationCycle)
defer expireTicker.Stop()
// The main event loop for peer operations.
for {
select {
case <-expireTicker.C:
peer.expire()
case <-transmitTicker.C:
// Check for neighbors before attempting to broadcast to avoid unnecessary logs.
if peer.host.Neighbors() == 0 {
log.Warn("No neighbors found, skipping transmission", "peer", peer.ID())
continue
}
if err := peer.broadcast(); err != nil {
// Use Trace for expected, non-critical failures.
log.Trace("Transmit broadcast failed", "reason", err, "peer", peer.ID())
// Return here as the failure might be critical.
return
}
case <-stateTicker.C:
if err := peer.state(); err != nil {
log.Trace("State broadcast failed", "reason", err, "peer", peer.ID())
return
}
case <-peer.quit:
log.Debug("Peer update loop terminated", "peer", peer.ID())
return
}
}
}
func (peer *Peer) state() error {
state := PeerInfo{
Listen: uint64(peer.host.LocalPort()),
Root: peer.host.monitor.DB().Root(),
Files: uint64(peer.host.Congress()),
Leafs: uint64(len(peer.host.monitor.DB().Blocks())),
}
if err := p2p.Send(peer.ws, params.StatusCode, &state); err != nil {
return err
}
return nil
}
type Query struct {
Hash string `json:"hash"`
Size uint64 `json:"size"`
}
func (peer *Peer) seen(hash string) {
if !peer.seeding.Contains(hash) {
peer.seeding.Add(hash)
}
}
func (peer *Peer) mark(hash string) {
peer.known.Add(hash)
}
func (peer *Peer) marked(hash string) bool {
return peer.known.Contains(hash)
}
func (peer *Peer) broadcast() error {
keys := peer.host.Envelopes().Keys()
for _, k := range keys {
// Ensure the key is a string and handle potential type assertion failures.
keyStr, ok := k.Interface().(string)
if !ok {
// Log a warning if the key is not the expected type.
log.Warn("Envelopes key is not a string, skipping", "keyType", reflect.TypeOf(k.Interface()).String())
continue
}
// Check if the peer has already processed this key.
if peer.marked(keyStr) {
continue
}
// Get the value and handle potential errors.
v, err := peer.host.Envelopes().Get(keyStr)
if err != nil {
log.Warn("Failed to get envelope value, skipping", "key", keyStr, "err", err)
continue
}
// Construct the query object.
query := Query{
Hash: keyStr,
Size: v.Value().(uint64), // Assuming the value's type assertion is safe.
}
// Send the query and handle any transmission errors.
if err := p2p.Send(peer.ws, params.QueryCode, &query); err != nil {
// Return immediately on a send failure.
return err
}
// Update metrics and mark the key as sent.
peer.host.sent.Add(1)
peer.mark(keyStr)
}
return nil
}
func (peer *Peer) call(msg any) {
peer.msgChan <- msg
}
func (peer *Peer) calling() {
for {
select {
case msg := <-peer.msgChan:
if err := p2p.Send(peer.ws, params.MsgCode, &msg); err != nil {
log.Warn("Msg sending failed", "msg", msg, "id", peer.id, "err", err)
return
}
log.Info("Msg sending", "msg", msg, "id", peer.id)
case <-peer.quit:
return
}
}
}
func (peer *Peer) handshake() error {
log.Debug("Nas handshake", "peer", peer.ID())
errc := make(chan error, 2)
peer.wg.Add(1)
go func() {
defer peer.wg.Done()
log.Debug("Nas send items", "status", params.StatusCode, "version", params.ProtocolVersion)
info := PeerInfo{
Listen: uint64(peer.host.LocalPort()),
Root: peer.host.monitor.DB().Root(),
Files: uint64(peer.host.Congress()),
Leafs: uint64(len(peer.host.monitor.DB().Blocks())),
}
select {
case errc <- p2p.SendItems(peer.ws, params.StatusCode, params.ProtocolVersion, &info):
case <-peer.quit:
}
log.Debug("Nas send items OK", "status", params.StatusCode, "version", params.ProtocolVersion, "len", len(errc))
}()
// Fetch the remote status packet and verify protocol match
peer.wg.Add(1)
go func() {
defer peer.wg.Done()
select {
case errc <- peer.readStatus():
case <-peer.quit:
}
}()
timeout := time.NewTimer(params.HandshakeTimeout)
defer timeout.Stop()
for i := 0; i < 2; i++ {
select {
case err := <-errc:
if err != nil {
return fmt.Errorf("peer [%x] failed to send status packet: %v", peer.ID(), err)
}
case <-timeout.C:
log.Info("Handshake timeout")
return fmt.Errorf("peer [%x] timeout", peer.ID())
case <-peer.quit:
return nil
}
}
log.Debug("Nas p2p hanshake success", "id", peer.ID())
return nil
}
func (peer *Peer) readStatus() error {
packet, err := peer.ws.ReadMsg()
if err != nil {
return err
}
defer packet.Discard()
if packet.Code != params.StatusCode {
return fmt.Errorf("peer [%x] sent packet %x before status packet", peer.ID(), packet.Code)
}
s := rlp.NewStream(packet.Payload, uint64(packet.Size))
_, err = s.List()
if err != nil {
return fmt.Errorf("peer [%x] sent bad status message: %v", peer.ID(), err)
}
peerVersion, err := s.Uint()
if err != nil {
return fmt.Errorf("peer [%x] sent bad status message (unable to decode version): %v", peer.ID(), err)
}
if peerVersion != params.ProtocolVersion {
return fmt.Errorf("peer [%x]: protocol version mismatch %d != %d", peer.ID(), peerVersion, params.ProtocolVersion)
}
err = s.Decode(&peer.peerInfo)
if err != nil {
return fmt.Errorf("peer [%x] failed to send peer info packet: %v", peer.ID(), err)
}
peer.version = peerVersion
return nil
}
func (peer *Peer) stop() error {
peer.once.Do(func() {
close(peer.quit)
peer.wg.Wait()
})
return nil
}
func (peer *Peer) ID() string {
id := peer.peer.ID()
return id.String()
}