This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
gossip.go
512 lines (459 loc) · 12.2 KB
/
gossip.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Copyright (C) 2013-2018, The MetaCurrency Project (Eric Harris-Braun, Arthur Brock, et. al.)
// Use of this source code is governed by GPLv3 found in the LICENSE file
//----------------------------------------------------------------------------------------
// gossip implements the gossip protocol for the distributed hash table
package holochain
import (
"errors"
"fmt"
. "github.com/holochain/holochain-proto/hash"
peer "github.com/libp2p/go-libp2p-peer"
"github.com/tidwall/buntdb"
"math/rand"
"sort"
"strconv"
"strings"
"time"
)
// Put holds a put or link for gossiping
type Put struct {
Idx int
M Message
}
// Gossip holds a gossip message
type Gossip struct {
Puts []Put
}
// GossipReq holds a gossip request
type GossipReq struct {
MyIdx int
YourIdx int
}
// we also gossip about peers too, keeping lists of different peers e.g. blockedlist etc
type PeerListType string
const (
BlockedList = "blockedlist"
)
type PeerRecord struct {
ID peer.ID
Warrant string // evidence, reasons, documentation of why peer is in this list
}
type PeerList struct {
Type PeerListType
Records []PeerRecord
}
var ErrDHTErrNoGossipersAvailable error = errors.New("no gossipers available")
var ErrDHTExpectedGossipReqInBody error = errors.New("expected gossip request")
var ErrNoSuchIdx error = errors.New("no such change index")
//HaveFingerprint returns true if we have seen the given fingerprint
func (dht *DHT) HaveFingerprint(f Hash) (result bool, err error) {
index, err := dht.GetFingerprint(f)
if err == nil {
result = index >= 0
}
return
}
// GetFingerprint returns the index that of the message that made a change or -1 if we don't have it
func (dht *DHT) GetFingerprint(f Hash) (index int, err error) {
index = -1
db := dht.ht.(*BuntHT).db
err = db.View(func(tx *buntdb.Tx) error {
idxStr, e := tx.Get("f:" + f.String())
if e == buntdb.ErrNotFound {
return nil
}
if e != nil {
return e
}
index, e = strconv.Atoi(idxStr)
if e != nil {
return e
}
return nil
})
return
}
// GetPuts returns a list of puts after the given index
func (dht *DHT) GetPuts(since int) (puts []Put, err error) {
puts = make([]Put, 0)
db := dht.ht.(*BuntHT).db
err = db.View(func(tx *buntdb.Tx) error {
err = tx.AscendGreaterOrEqual("idx", string(since), func(key, value string) bool {
x := strings.Split(key, ":")
idx, _ := strconv.Atoi(x[1])
if idx >= since {
p := Put{Idx: idx}
if value != "" {
err := ByteDecoder([]byte(value), &p.M)
if err != nil {
return false
}
}
puts = append(puts, p)
}
return true
})
sort.Slice(puts, func(i, j int) bool { return puts[i].Idx < puts[j].Idx })
return err
})
return
}
// GetGossiper loads returns last known index of the gossiper, and adds them if not didn't exist before
func (dht *DHT) GetGossiper(id peer.ID) (idx int, err error) {
key := "peer:" + peer.IDB58Encode(id)
db := dht.ht.(*BuntHT).db
err = db.View(func(tx *buntdb.Tx) error {
var e error
idx, e = getIntVal(key, tx)
if e != nil {
return e
}
return nil
})
return
}
type GossiperData struct {
ID peer.ID
PutIdx int
}
func (dht *DHT) GetGossipers() (gossipers []GossiperData, err error) {
var glist []peer.ID
glist, err = dht._getGossipers()
if err != nil {
return
}
for _, id := range glist {
var idx int
idx, err = dht.GetGossiper(id)
if err != nil {
return
}
gossipers = append(gossipers, GossiperData{ID: id, PutIdx: idx})
}
return
}
func (dht *DHT) getGossipers() (glist []peer.ID, err error) {
glist, err = dht._getGossipers()
if err != nil {
return
}
ns := dht.config.RedundancyFactor
glist = dht.h.node.filterInactviePeers(glist, ns)
return
}
func (dht *DHT) _getGossipers() (glist []peer.ID, err error) {
glist = make([]peer.ID, 0)
db := dht.ht.(*BuntHT).db
err = db.View(func(tx *buntdb.Tx) error {
err = tx.Ascend("peer", func(key, value string) bool {
x := strings.Split(key, ":")
id, e := peer.IDB58Decode(x[1])
if e != nil {
return false
}
// idx, _ := strconv.Atoi(value)
glist = append(glist, id)
return true
})
return nil
})
ns := dht.config.RedundancyFactor
if ns > 1 {
size := len(glist)
hlist := make([]Hash, size)
for i := 0; i < size; i++ {
h := HashFromPeerID(glist[i])
hlist[i] = h
}
me := HashFromPeerID(dht.h.nodeID)
hlist = SortByDistance(me, hlist)
glist = make([]peer.ID, len(hlist))
for i := 0; i < len(hlist); i++ {
glist[i] = PeerIDFromHash(hlist[i])
}
}
return
}
// FindGossiper picks a random DHT node to gossip with
func (dht *DHT) FindGossiper() (g peer.ID, err error) {
var glist []peer.ID
glist, err = dht.getGossipers()
if err != nil {
return
}
if len(glist) == 0 {
err = ErrDHTErrNoGossipersAvailable
} else {
g = glist[rand.Intn(len(glist))]
}
return
}
// AddGossiper adds a new gossiper to the gossiper store
func (dht *DHT) AddGossiper(id peer.ID) (err error) {
// never add ourselves as a gossiper
if id == dht.h.node.HashAddr {
return
}
err = dht.updateGossiper(id, 0)
return
}
// internal update gossiper function, assumes all checks have been made
func (dht *DHT) updateGossiper(id peer.ID, newIdx int) (err error) {
db := dht.ht.(*BuntHT).db
err = db.Update(func(tx *buntdb.Tx) error {
key := "peer:" + peer.IDB58Encode(id)
idx, e := getIntVal(key, tx)
if e != nil {
return e
}
if newIdx < idx {
return nil
}
sidx := fmt.Sprintf("%d", newIdx)
_, _, err = tx.Set(key, sidx, nil)
if err != nil {
return err
}
return nil
})
return
}
// UpdateGossiper updates a gossiper
func (dht *DHT) UpdateGossiper(id peer.ID, newIdx int) (err error) {
if dht.h.node.IsBlocked(id) {
dht.glog.Logf("gossiper %v on blocklist, deleting", id)
dht.DeleteGossiper(id) // ignore error
return
}
dht.glog.Logf("updating %v to %d", id, newIdx)
err = dht.updateGossiper(id, newIdx)
return
}
// DeleteGossiper removes a gossiper from the database
func (dht *DHT) DeleteGossiper(id peer.ID) (err error) {
dht.glog.Logf("deleting %v", id)
db := dht.ht.(*BuntHT).db
err = db.Update(func(tx *buntdb.Tx) error {
key := "peer:" + peer.IDB58Encode(id)
_, e := tx.Delete(key)
return e
})
return
}
const (
GossipBackPutDelay = 100 * time.Millisecond
)
// GossipReceiver implements the handler for the gossip protocol
func GossipReceiver(h *Holochain, m *Message) (response interface{}, err error) {
dht := h.dht
switch m.Type {
case GOSSIP_REQUEST:
dht.glog.Logf("GossipReceiver got: %v", m)
switch t := m.Body.(type) {
case GossipReq:
dht.glog.Logf("%v wants my puts since %d and is at %d", m.From, t.YourIdx, t.MyIdx)
// give the gossiper what they want
var puts []Put
puts, err = h.dht.GetPuts(t.YourIdx)
g := Gossip{Puts: puts}
response = g
// check to see what we know they said, and if our record is less
// that where they are currently at, gossip back
idx, e := h.dht.GetGossiper(m.From)
if e == nil && idx < t.MyIdx {
dht.glog.Logf("we only have %d of %d from %v so gossiping back", idx, t.MyIdx, m.From)
pi := h.node.host.Peerstore().PeerInfo(m.From)
if len(pi.Addrs) == 0 {
dht.glog.Logf("NO ADDRESSES FOR PEER:%v", pi)
}
// queue up a request to gossip back
go func() {
defer func() {
if r := recover(); r != nil {
// ignore writes past close
}
}()
// but give them a chance to finish handling the response
// from this request first so sleep a bit per put
time.Sleep(GossipBackPutDelay * time.Duration(len(puts)))
dht.gchan <- gossipWithReq{m.From}
}()
}
default:
err = ErrDHTExpectedGossipReqInBody
}
default:
err = fmt.Errorf("message type %d not in holochain-gossip protocol", int(m.Type))
}
return
}
// gossipWith gossips with a peer asking for everything after since
func (dht *DHT) gossipWith(id peer.ID) (err error) {
// prevent rentrance
dht.glk.Lock()
defer dht.glk.Unlock()
dht.glog.Logf("starting gossipWith %v", id)
defer func() {
dht.glog.Logf("finish gossipWith %v, err=%v", id, err)
}()
var myIdx, yourIdx int
myIdx, err = dht.GetIdx()
if err != nil {
return
}
yourIdx, err = dht.GetGossiper(id)
if err != nil {
return
}
var r interface{}
msg := dht.h.node.NewMessage(GOSSIP_REQUEST, GossipReq{MyIdx: myIdx, YourIdx: yourIdx + 1})
r, err = dht.h.Send(dht.h.node.ctx, GossipProtocol, id, msg, 0)
if err != nil {
return
}
gossip := r.(Gossip)
puts := gossip.Puts
// gossiper has more stuff that we new about before so update the gossipers status
// and also run their puts
count := len(puts)
if count > 0 {
dht.glog.Logf("queuing %d puts:\n%v", count, puts)
var idx int
for i, p := range puts {
idx = i + yourIdx + 1
// put the message into the gossip put handling queue so we can return quickly
dht.gossipPuts <- p
}
err = dht.UpdateGossiper(id, idx)
} else {
dht.glog.Log("no new puts received")
}
return
}
// gossipPut handles a given put
func (dht *DHT) gossipPut(p Put) (err error) {
f, e := p.M.Fingerprint()
if e == nil {
// dht.sources[p.M.From] = true
// dht.fingerprints[f.String()[2:4]] = true
dht.glog.Logf("PUT--%d (fingerprint: %v)", p.Idx, f)
exists, e := dht.HaveFingerprint(f)
if !exists && e == nil {
dht.glog.Logf("PUT--%d calling ActionReceiver", p.Idx)
r, e := ActionReceiver(dht.h, &p.M)
dht.glog.Logf("PUT--%d ActionReceiver returned %v with err %v", p.Idx, r, e)
if e != nil {
// put receiver error so do what? probably nothing because
// put will get retried
}
} else {
if e == nil {
dht.glog.Logf("already have fingerprint %v", f)
} else {
dht.glog.Logf("error in HaveFingerprint %v", e)
}
}
} else {
dht.glog.Logf("error calculating fingerprint for %v", p)
}
return
}
func handleGossipPut(dht *DHT, x interface{}) (err error) {
p := x.(Put)
err = dht.gossipPut(p)
return
}
// gossip picks a random node in my neighborhood and sends gossips with it
func (dht *DHT) gossip() (err error) {
var g peer.ID
g, err = dht.FindGossiper()
if err != nil {
return
}
dht.gchan <- gossipWithReq{g}
return
}
// GossipTask runs a gossip and logs any errors
func GossipTask(h *Holochain) {
if h.dht != nil && h.dht.gchan != nil {
err := h.dht.gossip()
if err != nil {
h.dht.glog.Logf("error: %v", err)
}
}
}
func handleGossipWith(dht *DHT, x interface{}) (err error) {
g := x.(gossipWithReq)
err = dht.gossipWith(g.id)
return
}
func (dht *DHT) handleTillDone(errtext string, channel Channel, handlerFn func(*DHT, interface{}) error) (err error) {
var done bool
for !done {
dht.glog.Logf("%s: waiting for request", errtext)
x, ok := <-channel
if !ok {
done = true
break
} else {
err = handlerFn(dht, x)
if err != nil {
dht.glog.Logf("%s: got err: %v", errtext, err)
}
}
}
dht.glog.Logf("%s: channel closed, stopping", errtext)
return nil
}
// HandleGossipWiths waits on a channel for gossipWith requests
func (dht *DHT) HandleGossipWiths() (err error) {
err = dht.handleTillDone("HandleGossipWiths", dht.gchan, handleGossipWith)
return
}
// HandleGossipPuts waits on a channel for gossip changes
func (dht *DHT) HandleGossipPuts() (err error) {
err = dht.handleTillDone("HandleGossipPuts", dht.gossipPuts, handleGossipPut)
return nil
}
// getList returns the peer list of the given type
func (dht *DHT) getList(listType PeerListType) (result PeerList, err error) {
result.Type = listType
result.Records = make([]PeerRecord, 0)
db := dht.ht.(*BuntHT).db
err = db.View(func(tx *buntdb.Tx) error {
err = tx.Ascend("list", func(key, value string) bool {
x := strings.Split(key, ":")
if x[1] == string(listType) {
pid, e := peer.IDB58Decode(x[2])
if e != nil {
return false
}
r := PeerRecord{ID: pid, Warrant: value}
result.Records = append(result.Records, r)
}
return true
})
return nil
})
return
}
// addToList adds the peers to a list
func (dht *DHT) addToList(m *Message, list PeerList) (err error) {
dht.dlog.Logf("addToList %s=>%v", list.Type, list.Records)
db := dht.ht.(*BuntHT).db
err = db.Update(func(tx *buntdb.Tx) error {
_, err = incIdx(tx, m)
if err != nil {
return err
}
for _, r := range list.Records {
k := peer.IDB58Encode(r.ID)
_, _, err = tx.Set("list:"+string(list.Type)+":"+k, r.Warrant, nil)
if err != nil {
return err
}
}
return err
})
return
}