forked from anyproto/go-threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads.go
670 lines (600 loc) · 14.7 KB
/
threads.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
package threads
import (
"context"
"fmt"
"io"
"sync"
"time"
"github.com/ipfs/go-cid"
bs "github.com/ipfs/go-ipfs-blockstore"
format "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
ic "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
pstore "github.com/libp2p/go-libp2p-core/peerstore"
gostream "github.com/libp2p/go-libp2p-gostream"
ma "github.com/multiformats/go-multiaddr"
"github.com/textileio/go-textile-core/broadcast"
"github.com/textileio/go-textile-core/thread"
tserv "github.com/textileio/go-textile-core/threadservice"
tstore "github.com/textileio/go-textile-core/threadstore"
"github.com/textileio/go-textile-threads/cbor"
pb "github.com/textileio/go-textile-threads/pb"
"github.com/textileio/go-textile-threads/util"
logger "github.com/whyrusleeping/go-logging"
"google.golang.org/grpc"
)
var log = logging.Logger("threads")
// MaxPullLimit is the maximum page size for pulling records.
var MaxPullLimit = 10000
// PullInterval is the interval between automatic log pulls.
var PullInterval = time.Second * 10
// threads is an implementation of Threadservice.
type threads struct {
format.DAGService
host host.Host
bstore bs.Blockstore
store tstore.Threadstore
rpc *grpc.Server
service *service
bus *broadcast.Broadcaster
ctx context.Context
cancel context.CancelFunc
}
// NewThreads creates an instance of threads from the given host and thread store.
func NewThreads(
ctx context.Context,
h host.Host,
bstore bs.Blockstore,
ds format.DAGService,
ts tstore.Threadstore,
writer io.Writer,
debug bool,
) (tserv.Threadservice, error) {
var err error
if debug {
err = setLogLevels(map[string]logger.Level{
"threads": logger.DEBUG,
"threadstore": logger.DEBUG,
//"ipfslite": logger.DEBUG,
}, writer, true)
if err != nil {
return nil, err
}
}
ctx, cancel := context.WithCancel(ctx)
t := &threads{
DAGService: ds,
host: h,
bstore: bstore,
store: ts,
rpc: grpc.NewServer(),
bus: broadcast.NewBroadcaster(0),
ctx: ctx,
cancel: cancel,
}
t.service, err = newService(t)
if err != nil {
return nil, err
}
listener, err := gostream.Listen(h, ThreadProtocol)
if err != nil {
return nil, err
}
go t.rpc.Serve(listener)
pb.RegisterThreadsServer(t.rpc, t.service)
go t.startPulling()
return t, nil
}
// Close the threads instance.
func (t *threads) Close() (err error) {
t.rpc.GracefulStop()
var errs []error
weakClose := func(name string, c interface{}) {
if cl, ok := c.(io.Closer); ok {
if err = cl.Close(); err != nil {
errs = append(errs, fmt.Errorf("%s error: %s", name, err))
}
}
}
weakClose("DAGService", t.DAGService)
weakClose("host", t.host)
weakClose("threadstore", t.store)
t.bus.Discard()
t.cancel()
if len(errs) > 0 {
return fmt.Errorf("failed while closing threads; err(s): %q", errs)
}
return nil
}
// Host returns the underlying libp2p host.
func (t *threads) Host() host.Host {
return t.host
}
func (t *threads) Store() tstore.Threadstore {
return t.store
}
// AddThread from a multiaddress.
func (t *threads) AddThread(ctx context.Context, addr ma.Multiaddr) (info thread.Info, err error) {
idstr, err := addr.ValueForProtocol(ThreadCode)
if err != nil {
return
}
id, err := thread.Decode(idstr)
if err != nil {
return
}
p, err := addr.ValueForProtocol(ma.P_P2P)
if err != nil {
return
}
pid, err := peer.IDB58Decode(p)
if err != nil {
return
}
if pid.String() == t.host.ID().String() {
return t.store.ThreadInfo(id)
}
lgs, err := t.service.getLogs(ctx, id, pid)
if err != nil {
return
}
// @todo: ensure does not exist? or overwrite with newer info from owner?
for _, l := range lgs {
if err = t.store.AddLog(id, l); err != nil {
return
}
}
go func() {
if err := t.PullThread(t.ctx, id); err != nil {
log.Errorf("error pulling thread %s: %s", id.String(), err)
}
}()
return t.store.ThreadInfo(id)
}
// PullThread for new records.
// Logs owned by this host are traversed locally.
// Remotely addressed logs are pulled from the network.
// @todo: We have to be able to pull for new logs as well.
// @todo: This could mean that we should ask each address for a diff
// @todo: from offset for all their logs, including logs we don't request.
func (t *threads) PullThread(ctx context.Context, id thread.ID) error {
wg := sync.WaitGroup{}
lgs, err := t.getLogs(id)
if err != nil {
return err
}
for _, lg := range lgs {
wg.Add(1)
go func(lg thread.LogInfo) {
defer wg.Done()
var offset cid.Cid
if len(lg.Heads) > 0 {
has, err := t.bstore.Has(lg.Heads[0])
if err != nil {
log.Error(err)
return
}
if has {
offset = lg.Heads[0]
}
}
var recs []thread.Record
var err error
if lg.PrivKey != nil { // This is our own log
recs, err = t.getLocal(ctx, id, lg.ID, offset, MaxPullLimit)
} else {
// Pull from addresses
recs, err = t.service.getRecords(ctx, id, lg.ID, offset, MaxPullLimit)
}
if err != nil {
log.Error(err)
return
}
for _, r := range recs {
err = t.putRecord(ctx, r, tserv.PutOpt.ThreadID(id), tserv.PutOpt.LogID(lg.ID))
if err != nil {
log.Error(err)
return
}
}
}(lg)
}
wg.Wait()
return nil
}
// Delete a thread.
func (t *threads) DeleteThread(ctx context.Context, id thread.ID) error {
panic("implement me")
}
// AddFollower to a thread.
func (t *threads) AddFollower(ctx context.Context, id thread.ID, pid peer.ID) error {
lgs, err := t.getLogs(id)
if err != nil {
return err
}
for _, l := range lgs {
if err = t.service.pushLog(ctx, id, l, pid); err != nil {
return err
}
}
// Update local addresses
pro := ma.ProtocolWithCode(ma.P_P2P).Name
addr, err := ma.NewMultiaddr("/" + pro + "/" + pid.String())
if err != nil {
return err
}
ownlg, err := util.GetOwnLog(t, id)
if err != nil {
return err
}
if err = t.store.AddAddr(id, ownlg.ID, addr, pstore.PermanentAddrTTL); err != nil {
return err
}
// Send the updated log to peers
var addrs []ma.Multiaddr
info, err := t.store.ThreadInfo(id)
if err != nil {
return err
}
for _, l := range info.Logs {
if l.String() == ownlg.ID.String() {
continue
}
laddrs, err := t.store.Addrs(id, l)
if err != nil {
return err
}
addrs = append(addrs, laddrs...)
}
wg := sync.WaitGroup{}
for _, addr := range addrs {
wg.Add(1)
go func(addr ma.Multiaddr) {
defer wg.Done()
p, err := addr.ValueForProtocol(ma.P_P2P)
if err != nil {
log.Error(err)
return
}
pid, err := peer.IDB58Decode(p)
if err != nil {
log.Error(err)
return
}
if err = t.service.pushLog(ctx, id, ownlg, pid); err != nil {
log.Errorf("error pushing log %s to %s", ownlg.ID, p)
}
}(addr)
}
wg.Wait()
return nil
}
// AddRecord with body. See AddOption for more.
func (t *threads) AddRecord(
ctx context.Context,
body format.Node,
opts ...tserv.AddOption,
) (r tserv.Record, err error) {
// Get or create a log for the new node
settings := tserv.AddOptions(opts...)
lg, err := util.GetOrCreateOwnLog(t, settings.ThreadID)
if err != nil {
return
}
// Write a record locally
rec, err := t.createRecord(ctx, body, lg, settings)
if err != nil {
return
}
log.Infof("added record %s (thread=%s, log=%s)", rec.Cid().String(), settings.ThreadID, lg.ID)
// Push out the new record
err = t.service.pushRecord(ctx, rec, settings.ThreadID, lg.ID, settings)
if err != nil {
return
}
// Notify local listeners
r = &record{
Record: rec,
threadID: settings.ThreadID,
logID: lg.ID,
}
if err = t.bus.Send(r); err != nil {
return
}
return r, nil
}
// GetRecord returns the record at cid.
func (t *threads) GetRecord(
ctx context.Context,
id thread.ID,
lid peer.ID,
rid cid.Cid,
) (thread.Record, error) {
lg, err := t.store.LogInfo(id, lid)
if err != nil {
return nil, err
}
if lg.PubKey == nil {
return nil, fmt.Errorf("log not found")
}
return cbor.GetRecord(ctx, t, rid, lg.FollowKey)
}
// record wraps a thread.Record within a thread and log context.
type record struct {
thread.Record
threadID thread.ID
logID peer.ID
}
// Value returns the underlying record.
func (r *record) Value() thread.Record {
return r
}
// ThreadID returns the record's thread ID.
func (r *record) ThreadID() thread.ID {
return r.threadID
}
// LogID returns the record's log ID.
func (r *record) LogID() peer.ID {
return r.logID
}
// subscription receives thread record updates.
type subscription struct {
l *broadcast.Listener
ch chan tserv.Record
}
// Discard closes the subscription, disabling the reception of further records.
func (l *subscription) Discard() {
l.l.Discard()
}
// Channel returns the channel that receives records.
func (l *subscription) Channel() <-chan tserv.Record {
return l.ch
}
// Subscribe returns a read-only channel of records.
func (t *threads) Subscribe(opts ...tserv.SubOption) tserv.Subscription {
settings := tserv.SubOptions(opts...)
filter := make(map[thread.ID]struct{})
for _, id := range settings.ThreadIDs {
if id.Defined() {
filter[id] = struct{}{}
}
}
listener := &subscription{
l: t.bus.Listen(),
ch: make(chan tserv.Record),
}
go func() {
for {
select {
case i, ok := <-listener.l.Channel():
if !ok {
close(listener.ch)
return
}
r, ok := i.(*record)
if ok {
if len(filter) > 0 {
if _, ok := filter[r.threadID]; ok {
listener.ch <- r
}
} else {
listener.ch <- r
}
} else {
log.Warning("listener received a non-record value")
}
}
}
}()
return listener
}
// putRecord adds an existing record. See PutOption for more.
func (t *threads) putRecord(ctx context.Context, rec thread.Record, opts ...tserv.PutOption) error {
knownRecord, err := t.bstore.Has(rec.Cid())
if err != nil {
return err
}
if knownRecord {
return nil
}
// Get or create a log for the new rec
settings := tserv.PutOptions(opts...)
lg, err := util.GetOrCreateLog(t, settings.ThreadID, settings.LogID)
if err != nil {
return err
}
// Save the record locally
// Note: These get methods will return cached nodes.
block, err := rec.GetBlock(ctx, t)
if err != nil {
return err
}
event, ok := block.(*cbor.Event)
if !ok {
return fmt.Errorf("invalid event")
}
header, err := event.GetHeader(ctx, t, nil)
if err != nil {
return err
}
body, err := event.GetBody(ctx, t, nil)
if err != nil {
return err
}
err = t.AddMany(ctx, []format.Node{rec, event, header, body})
if err != nil {
return err
}
// Update head
if err = t.store.SetHead(settings.ThreadID, lg.ID, rec.Cid()); err != nil {
return err
}
log.Infof("put record %s (thread=%s, log=%s)", rec.Cid().String(), settings.ThreadID, lg.ID)
// Notify local listeners
return t.bus.Send(&record{
Record: rec,
threadID: settings.ThreadID,
logID: lg.ID,
})
}
// getPrivKey returns the host's private key.
func (t *threads) getPrivKey() ic.PrivKey {
return t.host.Peerstore().PrivKey(t.host.ID())
}
// createLog calls util.CreateLog.
func (t *threads) createLog() (thread.LogInfo, error) {
return util.CreateLog(t.host.ID())
}
// createRecord creates a new record with the given body as a new event body.
func (t *threads) createRecord(
ctx context.Context,
body format.Node,
lg thread.LogInfo,
settings *tserv.AddSettings,
) (thread.Record, error) {
if settings.Key == nil {
logKey, err := t.store.ReadKey(settings.ThreadID, settings.KeyLog)
if err != nil {
return nil, err
}
if logKey != nil {
settings.Key = logKey
} else {
settings.Key = lg.ReadKey
}
}
event, err := cbor.NewEvent(ctx, t, body, settings)
if err != nil {
return nil, err
}
prev := cid.Undef
if len(lg.Heads) != 0 {
prev = lg.Heads[0]
}
rec, err := cbor.NewRecord(ctx, t, event, prev, lg.PrivKey, lg.FollowKey)
if err != nil {
return nil, err
}
// Update head
if err = t.store.SetHead(settings.ThreadID, lg.ID, rec.Cid()); err != nil {
return nil, err
}
return rec, nil
}
// getLogs returns info about the logs in the given thread.
func (t *threads) getLogs(id thread.ID) ([]thread.LogInfo, error) {
lgs := make([]thread.LogInfo, 0)
ti, err := t.store.ThreadInfo(id)
if err != nil {
return nil, err
}
for _, lid := range ti.Logs {
lg, err := t.store.LogInfo(id, lid)
if err != nil {
return nil, err
}
lgs = append(lgs, lg)
}
return lgs, nil
}
// getLocal returns local records from the given thread that are ahead of
// offset but not farther than limit.
// It is possible to reach limit before offset, meaning that the caller
// will be responsible for the remaining traversal.
func (t *threads) getLocal(
ctx context.Context,
id thread.ID,
lid peer.ID,
offset cid.Cid,
limit int,
) ([]thread.Record, error) {
lg, err := t.store.LogInfo(id, lid)
if err != nil {
return nil, err
}
if lg.PubKey == nil {
return nil, fmt.Errorf("log not found")
}
var recs []thread.Record
if limit <= 0 {
return recs, nil
}
if len(lg.Heads) != 1 {
return nil, fmt.Errorf("log head must reference exactly one node")
}
cursor := lg.Heads[0]
for {
if cursor.String() == offset.String() {
break
}
r, err := cbor.GetRecord(ctx, t, cursor, lg.FollowKey)
if err != nil {
return nil, err
}
recs = append([]thread.Record{r}, recs...)
if len(recs) >= MaxPullLimit {
break
}
cursor = r.PrevID()
}
return recs, nil
}
// startPulling periodically pulls on all threads.
// @todo: Ensure that a thread is not pulled concurrently (#26).
func (t *threads) startPulling() {
tick := time.NewTicker(PullInterval)
defer tick.Stop()
for {
select {
case <-tick.C:
ts, err := t.store.Threads()
if err != nil {
log.Errorf("error listing threads: %s", err)
continue
}
for _, id := range ts {
go func(id thread.ID) {
if err := t.PullThread(t.ctx, id); err != nil {
log.Errorf("error pulling thread %s: %s", id.String(), err)
}
}(id)
}
case <-t.ctx.Done():
return
}
}
}
// setLogLevels sets the logging levels of the given log systems.
// color controls whether or not color codes are included in the output.
func setLogLevels(systems map[string]logger.Level, writer io.Writer, color bool) error {
if writer != nil {
backendFile := logger.NewLogBackend(writer, "", 0)
logger.SetBackend(backendFile)
}
var form string
if color {
form = logging.LogFormats["color"]
} else {
form = logging.LogFormats["nocolor"]
}
logger.SetFormatter(logger.MustStringFormatter(form))
logging.SetAllLoggers(logger.ERROR)
var err error
for sys, level := range systems {
if sys == "*" {
for _, s := range logging.GetSubsystems() {
err = logging.SetLogLevel(s, level.String())
if err != nil {
return err
}
}
}
err = logging.SetLogLevel(sys, level.String())
if err != nil {
return err
}
}
return nil
}