This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathgc.js
245 lines (193 loc) · 8.43 KB
/
gc.js
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
/* eslint-env mocha */
'use strict'
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { DAGNode } = require('ipld-dag-pb')
const CID = require('cids')
function cidV0ToV1Raw (hash) {
const multihash = new CID(hash).multihash
return new CID(1, 'raw', multihash).toString()
}
module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()
describe('.repo.gc', () => {
let ipfs
before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)
common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})
after((done) => common.teardown(done))
it('should run garbage collection', (done) => {
ipfs.repo.gc((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
done()
})
})
it('should run garbage collection (promised)', () => {
return ipfs.repo.gc().then((res) => {
expect(res).to.exist()
})
})
it('should clean up unpinned data', async () => {
// Get initial list of local blocks
const refsBeforeAdd = await ipfs.refs.local()
// Add some data. Note: this will implicitly pin the data, which causes
// some blocks to be added for the data itself and for the pinning
// information that refers to the blocks
const addRes = await ipfs.add(Buffer.from('apples'))
const hash = addRes[0].hash
const cidV1 = cidV0ToV1Raw(hash)
// Get the list of local blocks after the add, should be bigger than
// the initial list and contain hash
const refsAfterAdd = await ipfs.refs.local()
expect(refsAfterAdd.length).to.be.gt(refsBeforeAdd.length)
expect(refsAfterAdd.map(r => r.ref)).includes(cidV1)
// Run garbage collection
await ipfs.repo.gc()
// Get the list of local blocks after GC, should still contain the hash,
// because the file is still pinned
const refsAfterGc = await ipfs.refs.local()
expect(refsAfterGc.map(r => r.ref)).includes(cidV1)
// Unpin the data
await ipfs.pin.rm(hash)
// Run garbage collection
await ipfs.repo.gc()
// The list of local blocks should no longer contain the hash
const refsAfterUnpinAndGc = await ipfs.refs.local()
expect(refsAfterUnpinAndGc.map(r => r.ref)).not.includes(cidV1)
})
it('should clean up removed MFS files', async () => {
// Get initial list of local blocks
const refsBeforeAdd = await ipfs.refs.local()
// Add a file to MFS
await ipfs.files.write('/test', Buffer.from('oranges'), { create: true })
const stats = await ipfs.files.stat('/test')
expect(stats.type).to.equal('file')
const cidV1 = cidV0ToV1Raw(stats.hash)
// Get the list of local blocks after the add, should be bigger than
// the initial list and contain hash
const refsAfterAdd = await ipfs.refs.local()
expect(refsAfterAdd.length).to.be.gt(refsBeforeAdd.length)
expect(refsAfterAdd.map(r => r.ref)).includes(cidV1)
// Run garbage collection
await ipfs.repo.gc()
// Get the list of local blocks after GC, should still contain the hash,
// because the file is in MFS
const refsAfterGc = await ipfs.refs.local()
expect(refsAfterGc.map(r => r.ref)).includes(cidV1)
// Remove the file
await ipfs.files.rm('/test')
// Run garbage collection
await ipfs.repo.gc()
// The list of local blocks should no longer contain the hash
const refsAfterUnpinAndGc = await ipfs.refs.local()
expect(refsAfterUnpinAndGc.map(r => r.ref)).not.includes(cidV1)
})
it('should clean up block only after unpinned and removed from MFS', async () => {
// Get initial list of local blocks
const refsBeforeAdd = await ipfs.refs.local()
// Add a file to MFS
await ipfs.files.write('/test', Buffer.from('peaches'), { create: true })
const stats = await ipfs.files.stat('/test')
expect(stats.type).to.equal('file')
const mfsFileCidV1 = cidV0ToV1Raw(stats.hash)
// Get the CID of the data in the file
const block = await ipfs.block.get(mfsFileCidV1)
// Add the data to IPFS (which implicitly pins the data)
const addRes = await ipfs.add(block.data)
const dataHash = addRes[0].hash
const dataCidV1 = cidV0ToV1Raw(dataHash)
// Get the list of local blocks after the add, should be bigger than
// the initial list and contain the data hash
const refsAfterAdd = await ipfs.refs.local()
expect(refsAfterAdd.length).to.be.gt(refsBeforeAdd.length)
const hashesAfterAdd = refsAfterAdd.map(r => r.ref)
expect(hashesAfterAdd).includes(dataCidV1)
// Run garbage collection
await ipfs.repo.gc()
// Get the list of local blocks after GC, should still contain the hash,
// because the file is pinned and in MFS
const refsAfterGc = await ipfs.refs.local()
const hashesAfterGc = refsAfterGc.map(r => r.ref)
expect(hashesAfterGc).includes(dataCidV1)
// Remove the file
await ipfs.files.rm('/test')
// Run garbage collection
await ipfs.repo.gc()
// Get the list of local blocks after GC, should still contain the hash,
// because the file is still pinned
const refsAfterRmAndGc = await ipfs.refs.local()
const hashesAfterRmAndGc = refsAfterRmAndGc.map(r => r.ref)
expect(hashesAfterRmAndGc).not.includes(mfsFileCidV1)
expect(hashesAfterRmAndGc).includes(dataCidV1)
// Unpin the data
await ipfs.pin.rm(dataHash)
// Run garbage collection
await ipfs.repo.gc()
// The list of local blocks should no longer contain the hashes
const refsAfterUnpinAndGc = await ipfs.refs.local()
const hashesAfterUnpinAndGc = refsAfterUnpinAndGc.map(r => r.ref)
expect(hashesAfterUnpinAndGc).not.includes(mfsFileCidV1)
expect(hashesAfterUnpinAndGc).not.includes(dataCidV1)
})
it('should clean up indirectly pinned data after recursive pin removal', async () => {
// Get initial list of local blocks
const refsBeforeAdd = await ipfs.refs.local()
// Add some data
const addRes = await ipfs.add(Buffer.from('pears'))
const dataHash = addRes[0].hash
const dataHashCidV1 = cidV0ToV1Raw(dataHash)
// Unpin the data
await ipfs.pin.rm(dataHash)
// Create a link to the data from an object
const obj = await new DAGNode(Buffer.from('fruit'), [{
Name: 'p',
Hash: dataHash,
TSize: addRes[0].size
}])
// Put the object into IPFS
const objHash = (await ipfs.object.put(obj)).toString()
const objCidV1 = cidV0ToV1Raw(objHash)
// Putting an object doesn't pin it
expect((await ipfs.pin.ls()).map(p => p.hash)).not.includes(objHash)
// Get the list of local blocks after the add, should be bigger than
// the initial list and contain data and object hash
const refsAfterAdd = await ipfs.refs.local()
expect(refsAfterAdd.length).to.be.gt(refsBeforeAdd.length)
const hashesAfterAdd = refsAfterAdd.map(r => r.ref)
expect(hashesAfterAdd).includes(objCidV1)
expect(hashesAfterAdd).includes(dataHashCidV1)
// Recursively pin the object
await ipfs.pin.add(objHash, { recursive: true })
// The data should now be indirectly pinned
const pins = await ipfs.pin.ls()
expect(pins.find(p => p.hash === dataHash).type).to.eql('indirect')
// Run garbage collection
await ipfs.repo.gc()
// Get the list of local blocks after GC, should still contain the data
// hash, because the data is still (indirectly) pinned
const refsAfterGc = await ipfs.refs.local()
expect(refsAfterGc.map(r => r.ref)).includes(dataHashCidV1)
// Recursively unpin the object
await ipfs.pin.rm(objHash)
// Run garbage collection
await ipfs.repo.gc()
// The list of local blocks should no longer contain the hashes
const refsAfterUnpinAndGc = await ipfs.refs.local()
const hashesAfterUnpinAndGc = refsAfterUnpinAndGc.map(r => r.ref)
expect(hashesAfterUnpinAndGc).not.includes(objCidV1)
expect(hashesAfterUnpinAndGc).not.includes(dataHashCidV1)
})
})
}