Skip to content

Commit 71d5f6f

Browse files
committed
dht interface beginnings
1 parent be8cafa commit 71d5f6f

File tree

4 files changed

+115
-3
lines changed

4 files changed

+115
-3
lines changed

README.md

-3
This file was deleted.

dht.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package dht
2+
3+
// TODO. SEE https://github.com/jbenet/node-ipfs/blob/master/submodules/ipfs-dht/index.js
4+
5+
6+
// IpfsDHT is an implementation of Kademlia with Coral and S/Kademlia modifications.
7+
// It is used to implement the base IpfsRouting module.
8+
type IpfsDHT struct {
9+
routes RoutingTable
10+
}

routing.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package dht
2+
3+
import (
4+
"time"
5+
peer "github.com/jbenet/go-ipfs/peer"
6+
u "github.com/jbenet/go-ipfs/util"
7+
)
8+
9+
10+
// This file implements the Routing interface for the IpfsDHT struct.
11+
12+
// Basic Put/Get
13+
14+
// PutValue adds value corresponding to given Key.
15+
func (s *IpfsDHT) PutValue(key u.Key, value []byte) (error) {
16+
return u.ErrNotImplemented
17+
}
18+
19+
// GetValue searches for the value corresponding to given Key.
20+
func (s *IpfsDHT) GetValue(key u.Key, timeout time.Duration) ([]byte, error) {
21+
return nil, u.ErrNotImplemented
22+
}
23+
24+
25+
// Value provider layer of indirection.
26+
// This is what DSHTs (Coral and MainlineDHT) do to store large values in a DHT.
27+
28+
// Announce that this node can provide value for given key
29+
func (s *IpfsDHT) Provide(key u.Key) (error) {
30+
return u.ErrNotImplemented
31+
}
32+
33+
// FindProviders searches for peers who can provide the value for given key.
34+
func (s *IpfsDHT) FindProviders(key u.Key, timeout time.Duration) (*peer.Peer, error) {
35+
return nil, u.ErrNotImplemented
36+
}
37+
38+
39+
// Find specific Peer
40+
41+
// FindPeer searches for a peer with given ID.
42+
func (s *IpfsDHT) FindPeer(id peer.ID, timeout time.Duration) (*peer.Peer, error) {
43+
return nil, u.ErrNotImplemented
44+
}

table.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package dht
2+
3+
import (
4+
"container/list"
5+
)
6+
7+
8+
// ID for IpfsDHT should be a byte slice, to allow for simpler operations
9+
// (xor). DHT ids are based on the peer.IDs.
10+
//
11+
// NOTE: peer.IDs are biased because they are (a) multihashes (first bytes
12+
// biased), and (b) first bits are zeroes when using the S/Kademlia PoW.
13+
// Thus, may need to re-hash keys (uniform dist). TODO(jbenet)
14+
type ID []byte
15+
16+
// Bucket holds a list of peers.
17+
type Bucket []*list.List
18+
19+
20+
// RoutingTable defines the routing table.
21+
type RoutingTable struct {
22+
23+
// kBuckets define all the fingers to other nodes.
24+
Buckets []Bucket
25+
}
26+
27+
28+
func (id ID) commonPrefixLen() int {
29+
for i := 0; i < len(id); i++ {
30+
for j := 0; j < 8; j++ {
31+
if (id[i] >> uint8(7 - j)) & 0x1 != 0 {
32+
return i * 8 + j;
33+
}
34+
}
35+
}
36+
return len(id) * 8 - 1;
37+
}
38+
39+
func xor(a, b ID) ID {
40+
41+
// ids may actually be of different sizes.
42+
var ba ID
43+
var bb ID
44+
if len(a) >= len(b) {
45+
ba = a
46+
bb = b
47+
} else {
48+
ba = b
49+
bb = a
50+
}
51+
52+
c := make(ID, len(ba))
53+
for i := 0; i < len(ba); i++ {
54+
if len(bb) > i {
55+
c[i] = ba[i] ^ bb[i]
56+
} else {
57+
c[i] = ba[i] ^ 0
58+
}
59+
}
60+
return c
61+
}

0 commit comments

Comments
 (0)