From 0296a0024277031efabdb21e79bacdbdf00b339d Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Wed, 22 Nov 2023 21:15:47 -0800 Subject: [PATCH 1/2] http: fix panic --- go.mod | 2 +- http/api.go | 3 +-- http/ipfs.go | 6 +++--- sia/types.go | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 5b78a24..1d5642e 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,6 @@ require ( github.com/libp2p/go-libp2p v0.31.0 github.com/libp2p/go-libp2p-kad-dht v0.24.4 github.com/multiformats/go-multiaddr v0.11.0 - github.com/multiformats/go-multibase v0.2.0 github.com/multiformats/go-multicodec v0.9.0 github.com/multiformats/go-multihash v0.2.3 go.sia.tech/jape v0.11.0 @@ -118,6 +117,7 @@ require ( github.com/multiformats/go-base36 v0.2.0 // indirect github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect + github.com/multiformats/go-multibase v0.2.0 // indirect github.com/multiformats/go-multistream v0.4.1 // indirect github.com/multiformats/go-varint v0.0.7 // indirect github.com/nxadm/tail v1.4.11 // indirect diff --git a/http/api.go b/http/api.go index 0385a8d..a590456 100644 --- a/http/api.go +++ b/http/api.go @@ -108,9 +108,8 @@ func (as *apiServer) handleUpload(jc jape.Context) { body := jc.Request.Body defer body.Close() - prefix := c.Prefix() opts := sia.CIDOptions{ - CIDBuilder: cid.V1Builder{Codec: prefix.Codec, MhType: prefix.MhType, MhLength: prefix.MhLength}, + CIDBuilder: cid.V1Builder{Codec: uint64(multicodec.DagPb), MhType: multihash.SHA2_256}, RawLeaves: true, } diff --git a/http/ipfs.go b/http/ipfs.go index 70ad452..f6458ba 100644 --- a/http/ipfs.go +++ b/http/ipfs.go @@ -29,8 +29,8 @@ func getURLCID(r *http.Request) (c cid.Cid, path []string, redirect bool, _ erro } path = strings.Split(strings.TrimSpace(strings.Trim(r.URL.Path, "/")), "/") - if path[0] == "" { - path = nil + if len(path) != 0 && path[0] == "" { // ignore leading slash + path = path[1:] } // try to parse the subdomain as a CID @@ -41,7 +41,7 @@ func getURLCID(r *http.Request) (c cid.Cid, path []string, redirect bool, _ erro } // check if the path contains a CID - if len(path) >= 2 && path[0] == "ipfs" || path[0] == "ipns" { + if len(path) >= 2 && (path[0] == "ipfs" || path[0] == "ipns") { cidStr, path = path[1], path[2:] if c, err := cid.Parse(cidStr); err == nil { diff --git a/sia/types.go b/sia/types.go index fec52e2..73775aa 100644 --- a/sia/types.go +++ b/sia/types.go @@ -29,7 +29,7 @@ type ( } // A Block is an IPFS chunk with metadata for efficient storage and - // retrieval from renterd + // retrieval from a renterd object Block struct { CID cid.Cid `json:"cid"` Data RenterdData `json:"data"` From 8327f42c38bb3bbf81d6b2b8703f7706f712bed1 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Wed, 22 Nov 2023 21:16:05 -0800 Subject: [PATCH 2/2] persist: store blocks by multihash instead of CID --- persist/badger/cids.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/persist/badger/cids.go b/persist/badger/cids.go index b04c2c0..7cb0040 100644 --- a/persist/badger/cids.go +++ b/persist/badger/cids.go @@ -13,7 +13,7 @@ import ( // HasBlock returns true if the CID is in the store func (s *Store) HasBlock(_ context.Context, c cid.Cid) (ok bool, err error) { err = s.db.View(func(txn *badger.Txn) error { - _, err := txn.Get([]byte(c.Bytes())) + _, err := txn.Get([]byte(c.Hash())) if err != nil { if err == badger.ErrKeyNotFound { return nil @@ -29,7 +29,7 @@ func (s *Store) HasBlock(_ context.Context, c cid.Cid) (ok bool, err error) { // GetBlock returns the block metadata for a given CID func (s *Store) GetBlock(_ context.Context, c cid.Cid) (cm sia.Block, err error) { err = s.db.View(func(txn *badger.Txn) error { - item, err := txn.Get([]byte(c.Bytes())) + item, err := txn.Get([]byte(c.Hash())) if err != nil { if err == badger.ErrKeyNotFound { return sia.ErrNotFound @@ -50,7 +50,7 @@ func (s *Store) AddBlocks(_ context.Context, blocks []sia.Block) error { buf, err := json.Marshal(block) if err != nil { return err - } else if err := txn.Set([]byte(block.CID.Bytes()), buf); err != nil { + } else if err := txn.Set([]byte(block.CID.Hash()), buf); err != nil { return err } }