This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathgc.js
228 lines (175 loc) · 7.08 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
/* eslint-env mocha */
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import all from 'it-all'
import drain from 'it-drain'
import { CID } from 'multiformats/cid'
import { base64 } from 'multiformats/bases/base64'
/**
* @param {import('ipfs-core-types').IPFS} ipfs
*/
async function getBaseEncodedMultihashes (ipfs) {
const refs = await all(ipfs.refs.local())
return refs.map(r => base64.encode(CID.parse(r.ref).multihash.bytes))
}
/**
* @param {import('ipfs-core-types').IPFS} ipfs
* @param {CID} cid
*/
async function shouldHaveRef (ipfs, cid) {
return expect(getBaseEncodedMultihashes(ipfs)).to.eventually.include(base64.encode(cid.multihash.bytes))
}
/**
* @param {import('ipfs-core-types').IPFS} ipfs
* @param {CID} cid
*/
async function shouldNotHaveRef (ipfs, cid) {
return expect(getBaseEncodedMultihashes(ipfs)).to.eventually.not.include(base64.encode(cid.multihash.bytes))
}
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {object} options
*/
export function testGc (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.repo.gc', () => {
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async () => {
ipfs = (await factory.spawn()).api
})
after(() => factory.clean())
it('should run garbage collection', async () => {
const res = await ipfs.add(uint8ArrayFromString('apples'))
const pinset = await all(ipfs.pin.ls())
expect(pinset.map(obj => obj.cid.toString())).includes(res.cid.toString())
await ipfs.pin.rm(res.cid)
await all(ipfs.repo.gc())
const finalPinset = await all(ipfs.pin.ls())
expect(finalPinset.map(obj => obj.cid.toString())).not.includes(res.cid.toString())
})
it('should clean up unpinned data', async () => {
// 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(uint8ArrayFromString('apples'))
const cid = addRes.cid
// Get the list of local blocks after the add, should be bigger than
// the initial list and contain hash
await shouldHaveRef(ipfs, cid)
// Run garbage collection
await drain(ipfs.repo.gc())
// Get the list of local blocks after GC, should still contain the hash,
// because the file is still pinned
await shouldHaveRef(ipfs, cid)
// Unpin the data
await ipfs.pin.rm(cid)
// Run garbage collection
await all(ipfs.repo.gc())
// The list of local blocks should no longer contain the hash
await shouldNotHaveRef(ipfs, cid)
})
it('should clean up removed MFS files', async () => {
// Add a file to MFS
await ipfs.files.write('/test', uint8ArrayFromString('oranges'), { create: true })
const stats = await ipfs.files.stat('/test')
expect(stats.type).to.equal('file')
// Get the list of local blocks after the add, should be bigger than
// the initial list and contain hash
await shouldHaveRef(ipfs, stats.cid)
// Run garbage collection
await drain(ipfs.repo.gc())
// Get the list of local blocks after GC, should still contain the hash,
// because the file is in MFS
await shouldHaveRef(ipfs, stats.cid)
// Remove the file
await ipfs.files.rm('/test')
// Run garbage collection
await drain(ipfs.repo.gc())
// The list of local blocks should no longer contain the hash
await shouldNotHaveRef(ipfs, stats.cid)
})
it('should clean up block only after unpinned and removed from MFS', async () => {
// Add a file to MFS
await ipfs.files.write('/test', uint8ArrayFromString('peaches'), { create: true })
const stats = await ipfs.files.stat('/test')
expect(stats.type).to.equal('file')
const mfsFileCid = stats.cid
// Get the CID of the data in the file
const block = await ipfs.block.get(mfsFileCid)
// Add the data to IPFS (which implicitly pins the data)
const addRes = await ipfs.add(block)
const dataCid = addRes.cid
// Get the list of local blocks after the add, should be bigger than
// the initial list and contain the data hash
await shouldHaveRef(ipfs, dataCid)
// Run garbage collection
await drain(ipfs.repo.gc())
// Get the list of local blocks after GC, should still contain the hash,
// because the file is pinned and in MFS
await shouldHaveRef(ipfs, dataCid)
// Remove the file
await ipfs.files.rm('/test')
// Run garbage collection
await drain(ipfs.repo.gc())
// Get the list of local blocks after GC, should still contain the hash,
// because the file is still pinned
await shouldNotHaveRef(ipfs, mfsFileCid)
await shouldHaveRef(ipfs, dataCid)
// Unpin the data
await ipfs.pin.rm(dataCid)
// Run garbage collection
await drain(ipfs.repo.gc())
// The list of local blocks should no longer contain the hashes
await shouldNotHaveRef(ipfs, mfsFileCid)
await shouldNotHaveRef(ipfs, dataCid)
})
it('should clean up indirectly pinned data after recursive pin removal', async () => {
// Add some data
const addRes = await ipfs.add(uint8ArrayFromString('pears'))
const dataCid = addRes.cid
// Unpin the data
await ipfs.pin.rm(dataCid)
// Create a link to the data from an object
const obj = {
Data: uint8ArrayFromString('fruit'),
Links: [{
Name: 'p',
Hash: dataCid,
Tsize: addRes.size
}]
}
// Put the object into IPFS
const objCid = await ipfs.object.put(obj)
// Putting an object doesn't pin it
expect((await all(ipfs.pin.ls())).map(p => p.cid.toString())).not.includes(objCid.toString())
// Get the list of local blocks after the add, should be bigger than
// the initial list and contain data and object hash
await shouldHaveRef(ipfs, objCid)
await shouldHaveRef(ipfs, dataCid)
// Recursively pin the object
await ipfs.pin.add(objCid, { recursive: true })
// The data should now be indirectly pinned
const pins = await all(ipfs.pin.ls())
expect(pins.find(p => p.cid.toString() === dataCid.toString())).to.have.property('type', 'indirect')
// Run garbage collection
await drain(ipfs.repo.gc())
// Get the list of local blocks after GC, should still contain the data
// hash, because the data is still (indirectly) pinned
await shouldHaveRef(ipfs, objCid)
await shouldHaveRef(ipfs, dataCid)
// Recursively unpin the object
await ipfs.pin.rm(objCid.toString())
// Run garbage collection
await drain(ipfs.repo.gc())
// The list of local blocks should no longer contain the hashes
await shouldNotHaveRef(ipfs, objCid)
await shouldNotHaveRef(ipfs, dataCid)
})
})
}