Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-routing
Commits
fed1d407
Commit
fed1d407
authored
10 years ago
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved routing table code into its own package
parent
f4af2651
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
30 additions
and
28 deletions
+30
-28
dht/dht.go
dht/dht.go
+7
-6
dht/diag.go
dht/diag.go
+1
-1
dht/routing.go
dht/routing.go
+7
-6
kbucket/bucket.go
kbucket/bucket.go
+1
-1
kbucket/table.go
kbucket/table.go
+3
-3
kbucket/table_test.go
kbucket/table_test.go
+9
-9
kbucket/util.go
kbucket/util.go
+2
-2
No files found.
dht/dht.go
View file @
fed1d407
...
...
@@ -10,6 +10,7 @@ import (
peer
"github.com/jbenet/go-ipfs/peer"
swarm
"github.com/jbenet/go-ipfs/swarm"
u
"github.com/jbenet/go-ipfs/util"
kb
"github.com/jbenet/go-ipfs/routing/kbucket"
ma
"github.com/jbenet/go-multiaddr"
...
...
@@ -25,7 +26,7 @@ import (
type
IpfsDHT
struct
{
// Array of routing tables for differently distanced nodes
// NOTE: (currently, only a single table is used)
routes
[]
*
RoutingTable
routes
[]
*
kb
.
RoutingTable
network
*
swarm
.
Swarm
...
...
@@ -84,8 +85,8 @@ func NewDHT(p *peer.Peer) (*IpfsDHT, error) {
dht
.
listeners
=
make
(
map
[
uint64
]
*
listenInfo
)
dht
.
providers
=
make
(
map
[
u
.
Key
][]
*
providerInfo
)
dht
.
shutdown
=
make
(
chan
struct
{})
dht
.
routes
=
make
([]
*
RoutingTable
,
1
)
dht
.
routes
[
0
]
=
NewRoutingTable
(
20
,
c
onvertPeerID
(
p
.
ID
))
dht
.
routes
=
make
([]
*
kb
.
RoutingTable
,
1
)
dht
.
routes
[
0
]
=
kb
.
NewRoutingTable
(
20
,
kb
.
C
onvertPeerID
(
p
.
ID
))
dht
.
birth
=
time
.
Now
()
return
dht
,
nil
}
...
...
@@ -253,7 +254,7 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *PBDHTMessage) {
}
}
else
if
err
==
ds
.
ErrNotFound
{
// Find closest peer(s) to desired key and reply with that info
closer
:=
dht
.
routes
[
0
]
.
NearestPeer
(
c
onvertKey
(
u
.
Key
(
pmes
.
GetKey
())))
closer
:=
dht
.
routes
[
0
]
.
NearestPeer
(
kb
.
C
onvertKey
(
u
.
Key
(
pmes
.
GetKey
())))
resp
=
&
DHTMessage
{
Response
:
true
,
Id
:
*
pmes
.
Id
,
...
...
@@ -290,7 +291,7 @@ func (dht *IpfsDHT) handlePing(p *peer.Peer, pmes *PBDHTMessage) {
func
(
dht
*
IpfsDHT
)
handleFindPeer
(
p
*
peer
.
Peer
,
pmes
*
PBDHTMessage
)
{
success
:=
true
u
.
POut
(
"handleFindPeer: searching for '%s'"
,
peer
.
ID
(
pmes
.
GetKey
())
.
Pretty
())
closest
:=
dht
.
routes
[
0
]
.
NearestPeer
(
c
onvertKey
(
u
.
Key
(
pmes
.
GetKey
())))
closest
:=
dht
.
routes
[
0
]
.
NearestPeer
(
kb
.
C
onvertKey
(
u
.
Key
(
pmes
.
GetKey
())))
if
closest
==
nil
{
u
.
PErr
(
"handleFindPeer: could not find anything."
)
success
=
false
...
...
@@ -432,7 +433,7 @@ func (dht *IpfsDHT) handleDiagnostic(p *peer.Peer, pmes *PBDHTMessage) {
}
dht
.
diaglock
.
Unlock
()
seq
:=
dht
.
routes
[
0
]
.
NearestPeers
(
c
onvertPeerID
(
dht
.
self
.
ID
),
10
)
seq
:=
dht
.
routes
[
0
]
.
NearestPeers
(
kb
.
C
onvertPeerID
(
dht
.
self
.
ID
),
10
)
listen_chan
:=
dht
.
ListenFor
(
pmes
.
GetId
(),
len
(
seq
),
time
.
Second
*
30
)
for
_
,
ps
:=
range
seq
{
...
...
This diff is collapsed.
Click to expand it.
dht/diag.go
View file @
fed1d407
...
...
@@ -37,7 +37,7 @@ func (dht *IpfsDHT) getDiagInfo() *diagInfo {
di
.
LifeSpan
=
time
.
Since
(
dht
.
birth
)
di
.
Keys
=
nil
// Currently no way to query datastore
for
_
,
p
:=
range
dht
.
routes
[
0
]
.
l
istpeers
()
{
for
_
,
p
:=
range
dht
.
routes
[
0
]
.
L
istpeers
()
{
di
.
Connections
=
append
(
di
.
Connections
,
connDiagInfo
{
p
.
GetLatency
(),
p
.
ID
})
}
return
di
...
...
This diff is collapsed.
Click to expand it.
dht/routing.go
View file @
fed1d407
...
...
@@ -12,6 +12,7 @@ import (
ma
"github.com/jbenet/go-multiaddr"
peer
"github.com/jbenet/go-ipfs/peer"
kb
"github.com/jbenet/go-ipfs/routing/kbucket"
swarm
"github.com/jbenet/go-ipfs/swarm"
u
"github.com/jbenet/go-ipfs/util"
)
...
...
@@ -33,7 +34,7 @@ func GenerateMessageID() uint64 {
// This is the top level "Store" operation of the DHT
func
(
s
*
IpfsDHT
)
PutValue
(
key
u
.
Key
,
value
[]
byte
)
error
{
var
p
*
peer
.
Peer
p
=
s
.
routes
[
0
]
.
NearestPeer
(
c
onvertKey
(
key
))
p
=
s
.
routes
[
0
]
.
NearestPeer
(
kb
.
C
onvertKey
(
key
))
if
p
==
nil
{
return
errors
.
New
(
"Table returned nil peer!"
)
}
...
...
@@ -46,7 +47,7 @@ func (s *IpfsDHT) PutValue(key u.Key, value []byte) error {
// returned along with util.ErrSearchIncomplete
func
(
s
*
IpfsDHT
)
GetValue
(
key
u
.
Key
,
timeout
time
.
Duration
)
([]
byte
,
error
)
{
var
p
*
peer
.
Peer
p
=
s
.
routes
[
0
]
.
NearestPeer
(
c
onvertKey
(
key
))
p
=
s
.
routes
[
0
]
.
NearestPeer
(
kb
.
C
onvertKey
(
key
))
if
p
==
nil
{
return
nil
,
errors
.
New
(
"Table returned nil peer!"
)
}
...
...
@@ -90,7 +91,7 @@ func (s *IpfsDHT) GetValue(key u.Key, timeout time.Duration) ([]byte, error) {
// Announce that this node can provide value for given key
func
(
s
*
IpfsDHT
)
Provide
(
key
u
.
Key
)
error
{
peers
:=
s
.
routes
[
0
]
.
NearestPeers
(
c
onvertKey
(
key
),
PoolSize
)
peers
:=
s
.
routes
[
0
]
.
NearestPeers
(
kb
.
C
onvertKey
(
key
),
PoolSize
)
if
len
(
peers
)
==
0
{
//return an error
}
...
...
@@ -110,7 +111,7 @@ func (s *IpfsDHT) Provide(key u.Key) error {
// FindProviders searches for peers who can provide the value for given key.
func
(
s
*
IpfsDHT
)
FindProviders
(
key
u
.
Key
,
timeout
time
.
Duration
)
([]
*
peer
.
Peer
,
error
)
{
p
:=
s
.
routes
[
0
]
.
NearestPeer
(
c
onvertKey
(
key
))
p
:=
s
.
routes
[
0
]
.
NearestPeer
(
kb
.
C
onvertKey
(
key
))
pmes
:=
DHTMessage
{
Type
:
PBDHTMessage_GET_PROVIDERS
,
...
...
@@ -168,7 +169,7 @@ func (s *IpfsDHT) FindProviders(key u.Key, timeout time.Duration) ([]*peer.Peer,
// FindPeer searches for a peer with given ID.
func
(
s
*
IpfsDHT
)
FindPeer
(
id
peer
.
ID
,
timeout
time
.
Duration
)
(
*
peer
.
Peer
,
error
)
{
p
:=
s
.
routes
[
0
]
.
NearestPeer
(
c
onvertPeerID
(
id
))
p
:=
s
.
routes
[
0
]
.
NearestPeer
(
kb
.
C
onvertPeerID
(
id
))
pmes
:=
DHTMessage
{
Type
:
PBDHTMessage_FIND_NODE
,
...
...
@@ -242,7 +243,7 @@ func (dht *IpfsDHT) Ping(p *peer.Peer, timeout time.Duration) error {
func
(
dht
*
IpfsDHT
)
GetDiagnostic
(
timeout
time
.
Duration
)
([]
*
diagInfo
,
error
)
{
u
.
DOut
(
"Begin Diagnostic"
)
//Send to N closest peers
targets
:=
dht
.
routes
[
0
]
.
NearestPeers
(
c
onvertPeerID
(
dht
.
self
.
ID
),
10
)
targets
:=
dht
.
routes
[
0
]
.
NearestPeers
(
kb
.
C
onvertPeerID
(
dht
.
self
.
ID
),
10
)
// TODO: Add timeout to this struct so nodes know when to return
pmes
:=
DHTMessage
{
...
...
This diff is collapsed.
Click to expand it.
dh
t/bucket.go
→
kbucke
t/bucket.go
View file @
fed1d407
...
...
@@ -48,7 +48,7 @@ func (b *Bucket) Split(cpl int, target ID) *Bucket {
out
:=
list
.
New
()
e
:=
bucket_list
.
Front
()
for
e
!=
nil
{
peer_id
:=
c
onvertPeerID
(
e
.
Value
.
(
*
peer
.
Peer
)
.
ID
)
peer_id
:=
C
onvertPeerID
(
e
.
Value
.
(
*
peer
.
Peer
)
.
ID
)
peer_cpl
:=
prefLen
(
peer_id
,
target
)
if
peer_cpl
>
cpl
{
cur
:=
e
...
...
This diff is collapsed.
Click to expand it.
dh
t/table.go
→
kbucke
t/table.go
View file @
fed1d407
...
...
@@ -36,7 +36,7 @@ func NewRoutingTable(bucketsize int, local_id ID) *RoutingTable {
func
(
rt
*
RoutingTable
)
Update
(
p
*
peer
.
Peer
)
*
peer
.
Peer
{
rt
.
tabLock
.
Lock
()
defer
rt
.
tabLock
.
Unlock
()
peer_id
:=
c
onvertPeerID
(
p
.
ID
)
peer_id
:=
C
onvertPeerID
(
p
.
ID
)
cpl
:=
xor
(
peer_id
,
rt
.
local
)
.
commonPrefixLen
()
b_id
:=
cpl
...
...
@@ -97,7 +97,7 @@ func (p peerSorterArr) Less(a, b int) bool {
func
copyPeersFromList
(
target
ID
,
peerArr
peerSorterArr
,
peerList
*
list
.
List
)
peerSorterArr
{
for
e
:=
peerList
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
p
:=
e
.
Value
.
(
*
peer
.
Peer
)
p_id
:=
c
onvertPeerID
(
p
.
ID
)
p_id
:=
C
onvertPeerID
(
p
.
ID
)
pd
:=
peerDistance
{
p
:
p
,
distance
:
xor
(
target
,
p_id
),
...
...
@@ -173,7 +173,7 @@ func (rt *RoutingTable) Size() int {
}
// NOTE: This is potentially unsafe... use at your own risk
func
(
rt
*
RoutingTable
)
l
istpeers
()
[]
*
peer
.
Peer
{
func
(
rt
*
RoutingTable
)
L
istpeers
()
[]
*
peer
.
Peer
{
var
peers
[]
*
peer
.
Peer
for
_
,
buck
:=
range
rt
.
Buckets
{
for
e
:=
buck
.
getIter
();
e
!=
nil
;
e
=
e
.
Next
()
{
...
...
This diff is collapsed.
Click to expand it.
dh
t/table_test.go
→
kbucke
t/table_test.go
View file @
fed1d407
...
...
@@ -36,7 +36,7 @@ func TestBucket(t *testing.T) {
}
local
:=
_randPeer
()
local_id
:=
c
onvertPeerID
(
local
.
ID
)
local_id
:=
C
onvertPeerID
(
local
.
ID
)
i
:=
rand
.
Intn
(
len
(
peers
))
e
:=
b
.
Find
(
peers
[
i
]
.
ID
)
...
...
@@ -44,10 +44,10 @@ func TestBucket(t *testing.T) {
t
.
Errorf
(
"Failed to find peer: %v"
,
peers
[
i
])
}
spl
:=
b
.
Split
(
0
,
c
onvertPeerID
(
local
.
ID
))
spl
:=
b
.
Split
(
0
,
C
onvertPeerID
(
local
.
ID
))
llist
:=
(
*
list
.
List
)(
b
)
for
e
:=
llist
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
p
:=
c
onvertPeerID
(
e
.
Value
.
(
*
peer
.
Peer
)
.
ID
)
p
:=
C
onvertPeerID
(
e
.
Value
.
(
*
peer
.
Peer
)
.
ID
)
cpl
:=
xor
(
p
,
local_id
)
.
commonPrefixLen
()
if
cpl
>
0
{
t
.
Fatalf
(
"Split failed. found id with cpl > 0 in 0 bucket"
)
...
...
@@ -56,7 +56,7 @@ func TestBucket(t *testing.T) {
rlist
:=
(
*
list
.
List
)(
spl
)
for
e
:=
rlist
.
Front
();
e
!=
nil
;
e
=
e
.
Next
()
{
p
:=
c
onvertPeerID
(
e
.
Value
.
(
*
peer
.
Peer
)
.
ID
)
p
:=
C
onvertPeerID
(
e
.
Value
.
(
*
peer
.
Peer
)
.
ID
)
cpl
:=
xor
(
p
,
local_id
)
.
commonPrefixLen
()
if
cpl
==
0
{
t
.
Fatalf
(
"Split failed. found id with cpl == 0 in non 0 bucket"
)
...
...
@@ -67,7 +67,7 @@ func TestBucket(t *testing.T) {
// Right now, this just makes sure that it doesnt hang or crash
func
TestTableUpdate
(
t
*
testing
.
T
)
{
local
:=
_randPeer
()
rt
:=
NewRoutingTable
(
10
,
c
onvertPeerID
(
local
.
ID
))
rt
:=
NewRoutingTable
(
10
,
C
onvertPeerID
(
local
.
ID
))
peers
:=
make
([]
*
peer
.
Peer
,
100
)
for
i
:=
0
;
i
<
100
;
i
++
{
...
...
@@ -93,7 +93,7 @@ func TestTableUpdate(t *testing.T) {
func
TestTableFind
(
t
*
testing
.
T
)
{
local
:=
_randPeer
()
rt
:=
NewRoutingTable
(
10
,
c
onvertPeerID
(
local
.
ID
))
rt
:=
NewRoutingTable
(
10
,
C
onvertPeerID
(
local
.
ID
))
peers
:=
make
([]
*
peer
.
Peer
,
100
)
for
i
:=
0
;
i
<
5
;
i
++
{
...
...
@@ -102,7 +102,7 @@ func TestTableFind(t *testing.T) {
}
t
.
Logf
(
"Searching for peer: '%s'"
,
peers
[
2
]
.
ID
.
Pretty
())
found
:=
rt
.
NearestPeer
(
c
onvertPeerID
(
peers
[
2
]
.
ID
))
found
:=
rt
.
NearestPeer
(
C
onvertPeerID
(
peers
[
2
]
.
ID
))
if
!
found
.
ID
.
Equal
(
peers
[
2
]
.
ID
)
{
t
.
Fatalf
(
"Failed to lookup known node..."
)
}
...
...
@@ -110,7 +110,7 @@ func TestTableFind(t *testing.T) {
func
TestTableFindMultiple
(
t
*
testing
.
T
)
{
local
:=
_randPeer
()
rt
:=
NewRoutingTable
(
20
,
c
onvertPeerID
(
local
.
ID
))
rt
:=
NewRoutingTable
(
20
,
C
onvertPeerID
(
local
.
ID
))
peers
:=
make
([]
*
peer
.
Peer
,
100
)
for
i
:=
0
;
i
<
18
;
i
++
{
...
...
@@ -119,7 +119,7 @@ func TestTableFindMultiple(t *testing.T) {
}
t
.
Logf
(
"Searching for peer: '%s'"
,
peers
[
2
]
.
ID
.
Pretty
())
found
:=
rt
.
NearestPeers
(
c
onvertPeerID
(
peers
[
2
]
.
ID
),
15
)
found
:=
rt
.
NearestPeers
(
C
onvertPeerID
(
peers
[
2
]
.
ID
),
15
)
if
len
(
found
)
!=
15
{
t
.
Fatalf
(
"Got back different number of peers than we expected."
)
}
...
...
This diff is collapsed.
Click to expand it.
dh
t/util.go
→
kbucke
t/util.go
View file @
fed1d407
...
...
@@ -71,12 +71,12 @@ func equalizeSizes(a, b ID) (ID, ID) {
return
a
,
b
}
func
c
onvertPeerID
(
id
peer
.
ID
)
ID
{
func
C
onvertPeerID
(
id
peer
.
ID
)
ID
{
hash
:=
sha256
.
Sum256
(
id
)
return
hash
[
:
]
}
func
c
onvertKey
(
id
u
.
Key
)
ID
{
func
C
onvertKey
(
id
u
.
Key
)
ID
{
hash
:=
sha256
.
Sum256
([]
byte
(
id
))
return
hash
[
:
]
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment