@@ -7,6 +7,8 @@ const IPFSFactory = require('ipfsd-ctl')
77const pEvent = require ( 'p-event' )
88const env = require ( 'ipfs-utils/src/env' )
99const IPFS = require ( '../../src/core' )
10+ const CID = require ( 'cids' )
11+ const base32 = require ( 'base32.js' )
1012const { Errors } = require ( 'interface-datastore' )
1113
1214// We need to detect when a readLock or writeLock is requested for the tests
@@ -90,17 +92,17 @@ describe('gc', function () {
9092 name : 'add' ,
9193 add1 : ( ) => ipfs . add ( fixtures [ 0 ] , { pin : false } ) ,
9294 add2 : ( ) => ipfs . add ( fixtures [ 1 ] , { pin : false } ) ,
93- resToCid : ( res ) => res [ 0 ] . hash
95+ resToMultihash : ( res ) => base32 . encode ( new CID ( res [ 0 ] . hash ) . multihash )
9496 } , {
9597 name : 'object put' ,
9698 add1 : ( ) => ipfs . object . put ( { Data : 'obj put 1' , Links : [ ] } ) ,
9799 add2 : ( ) => ipfs . object . put ( { Data : 'obj put 2' , Links : [ ] } ) ,
98- resToCid : ( res ) => res . toString ( )
100+ resToMultihash : ( res ) => base32 . encode ( res . multihash )
99101 } , {
100102 name : 'block put' ,
101103 add1 : ( ) => ipfs . block . put ( Buffer . from ( 'block put 1' ) , null ) ,
102104 add2 : ( ) => ipfs . block . put ( Buffer . from ( 'block put 2' ) , null ) ,
103- resToCid : ( res ) => res . cid . toString ( )
105+ resToMultihash : ( res ) => base32 . encode ( res . cid . multihash )
104106 } ]
105107
106108 describe ( 'locks' , function ( ) {
@@ -122,9 +124,9 @@ describe('gc', function () {
122124 await gcStarted
123125 const add2 = test . add2 ( )
124126
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 )
128130
129131 // Should have garbage collected blocks from first add, because GC should
130132 // have waited for first add to finish
@@ -152,9 +154,9 @@ describe('gc', function () {
152154 await gcStarted
153155 const add2 = ipfs . add ( fixtures [ 3 ] , { pin : true } )
154156
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 )
158160
159161 // Should not have garbage collected blocks from first add, because GC should
160162 // have waited for first add + pin to finish (protected by pin)
@@ -168,7 +170,9 @@ describe('gc', function () {
168170 it ( 'garbage collection should wait for pending block rm to finish' , async ( ) => {
169171 // Add two blocks so that we can remove them
170172 const cid1 = ( await ipfs . block . put ( Buffer . from ( 'block to rm 1' ) , null ) ) . cid
173+ const cid1Multihash = base32 . encode ( cid1 . multihash )
171174 const cid2 = ( await ipfs . block . put ( Buffer . from ( 'block to rm 2' ) , null ) ) . cid
175+ const cid2Multihash = base32 . encode ( cid2 . multihash )
172176
173177 // Remove first block from IPFS
174178 // Note: block rm will take a write lock
@@ -185,33 +189,35 @@ describe('gc', function () {
185189 await gcStarted
186190 const rm2 = ipfs . block . rm ( cid2 )
187191
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' )
197195
198196 // 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 )
201204
202205 // Should not have garbage collected block from first block put, because
203206 // 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 )
205208
206209 // Should have garbage collected block from second block put, because GC
207210 // should have completed before second rm (removing second block put)
208- expect ( deleted ) . includes ( cid2 . toString ( ) )
211+ expect ( deleted ) . includes ( cid2Multihash )
209212 } )
210213
211214 it ( 'garbage collection should wait for pending pin add to finish' , async ( ) => {
212215 // 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 )
220+ const eh = ( await ipfs . refs . local ( ) ) . map ( r => base32 . encode ( new CID ( r . ref ) . multihash ) )
215221
216222 // Pin first block
217223 // Note: pin add will take a read lock
@@ -221,30 +227,32 @@ describe('gc', function () {
221227 // Once pin lock has been requested, start GC
222228 await pinLockRequested
223229 const gc = ipfs . repo . gc ( )
224- const deleted = ( await gc ) . map ( i => i . cid . toString ( ) )
230+ const deleted = ( await gc ) . map ( i => i . multihash )
225231 await pin1
226232
227233 // TODO: Adding pin for removed block never returns, which means the lock
228234 // never gets released
229235 // const pin2 = ipfs.pin.add(cid2)
230236
231237 // 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 ( ) )
238+ const localMultihashes = ( await ipfs . refs . local ( ) ) . map ( r => base32 . encode ( new CID ( r . ref ) . multihash ) )
239+ expect ( localMultihashes ) . not . includes ( cid2Multihash )
234240
235241 // Should not have garbage collected block from first block put, because
236242 // GC should have waited for pin (protecting first block put) to finish
237- expect ( deleted ) . not . includes ( cid1 . toString ( ) )
243+ expect ( deleted ) . not . includes ( cid1Multihash )
238244
239245 // Should have garbage collected block from second block put, because GC
240246 // should have completed before second pin
241- expect ( deleted ) . includes ( cid2 . toString ( ) )
247+ expect ( deleted ) . includes ( cid2Multihash )
242248 } )
243249
244250 it ( 'garbage collection should wait for pending pin rm to finish' , async ( ) => {
245251 // Add two blocks so that we can pin them
246252 const cid1 = ( await ipfs . block . put ( Buffer . from ( 'block to pin rm 1' ) , null ) ) . cid
247253 const cid2 = ( await ipfs . block . put ( Buffer . from ( 'block to pin rm 2' ) , null ) ) . cid
254+ const cid1Multihash = base32 . encode ( cid1 . multihash )
255+ const cid2Multihash = base32 . encode ( cid2 . multihash )
248256
249257 // Pin blocks
250258 await ipfs . pin . add ( cid1 )
@@ -265,17 +273,17 @@ describe('gc', function () {
265273 await gcStarted
266274 const pinRm2 = ipfs . pin . rm ( cid2 )
267275
268- const deleted = ( await gc ) . map ( i => i . cid . toString ( ) )
276+ const deleted = ( await gc ) . map ( i => i . multihash )
269277 await pinRm1
270278 await pinRm2
271279
272280 // Should have garbage collected block from first block put, because
273281 // GC should have waited for pin rm (unpinning first block put) to finish
274- expect ( deleted ) . includes ( cid1 . toString ( ) )
282+ expect ( deleted ) . includes ( cid1Multihash )
275283
276284 // Should not have garbage collected block from second block put, because
277285 // GC should have completed before second block was unpinned
278- expect ( deleted ) . not . includes ( cid2 . toString ( ) )
286+ expect ( deleted ) . not . includes ( cid2Multihash )
279287 } )
280288 } )
281289} )
0 commit comments