Skip to content

Commit

Permalink
http, sia: use v1 CIDs by default
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Nov 21, 2023
1 parent 6af10cc commit f34cff6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
31 changes: 24 additions & 7 deletions http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"net/http"

"github.com/ipfs/go-cid"
"github.com/multiformats/go-multicodec"
"github.com/multiformats/go-multihash"
"go.sia.tech/fsd/config"
"go.sia.tech/fsd/ipfs"
"go.sia.tech/fsd/sia"
Expand All @@ -29,7 +31,8 @@ func (as *apiServer) handleCalculate(jc jape.Context) {
body := jc.Request.Body
defer body.Close()

blocks, err := as.sia.CalculateBlocks(ctx, body)
builder := cid.V1Builder{Codec: uint64(multicodec.DagPb), MhType: multihash.SHA2_256}
blocks, err := as.sia.CalculateBlocks(ctx, body, builder, true)
if err != nil {
jc.Error(err, http.StatusInternalServerError)
return
Expand All @@ -43,20 +46,31 @@ func (as *apiServer) handlePin(jc jape.Context) {
if err := jc.DecodeParam("cid", &cidStr); err != nil {
return
}
cid, err := cid.Parse(cidStr)
c, err := cid.Parse(cidStr)
if err != nil {
jc.Error(err, http.StatusBadRequest)
return
}

r, err := as.ipfs.DownloadCID(ctx, cid, nil)
r, err := as.ipfs.DownloadCID(ctx, c, nil)
if err != nil {
jc.Error(err, http.StatusInternalServerError)
return
}
defer r.Close()

if err := as.sia.UploadCID(ctx, cid, r); err != nil {
var builder cid.Builder
var rawLeaves bool
switch c.Version() {
case 1:
prefix := c.Prefix()
builder = cid.V1Builder{Codec: prefix.Codec, MhType: prefix.MhType, MhLength: prefix.MhLength}
rawLeaves = true // TODO: detect from the dag
case 0:
builder = cid.V0Builder{}
}

if err := as.sia.UploadCID(ctx, c, r, builder, rawLeaves); err != nil {
jc.Error(err, http.StatusInternalServerError)
return
}
Expand All @@ -68,7 +82,7 @@ func (as *apiServer) handleUpload(jc jape.Context) {
if err := jc.DecodeParam("cid", &cidStr); err != nil {
return
}
cid, err := cid.Parse(cidStr)
c, err := cid.Parse(cidStr)
if err != nil {
jc.Error(err, http.StatusBadRequest)
return
Expand All @@ -77,14 +91,17 @@ func (as *apiServer) handleUpload(jc jape.Context) {
body := jc.Request.Body
defer body.Close()

err = as.sia.UploadCID(ctx, cid, body)
prefix := c.Prefix()
builder := cid.V1Builder{Codec: prefix.Codec, MhType: prefix.MhType, MhLength: prefix.MhLength}

err = as.sia.UploadCID(ctx, c, body, builder, true)
if err != nil {
jc.Error(err, http.StatusInternalServerError)
return
}

// the root cid is the first block
jc.Encode(cid.String())
jc.Encode(c.String())
}

func (as *apiServer) handleVerifyCID(jc jape.Context) {
Expand Down
16 changes: 10 additions & 6 deletions sia/sia.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type (
var ErrNotFound = errors.New("not found")

// UploadCID uploads a CID to the renterd node
func (n *Node) UploadCID(ctx context.Context, c cid.Cid, r io.Reader) error {
func (n *Node) UploadCID(ctx context.Context, c cid.Cid, r io.Reader, builder cid.Builder, rawLeaves bool) error {
log := n.log.Named("upload").With(zap.Stringer("cid", c), zap.String("bucket", n.renterd.Bucket))

dataKey := c.String()
Expand Down Expand Up @@ -89,8 +89,10 @@ func (n *Node) UploadCID(ctx context.Context, c cid.Cid, r io.Reader) error {
spl := chunker.NewSizeSplitter(r, chunker.DefaultBlockSize)

dbp := ihelpers.DagBuilderParams{
Maxlinks: ihelpers.DefaultLinksPerBlock,
Dagserv: dagSvc,
Maxlinks: ihelpers.DefaultLinksPerBlock,
Dagserv: dagSvc,
CidBuilder: builder,
RawLeaves: rawLeaves,
}
db, err := dbp.New(spl)
if err != nil {
Expand Down Expand Up @@ -189,14 +191,16 @@ func (n *Node) ProxyHTTPDownload(cid cid.Cid, r *http.Request, w http.ResponseWr
}

// CalculateBlocks calculates the blocks for a given reader and returns them
func (n *Node) CalculateBlocks(ctx context.Context, r io.Reader) ([]Block, error) {
func (n *Node) CalculateBlocks(ctx context.Context, r io.Reader, builder cid.Builder, rawLeaves bool) ([]Block, error) {
dagSvc := NewUnixFileUploader("", io.Discard, io.Discard, n.log.Named("calculate"))

spl := chunker.NewSizeSplitter(r, chunker.DefaultBlockSize)

dbp := ihelpers.DagBuilderParams{
Maxlinks: ihelpers.DefaultLinksPerBlock,
Dagserv: dagSvc,
Maxlinks: ihelpers.DefaultLinksPerBlock,
Dagserv: dagSvc,
CidBuilder: builder,
RawLeaves: rawLeaves,
}
db, err := dbp.New(spl)
if err != nil {
Expand Down

0 comments on commit f34cff6

Please sign in to comment.