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
p2p
go-p2p-kad-dht
Commits
c6ad92e8
Commit
c6ad92e8
authored
Feb 19, 2018
by
Dirk McCormick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor GetClosestPeers()
parent
9792ab13
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
42 deletions
+5
-42
lookup.go
lookup.go
+5
-42
No files found.
lookup.go
View file @
c6ad92e8
...
...
@@ -7,30 +7,12 @@ import (
cid
"github.com/ipfs/go-cid"
logging
"github.com/ipfs/go-log"
pb
"github.com/libp2p/go-libp2p-kad-dht/pb"
kb
"github.com/libp2p/go-libp2p-kbucket"
peer
"github.com/libp2p/go-libp2p-peer"
pstore
"github.com/libp2p/go-libp2p-peerstore"
notif
"github.com/libp2p/go-libp2p-routing/notifications"
)
// Required in order for proper JSON marshaling
func
pointerizePeerInfos
(
pis
[]
pstore
.
PeerInfo
)
[]
*
pstore
.
PeerInfo
{
out
:=
make
([]
*
pstore
.
PeerInfo
,
len
(
pis
))
for
i
,
p
:=
range
pis
{
np
:=
p
out
[
i
]
=
&
np
}
return
out
}
func
toPeerInfos
(
ps
[]
peer
.
ID
)
[]
*
pstore
.
PeerInfo
{
out
:=
make
([]
*
pstore
.
PeerInfo
,
len
(
ps
))
for
i
,
p
:=
range
ps
{
out
[
i
]
=
&
pstore
.
PeerInfo
{
ID
:
p
}
}
return
out
}
func
tryFormatLoggableKey
(
k
string
)
(
string
,
error
)
{
if
len
(
k
)
==
0
{
return
""
,
fmt
.
Errorf
(
"loggableKey is empty"
)
...
...
@@ -90,22 +72,21 @@ func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key string) (<-chan pee
ID
:
p
,
})
closer
,
err
:=
dht
.
closer
Peer
s
Single
(
ctx
,
key
,
p
)
pmes
,
err
:=
dht
.
find
PeerSingle
(
ctx
,
p
,
peer
.
ID
(
key
)
)
if
err
!=
nil
{
log
.
Debugf
(
"error getting closer peers: %s"
,
err
)
return
nil
,
err
}
peerinfos
:=
toPeerInfos
(
closer
)
peers
:=
pb
.
PBPeersToPeerInfos
(
pmes
.
GetCloserPeers
())
// For DHT query command
notif
.
PublishQueryEvent
(
parent
,
&
notif
.
QueryEvent
{
Type
:
notif
.
PeerResponse
,
ID
:
p
,
Responses
:
peer
infos
,
// todo: remove need for this pointerize thing
Responses
:
peer
s
,
})
return
&
dhtQueryResult
{
closerPeers
:
peer
info
s
},
nil
return
&
dhtQueryResult
{
closerPeers
:
peers
},
nil
})
go
func
()
{
...
...
@@ -131,21 +112,3 @@ func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key string) (<-chan pee
return
out
,
nil
}
func
(
dht
*
IpfsDHT
)
closerPeersSingle
(
ctx
context
.
Context
,
key
string
,
p
peer
.
ID
)
([]
peer
.
ID
,
error
)
{
pmes
,
err
:=
dht
.
findPeerSingle
(
ctx
,
p
,
peer
.
ID
(
key
))
if
err
!=
nil
{
return
nil
,
err
}
closer
:=
pmes
.
GetCloserPeers
()
out
:=
make
([]
peer
.
ID
,
0
,
len
(
closer
))
for
_
,
pbp
:=
range
closer
{
pid
:=
peer
.
ID
(
pbp
.
GetId
())
if
pid
!=
dht
.
self
{
// dont add self
dht
.
peerstore
.
AddAddrs
(
pid
,
pbp
.
Addresses
(),
pstore
.
TempAddrTTL
)
out
=
append
(
out
,
pid
)
}
}
return
out
,
nil
}
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