Remove existing provider system (will fail)
Michael Avila committed
Jul 3, 2019 at 14:22 UTC
40af13e8b9ec84b129191fcdfba9fdcda6a51c69
9 files changed
-835
provider/offline.go
deleted
-28
@@ -1,28 +0,0 @@
1
-package provider
2
-
3
-import (
4
- "context"
5
- "github.com/ipfs/go-cid"
6
-)
7
-
8
-type offlineProvider struct{}
9
-
10
-// NewOfflineProvider creates a ProviderSystem that does nothing
11
-func NewOfflineProvider() System {
12
- return &offlineProvider{}
13
-}
14
-
15
-func (op *offlineProvider) Run() {
16
-}
17
-
18
-func (op *offlineProvider) Close() error {
19
- return nil
20
-}
21
-
22
-func (op *offlineProvider) Provide(cid.Cid) error {
23
- return nil
24
-}
25
-
26
-func (op *offlineProvider) Reprovide(context.Context) error {
27
- return nil
28
-}
provider/provider.go
deleted
-26
@@ -1,26 +0,0 @@
1
-package provider
2
-
3
-import (
4
- "context"
5
- "github.com/ipfs/go-cid"
6
-)
7
-
8
-// Provider announces blocks to the network
9
-type Provider interface {
10
- // Run is used to begin processing the provider work
11
- Run()
12
- // Provide takes a cid and makes an attempt to announce it to the network
13
- Provide(cid.Cid) error
14
- // Close stops the provider
15
- Close() error
16
-}
17
-
18
-// Reprovider reannounces blocks to the network
19
-type Reprovider interface {
20
- // Run is used to begin processing the reprovider work and waiting for reprovide triggers
21
- Run()
22
- // Trigger a reprovide
23
- Trigger(context.Context) error
24
- // Close stops the reprovider
25
- Close() error
26
-}
provider/queue/queue.go
deleted
-148
@@ -1,148 +0,0 @@
1
-package queue
2
-
3
-import (
4
- "context"
5
- "fmt"
6
- "time"
7
-
8
- cid "github.com/ipfs/go-cid"
9
- datastore "github.com/ipfs/go-datastore"
10
- namespace "github.com/ipfs/go-datastore/namespace"
11
- query "github.com/ipfs/go-datastore/query"
12
- logging "github.com/ipfs/go-log"
13
-)
14
-
15
-var log = logging.Logger("provider.queue")
16
-
17
-// Queue provides a durable, FIFO interface to the datastore for storing cids
18
-//
19
-// Durability just means that cids in the process of being provided when a
20
-// crash or shutdown occurs will still be in the queue when the node is
21
-// brought back online.
22
-type Queue struct {
23
- // used to differentiate queues in datastore
24
- // e.g. provider vs reprovider
25
- name string
26
- ctx context.Context
27
- ds datastore.Datastore // Must be threadsafe
28
- dequeue chan cid.Cid
29
- enqueue chan cid.Cid
30
- close context.CancelFunc
31
- closed chan struct{}
32
-}
33
-
34
-// NewQueue creates a queue for cids
35
-func NewQueue(ctx context.Context, name string, ds datastore.Datastore) (*Queue, error) {
36
- namespaced := namespace.Wrap(ds, datastore.NewKey("/"+name+"/queue/"))
37
- cancelCtx, cancel := context.WithCancel(ctx)
38
- q := &Queue{
39
- name: name,
40
- ctx: cancelCtx,
41
- ds: namespaced,
42
- dequeue: make(chan cid.Cid),
43
- enqueue: make(chan cid.Cid),
44
- close: cancel,
45
- closed: make(chan struct{}, 1),
46
- }
47
- q.work()
48
- return q, nil
49
-}
50
-
51
-// Close stops the queue
52
-func (q *Queue) Close() error {
53
- q.close()
54
- <-q.closed
55
- return nil
56
-}
57
-
58
-// Enqueue puts a cid in the queue
59
-func (q *Queue) Enqueue(cid cid.Cid) {
60
- select {
61
- case q.enqueue <- cid:
62
- case <-q.ctx.Done():
63
- }
64
-}
65
-
66
-// Dequeue returns a channel that if listened to will remove entries from the queue
67
-func (q *Queue) Dequeue() <-chan cid.Cid {
68
- return q.dequeue
69
-}
70
-
71
-// Run dequeues and enqueues when available.
72
-func (q *Queue) work() {
73
- go func() {
74
- var k datastore.Key = datastore.Key{}
75
- var c cid.Cid = cid.Undef
76
-
77
- defer func() {
78
- close(q.closed)
79
- }()
80
-
81
- for {
82
- if c == cid.Undef {
83
- head, e := q.getQueueHead()
84
-
85
- if e != nil {
86
- log.Errorf("error querying for head of queue: %s, stopping provider", e)
87
- return
88
- } else if head != nil {
89
- k = datastore.NewKey(head.Key)
90
- c, e = cid.Parse(head.Value)
91
- if e != nil {
92
- log.Warningf("error parsing queue entry cid with key (%s), removing it from queue: %s", head.Key, e)
93
- err := q.ds.Delete(k)
94
- if err != nil {
95
- log.Errorf("error deleting queue entry with key (%s), due to error (%s), stopping provider", head.Key, err)
96
- return
97
- }
98
- continue
99
- }
100
- } else {
101
- c = cid.Undef
102
- }
103
- }
104
-
105
- // If c != cid.Undef set dequeue and attempt write, otherwise wait for enqueue
106
- var dequeue chan cid.Cid
107
- if c != cid.Undef {
108
- dequeue = q.dequeue
109
- }
110
-
111
- select {
112
- case toQueue := <-q.enqueue:
113
- keyPath := fmt.Sprintf("%d/%s", time.Now().UnixNano(), c.String())
114
- nextKey := datastore.NewKey(keyPath)
115
-
116
- if err := q.ds.Put(nextKey, toQueue.Bytes()); err != nil {
117
- log.Errorf("Failed to enqueue cid: %s", err)
118
- continue
119
- }
120
- case dequeue <- c:
121
- err := q.ds.Delete(k)
122
-
123
- if err != nil {
124
- log.Errorf("Failed to delete queued cid %s with key %s: %s", c, k, err)
125
- continue
126
- }
127
- c = cid.Undef
128
- case <-q.ctx.Done():
129
- return
130
- }
131
- }
132
- }()
133
-}
134
-
135
-func (q *Queue) getQueueHead() (*query.Result, error) {
136
- qry := query.Query{Orders: []query.Order{query.OrderByKey{}}, Limit: 1}
137
- results, err := q.ds.Query(qry)
138
- if err != nil {
139
- return nil, err
140
- }
141
- defer results.Close()
142
- r, ok := results.NextSync()
143
- if !ok {
144
- return nil, nil
145
- }
146
-
147
- return &r, nil
148
-}
provider/queue/queue_test.go
deleted
-133
@@ -1,133 +0,0 @@
1
-package queue
2
-
3
-import (
4
- "context"
5
- "testing"
6
- "time"
7
-
8
- "github.com/ipfs/go-cid"
9
- "github.com/ipfs/go-datastore"
10
- "github.com/ipfs/go-datastore/sync"
11
- "github.com/ipfs/go-ipfs-blocksutil"
12
-)
13
-
14
-var blockGenerator = blocksutil.NewBlockGenerator()
15
-
16
-func makeCids(n int) []cid.Cid {
17
- cids := make([]cid.Cid, 0, n)
18
- for i := 0; i < n; i++ {
19
- c := blockGenerator.Next().Cid()
20
- cids = append(cids, c)
21
- }
22
- return cids
23
-}
24
-
25
-func assertOrdered(cids []cid.Cid, q *Queue, t *testing.T) {
26
- for _, c := range cids {
27
- select {
28
- case dequeued := <-q.dequeue:
29
- if c != dequeued {
30
- t.Fatalf("Error in ordering of CIDs retrieved from queue. Expected: %s, got: %s", c, dequeued)
31
- }
32
-
33
- case <-time.After(time.Second * 1):
34
- t.Fatal("Timeout waiting for cids to be provided.")
35
- }
36
- }
37
-}
38
-
39
-func TestBasicOperation(t *testing.T) {
40
- ctx := context.Background()
41
- defer ctx.Done()
42
-
43
- ds := sync.MutexWrap(datastore.NewMapDatastore())
44
- queue, err := NewQueue(ctx, "test", ds)
45
- if err != nil {
46
- t.Fatal(err)
47
- }
48
-
49
- cids := makeCids(10)
50
-
51
- for _, c := range cids {
52
- queue.Enqueue(c)
53
- }
54
-
55
- assertOrdered(cids, queue, t)
56
-}
57
-
58
-func TestMangledData(t *testing.T) {
59
- ctx := context.Background()
60
- defer ctx.Done()
61
-
62
- ds := sync.MutexWrap(datastore.NewMapDatastore())
63
- queue, err := NewQueue(ctx, "test", ds)
64
- if err != nil {
65
- t.Fatal(err)
66
- }
67
-
68
- cids := makeCids(10)
69
- for _, c := range cids {
70
- queue.Enqueue(c)
71
- }
72
-
73
- // put bad data in the queue
74
- queueKey := datastore.NewKey("/test/0")
75
- err = queue.ds.Put(queueKey, []byte("borked"))
76
- if err != nil {
77
- t.Fatal(err)
78
- }
79
-
80
- // expect to only see the valid cids we entered
81
- expected := cids
82
- assertOrdered(expected, queue, t)
83
-}
84
-
85
-func TestInitialization(t *testing.T) {
86
- ctx := context.Background()
87
- defer ctx.Done()
88
-
89
- ds := sync.MutexWrap(datastore.NewMapDatastore())
90
- queue, err := NewQueue(ctx, "test", ds)
91
- if err != nil {
92
- t.Fatal(err)
93
- }
94
-
95
- cids := makeCids(10)
96
- for _, c := range cids {
97
- queue.Enqueue(c)
98
- }
99
-
100
- assertOrdered(cids[:5], queue, t)
101
-
102
- // make a new queue, same data
103
- queue, err = NewQueue(ctx, "test", ds)
104
- if err != nil {
105
- t.Fatal(err)
106
- }
107
-
108
- assertOrdered(cids[5:], queue, t)
109
-}
110
-
111
-func TestInitializationWithManyCids(t *testing.T) {
112
- ctx := context.Background()
113
- defer ctx.Done()
114
-
115
- ds := sync.MutexWrap(datastore.NewMapDatastore())
116
- queue, err := NewQueue(ctx, "test", ds)
117
- if err != nil {
118
- t.Fatal(err)
119
- }
120
-
121
- cids := makeCids(25)
122
- for _, c := range cids {
123
- queue.Enqueue(c)
124
- }
125
-
126
- // make a new queue, same data
127
- queue, err = NewQueue(ctx, "test", ds)
128
- if err != nil {
129
- t.Fatal(err)
130
- }
131
-
132
- assertOrdered(cids, queue, t)
133
-}
provider/simple/provider.go
deleted
-72
@@ -1,72 +0,0 @@
1
-// Package simple implements structures and methods to provide blocks,
2
-// keep track of which blocks are provided, and to allow those blocks to
3
-// be reprovided.
4
-package simple
5
-
6
-import (
7
- "context"
8
-
9
- cid "github.com/ipfs/go-cid"
10
- q "github.com/ipfs/go-ipfs/provider/queue"
11
- logging "github.com/ipfs/go-log"
12
- routing "github.com/libp2p/go-libp2p-core/routing"
13
-)
14
-
15
-var logP = logging.Logger("provider.simple")
16
-
17
-const provideOutgoingWorkerLimit = 8
18
-
19
-// Provider announces blocks to the network
20
-type Provider struct {
21
- ctx context.Context
22
- // the CIDs for which provide announcements should be made
23
- queue *q.Queue
24
- // used to announce providing to the network
25
- contentRouting routing.ContentRouting
26
-}
27
-
28
-// NewProvider creates a provider that announces blocks to the network using a content router
29
-func NewProvider(ctx context.Context, queue *q.Queue, contentRouting routing.ContentRouting) *Provider {
30
- return &Provider{
31
- ctx: ctx,
32
- queue: queue,
33
- contentRouting: contentRouting,
34
- }
35
-}
36
-
37
-// Close stops the provider
38
-func (p *Provider) Close() error {
39
- p.queue.Close()
40
- return nil
41
-}
42
-
43
-// Run workers to handle provide requests.
44
-func (p *Provider) Run() {
45
- p.handleAnnouncements()
46
-}
47
-
48
-// Provide the given cid using specified strategy.
49
-func (p *Provider) Provide(root cid.Cid) error {
50
- p.queue.Enqueue(root)
51
- return nil
52
-}
53
-
54
-// Handle all outgoing cids by providing (announcing) them
55
-func (p *Provider) handleAnnouncements() {
56
- for workers := 0; workers < provideOutgoingWorkerLimit; workers++ {
57
- go func() {
58
- for p.ctx.Err() == nil {
59
- select {
60
- case <-p.ctx.Done():
61
- return
62
- case c := <-p.queue.Dequeue():
63
- logP.Info("announce - start - ", c)
64
- if err := p.contentRouting.Provide(p.ctx, c, true); err != nil {
65
- logP.Warningf("Unable to provide entry: %s, %s", c, err)
66
- }
67
- logP.Info("announce - end - ", c)
68
- }
69
- }
70
- }()
71
- }
72
-}
provider/simple/provider_test.go
deleted
-83
@@ -1,83 +0,0 @@
1
-package simple_test
2
-
3
-import (
4
- "context"
5
- "math/rand"
6
- "testing"
7
- "time"
8
-
9
- cid "github.com/ipfs/go-cid"
10
- datastore "github.com/ipfs/go-datastore"
11
- sync "github.com/ipfs/go-datastore/sync"
12
- blocksutil "github.com/ipfs/go-ipfs-blocksutil"
13
- peer "github.com/libp2p/go-libp2p-core/peer"
14
-
15
- q "github.com/ipfs/go-ipfs/provider/queue"
16
-
17
- . "github.com/ipfs/go-ipfs/provider/simple"
18
-)
19
-
20
-var blockGenerator = blocksutil.NewBlockGenerator()
21
-
22
-type mockRouting struct {
23
- provided chan cid.Cid
24
-}
25
-
26
-func (r *mockRouting) Provide(ctx context.Context, cid cid.Cid, recursive bool) error {
27
- r.provided <- cid
28
- return nil
29
-}
30
-
31
-func (r *mockRouting) FindProvidersAsync(ctx context.Context, cid cid.Cid, timeout int) <-chan peer.AddrInfo {
32
- return nil
33
-}
34
-
35
-func mockContentRouting() *mockRouting {
36
- r := mockRouting{}
37
- r.provided = make(chan cid.Cid)
38
- return &r
39
-}
40
-
41
-func TestAnnouncement(t *testing.T) {
42
- ctx := context.Background()
43
- defer ctx.Done()
44
-
45
- ds := sync.MutexWrap(datastore.NewMapDatastore())
46
- queue, err := q.NewQueue(ctx, "test", ds)
47
- if err != nil {
48
- t.Fatal(err)
49
- }
50
-
51
- r := mockContentRouting()
52
-
53
- prov := NewProvider(ctx, queue, r)
54
- prov.Run()
55
-
56
- cids := cid.NewSet()
57
-
58
- for i := 0; i < 100; i++ {
59
- c := blockGenerator.Next().Cid()
60
- cids.Add(c)
61
- }
62
-
63
- go func() {
64
- for _, c := range cids.Keys() {
65
- err = prov.Provide(c)
66
- // A little goroutine stirring to exercise some different states
67
- r := rand.Intn(10)
68
- time.Sleep(time.Microsecond * time.Duration(r))
69
- }
70
- }()
71
-
72
- for cids.Len() > 0 {
73
- select {
74
- case cp := <-r.provided:
75
- if !cids.Has(cp) {
76
- t.Fatal("Wrong CID provided")
77
- }
78
- cids.Remove(cp)
79
- case <-time.After(time.Second * 5):
80
- t.Fatal("Timeout waiting for cids to be provided.")
81
- }
82
- }
83
-}
provider/simple/reprovide.go
deleted
-225
@@ -1,225 +0,0 @@
1
-package simple
2
-
3
-import (
4
- "context"
5
- "fmt"
6
- "time"
7
-
8
- backoff "github.com/cenkalti/backoff"
9
- cid "github.com/ipfs/go-cid"
10
- cidutil "github.com/ipfs/go-cidutil"
11
- blocks "github.com/ipfs/go-ipfs-blockstore"
12
- pin "github.com/ipfs/go-ipfs/pin"
13
- ipld "github.com/ipfs/go-ipld-format"
14
- logging "github.com/ipfs/go-log"
15
- merkledag "github.com/ipfs/go-merkledag"
16
- verifcid "github.com/ipfs/go-verifcid"
17
- routing "github.com/libp2p/go-libp2p-core/routing"
18
-)
19
-
20
-var logR = logging.Logger("reprovider.simple")
21
-
22
-//KeyChanFunc is function streaming CIDs to pass to content routing
23
-type KeyChanFunc func(context.Context) (<-chan cid.Cid, error)
24
-type doneFunc func(error)
25
-
26
-// Reprovider reannounces blocks to the network
27
-type Reprovider struct {
28
- ctx context.Context
29
- trigger chan doneFunc
30
-
31
- // The routing system to provide values through
32
- rsys routing.ContentRouting
33
-
34
- keyProvider KeyChanFunc
35
-
36
- tick time.Duration
37
-}
38
-
39
-// NewReprovider creates new Reprovider instance.
40
-func NewReprovider(ctx context.Context, reprovideIniterval time.Duration, rsys routing.ContentRouting, keyProvider KeyChanFunc) *Reprovider {
41
- return &Reprovider{
42
- ctx: ctx,
43
- trigger: make(chan doneFunc),
44
-
45
- rsys: rsys,
46
- keyProvider: keyProvider,
47
- tick: reprovideIniterval,
48
- }
49
-}
50
-
51
-// Close the reprovider
52
-func (rp *Reprovider) Close() error {
53
- return nil
54
-}
55
-
56
-// Run re-provides keys with 'tick' interval or when triggered
57
-func (rp *Reprovider) Run() {
58
- // dont reprovide immediately.
59
- // may have just started the daemon and shutting it down immediately.
60
- // probability( up another minute | uptime ) increases with uptime.
61
- after := time.After(time.Minute)
62
- var done doneFunc
63
- for {
64
- if rp.tick == 0 {
65
- after = make(chan time.Time)
66
- }
67
-
68
- select {
69
- case <-rp.ctx.Done():
70
- return
71
- case done = <-rp.trigger:
72
- case <-after:
73
- }
74
-
75
- //'mute' the trigger channel so when `ipfs bitswap reprovide` is called
76
- //a 'reprovider is already running' error is returned
77
- unmute := rp.muteTrigger()
78
-
79
- err := rp.Reprovide()
80
- if err != nil {
81
- logR.Debug(err)
82
- }
83
-
84
- if done != nil {
85
- done(err)
86
- }
87
-
88
- unmute()
89
-
90
- after = time.After(rp.tick)
91
- }
92
-}
93
-
94
-// Reprovide registers all keys given by rp.keyProvider to libp2p content routing
95
-func (rp *Reprovider) Reprovide() error {
96
- keychan, err := rp.keyProvider(rp.ctx)
97
- if err != nil {
98
- return fmt.Errorf("failed to get key chan: %s", err)
99
- }
100
- for c := range keychan {
101
- // hash security
102
- if err := verifcid.ValidateCid(c); err != nil {
103
- logR.Errorf("insecure hash in reprovider, %s (%s)", c, err)
104
- continue
105
- }
106
- op := func() error {
107
- err := rp.rsys.Provide(rp.ctx, c, true)
108
- if err != nil {
109
- logR.Debugf("Failed to provide key: %s", err)
110
- }
111
- return err
112
- }
113
-
114
- // TODO: this backoff library does not respect our context, we should
115
- // eventually work contexts into it. low priority.
116
- err := backoff.Retry(op, backoff.NewExponentialBackOff())
117
- if err != nil {
118
- logR.Debugf("Providing failed after number of retries: %s", err)
119
- return err
120
- }
121
- }
122
- return nil
123
-}
124
-
125
-// Trigger starts reprovision process in rp.Run and waits for it
126
-func (rp *Reprovider) Trigger(ctx context.Context) error {
127
- progressCtx, done := context.WithCancel(ctx)
128
-
129
- var err error
130
- df := func(e error) {
131
- err = e
132
- done()
133
- }
134
-
135
- select {
136
- case <-rp.ctx.Done():
137
- return context.Canceled
138
- case <-ctx.Done():
139
- return context.Canceled
140
- case rp.trigger <- df:
141
- <-progressCtx.Done()
142
- return err
143
- }
144
-}
145
-
146
-func (rp *Reprovider) muteTrigger() context.CancelFunc {
147
- ctx, cf := context.WithCancel(rp.ctx)
148
- go func() {
149
- defer cf()
150
- for {
151
- select {
152
- case <-ctx.Done():
153
- return
154
- case done := <-rp.trigger:
155
- done(fmt.Errorf("reprovider is already running"))
156
- }
157
- }
158
- }()
159
-
160
- return cf
161
-}
162
-
163
-// Strategies
164
-
165
-// NewBlockstoreProvider returns key provider using bstore.AllKeysChan
166
-func NewBlockstoreProvider(bstore blocks.Blockstore) KeyChanFunc {
167
- return func(ctx context.Context) (<-chan cid.Cid, error) {
168
- return bstore.AllKeysChan(ctx)
169
- }
170
-}
171
-
172
-// NewPinnedProvider returns provider supplying pinned keys
173
-func NewPinnedProvider(onlyRoots bool) func(pin.Pinner, ipld.DAGService) KeyChanFunc {
174
- return func(pinning pin.Pinner, dag ipld.DAGService) KeyChanFunc {
175
- return func(ctx context.Context) (<-chan cid.Cid, error) {
176
- set, err := pinSet(ctx, pinning, dag, onlyRoots)
177
- if err != nil {
178
- return nil, err
179
- }
180
-
181
- outCh := make(chan cid.Cid)
182
- go func() {
183
- defer close(outCh)
184
- for c := range set.New {
185
- select {
186
- case <-ctx.Done():
187
- return
188
- case outCh <- c:
189
- }
190
- }
191
-
192
- }()
193
-
194
- return outCh, nil
195
- }
196
- }
197
-}
198
-
199
-func pinSet(ctx context.Context, pinning pin.Pinner, dag ipld.DAGService, onlyRoots bool) (*cidutil.StreamingSet, error) {
200
- set := cidutil.NewStreamingSet()
201
-
202
- go func() {
203
- ctx, cancel := context.WithCancel(ctx)
204
- defer cancel()
205
- defer close(set.New)
206
-
207
- for _, key := range pinning.DirectKeys() {
208
- set.Visitor(ctx)(key)
209
- }
210
-
211
- for _, key := range pinning.RecursiveKeys() {
212
- set.Visitor(ctx)(key)
213
-
214
- if !onlyRoots {
215
- err := merkledag.EnumerateChildren(ctx, merkledag.GetLinksWithDAG(dag), key, set.Visitor(ctx))
216
- if err != nil {
217
- logR.Errorf("reprovide indirect pins: %s", err)
218
- return
219
- }
220
- }
221
- }
222
- }()
223
-
224
- return set, nil
225
-}
provider/simple/reprovide_test.go
deleted
-61
@@ -1,61 +0,0 @@
1
-package simple_test
2
-
3
-import (
4
- "context"
5
- "testing"
6
- "time"
7
-
8
- blocks "github.com/ipfs/go-block-format"
9
- ds "github.com/ipfs/go-datastore"
10
- dssync "github.com/ipfs/go-datastore/sync"
11
- "github.com/ipfs/go-ipfs-blockstore"
12
- mock "github.com/ipfs/go-ipfs-routing/mock"
13
- peer "github.com/libp2p/go-libp2p-core/peer"
14
- testutil "github.com/libp2p/go-libp2p-testing/net"
15
-
16
- . "github.com/ipfs/go-ipfs/provider/simple"
17
-)
18
-
19
-func TestReprovide(t *testing.T) {
20
- ctx, cancel := context.WithCancel(context.Background())
21
- defer cancel()
22
-
23
- mrserv := mock.NewServer()
24
-
25
- idA := testutil.RandIdentityOrFatal(t)
26
- idB := testutil.RandIdentityOrFatal(t)
27
-
28
- clA := mrserv.Client(idA)
29
- clB := mrserv.Client(idB)
30
-
31
- bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
32
-
33
- blk := blocks.NewBlock([]byte("this is a test"))
34
- err := bstore.Put(blk)
35
- if err != nil {
36
- t.Fatal(err)
37
- }
38
-
39
- keyProvider := NewBlockstoreProvider(bstore)
40
- reprov := NewReprovider(ctx, time.Hour, clA, keyProvider)
41
- err = reprov.Reprovide()
42
- if err != nil {
43
- t.Fatal(err)
44
- }
45
-
46
- var providers []peer.AddrInfo
47
- maxProvs := 100
48
-
49
- provChan := clB.FindProvidersAsync(ctx, blk.Cid(), maxProvs)
50
- for p := range provChan {
51
- providers = append(providers, p)
52
- }
53
-
54
- if len(providers) == 0 {
55
- t.Fatal("Should have gotten a provider")
56
- }
57
-
58
- if providers[0].ID != idA.ID() {
59
- t.Fatal("Somehow got the wrong peer back as a provider.")
60
- }
61
-}
provider/system.go
deleted
-59
@@ -1,59 +0,0 @@
1
-package provider
2
-
3
-import (
4
- "context"
5
- "github.com/ipfs/go-cid"
6
-)
7
-
8
-// System defines the interface for interacting with the value
9
-// provider system
10
-type System interface {
11
- Run()
12
- Close() error
13
- Provide(cid.Cid) error
14
- Reprovide(context.Context) error
15
-}
16
-
17
-type system struct {
18
- provider Provider
19
- reprovider Reprovider
20
-}
21
-
22
-// NewSystem constructs a new provider system from a provider and reprovider
23
-func NewSystem(provider Provider, reprovider Reprovider) System {
24
- return &system{provider, reprovider}
25
-}
26
-
27
-// Run the provider system by running the provider and reprovider
28
-func (s *system) Run() {
29
- go s.provider.Run()
30
- go s.reprovider.Run()
31
-}
32
-
33
-// Close the provider and reprovider
34
-func (s *system) Close() error {
35
- var errs []error
36
-
37
- if err := s.provider.Close(); err != nil {
38
- errs = append(errs, err)
39
- }
40
-
41
- if err := s.reprovider.Close(); err != nil {
42
- errs = append(errs, err)
43
- }
44
-
45
- if len(errs) > 0 {
46
- return errs[0]
47
- }
48
- return nil
49
-}
50
-
51
-// Provide a value
52
-func (s *system) Provide(cid cid.Cid) error {
53
- return s.provider.Provide(cid)
54
-}
55
-
56
-// Reprovide all the previously provided values
57
-func (s *system) Reprovide(ctx context.Context) error {
58
- return s.reprovider.Trigger(ctx)
59
-}