33
44const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
55const { 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
713module . 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