Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 69c5a29

Browse files
committed
feat: support for reconstructed CIDs of refs-local
Related to ipfs/js-ipfs#2415
1 parent e836aea commit 69c5a29

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

SPEC/REFS.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,12 @@ pull(
102102

103103
#### `refs.local`
104104

105-
> Output all local references (CIDs of all blocks in the blockstore)
105+
> Output all local references (CIDs of all blocks in the blockstore. CIDs are reconstructed, hence they might not match the CIDs under the blocks were originally stored)
106106
107-
##### `ipfs.refs.local([callback])`
107+
##### `ipfs.refs.local([options], [callback])`
108+
109+
`options` is an optional object that may contain the following keys:
110+
- `multihash (false)`: instead of reconstructed CIDs, the original multihashes are returned as base32 encoded strings
108111

109112
`callback` must follow `function (err, refs) {}` signature, where `err` is an error if the operation was not successful and `refs` is an array of `{ ref: "myref", err: "error msg" }`
110113

@@ -129,10 +132,13 @@ ipfs.refs.local(function (err, refs) {
129132
})
130133
```
131134

132-
#### `refs.localReadableStream`
135+
#### `refs.localReadableStream([options])`
133136

134137
> Output all local references using a [Readable Stream][rs]
135138
139+
`options` is an optional object that may contain the following keys:
140+
- `multihash (false)`: instead of reconstructed CIDs, the original multihashes are returned as base32 encoded strings
141+
136142
##### `ipfs.localReadableStream()` -> [Readable Stream][rs]
137143

138144
**Example:**
@@ -148,10 +154,13 @@ stream.on('data', function (ref) {
148154
})
149155
```
150156

151-
#### `refs.localPullStream`
157+
#### `refs.localPullStream([options])`
152158

153159
> Output all local references using a [Pull Stream][ps].
154160
161+
`options` is an optional object that may contain the following keys:
162+
- `multihash (false)`: instead of reconstructed CIDs, the original multihashes are returned as base32 encoded strings
163+
155164
##### `ipfs.refs.localReadableStream()` -> [Pull Stream][ps]
156165

157166
**Example:**

src/files-regular/refs-local-tests.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ module.exports = (createCommon, suiteName, ipfsRefsLocal, options) => {
4949
expect(err).to.not.exist()
5050

5151
const cids = refs.map(r => r.ref)
52-
expect(cids).to.include('QmVwdDCY4SPGVFnNCiZnX5CtzwWDn6kAM98JXzKxE3kCmn')
53-
expect(cids).to.include('QmR4nFjTu18TyANgC65ArNWp5Yaab1gPzQ4D8zp7Kx3vhr')
52+
expect(cids).to.include('bafkreicuinkdxczmxol5edpb2jumkbkvtoehj6qixz6yvvxgstp3cr5hey')
53+
expect(cids).to.include('bafkreigm5vpfwjayhkmp7d3gc6hwj4c536ns6ajxi3cyi3uulta45rpyzy')
5454

5555
done()
5656
})

src/repo/gc.js

+30-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
const { getDescribe, getIt, expect } = require('../utils/mocha')
55
const { DAGNode } = require('ipld-dag-pb')
6+
const CID = require('cids')
7+
8+
function cidV0ToV1Raw (hash) {
9+
const multihash = new CID(hash).multihash
10+
return new CID(1, 'raw', multihash).toString()
11+
}
612

713
module.exports = (createCommon, options) => {
814
const describe = getDescribe(options)
@@ -52,20 +58,21 @@ module.exports = (createCommon, options) => {
5258
// information that refers to the blocks
5359
const addRes = await ipfs.add(Buffer.from('apples'))
5460
const hash = addRes[0].hash
61+
const cidV1 = cidV0ToV1Raw(hash)
5562

5663
// Get the list of local blocks after the add, should be bigger than
5764
// the initial list and contain hash
5865
const refsAfterAdd = await ipfs.refs.local()
5966
expect(refsAfterAdd.length).to.be.gt(refsBeforeAdd.length)
60-
expect(refsAfterAdd.map(r => r.ref)).includes(hash)
67+
expect(refsAfterAdd.map(r => r.ref)).includes(cidV1)
6168

6269
// Run garbage collection
6370
await ipfs.repo.gc()
6471

6572
// Get the list of local blocks after GC, should still contain the hash,
6673
// because the file is still pinned
6774
const refsAfterGc = await ipfs.refs.local()
68-
expect(refsAfterGc.map(r => r.ref)).includes(hash)
75+
expect(refsAfterGc.map(r => r.ref)).includes(cidV1)
6976

7077
// Unpin the data
7178
await ipfs.pin.rm(hash)
@@ -75,7 +82,7 @@ module.exports = (createCommon, options) => {
7582

7683
// The list of local blocks should no longer contain the hash
7784
const refsAfterUnpinAndGc = await ipfs.refs.local()
78-
expect(refsAfterUnpinAndGc.map(r => r.ref)).not.includes(hash)
85+
expect(refsAfterUnpinAndGc.map(r => r.ref)).not.includes(cidV1)
7986
})
8087

8188
it('should clean up removed MFS files', async () => {
@@ -86,21 +93,21 @@ module.exports = (createCommon, options) => {
8693
await ipfs.files.write('/test', Buffer.from('oranges'), { create: true })
8794
const stats = await ipfs.files.stat('/test')
8895
expect(stats.type).to.equal('file')
89-
const hash = stats.hash
96+
const cidV1 = cidV0ToV1Raw(stats.hash)
9097

9198
// Get the list of local blocks after the add, should be bigger than
9299
// the initial list and contain hash
93100
const refsAfterAdd = await ipfs.refs.local()
94101
expect(refsAfterAdd.length).to.be.gt(refsBeforeAdd.length)
95-
expect(refsAfterAdd.map(r => r.ref)).includes(hash)
102+
expect(refsAfterAdd.map(r => r.ref)).includes(cidV1)
96103

97104
// Run garbage collection
98105
await ipfs.repo.gc()
99106

100107
// Get the list of local blocks after GC, should still contain the hash,
101108
// because the file is in MFS
102109
const refsAfterGc = await ipfs.refs.local()
103-
expect(refsAfterGc.map(r => r.ref)).includes(hash)
110+
expect(refsAfterGc.map(r => r.ref)).includes(cidV1)
104111

105112
// Remove the file
106113
await ipfs.files.rm('/test')
@@ -110,7 +117,7 @@ module.exports = (createCommon, options) => {
110117

111118
// The list of local blocks should no longer contain the hash
112119
const refsAfterUnpinAndGc = await ipfs.refs.local()
113-
expect(refsAfterUnpinAndGc.map(r => r.ref)).not.includes(hash)
120+
expect(refsAfterUnpinAndGc.map(r => r.ref)).not.includes(cidV1)
114121
})
115122

116123
it('should clean up block only after unpinned and removed from MFS', async () => {
@@ -121,21 +128,22 @@ module.exports = (createCommon, options) => {
121128
await ipfs.files.write('/test', Buffer.from('peaches'), { create: true })
122129
const stats = await ipfs.files.stat('/test')
123130
expect(stats.type).to.equal('file')
124-
const mfsFileHash = stats.hash
131+
const mfsFileCidV1 = cidV0ToV1Raw(stats.hash)
125132

126133
// Get the CID of the data in the file
127-
const block = await ipfs.block.get(mfsFileHash)
134+
const block = await ipfs.block.get(mfsFileCidV1)
128135

129136
// Add the data to IPFS (which implicitly pins the data)
130137
const addRes = await ipfs.add(block.data)
131138
const dataHash = addRes[0].hash
139+
const dataCidV1 = cidV0ToV1Raw(dataHash)
132140

133141
// Get the list of local blocks after the add, should be bigger than
134142
// the initial list and contain the data hash
135143
const refsAfterAdd = await ipfs.refs.local()
136144
expect(refsAfterAdd.length).to.be.gt(refsBeforeAdd.length)
137145
const hashesAfterAdd = refsAfterAdd.map(r => r.ref)
138-
expect(hashesAfterAdd).includes(dataHash)
146+
expect(hashesAfterAdd).includes(dataCidV1)
139147

140148
// Run garbage collection
141149
await ipfs.repo.gc()
@@ -144,7 +152,7 @@ module.exports = (createCommon, options) => {
144152
// because the file is pinned and in MFS
145153
const refsAfterGc = await ipfs.refs.local()
146154
const hashesAfterGc = refsAfterGc.map(r => r.ref)
147-
expect(hashesAfterGc).includes(dataHash)
155+
expect(hashesAfterGc).includes(dataCidV1)
148156

149157
// Remove the file
150158
await ipfs.files.rm('/test')
@@ -156,8 +164,8 @@ module.exports = (createCommon, options) => {
156164
// because the file is still pinned
157165
const refsAfterRmAndGc = await ipfs.refs.local()
158166
const hashesAfterRmAndGc = refsAfterRmAndGc.map(r => r.ref)
159-
expect(hashesAfterRmAndGc).not.includes(mfsFileHash)
160-
expect(hashesAfterRmAndGc).includes(dataHash)
167+
expect(hashesAfterRmAndGc).not.includes(mfsFileCidV1)
168+
expect(hashesAfterRmAndGc).includes(dataCidV1)
161169

162170
// Unpin the data
163171
await ipfs.pin.rm(dataHash)
@@ -168,8 +176,8 @@ module.exports = (createCommon, options) => {
168176
// The list of local blocks should no longer contain the hashes
169177
const refsAfterUnpinAndGc = await ipfs.refs.local()
170178
const hashesAfterUnpinAndGc = refsAfterUnpinAndGc.map(r => r.ref)
171-
expect(hashesAfterUnpinAndGc).not.includes(mfsFileHash)
172-
expect(hashesAfterUnpinAndGc).not.includes(dataHash)
179+
expect(hashesAfterUnpinAndGc).not.includes(mfsFileCidV1)
180+
expect(hashesAfterUnpinAndGc).not.includes(dataCidV1)
173181
})
174182

175183
it('should clean up indirectly pinned data after recursive pin removal', async () => {
@@ -179,6 +187,7 @@ module.exports = (createCommon, options) => {
179187
// Add some data
180188
const addRes = await ipfs.add(Buffer.from('pears'))
181189
const dataHash = addRes[0].hash
190+
const dataHashCidV1 = cidV0ToV1Raw(dataHash)
182191

183192
// Unpin the data
184193
await ipfs.pin.rm(dataHash)
@@ -192,6 +201,7 @@ module.exports = (createCommon, options) => {
192201

193202
// Put the object into IPFS
194203
const objHash = (await ipfs.object.put(obj)).toString()
204+
const objCidV1 = cidV0ToV1Raw(objHash)
195205

196206
// Putting an object doesn't pin it
197207
expect((await ipfs.pin.ls()).map(p => p.hash)).not.includes(objHash)
@@ -201,8 +211,8 @@ module.exports = (createCommon, options) => {
201211
const refsAfterAdd = await ipfs.refs.local()
202212
expect(refsAfterAdd.length).to.be.gt(refsBeforeAdd.length)
203213
const hashesAfterAdd = refsAfterAdd.map(r => r.ref)
204-
expect(hashesAfterAdd).includes(objHash)
205-
expect(hashesAfterAdd).includes(dataHash)
214+
expect(hashesAfterAdd).includes(objCidV1)
215+
expect(hashesAfterAdd).includes(dataHashCidV1)
206216

207217
// Recursively pin the object
208218
await ipfs.pin.add(objHash, { recursive: true })
@@ -217,7 +227,7 @@ module.exports = (createCommon, options) => {
217227
// Get the list of local blocks after GC, should still contain the data
218228
// hash, because the data is still (indirectly) pinned
219229
const refsAfterGc = await ipfs.refs.local()
220-
expect(refsAfterGc.map(r => r.ref)).includes(dataHash)
230+
expect(refsAfterGc.map(r => r.ref)).includes(dataHashCidV1)
221231

222232
// Recursively unpin the object
223233
await ipfs.pin.rm(objHash)
@@ -228,8 +238,8 @@ module.exports = (createCommon, options) => {
228238
// The list of local blocks should no longer contain the hashes
229239
const refsAfterUnpinAndGc = await ipfs.refs.local()
230240
const hashesAfterUnpinAndGc = refsAfterUnpinAndGc.map(r => r.ref)
231-
expect(hashesAfterUnpinAndGc).not.includes(objHash)
232-
expect(hashesAfterUnpinAndGc).not.includes(dataHash)
241+
expect(hashesAfterUnpinAndGc).not.includes(objCidV1)
242+
expect(hashesAfterUnpinAndGc).not.includes(dataHashCidV1)
233243
})
234244
})
235245
}

0 commit comments

Comments
 (0)