diff --git a/merkledag/coding.go b/merkledag/coding.go index 81cc1fc7c..cbd2de74a 100644 --- a/merkledag/coding.go +++ b/merkledag/coding.go @@ -2,6 +2,7 @@ package merkledag import ( "fmt" + "sort" mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" @@ -30,6 +31,7 @@ func (n *Node) Unmarshal(encoded []byte) error { } n.Links[i].Hash = h } + sort.Stable(LinkSlice(n.Links)) // keep links sorted n.Data = pbn.GetData() return nil @@ -59,6 +61,8 @@ func (n *Node) Marshal() ([]byte, error) { func (n *Node) getPBNode() *pb.PBNode { pbn := &pb.PBNode{} pbn.Links = make([]*pb.PBLink, len(n.Links)) + + sort.Stable(LinkSlice(n.Links)) // keep links sorted for i, l := range n.Links { pbn.Links[i] = &pb.PBLink{} pbn.Links[i].Name = &l.Name @@ -73,6 +77,7 @@ func (n *Node) getPBNode() *pb.PBNode { // Encoded returns the encoded raw data version of a Node instance. // It may use a cached encoded version, unless the force flag is given. func (n *Node) Encoded(force bool) ([]byte, error) { + sort.Stable(LinkSlice(n.Links)) // keep links sorted if n.encoded == nil || force { var err error n.encoded, err = n.Marshal() diff --git a/merkledag/merkledag.go b/merkledag/merkledag.go index 007b5d055..cd50d9e5b 100644 --- a/merkledag/merkledag.go +++ b/merkledag/merkledag.go @@ -66,6 +66,12 @@ type Link struct { Node *Node } +type LinkSlice []*Link + +func (ls LinkSlice) Len() int { return len(ls) } +func (ls LinkSlice) Swap(a, b int) { ls[a], ls[b] = ls[b], ls[a] } +func (ls LinkSlice) Less(a, b int) bool { return ls[a].Name < ls[b].Name } + // MakeLink creates a link to the given node func MakeLink(n *Node) (*Link, error) { s, err := n.Size()