@@ -7,15 +7,15 @@ import (
7
7
"time"
8
8
9
9
path "github.com/ipfs/go-ipfs/path"
10
+ mockrouting "github.com/ipfs/go-ipfs/routing/mock"
11
+ testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
10
12
11
13
ci "gx/ipfs/QmP1DfoUjiWH2ZBo1PBH6FupdBucbDepx3HpWmEY6JMUpY/go-libp2p-crypto"
12
14
routing "gx/ipfs/QmP1wMAqk6aZYRZirbaAwmrNeqFRgQrwBt3orUtvSa1UYD/go-libp2p-routing"
13
15
floodsub "gx/ipfs/QmUpeULWfmtsgCnfuRN3BHsfhHvBxNphoYh4La4CMxGt2Z/floodsub"
14
16
p2phost "gx/ipfs/QmUywuGNZoUKV8B9iyvup9bPkLiMrhTsyVMkeSXW5VxAfC/go-libp2p-host"
15
17
ds "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/go-datastore"
16
- pstore "gx/ipfs/QmXZSd1qR5BxZkPyuwfT5jpqQFScZccoZvDneXsKzCNHWX/go-libp2p-peerstore"
17
18
netutil "gx/ipfs/Qma2j8dYePrvN5DoNgwh1uAuu3FFtEtrUQFmr737ws8nCp/go-libp2p-netutil"
18
- cid "gx/ipfs/Qma4RJSuh7mMeJQYCqMbKzekn6EwBo7HEs5AQYjVRMQATB/go-cid"
19
19
bhost "gx/ipfs/Qma4Xhhqtr9tpV814eNjbLHzjuDaRjs96XLcZPJiR742ZV/go-libp2p-blankhost"
20
20
peer "gx/ipfs/QmdS9KpbDyPrieswibZhkod1oXqRwZJrUPzxCofAMWpFGq/go-libp2p-peer"
21
21
)
@@ -36,13 +36,19 @@ func newNetHosts(ctx context.Context, t *testing.T, n int) []p2phost.Host {
36
36
return out
37
37
}
38
38
39
- type mockDHT struct {
39
+ // PubKeyFetcher implementation with a global key store
40
+ type mockKeyStore struct {
40
41
keys map [peer.ID ]ci.PubKey
41
- prov map [string ][]pstore.PeerInfo
42
42
mx sync.Mutex
43
43
}
44
44
45
- func (m * mockDHT ) getPubKey (id peer.ID ) (ci.PubKey , error ) {
45
+ func (m * mockKeyStore ) addPubKey (id peer.ID , pkey ci.PubKey ) {
46
+ m .mx .Lock ()
47
+ defer m .mx .Unlock ()
48
+ m .keys [id ] = pkey
49
+ }
50
+
51
+ func (m * mockKeyStore ) getPubKey (id peer.ID ) (ci.PubKey , error ) {
46
52
m .mx .Lock ()
47
53
defer m .mx .Unlock ()
48
54
pkey , ok := m .keys [id ]
@@ -53,86 +59,31 @@ func (m *mockDHT) getPubKey(id peer.ID) (ci.PubKey, error) {
53
59
return nil , routing .ErrNotFound
54
60
}
55
61
56
- func (m * mockDHT ) addPubKey (id peer.ID , pkey ci.PubKey ) {
57
- m .mx .Lock ()
58
- defer m .mx .Unlock ()
59
- m .keys [id ] = pkey
60
- }
61
-
62
- func (m * mockDHT ) getProviders (cid * cid.Cid ) []pstore.PeerInfo {
63
- m .mx .Lock ()
64
- defer m .mx .Unlock ()
65
- return m .prov [cid .String ()]
66
- }
67
-
68
- func (m * mockDHT ) addProvider (cid * cid.Cid , pi pstore.PeerInfo ) {
69
- m .mx .Lock ()
70
- defer m .mx .Unlock ()
71
- key := cid .String ()
72
- m .prov [key ] = append (m .prov [key ], pi )
62
+ func (m * mockKeyStore ) GetPublicKey (ctx context.Context , id peer.ID ) (ci.PubKey , error ) {
63
+ return m .getPubKey (id )
73
64
}
74
65
75
- func newMockDHT () * mockDHT {
76
- return & mockDHT {
66
+ func newMockKeyStore () * mockKeyStore {
67
+ return & mockKeyStore {
77
68
keys : make (map [peer.ID ]ci.PubKey ),
78
- prov : make (map [string ][]pstore.PeerInfo ),
79
69
}
80
70
}
81
71
82
- type mockRouting struct {
83
- pi pstore.PeerInfo
84
- dht * mockDHT
85
- }
86
-
87
- func (m * mockRouting ) Provide (ctx context.Context , cid * cid.Cid , announce bool ) error {
88
- m .dht .addProvider (cid , m .pi )
89
- return nil
90
- }
91
-
92
- func (m * mockRouting ) FindProvidersAsync (ctx context.Context , cid * cid.Cid , count int ) <- chan pstore.PeerInfo {
93
- ch := make (chan pstore.PeerInfo )
94
- go func () {
95
- defer close (ch )
96
- k := 0
97
- loop:
98
- for _ , pi := range m .dht .getProviders (cid ) {
99
- if k < count {
100
- select {
101
- case ch <- pi :
102
- k ++
103
- case <- ctx .Done ():
104
- break loop
105
- }
106
- } else {
107
- break loop
108
- }
109
- }
110
- }()
111
-
112
- return ch
113
- }
114
-
115
- func (m * mockRouting ) GetPublicKey (ctx context.Context , id peer.ID ) (ci.PubKey , error ) {
116
- return m .dht .getPubKey (id )
117
- }
118
-
119
- func newMockRouting (dht * mockDHT , host p2phost.Host ) * mockRouting {
72
+ func newMockRouting (ms mockrouting.Server , ks * mockKeyStore , host p2phost.Host ) routing.ContentRouting {
120
73
id := host .ID ()
121
74
75
+ privk := host .Peerstore ().PrivKey (id )
122
76
pubk := host .Peerstore ().PubKey (id )
123
- dht .addPubKey (id , pubk )
124
-
125
77
pi := host .Peerstore ().PeerInfo (id )
126
- return & mockRouting {
127
- pi : pi ,
128
- dht : dht ,
129
- }
78
+
79
+ ks .addPubKey (id , pubk )
80
+ return ms .Client (testutil .NewIdentity (id , pi .Addrs [0 ], privk , pubk ))
130
81
}
131
82
132
- func newMockRoutingForHosts (dht * mockDHT , hosts []p2phost.Host ) []* mockRouting {
133
- rs := make ([]* mockRouting , len (hosts ))
83
+ func newMockRoutingForHosts (ms mockrouting. Server , ks * mockKeyStore , hosts []p2phost.Host ) []routing. ContentRouting {
84
+ rs := make ([]routing. ContentRouting , len (hosts ))
134
85
for i := 0 ; i < len (hosts ); i ++ {
135
- rs [i ] = newMockRouting (dht , hosts [i ])
86
+ rs [i ] = newMockRouting (ms , ks , hosts [i ])
136
87
}
137
88
return rs
138
89
}
@@ -141,20 +92,21 @@ func TestPubsubPublishResolve(t *testing.T) {
141
92
ctx , cancel := context .WithCancel (context .Background ())
142
93
defer cancel ()
143
94
144
- dht := newMockDHT ()
95
+ ms := mockrouting .NewServer ()
96
+ ks := newMockKeyStore ()
145
97
146
98
pubhost := newNetHost (ctx , t )
147
- pubmr := newMockRouting (dht , pubhost )
99
+ pubmr := newMockRouting (ms , ks , pubhost )
148
100
pub := NewPubsubPublisher (ctx , ds .NewMapDatastore (), pubhost , pubmr , floodsub .NewFloodSub (ctx , pubhost ))
149
101
privk := pubhost .Peerstore ().PrivKey (pubhost .ID ())
150
102
151
103
name := "/ipns/" + pubhost .ID ().Pretty ()
152
104
153
105
reshosts := newNetHosts (ctx , t , 20 )
154
- resmrs := newMockRoutingForHosts (dht , reshosts )
106
+ resmrs := newMockRoutingForHosts (ms , ks , reshosts )
155
107
res := make ([]Resolver , len (reshosts ))
156
108
for i := 0 ; i < len (res ); i ++ {
157
- res [i ] = NewPubsubResolver (ctx , reshosts [i ], resmrs [i ], resmrs [ i ] , floodsub .NewFloodSub (ctx , reshosts [i ]))
109
+ res [i ] = NewPubsubResolver (ctx , reshosts [i ], resmrs [i ], ks , floodsub .NewFloodSub (ctx , reshosts [i ]))
158
110
}
159
111
160
112
time .Sleep (time .Millisecond * 100 )
@@ -171,7 +123,7 @@ func TestPubsubPublishResolve(t *testing.T) {
171
123
t .Fatal (err )
172
124
}
173
125
174
- time .Sleep (time .Second * 1 )
126
+ time .Sleep (time .Second * 3 )
175
127
for i := 0 ; i < len (res ); i ++ {
176
128
checkResolve (ctx , t , res [i ], name , val )
177
129
}
@@ -182,11 +134,10 @@ func TestPubsubPublishResolve(t *testing.T) {
182
134
t .Fatal (err )
183
135
}
184
136
185
- time .Sleep (time .Second * 1 )
137
+ time .Sleep (time .Second * 3 )
186
138
for i := 0 ; i < len (res ); i ++ {
187
139
checkResolve (ctx , t , res [i ], name , val )
188
140
}
189
-
190
141
}
191
142
192
143
func checkResolveNotFound (ctx context.Context , t * testing.T , resolver Resolver , name string ) {
0 commit comments