Skip to content

Commit

Permalink
feat: switch to raw multihashes for blocks
Browse files Browse the repository at this point in the history
Part of: #6815
  • Loading branch information
Stebalien authored and hsanjuan committed Feb 3, 2020
1 parent 6e6cb2e commit 19bdde9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 93 deletions.
2 changes: 1 addition & 1 deletion core/commands/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ var RefsLocalCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List all local references.",
ShortDescription: `
Displays the hashes of all local objects.
Displays the hashes of all local objects. NOTE: This treats all local objects as "raw blocks" and returns CIDv1-Raw CIDs.
`,
},

Expand Down
2 changes: 0 additions & 2 deletions core/node/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/ipfs/go-filestore"
"github.com/ipfs/go-ipfs/core/node/helpers"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-ipfs/thirdparty/cidv0v1"
"github.com/ipfs/go-ipfs/thirdparty/verifbs"
)

Expand Down Expand Up @@ -61,7 +60,6 @@ func BaseBlockstoreCtor(cacheOpts blockstore.CacheOpts, nilRepo bool, hashOnRead
}

bs = blockstore.NewIdStore(bs)
bs = cidv0v1.NewBlockstore(bs)

if hashOnRead { // TODO: review: this is how it was done originally, is there a reason we can't just pass this directly?
bs.HashOnRead(true)
Expand Down
35 changes: 33 additions & 2 deletions gc/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ type Result struct {
Error error
}

// converts a set of CIDs with different codecs to a set of CIDs with the raw codec.
func toRawCids(set *cid.Set) *cid.Set {
newSet := cid.NewSet()
set.ForEach(func(c cid.Cid) error {
newSet.Add(cid.NewCidV1(cid.Raw, c.Hash()))
return nil
})
return newSet
}

// GC performs a mark and sweep garbage collection of the blocks in the blockstore
// first, it creates a 'marked' set and adds to it the following:
// - all recursively pinned blocks, plus all of their descendants (recursively)
Expand Down Expand Up @@ -60,6 +70,15 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, dstor dstore.Datastore, pn
}
return
}

// The blockstore reports raw blocks. We need to remove the codecs from the CIDs.
gcs = toRawCids(gcs)
emark.Append(logging.LoggableMap{
"blackSetSize": fmt.Sprintf("%d", gcs.Len()),
})
emark.Done()
esweep := log.EventBegin(ctx, "GC.sweep")

keychan, err := bs.AllKeysChan(ctx)
if err != nil {
select {
Expand All @@ -79,6 +98,8 @@ func GC(ctx context.Context, bs bstore.GCBlockstore, dstor dstore.Datastore, pn
if !ok {
break loop
}
// NOTE: assumes that all CIDs returned by the keychan are _raw_ CIDv1 CIDs.
// This means we keep the block as long as we want it somewhere (CIDv1, CIDv0, Raw, other...).
if !gcs.Has(k) {
err := bs.DeleteBlock(k)
removed++
Expand Down Expand Up @@ -154,7 +175,9 @@ func Descendants(ctx context.Context, getLinks dag.GetLinks, set *cid.Set, roots

for _, c := range roots {
// Walk recursively walks the dag and adds the keys to the given set
err := dag.Walk(ctx, verifyGetLinks, c, set.Visit, dag.Concurrent())
err := dag.Walk(ctx, verifyGetLinks, c, func(k cid.Cid) bool {
return set.Visit(toCidV1(k))
}, dag.Concurrent())

if err != nil {
err = verboseCidError(err)
Expand All @@ -165,6 +188,14 @@ func Descendants(ctx context.Context, getLinks dag.GetLinks, set *cid.Set, roots
return nil
}

// toCidV1 converts any CIDv0s to CIDv1s.
func toCidV1(c cid.Cid) cid.Cid {
if c.Version() == 0 {
return cid.NewCidV1(c.Type(), c.Hash())
}
return c
}

// ColoredSet computes the set of nodes in the graph that are pinned by the
// pins in the given pinner.
func ColoredSet(ctx context.Context, pn pin.Pinner, ng ipld.NodeGetter, bestEffortRoots []cid.Cid, output chan<- Result) (*cid.Set, error) {
Expand Down Expand Up @@ -225,7 +256,7 @@ func ColoredSet(ctx context.Context, pn pin.Pinner, ng ipld.NodeGetter, bestEffo
return nil, err
}
for _, k := range dkeys {
gcs.Add(k)
gcs.Add(toCidV1(k))
}

ikeys, err := pn.InternalPins(ctx)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/ipfs/go-ipfs-chunker v0.0.3
github.com/ipfs/go-ipfs-cmds v0.1.1
github.com/ipfs/go-ipfs-config v0.2.0
github.com/ipfs/go-ipfs-ds-help v0.0.1
github.com/ipfs/go-ipfs-ds-help v0.0.2-0.20200107180048-11890cc86e62
github.com/ipfs/go-ipfs-exchange-interface v0.0.1
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
github.com/ipfs/go-ipfs-files v0.0.6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ github.com/ipfs/go-ipfs-delay v0.0.1 h1:r/UXYyRcddO6thwOnhiznIAiSvxMECGgtv35Xs1I
github.com/ipfs/go-ipfs-delay v0.0.1/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
github.com/ipfs/go-ipfs-ds-help v0.0.1 h1:QBg+Ts2zgeemK/dB0saiF/ykzRGgfoFMT90Rzo0OnVU=
github.com/ipfs/go-ipfs-ds-help v0.0.1/go.mod h1:gtP9xRaZXqIQRh1HRpp595KbBEdgqWFxefeVKOV8sxo=
github.com/ipfs/go-ipfs-ds-help v0.0.2-0.20200107180048-11890cc86e62 h1:ib57ZzQCRZ/scmBO32DqWThjwpTqBW/fGqurYDE/rFA=
github.com/ipfs/go-ipfs-ds-help v0.0.2-0.20200107180048-11890cc86e62/go.mod h1:zjN0aB3d6VyzJTSvcylSYxo+LSR/ELEfNnQM8p/vwc8=
github.com/ipfs/go-ipfs-exchange-interface v0.0.1 h1:LJXIo9W7CAmugqI+uofioIpRb6rY30GUu7G6LUfpMvM=
github.com/ipfs/go-ipfs-exchange-interface v0.0.1/go.mod h1:c8MwfHjtQjPoDyiy9cFquVtVHkO9b9Ob3FG91qJnWCM=
github.com/ipfs/go-ipfs-exchange-offline v0.0.1 h1:P56jYKZF7lDDOLx5SotVh5KFxoY6C81I1NSHW1FxGew=
Expand Down
87 changes: 0 additions & 87 deletions thirdparty/cidv0v1/blockstore.go

This file was deleted.

0 comments on commit 19bdde9

Please sign in to comment.