Commit fc23250f authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

added .Key

parent e9cb869d
package merkledag package merkledag
import ( import (
u "github.com/jbenet/go-ipfs/util"
mh "github.com/jbenet/go-multihash" mh "github.com/jbenet/go-multihash"
) )
// can't use []byte/Multihash for keys :(
// so have to convert Multihash bytes to string
type NodeMap map[u.Key]*Node
// A node in the IPFS Merkle DAG. // A node in the IPFS Merkle DAG.
// nodes have opaque data and a set of navigable links. // nodes have opaque data and a set of navigable links.
type Node struct { type Node struct {
...@@ -24,6 +29,9 @@ type Link struct { ...@@ -24,6 +29,9 @@ type Link struct {
// multihash of the target object // multihash of the target object
Hash mh.Multihash Hash mh.Multihash
// a ptr to the actual node for graph manipulation
Node *Node
} }
func (n *Node) AddNodeLink(name string, that *Node) error { func (n *Node) AddNodeLink(name string, that *Node) error {
...@@ -66,3 +74,8 @@ func (n *Node) Multihash() (mh.Multihash, error) { ...@@ -66,3 +74,8 @@ func (n *Node) Multihash() (mh.Multihash, error) {
return mh.Sum(b, mh.SHA2_256, -1) return mh.Sum(b, mh.SHA2_256, -1)
} }
func (n *Node) Key() (u.Key, error) {
h, err := n.Multihash()
return u.Key(h), err
}
...@@ -2,7 +2,7 @@ package merkledag ...@@ -2,7 +2,7 @@ package merkledag
import ( import (
"fmt" "fmt"
// mh "github.com/jbenet/go-multihash" u "github.com/jbenet/go-ipfs/util"
"testing" "testing"
) )
...@@ -40,6 +40,15 @@ func TestNode(t *testing.T) { ...@@ -40,6 +40,15 @@ func TestNode(t *testing.T) {
} else { } else {
fmt.Println("hash:", h) fmt.Println("hash:", h)
} }
k, err := n.Key()
if err != nil {
t.Error(err)
} else if k != u.Key(h) {
t.Error("Key is not equivalent to multihash")
} else {
fmt.Println("key: ", k)
}
} }
printn("beep", n1) printn("beep", n1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment