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
e835efca
Commit
e835efca
authored
Mar 07, 2018
by
Dirk McCormick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
GetPublicKey: Remove unneccesary public key verification. Simplify select.
parent
40eb5dc9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
26 deletions
+27
-26
records.go
records.go
+27
-26
No files found.
records.go
View file @
e835efca
...
...
@@ -40,12 +40,20 @@ func (dht *IpfsDHT) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, err
// Try getting the public key both directly from the node it identifies
// and from the DHT, in parallel
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
defer
cancel
()
resp
:=
make
(
chan
pubkrs
,
2
)
go
func
()
{
pubk
,
err
:=
dht
.
getPublicKeyFromNode
(
ctx
,
p
)
resp
<-
pubkrs
{
pubk
,
err
}
}()
// Note that the number of open connections is capped by the dial
// limiter, so there is a chance that getPublicKeyFromDHT(), which
// potentially opens a lot of connections, will block
// getPublicKeyFromNode() from getting a connection.
// Currently this doesn't seem to cause an issue so leaving as is
// for now.
go
func
()
{
pubk
,
err
:=
dht
.
getPublicKeyFromDHT
(
ctx
,
p
)
resp
<-
pubkrs
{
pubk
,
err
}
...
...
@@ -55,20 +63,16 @@ func (dht *IpfsDHT) GetPublicKey(ctx context.Context, p peer.ID) (ci.PubKey, err
// a public key (or for both to error out)
var
err
error
for
i
:=
0
;
i
<
2
;
i
++
{
select
{
case
<-
ctx
.
Done
()
:
return
nil
,
ctx
.
Err
()
case
r
:=
<-
resp
:
if
r
.
err
==
nil
{
// Found the public key
err
:=
dht
.
peerstore
.
AddPubKey
(
p
,
r
.
pubk
)
if
err
!=
nil
{
log
.
Warningf
(
"Failed to add public key to peerstore for %v"
,
p
)
}
return
r
.
pubk
,
nil
r
:=
<-
resp
if
r
.
err
==
nil
{
// Found the public key
err
:=
dht
.
peerstore
.
AddPubKey
(
p
,
r
.
pubk
)
if
err
!=
nil
{
log
.
Warningf
(
"Failed to add public key to peerstore for %v"
,
p
)
}
err
=
r
.
err
return
r
.
pubk
,
nil
}
err
=
r
.
err
}
// Both go routines failed to find a public key
...
...
@@ -89,14 +93,16 @@ func (dht *IpfsDHT) getPublicKeyFromDHT(ctx context.Context, p peer.ID) (ci.PubK
return
nil
,
routing
.
ErrNotFound
}
_
,
err
=
dht
.
Selector
.
BestRecord
(
pkkey
,
[][]
byte
{
vals
[
0
]
.
Val
}
)
pubk
,
err
:
=
ci
.
UnmarshalPublicKey
(
vals
[
0
]
.
Val
)
if
err
!=
nil
{
log
.
Errorf
(
"
Failed to verify
public key
for %v
retrieved from DHT
:
%
s
"
,
p
,
err
)
log
.
Errorf
(
"
Could not unmarshall
public key retrieved from DHT
for
%
v
"
,
p
)
return
nil
,
err
}
log
.
Debugf
(
"DHT got public key for %s from DHT"
,
p
)
return
dht
.
pubkFromVal
(
p
,
vals
[
0
]
.
Val
)
// Note: No need to check that public key hash matches peer ID
// because this is done by GetValues()
log
.
Debugf
(
"Got public key for %s from DHT"
,
p
)
return
pubk
,
nil
}
func
(
dht
*
IpfsDHT
)
getPublicKeyFromNode
(
ctx
context
.
Context
,
p
peer
.
ID
)
(
ci
.
PubKey
,
error
)
{
...
...
@@ -116,30 +122,25 @@ func (dht *IpfsDHT) getPublicKeyFromNode(ctx context.Context, p peer.ID) (ci.Pub
// node doesn't have key :(
record
:=
pmes
.
GetRecord
()
if
record
==
nil
{
return
nil
,
fmt
.
Errorf
(
"
N
ode %v not responding with its public key"
,
p
)
return
nil
,
fmt
.
Errorf
(
"
n
ode %v not responding with its public key"
,
p
)
}
val
:=
record
.
GetValue
()
log
.
Debugf
(
"DHT got public key from node %v itself"
,
p
)
return
dht
.
pubkFromVal
(
p
,
val
)
}
func
(
dht
*
IpfsDHT
)
pubkFromVal
(
p
peer
.
ID
,
val
[]
byte
)
(
ci
.
PubKey
,
error
)
{
pubk
,
err
:=
ci
.
UnmarshalPublicKey
(
val
)
pubk
,
err
:=
ci
.
UnmarshalPublicKey
(
record
.
GetValue
())
if
err
!=
nil
{
log
.
Errorf
(
"
DHT c
ould not unmarshall public key for %v"
,
p
)
log
.
Errorf
(
"
C
ould not unmarshall public key for %v"
,
p
)
return
nil
,
err
}
// Make sure the public key matches the peer ID
id
,
err
:=
peer
.
IDFromPublicKey
(
pubk
)
if
err
!=
nil
{
log
.
Errorf
(
"
DHT c
ould not extract peer id from public key for %v"
,
p
)
log
.
Errorf
(
"
C
ould not extract peer id from public key for %v"
,
p
)
return
nil
,
err
}
if
id
!=
p
{
return
nil
,
fmt
.
Errorf
(
"public key %v does not match peer %v"
,
id
,
p
)
}
log
.
Debugf
(
"Got public key from node %v itself"
,
p
)
return
pubk
,
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