@@ -7,6 +7,8 @@ const IPFSFactory = require('ipfsd-ctl')
7
7
const pEvent = require ( 'p-event' )
8
8
const env = require ( 'ipfs-utils/src/env' )
9
9
const IPFS = require ( '../../src/core' )
10
+ const CID = require ( 'cids' )
11
+ const base32 = require ( 'base32.js' )
10
12
const { Errors } = require ( 'interface-datastore' )
11
13
12
14
// We need to detect when a readLock or writeLock is requested for the tests
@@ -90,17 +92,17 @@ describe('gc', function () {
90
92
name : 'add' ,
91
93
add1 : ( ) => ipfs . add ( fixtures [ 0 ] , { pin : false } ) ,
92
94
add2 : ( ) => ipfs . add ( fixtures [ 1 ] , { pin : false } ) ,
93
- resToCid : ( res ) => res [ 0 ] . hash
95
+ resToMultihash : ( res ) => base32 . encode ( new CID ( res [ 0 ] . hash ) . multihash )
94
96
} , {
95
97
name : 'object put' ,
96
98
add1 : ( ) => ipfs . object . put ( { Data : 'obj put 1' , Links : [ ] } ) ,
97
99
add2 : ( ) => ipfs . object . put ( { Data : 'obj put 2' , Links : [ ] } ) ,
98
- resToCid : ( res ) => res . toString ( )
100
+ resToMultihash : ( res ) => base32 . encode ( res . multihash )
99
101
} , {
100
102
name : 'block put' ,
101
103
add1 : ( ) => ipfs . block . put ( Buffer . from ( 'block put 1' ) , null ) ,
102
104
add2 : ( ) => ipfs . block . put ( Buffer . from ( 'block put 2' ) , null ) ,
103
- resToCid : ( res ) => res . cid . toString ( )
105
+ resToMultihash : ( res ) => base32 . encode ( res . cid . multihash )
104
106
} ]
105
107
106
108
describe ( 'locks' , function ( ) {
@@ -122,9 +124,9 @@ describe('gc', function () {
122
124
await gcStarted
123
125
const add2 = test . add2 ( )
124
126
125
- const deleted = ( await gc ) . map ( i => i . cid . toString ( ) )
126
- const add1Res = test . resToCid ( await add1 )
127
- const add2Res = test . resToCid ( await add2 )
127
+ const deleted = ( await gc ) . map ( i => i . multihash )
128
+ const add1Res = test . resToMultihash ( await add1 )
129
+ const add2Res = test . resToMultihash ( await add2 )
128
130
129
131
// Should have garbage collected blocks from first add, because GC should
130
132
// have waited for first add to finish
@@ -152,9 +154,9 @@ describe('gc', function () {
152
154
await gcStarted
153
155
const add2 = ipfs . add ( fixtures [ 3 ] , { pin : true } )
154
156
155
- const deleted = ( await gc ) . map ( i => i . cid . toString ( ) )
156
- const add1Res = ( await add1 ) [ 0 ] . hash
157
- const add2Res = ( await add2 ) [ 0 ] . hash
157
+ const deleted = ( await gc ) . map ( i => i . multihash )
158
+ const add1Res = base32 . encode ( new CID ( ( await add1 ) [ 0 ] . hash ) . multihash )
159
+ const add2Res = base32 . encode ( new CID ( ( await add2 ) [ 0 ] . hash ) . multihash )
158
160
159
161
// Should not have garbage collected blocks from first add, because GC should
160
162
// have waited for first add + pin to finish (protected by pin)
@@ -168,7 +170,9 @@ describe('gc', function () {
168
170
it ( 'garbage collection should wait for pending block rm to finish' , async ( ) => {
169
171
// Add two blocks so that we can remove them
170
172
const cid1 = ( await ipfs . block . put ( Buffer . from ( 'block to rm 1' ) , null ) ) . cid
173
+ const cid1Multihash = base32 . encode ( cid1 . multihash )
171
174
const cid2 = ( await ipfs . block . put ( Buffer . from ( 'block to rm 2' ) , null ) ) . cid
175
+ const cid2Multihash = base32 . encode ( cid2 . multihash )
172
176
173
177
// Remove first block from IPFS
174
178
// Note: block rm will take a write lock
@@ -185,33 +189,34 @@ describe('gc', function () {
185
189
await gcStarted
186
190
const rm2 = ipfs . block . rm ( cid2 )
187
191
188
- const deleted = ( await gc ) . map ( i => i . cid . toString ( ) )
189
- await rm1
190
-
191
- // Second rm should fail because GC has already removed that block
192
- try {
193
- await rm2
194
- } catch ( err ) {
195
- expect ( err . code ) . eql ( Errors . dbDeleteFailedError ( ) . code )
196
- }
192
+ const deleted = ( await gc ) . map ( i => i . multihash )
193
+ const rm1Out = await rm1
194
+ expect ( rm1Out [ 0 ] ) . to . not . have . property ( 'error' )
197
195
198
196
// Confirm second block has been removed
199
- const localRefs = ( await ipfs . refs . local ( ) ) . map ( r => r . ref )
200
- expect ( localRefs ) . not . includes ( cid2 . toString ( ) )
197
+ const localMultihashes = ( await ipfs . refs . local ( ) ) . map ( r => base32 . encode ( new CID ( r . ref ) . multihash ) )
198
+ expect ( localMultihashes ) . not . includes ( cid2Multihash )
199
+
200
+ // Second rm should fail because GC has already removed that block
201
+ expect ( ( await rm2 ) [ 0 ] )
202
+ . to . have . property ( 'error' )
203
+ . that . has . property ( 'code' ) . that . equal ( Errors . dbDeleteFailedError ( ) . code )
201
204
202
205
// Should not have garbage collected block from first block put, because
203
206
// GC should have waited for first rm (removing first block put) to finish
204
- expect ( deleted ) . not . includes ( cid1 . toString ( ) )
207
+ expect ( deleted ) . not . includes ( cid1Multihash )
205
208
206
209
// Should have garbage collected block from second block put, because GC
207
210
// should have completed before second rm (removing second block put)
208
- expect ( deleted ) . includes ( cid2 . toString ( ) )
211
+ expect ( deleted ) . includes ( cid2Multihash )
209
212
} )
210
213
211
214
it ( 'garbage collection should wait for pending pin add to finish' , async ( ) => {
212
215
// Add two blocks so that we can pin them
213
- const cid1 = ( await ipfs . block . put ( Buffer . from ( 'block to pin add 1' ) , null ) ) . cid
214
- const cid2 = ( await ipfs . block . put ( Buffer . from ( 'block to pin add 2' ) , null ) ) . cid
216
+ const cid1 = ( await ipfs . block . put ( Buffer . from ( 'block to test pin add 1' ) , null ) ) . cid
217
+ const cid2 = ( await ipfs . block . put ( Buffer . from ( 'block to test pin add 2' ) , null ) ) . cid
218
+ const cid1Multihash = base32 . encode ( cid1 . multihash )
219
+ const cid2Multihash = base32 . encode ( cid2 . multihash )
215
220
216
221
// Pin first block
217
222
// Note: pin add will take a read lock
@@ -221,30 +226,32 @@ describe('gc', function () {
221
226
// Once pin lock has been requested, start GC
222
227
await pinLockRequested
223
228
const gc = ipfs . repo . gc ( )
224
- const deleted = ( await gc ) . map ( i => i . cid . toString ( ) )
229
+ const deleted = ( await gc ) . map ( i => i . multihash )
225
230
await pin1
226
231
227
232
// TODO: Adding pin for removed block never returns, which means the lock
228
233
// never gets released
229
234
// const pin2 = ipfs.pin.add(cid2)
230
235
231
236
// Confirm second second block has been removed
232
- const localRefs = ( await ipfs . refs . local ( ) ) . map ( r => r . ref )
233
- expect ( localRefs ) . not . includes ( cid2 . toString ( ) )
237
+ const localMultihashes = ( await ipfs . refs . local ( ) ) . map ( r => base32 . encode ( new CID ( r . ref ) . multihash ) )
238
+ expect ( localMultihashes ) . not . includes ( cid2Multihash )
234
239
235
240
// Should not have garbage collected block from first block put, because
236
241
// GC should have waited for pin (protecting first block put) to finish
237
- expect ( deleted ) . not . includes ( cid1 . toString ( ) )
242
+ expect ( deleted ) . not . includes ( cid1Multihash )
238
243
239
244
// Should have garbage collected block from second block put, because GC
240
245
// should have completed before second pin
241
- expect ( deleted ) . includes ( cid2 . toString ( ) )
246
+ expect ( deleted ) . includes ( cid2Multihash )
242
247
} )
243
248
244
249
it ( 'garbage collection should wait for pending pin rm to finish' , async ( ) => {
245
250
// Add two blocks so that we can pin them
246
251
const cid1 = ( await ipfs . block . put ( Buffer . from ( 'block to pin rm 1' ) , null ) ) . cid
247
252
const cid2 = ( await ipfs . block . put ( Buffer . from ( 'block to pin rm 2' ) , null ) ) . cid
253
+ const cid1Multihash = base32 . encode ( cid1 . multihash )
254
+ const cid2Multihash = base32 . encode ( cid2 . multihash )
248
255
249
256
// Pin blocks
250
257
await ipfs . pin . add ( cid1 )
@@ -265,17 +272,17 @@ describe('gc', function () {
265
272
await gcStarted
266
273
const pinRm2 = ipfs . pin . rm ( cid2 )
267
274
268
- const deleted = ( await gc ) . map ( i => i . cid . toString ( ) )
275
+ const deleted = ( await gc ) . map ( i => i . multihash )
269
276
await pinRm1
270
277
await pinRm2
271
278
272
279
// Should have garbage collected block from first block put, because
273
280
// GC should have waited for pin rm (unpinning first block put) to finish
274
- expect ( deleted ) . includes ( cid1 . toString ( ) )
281
+ expect ( deleted ) . includes ( cid1Multihash )
275
282
276
283
// Should not have garbage collected block from second block put, because
277
284
// GC should have completed before second block was unpinned
278
- expect ( deleted ) . not . includes ( cid2 . toString ( ) )
285
+ expect ( deleted ) . not . includes ( cid2Multihash )
279
286
} )
280
287
} )
281
288
} )
0 commit comments