Skip to content

Commit

Permalink
Merge pull request #8 from SiaFoundation/nate/store-by-multihash
Browse files Browse the repository at this point in the history
Store blocks by multihash
  • Loading branch information
n8maninger authored Nov 23, 2023
2 parents 0a86fb6 + 8327f42 commit df93104
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
6 changes: 3 additions & 3 deletions http/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions persist/badger/cids.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion sia/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit df93104

Please sign in to comment.