added .Key
Juan Batiz-Benet committed
Jul 5, 2014 at 15:01 UTC
5eed33620f1417fd53efc5d864bea67943df8f6c
3 files changed
+26
-3
merkledag/merkledag.go
+13
@@ -1,9 +1,14 @@
1
package merkledag
2
3
import (
4
+ u "github.com/jbenet/go-ipfs/util"
5
mh "github.com/jbenet/go-multihash"
6
)
7
8
+// can't use []byte/Multihash for keys :(
9
+// so have to convert Multihash bytes to string
10
+type NodeMap map[u.Key]*Node
11
+
12
// A node in the IPFS Merkle DAG.
13
// nodes have opaque data and a set of navigable links.
14
type Node struct {
@@ -24,6 +29,9 @@ type Link struct {
29
30
// multihash of the target object
31
Hash mh.Multihash
32
+
33
+ // a ptr to the actual node for graph manipulation
34
+ Node *Node
35
}
36
37
func (n *Node) AddNodeLink(name string, that *Node) error {
@@ -66,3 +74,8 @@ func (n *Node) Multihash() (mh.Multihash, error) {
74
75
return mh.Sum(b, mh.SHA2_256, -1)
76
}
77
+
78
+func (n *Node) Key() (u.Key, error) {
79
+ h, err := n.Multihash()
80
+ return u.Key(h), err
81
+}
merkledag/merkledag_test.go
+10
-1
@@ -2,7 +2,7 @@ package merkledag
2
3
import (
4
"fmt"
5
- // mh "github.com/jbenet/go-multihash"
5
+ u "github.com/jbenet/go-ipfs/util"
6
"testing"
7
)
8
@@ -40,6 +40,15 @@ func TestNode(t *testing.T) {
40
} else {
41
fmt.Println("hash:", h)
42
}
43
+
44
+ k, err := n.Key()
45
+ if err != nil {
46
+ t.Error(err)
47
+ } else if k != u.Key(h) {
48
+ t.Error("Key is not equivalent to multihash")
49
+ } else {
50
+ fmt.Println("key: ", k)
51
+ }
52
}
53
54
printn("beep", n1)
peer/peer.go
+3
-2
@@ -1,14 +1,15 @@
1
package peer
2
3
import (
4
+ u "github.com/jbenet/go-ipfs/util"
5
ma "github.com/jbenet/go-multiaddr"
6
mh "github.com/jbenet/go-multihash"
7
)
8
9
type PeerId mh.Multihash
10
10
-// have to map string : *Peer because slices are not comparable.
11
-type PeerBook map[string]*Peer
11
+// have to map Key (string) : *Peer because slices are not comparable.
12
+type PeerBook map[u.Key]*Peer
13
14
type Peer struct {
15
Id PeerId