3
3
4
4
const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
5
5
const { 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
+ }
6
12
7
13
module . exports = ( createCommon , options ) => {
8
14
const describe = getDescribe ( options )
@@ -52,20 +58,21 @@ module.exports = (createCommon, options) => {
52
58
// information that refers to the blocks
53
59
const addRes = await ipfs . add ( Buffer . from ( 'apples' ) )
54
60
const hash = addRes [ 0 ] . hash
61
+ const cidV1 = cidV0ToV1Raw ( hash )
55
62
56
63
// Get the list of local blocks after the add, should be bigger than
57
64
// the initial list and contain hash
58
65
const refsAfterAdd = await ipfs . refs . local ( )
59
66
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 )
61
68
62
69
// Run garbage collection
63
70
await ipfs . repo . gc ( )
64
71
65
72
// Get the list of local blocks after GC, should still contain the hash,
66
73
// because the file is still pinned
67
74
const refsAfterGc = await ipfs . refs . local ( )
68
- expect ( refsAfterGc . map ( r => r . ref ) ) . includes ( hash )
75
+ expect ( refsAfterGc . map ( r => r . ref ) ) . includes ( cidV1 )
69
76
70
77
// Unpin the data
71
78
await ipfs . pin . rm ( hash )
@@ -75,7 +82,7 @@ module.exports = (createCommon, options) => {
75
82
76
83
// The list of local blocks should no longer contain the hash
77
84
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 )
79
86
} )
80
87
81
88
it ( 'should clean up removed MFS files' , async ( ) => {
@@ -86,21 +93,21 @@ module.exports = (createCommon, options) => {
86
93
await ipfs . files . write ( '/test' , Buffer . from ( 'oranges' ) , { create : true } )
87
94
const stats = await ipfs . files . stat ( '/test' )
88
95
expect ( stats . type ) . to . equal ( 'file' )
89
- const hash = stats . hash
96
+ const cidV1 = cidV0ToV1Raw ( stats . hash )
90
97
91
98
// Get the list of local blocks after the add, should be bigger than
92
99
// the initial list and contain hash
93
100
const refsAfterAdd = await ipfs . refs . local ( )
94
101
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 )
96
103
97
104
// Run garbage collection
98
105
await ipfs . repo . gc ( )
99
106
100
107
// Get the list of local blocks after GC, should still contain the hash,
101
108
// because the file is in MFS
102
109
const refsAfterGc = await ipfs . refs . local ( )
103
- expect ( refsAfterGc . map ( r => r . ref ) ) . includes ( hash )
110
+ expect ( refsAfterGc . map ( r => r . ref ) ) . includes ( cidV1 )
104
111
105
112
// Remove the file
106
113
await ipfs . files . rm ( '/test' )
@@ -110,7 +117,7 @@ module.exports = (createCommon, options) => {
110
117
111
118
// The list of local blocks should no longer contain the hash
112
119
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 )
114
121
} )
115
122
116
123
it ( 'should clean up block only after unpinned and removed from MFS' , async ( ) => {
@@ -121,21 +128,22 @@ module.exports = (createCommon, options) => {
121
128
await ipfs . files . write ( '/test' , Buffer . from ( 'peaches' ) , { create : true } )
122
129
const stats = await ipfs . files . stat ( '/test' )
123
130
expect ( stats . type ) . to . equal ( 'file' )
124
- const mfsFileHash = stats . hash
131
+ const mfsFileCidV1 = cidV0ToV1Raw ( stats . hash )
125
132
126
133
// 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 )
128
135
129
136
// Add the data to IPFS (which implicitly pins the data)
130
137
const addRes = await ipfs . add ( block . data )
131
138
const dataHash = addRes [ 0 ] . hash
139
+ const dataCidV1 = cidV0ToV1Raw ( dataHash )
132
140
133
141
// Get the list of local blocks after the add, should be bigger than
134
142
// the initial list and contain the data hash
135
143
const refsAfterAdd = await ipfs . refs . local ( )
136
144
expect ( refsAfterAdd . length ) . to . be . gt ( refsBeforeAdd . length )
137
145
const hashesAfterAdd = refsAfterAdd . map ( r => r . ref )
138
- expect ( hashesAfterAdd ) . includes ( dataHash )
146
+ expect ( hashesAfterAdd ) . includes ( dataCidV1 )
139
147
140
148
// Run garbage collection
141
149
await ipfs . repo . gc ( )
@@ -144,7 +152,7 @@ module.exports = (createCommon, options) => {
144
152
// because the file is pinned and in MFS
145
153
const refsAfterGc = await ipfs . refs . local ( )
146
154
const hashesAfterGc = refsAfterGc . map ( r => r . ref )
147
- expect ( hashesAfterGc ) . includes ( dataHash )
155
+ expect ( hashesAfterGc ) . includes ( dataCidV1 )
148
156
149
157
// Remove the file
150
158
await ipfs . files . rm ( '/test' )
@@ -156,8 +164,8 @@ module.exports = (createCommon, options) => {
156
164
// because the file is still pinned
157
165
const refsAfterRmAndGc = await ipfs . refs . local ( )
158
166
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 )
161
169
162
170
// Unpin the data
163
171
await ipfs . pin . rm ( dataHash )
@@ -168,8 +176,8 @@ module.exports = (createCommon, options) => {
168
176
// The list of local blocks should no longer contain the hashes
169
177
const refsAfterUnpinAndGc = await ipfs . refs . local ( )
170
178
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 )
173
181
} )
174
182
175
183
it ( 'should clean up indirectly pinned data after recursive pin removal' , async ( ) => {
@@ -179,6 +187,7 @@ module.exports = (createCommon, options) => {
179
187
// Add some data
180
188
const addRes = await ipfs . add ( Buffer . from ( 'pears' ) )
181
189
const dataHash = addRes [ 0 ] . hash
190
+ const dataHashCidV1 = cidV0ToV1Raw ( dataHash )
182
191
183
192
// Unpin the data
184
193
await ipfs . pin . rm ( dataHash )
@@ -192,6 +201,7 @@ module.exports = (createCommon, options) => {
192
201
193
202
// Put the object into IPFS
194
203
const objHash = ( await ipfs . object . put ( obj ) ) . toString ( )
204
+ const objCidV1 = cidV0ToV1Raw ( objHash )
195
205
196
206
// Putting an object doesn't pin it
197
207
expect ( ( await ipfs . pin . ls ( ) ) . map ( p => p . hash ) ) . not . includes ( objHash )
@@ -201,8 +211,8 @@ module.exports = (createCommon, options) => {
201
211
const refsAfterAdd = await ipfs . refs . local ( )
202
212
expect ( refsAfterAdd . length ) . to . be . gt ( refsBeforeAdd . length )
203
213
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 )
206
216
207
217
// Recursively pin the object
208
218
await ipfs . pin . add ( objHash , { recursive : true } )
@@ -217,7 +227,7 @@ module.exports = (createCommon, options) => {
217
227
// Get the list of local blocks after GC, should still contain the data
218
228
// hash, because the data is still (indirectly) pinned
219
229
const refsAfterGc = await ipfs . refs . local ( )
220
- expect ( refsAfterGc . map ( r => r . ref ) ) . includes ( dataHash )
230
+ expect ( refsAfterGc . map ( r => r . ref ) ) . includes ( dataHashCidV1 )
221
231
222
232
// Recursively unpin the object
223
233
await ipfs . pin . rm ( objHash )
@@ -228,8 +238,8 @@ module.exports = (createCommon, options) => {
228
238
// The list of local blocks should no longer contain the hashes
229
239
const refsAfterUnpinAndGc = await ipfs . refs . local ( )
230
240
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 )
233
243
} )
234
244
} )
235
245
}
0 commit comments