@@ -28,6 +28,16 @@ type Result struct {
2828 Error error
2929}
3030
31+ // converts a set of CIDs with different codecs to a set of CIDs with the raw codec.
32+ func toRawCids (set * cid.Set ) (* cid.Set , error ) {
33+ newSet := cid .NewSet ()
34+ err := set .ForEach (func (c cid.Cid ) error {
35+ newSet .Add (cid .NewCidV1 (cid .Raw , c .Hash ()))
36+ return nil
37+ })
38+ return newSet , err
39+ }
40+
3141// GC performs a mark and sweep garbage collection of the blocks in the blockstore
3242// first, it creates a 'marked' set and adds to it the following:
3343// - all recursively pinned blocks, plus all of their descendants (recursively)
@@ -60,6 +70,17 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, dstor dstore.Datastore, pn
6070 }
6171 return
6272 }
73+
74+ // The blockstore reports raw blocks. We need to remove the codecs from the CIDs.
75+ gcs , err = toRawCids (gcs )
76+ if err != nil {
77+ select {
78+ case output <- Result {Error : err }:
79+ case <- ctx .Done ():
80+ }
81+ return
82+ }
83+
6384 keychan , err := bs .AllKeysChan (ctx )
6485 if err != nil {
6586 select {
@@ -79,6 +100,8 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, dstor dstore.Datastore, pn
79100 if ! ok {
80101 break loop
81102 }
103+ // NOTE: assumes that all CIDs returned by the keychan are _raw_ CIDv1 CIDs.
104+ // This means we keep the block as long as we want it somewhere (CIDv1, CIDv0, Raw, other...).
82105 if ! gcs .Has (k ) {
83106 err := bs .DeleteBlock (ctx , k )
84107 removed ++
@@ -154,7 +177,9 @@ func Descendants(ctx context.Context, getLinks dag.GetLinks, set *cid.Set, roots
154177
155178 for _ , c := range roots {
156179 // Walk recursively walks the dag and adds the keys to the given set
157- err := dag .Walk (ctx , verifyGetLinks , c , set .Visit , dag .Concurrent ())
180+ err := dag .Walk (ctx , verifyGetLinks , c , func (k cid.Cid ) bool {
181+ return set .Visit (toCidV1 (k ))
182+ }, dag .Concurrent ())
158183
159184 if err != nil {
160185 err = verboseCidError (err )
@@ -165,6 +190,14 @@ func Descendants(ctx context.Context, getLinks dag.GetLinks, set *cid.Set, roots
165190 return nil
166191}
167192
193+ // toCidV1 converts any CIDv0s to CIDv1s.
194+ func toCidV1 (c cid.Cid ) cid.Cid {
195+ if c .Version () == 0 {
196+ return cid .NewCidV1 (c .Type (), c .Hash ())
197+ }
198+ return c
199+ }
200+
168201// ColoredSet computes the set of nodes in the graph that are pinned by the
169202// pins in the given pinner.
170203func ColoredSet (ctx context.Context , pn pin.Pinner , ng ipld.NodeGetter , bestEffortRoots []cid.Cid , output chan <- Result ) (* cid.Set , error ) {
@@ -225,7 +258,7 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ng ipld.NodeGetter, bestEffo
225258 return nil , err
226259 }
227260 for _ , k := range dkeys {
228- gcs .Add (k )
261+ gcs .Add (toCidV1 ( k ) )
229262 }
230263
231264 ikeys , err := pn .InternalPins (ctx )
0 commit comments