From 4cc1780705c3c627a2feee8be12731ebf1b287df Mon Sep 17 00:00:00 2001 From: Jeromy Date: Mon, 24 Nov 2014 08:28:48 +0000 Subject: [PATCH] fix issues in merkledag --- core/core.go | 1 + exchange/bitswap/network/ipfs_impl.go | 3 --- merkledag/merkledag.go | 35 ++++++++++++++++++--------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/core/core.go b/core/core.go index fcd421fc6..b3f858ddd 100644 --- a/core/core.go +++ b/core/core.go @@ -115,6 +115,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) { Config: cfg, } n.ContextCloser = ctxc.NewContextCloser(ctx, n.teardown) + ctx = n.Context() // setup datastore. if n.Datastore, err = makeDatastore(cfg.Datastore); err != nil { diff --git a/exchange/bitswap/network/ipfs_impl.go b/exchange/bitswap/network/ipfs_impl.go index c94a4859f..1a3c11b44 100644 --- a/exchange/bitswap/network/ipfs_impl.go +++ b/exchange/bitswap/network/ipfs_impl.go @@ -1,8 +1,6 @@ package network import ( - "errors" - context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message" @@ -54,7 +52,6 @@ func (bsnet *impl) HandleMessage( // TODO(brian): put this in a helper function if bsmsg == nil || p == nil { - bsnet.receiver.ReceiveError(errors.New("ReceiveMessage returned nil peer or message")) return nil } diff --git a/merkledag/merkledag.go b/merkledag/merkledag.go index 2673c59fc..06381bacf 100644 --- a/merkledag/merkledag.go +++ b/merkledag/merkledag.go @@ -163,17 +163,6 @@ func (n *Node) Multihash() (mh.Multihash, error) { return n.cached, nil } -// Searches this nodes links for one to the given key, -// returns the index of said link -func (n *Node) FindLink(k u.Key) (int, error) { - for i, lnk := range n.Links { - if u.Key(lnk.Hash) == k { - return i, nil - } - } - return -1, u.ErrNotFound -} - // Key returns the Multihash as a key, for maps. func (n *Node) Key() (u.Key, error) { h, err := n.Multihash() @@ -298,6 +287,17 @@ func FetchGraph(ctx context.Context, root *Node, serv DAGService) chan struct{} return done } +// Searches this nodes links for one to the given key, +// returns the index of said link +func FindLink(n *Node, k u.Key, found []*Node) (int, error) { + for i, lnk := range n.Links { + if u.Key(lnk.Hash) == k && found[i] == nil { + return i, nil + } + } + return -1, u.ErrNotFound +} + // BatchFetch will fill out all of the links of the given Node. // It returns a channel of nodes, which the caller can receive // all the child nodes of 'root' on, in proper order. @@ -324,7 +324,7 @@ func (ds *dagService) BatchFetch(ctx context.Context, root *Node) <-chan *Node { count := 0 for blk := range blkchan { count++ - i, err := root.FindLink(blk.Key()) + i, err := FindLink(root, blk.Key(), nodes) if err != nil { panic("Received block that wasnt in this nodes links!") } @@ -356,3 +356,14 @@ func (ds *dagService) BatchFetch(ctx context.Context, root *Node) <-chan *Node { return sig } + +func checkForDupes(ks []u.Key) bool { + seen := make(map[u.Key]struct{}) + for _, k := range ks { + if _, ok := seen[k]; ok { + return true + } + seen[k] = struct{}{} + } + return false +}