Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 664f595

Browse files
committed
fix: code review for tests
1 parent 23d462b commit 664f595

File tree

8 files changed

+179
-124
lines changed

8 files changed

+179
-124
lines changed

package.json

+3-11
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"execa": "^1.0.0",
7070
"form-data": "^2.3.3",
7171
"hat": "0.0.3",
72-
"interface-ipfs-core": "~0.88.0",
72+
"interface-ipfs-core": "ipfs/interface-ipfs-core#fix/ipns-over-pubsub-tests",
7373
"ipfsd-ctl": "~0.40.1",
7474
"ncp": "^2.0.0",
7575
"qs": "^6.5.2",
@@ -80,7 +80,6 @@
8080
"dependencies": {
8181
"@nodeutils/defaults-deep": "^1.1.0",
8282
"async": "^2.6.1",
83-
"base32.js": "~0.1.0",
8483
"big.js": "^5.2.2",
8584
"binary-querystring": "~0.1.2",
8685
"bl": "^2.1.2",
@@ -89,16 +88,9 @@
8988
"byteman": "^1.3.5",
9089
"cid-tool": "~0.2.0",
9190
"cids": "~0.5.5",
92-
<<<<<<< HEAD
93-
<<<<<<< HEAD
94-
"datastore-core": "~0.6.0",
95-
=======
96-
=======
9791
"class-is": "^1.1.0",
98-
"datastore-core": "~0.4.0",
99-
>>>>>>> fix: second pass of code review
100-
"datastore-pubsub": "~0.0.2",
101-
>>>>>>> feat: ipns over pubsub
92+
"datastore-core": "~0.6.0",
93+
"datastore-pubsub": "ipfs/js-datastore-pubsub#feat/encode-record-store-keys",
10294
"debug": "^4.1.0",
10395
"deep-extend": "~0.6.0",
10496
"err-code": "^1.1.2",

src/core/components/init.js

-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ const UnixFs = require('ipfs-unixfs')
1515
const IPNS = require('../ipns')
1616
const OfflineDatastore = require('../ipns/routing/offline-datastore')
1717

18-
const IPNS = require('../ipns')
19-
const OfflineDatastore = require('../ipns/routing/offline-datastore')
20-
2118
const addDefaultAssets = require('./init-assets')
2219

2320
module.exports = function init (self) {

src/core/components/pre-start.js

-11
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ const waterfall = require('async/waterfall')
77
const Keychain = require('libp2p-keychain')
88
const defaultsDeep = require('@nodeutils/defaults-deep')
99
const NoKeychain = require('./no-keychain')
10-
11-
const IPNS = require('../ipns')
12-
const OfflineDatastore = require('../ipns/routing/offline-datastore')
13-
1410
/*
1511
* Load stuff from Repo into memory
1612
*/
@@ -99,13 +95,6 @@ module.exports = function preStart (self) {
9995

10096
cb()
10197
},
102-
// Setup offline routing for IPNS.
103-
(cb) => {
104-
const offlineDatastore = new OfflineDatastore(self._repo)
105-
106-
self._ipns = new IPNS(offlineDatastore, self)
107-
cb()
108-
},
10998
(cb) => self.pin._load(cb)
11099
], callback)
111100
}

src/core/components/start.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports = (self) => {
6161

6262
// Create ipns routing with a set of datastores
6363
const routing = new TieredDatastore(ipnsStores)
64-
self._ipns = new IPNS(routing, self._repo, self._peerInfo, self._keychain, self._options, pubsubDs)
64+
self._ipns = new IPNS(routing, self._repo, self._peerInfo, self._keychain, self._options)
6565

6666
self._bitswap = new Bitswap(
6767
self._libp2pNode,

src/core/ipns/index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ const path = require('./path')
1717
const defaultRecordTtl = 60 * 1000
1818

1919
class IPNS {
20-
constructor (routing, repo, peerInfo, keychain, options, pubsub) {
20+
constructor (routing, repo, peerInfo, keychain, options) {
2121
this.publisher = new IpnsPublisher(routing, repo)
2222
this.republisher = new IpnsRepublisher(this.publisher, repo, peerInfo, keychain, options)
2323
this.resolver = new IpnsResolver(routing)
2424
this.cache = new Receptacle({ max: 1000 }) // Create an LRU cache with max 1000 items
2525
this.routing = routing
26-
this.pubsub = pubsub
2726
}
2827

2928
// Publish
@@ -72,7 +71,7 @@ class IPNS {
7271
options = options || {}
7372

7473
// If recursive, we should not try to get the cached value
75-
if (options.nocache && !options.recursive) {
74+
if (!options.nocache && !options.recursive) {
7675
// Try to get the record from cache
7776
const id = name.split('/')[2]
7877
const result = this.cache.get(id)

src/core/ipns/routing/offline-datastore.js

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
const { Key } = require('interface-datastore')
44
const { encodeBase32 } = require('./utils')
55

6+
const errcode = require('err-code')
7+
const debug = require('debug')
8+
const log = debug('jsipfs:ipns:offline-datastore')
9+
log.error = debug('jsipfs:ipns:offline-datastore:error')
10+
611
// Offline datastore aims to mimic the same encoding as routing when storing records
712
// to the local datastore
813
class OfflineDatastore {

test/cli/name-pubsub.js

+85-95
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,21 @@ describe('name-pubsub', () => {
8383

8484
// Connect
8585
before(function () {
86-
return ipfsA('swarm', 'connect', bMultiaddr).then((out) => {
87-
expect(out).to.eql(`connect ${bMultiaddr} success\n`)
88-
})
86+
return ipfsA('swarm', 'connect', bMultiaddr)
87+
.then((out) => {
88+
expect(out).to.eql(`connect ${bMultiaddr} success\n`)
89+
})
8990
})
9091

9192
after((done) => parallel(nodes.map((node) => (cb) => node.stop(cb)), done))
9293

9394
describe('pubsub commands', () => {
94-
before(function (done) {
95-
this.timeout(50 * 1000)
96-
done()
97-
})
98-
9995
it('should get enabled state of pubsub', function () {
100-
return ipfsA('name pubsub state').then((res) => {
101-
expect(res).to.exist()
102-
expect(res).to.have.string('enabled') // enabled
103-
})
96+
return ipfsA('name pubsub state')
97+
.then((res) => {
98+
expect(res).to.exist()
99+
expect(res).to.have.string('enabled') // enabled
100+
})
104101
})
105102

106103
it('should subscribe on name resolve', function () {
@@ -110,19 +107,17 @@ describe('name-pubsub', () => {
110107
.catch((err) => {
111108
expect(err).to.exist() // Not available (subscribed)
112109

113-
return Promise.all([
114-
ipfsB('pubsub ls'),
115-
ipfsB('name pubsub subs')
116-
])
117-
.then((res) => {
118-
expect(res).to.exist()
119-
120-
expect(res[0]).to.exist()
121-
expect(res[0]).to.have.string('/ipns/') // have an ipns subscribtion
110+
return ipfsB('pubsub ls')
111+
})
112+
.then((res) => {
113+
expect(res).to.exist()
114+
expect(res).to.have.string('/record/') // have a record ipns subscribtion
122115

123-
expect(res[1]).to.exist()
124-
expect(res[1]).to.have.string(`/ipns/${nodeAId.id}`) // have subscription
125-
})
116+
return ipfsB('name pubsub subs')
117+
})
118+
.then((res) => {
119+
expect(res).to.exist()
120+
expect(res).to.have.string(`/ipns/${nodeAId.id}`) // have subscription
126121
})
127122
})
128123

@@ -135,29 +130,27 @@ describe('name-pubsub', () => {
135130
expect(res).to.have.string('no subscription') // tried to cancel a not yet subscribed id
136131

137132
return ipfsA(`name resolve ${nodeBId.id}`)
138-
.catch((err) => {
139-
expect(err).to.exist() // Not available (subscribed now)
140-
141-
return ipfsA(`name pubsub cancel /ipns/${nodeBId.id}`)
142-
.then((res) => {
143-
expect(res).to.exist()
144-
expect(res).to.have.string('canceled') // canceled now
145-
146-
return Promise.all([
147-
ipfsA('pubsub ls'),
148-
ipfsA('name pubsub subs')
149-
])
150-
.then((res) => {
151-
expect(res).to.exist()
152-
153-
expect(res[0]).to.exist()
154-
expect(res[0]).to.not.have.string('/ipns/') // ipns subscribtion not available
155-
156-
expect(res[1]).to.exist()
157-
expect(res[1]).to.not.have.string(`/ipns/${nodeBId.id}`) // ipns subscribtion not available
158-
})
159-
})
160-
})
133+
})
134+
.catch((err) => {
135+
expect(err).to.exist() // Not available (subscribed now)
136+
137+
return ipfsA(`name pubsub cancel /ipns/${nodeBId.id}`)
138+
})
139+
.then((res) => {
140+
expect(res).to.exist()
141+
expect(res).to.have.string('canceled') // canceled now
142+
143+
return ipfsA('pubsub ls')
144+
})
145+
.then((res) => {
146+
expect(res).to.exist()
147+
expect(res).to.not.have.string('/ipns/') // ipns subscribtion not available
148+
149+
return ipfsA('name pubsub subs')
150+
})
151+
.then((res) => {
152+
expect(res).to.exist()
153+
expect(res).to.not.have.string(`/ipns/${nodeBId.id}`) // ipns subscribtion not available
161154
})
162155
})
163156
})
@@ -167,10 +160,11 @@ describe('name-pubsub', () => {
167160

168161
before(function (done) {
169162
this.timeout(50 * 1000)
170-
ipfsA('add src/init-files/init-docs/readme').then((out) => {
171-
cidAdded = out.split(' ')[1]
172-
done()
173-
})
163+
ipfsA('add src/init-files/init-docs/readme')
164+
.then((out) => {
165+
cidAdded = out.split(' ')[1]
166+
done()
167+
})
174168
})
175169

176170
it('should publish the received record to the subscriber', function () {
@@ -182,30 +176,28 @@ describe('name-pubsub', () => {
182176
expect(res).to.satisfy(checkAll([emptyDirCid])) // Empty dir received (subscribed)
183177

184178
return ipfsA(`name resolve ${nodeBId.id}`)
185-
.catch((err) => {
186-
expect(err).to.exist() // Not available (subscribed now)
187-
188-
return ipfsB(`name publish ${cidAdded}`)
189-
.then((res) => {
190-
// published to IpfsB and published through pubsub to ipfsa
191-
expect(res).to.exist()
192-
expect(res).to.satisfy(checkAll([cidAdded, nodeBId.id]))
193-
194-
return Promise.all([
195-
ipfsB(`name resolve ${nodeBId.id}`),
196-
ipfsA(`name resolve ${nodeBId.id}`)
197-
])
198-
.then((res) => {
199-
expect(res).to.exist()
200-
201-
expect(res[0]).to.exist()
202-
expect(res[0]).to.satisfy(checkAll([cidAdded]))
203-
204-
expect(res[1]).to.exist()
205-
expect(res[1]).to.satisfy(checkAll([cidAdded])) // value propagated to node B
206-
})
207-
})
208-
})
179+
})
180+
.catch((err) => {
181+
expect(err).to.exist() // Not available (subscribed now)
182+
183+
return ipfsB(`name publish ${cidAdded}`)
184+
})
185+
.then((res) => {
186+
// published to IpfsB and published through pubsub to ipfsa
187+
expect(res).to.exist()
188+
expect(res).to.satisfy(checkAll([cidAdded, nodeBId.id]))
189+
190+
return ipfsB(`name resolve ${nodeBId.id}`)
191+
})
192+
.then((res) => {
193+
expect(res).to.exist()
194+
expect(res).to.satisfy(checkAll([cidAdded]))
195+
196+
return ipfsA(`name resolve ${nodeBId.id}`)
197+
})
198+
.then((res) => {
199+
expect(res).to.exist()
200+
expect(res).to.satisfy(checkAll([cidAdded])) // value propagated to node B
209201
})
210202
})
211203
})
@@ -235,30 +227,28 @@ describe('name-pubsub', () => {
235227

236228
after((done) => parallel(nodes.map((node) => (cb) => node.stop(cb)), done))
237229

238-
it('should get disabled state of pubsub', function (done) {
239-
ipfsA('name pubsub state').then((res) => {
240-
expect(res).to.exist()
241-
expect(res).to.have.string('disabled')
242-
243-
done()
244-
})
230+
it('should get disabled state of pubsub', function () {
231+
return ipfsA('name pubsub state')
232+
.then((res) => {
233+
expect(res).to.exist()
234+
expect(res).to.have.string('disabled')
235+
})
245236
})
246237

247-
it('should get error getting the available subscriptions', function (done) {
248-
ipfsA('name pubsub subs').catch((err) => {
249-
expect(err).to.exist() // error as it is disabled
250-
expect(err.toString()).to.have.string('IPNS pubsub subsystem is not enabled')
251-
done()
252-
})
238+
it('should get error getting the available subscriptions', function () {
239+
return ipfsA('name pubsub subs')
240+
.catch((err) => {
241+
expect(err).to.exist() // error as it is disabled
242+
expect(err.toString()).to.have.string('IPNS pubsub subsystem is not enabled')
243+
})
253244
})
254245

255-
it('should get error canceling a subscription', function (done) {
256-
ipfsA('name pubsub cancel /ipns/QmSWxaPcGgf4TDnFEBDWz2JnbHywF14phmY9hNcAeBEK5v').catch((err) => {
257-
expect(err).to.exist() // error as it is disabled
258-
expect(err.toString()).to.have.string('IPNS pubsub subsystem is not enabled')
259-
260-
done()
261-
})
246+
it('should get error canceling a subscription', function () {
247+
return ipfsA('name pubsub cancel /ipns/QmSWxaPcGgf4TDnFEBDWz2JnbHywF14phmY9hNcAeBEK5v')
248+
.catch((err) => {
249+
expect(err).to.exist() // error as it is disabled
250+
expect(err.toString()).to.have.string('IPNS pubsub subsystem is not enabled')
251+
})
262252
})
263253
})
264254
})

0 commit comments

Comments
 (0)