Skip to content

Commit

Permalink
Improve error reporting and fix pin/set_test.go
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Mildred Ki'Lya <mildred-pub.git@mildred.fr>
  • Loading branch information
mildred authored and Shanti Bouchez-Mongardé committed Mar 4, 2016
1 parent 8f8020e commit b1f77e9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion merkledag/coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (n *Node) unmarshal(encoded []byte) error {
n.Links[i] = &Link{Name: l.GetName(), Size: l.GetTsize()}
h, err := mh.Cast(l.GetHash())
if err != nil {
return fmt.Errorf("Link hash is not valid multihash. %v", err)
return fmt.Errorf("Link hash #%d is not valid multihash. %v", i, err)
}
n.Links[i].Hash = h
}
Expand Down
8 changes: 6 additions & 2 deletions merkledag/merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ func (n *dagService) Get(ctx context.Context, k key.Key) (*Node, error) {
if err == bserv.ErrNotFound {
return nil, ErrNotFound
}
return nil, err
return nil, fmt.Errorf("Failed to get block for %s: %v", k.B58String(), err)
}

return DecodeProtobuf(b.Data)
res, err := DecodeProtobuf(b.Data)
if err != nil {
return nil, fmt.Errorf("Failed to decode Protocol Buffers: %v", err)
}
return res, nil
}

func (n *dagService) Remove(nd *Node) error {
Expand Down
4 changes: 2 additions & 2 deletions pin/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,12 @@ func loadSet(ctx context.Context, dag merkledag.DAGService, root *merkledag.Node
func loadMultiset(ctx context.Context, dag merkledag.DAGService, root *merkledag.Node, name string, internalKeys keyObserver) (map[key.Key]uint64, error) {
l, err := root.GetNodeLink(name)
if err != nil {
return nil, err
return nil, fmt.Errorf("Failed to get link %s: %v", name, err)
}
internalKeys(key.Key(l.Hash))
n, err := l.GetNode(ctx, dag)
if err != nil {
return nil, err
return nil, fmt.Errorf("Failed to get node from link %s: %v", name, err)
}

refcounts := make(map[key.Key]uint64)
Expand Down
21 changes: 21 additions & 0 deletions pin/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/ipfs/go-ipfs/blockservice"
"github.com/ipfs/go-ipfs/exchange/offline"
"github.com/ipfs/go-ipfs/merkledag"
mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
)

Expand All @@ -31,6 +33,14 @@ func TestMultisetRoundtrip(t *testing.T) {
dag := merkledag.NewDAGService(bserv)

fn := func(m map[key.Key]uint16) bool {
// Convert invalid multihash from input to valid ones
for k, v := range m {
if _, err := mh.Cast([]byte(k)); err != nil {
delete(m, k)
m[key.Key(u.Hash([]byte(k)))] = v
}
}

// Generate a smaller range for refcounts than full uint64, as
// otherwise this just becomes overly cpu heavy, splitting it
// out into too many items. That means we need to convert to
Expand All @@ -43,6 +53,17 @@ func TestMultisetRoundtrip(t *testing.T) {
if err != nil {
t.Fatalf("storing multiset: %v", err)
}

// Check that the node n is in the DAG
k, err := n.Key()
if err != nil {
t.Fatalf("Could not get key: %v", err)
}
_, err = dag.Get(ctx, k)
if err != nil {
t.Fatalf("Could not get node: %v", err)
}

root := &merkledag.Node{}
const linkName = "dummylink"
if err := root.AddNodeLink(linkName, n); err != nil {
Expand Down

0 comments on commit b1f77e9

Please sign in to comment.