-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Peerstore Fix #128
Peerstore Fix #128
Changes from 7 commits
a93a31f
0817ffa
53f0b11
e6b96b9
943c3df
2507680
c121930
ee3be12
198b6e2
c7bd8b7
484d600
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,23 +90,18 @@ func (s *SecurePipe) handshake() error { | |
return err | ||
} | ||
|
||
s.remote.PubKey, err = ci.UnmarshalPublicKey(proposeResp.GetPubkey()) | ||
// get remote identity | ||
remotePubKey, err := ci.UnmarshalPublicKey(proposeResp.GetPubkey()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
remoteID, err := IDFromPubKey(s.remote.PubKey) | ||
// get or construct peer | ||
s.remote, err = getOrConstructPeer(s.peers, remotePubKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if s.remote.ID != nil && !remoteID.Equal(s.remote.ID) { | ||
e := "Expected pubkey does not match sent pubkey: %v - %v" | ||
return fmt.Errorf(e, s.remote.ID.Pretty(), remoteID.Pretty()) | ||
} else if s.remote.ID == nil { | ||
s.remote.ID = remoteID | ||
} | ||
// u.POut("Remote Peer Identified as %s\n", s.remote.ID.Pretty()) | ||
u.DOut("[%s] Remote Peer Identified as %s\n", s.local.ID.Pretty(), s.remote.ID.Pretty()) | ||
|
||
exchange, err := selectBest(SupportedExchanges, proposeResp.GetExchanges()) | ||
if err != nil { | ||
|
@@ -340,3 +335,62 @@ func selectBest(myPrefs, theirPrefs string) (string, error) { | |
|
||
return "", errors.New("No algorithms in common!") | ||
} | ||
|
||
// getOrConstructPeer attempts to fetch a peer from a peerstore. | ||
// if succeeds, verify ID and PubKey match. | ||
// else, construct it. | ||
func getOrConstructPeer(peers peer.Peerstore, rpk ci.PubKey) (*peer.Peer, error) { | ||
|
||
rid, err := IDFromPubKey(rpk) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
npeer, err := peers.Get(rid) | ||
if err != nil { | ||
if err != peer.ErrNotFound { | ||
return nil, err // unexpected error happened. | ||
} | ||
|
||
// dont have peer, so construct it + add it to peerstore. | ||
npeer = &peer.Peer{ID: rid, PubKey: rpk} | ||
if err := peers.Put(npeer); err != nil { | ||
return nil, err | ||
} | ||
|
||
// done, return the newly constructed peer. | ||
return npeer, nil | ||
} | ||
|
||
// did have it locally. | ||
|
||
// let's verify ID | ||
if !npeer.ID.Equal(rid) { | ||
e := "Expected peer.ID does not match sent pubkey's hash: %v - %v" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be an interesting scenario that this could fail in.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I added that test case because I could be Dialing to a peer1 and then i receive back peer2's pubkey, and thus am like wat!? (note that if this is an incoming connection, peer is ErrNotFound above, and returns beforehand. Did prompt me to find a bug though (if |
||
return nil, fmt.Errorf(e, npeer.ID.Pretty(), rid.Pretty()) | ||
} | ||
|
||
if npeer.PubKey == nil { | ||
// didn't have a pubkey, just set it. | ||
npeer.PubKey = rpk | ||
return npeer, nil | ||
} | ||
|
||
// did have pubkey, let's verify it's really the same. | ||
// this shouldn't ever happen, given we hashed, etc, but it could mean | ||
// expected code (or protocol) invariants violated. | ||
|
||
lb, err1 := npeer.PubKey.Bytes() | ||
if err1 != nil { | ||
return nil, err1 | ||
} | ||
rb, err2 := rpk.Bytes() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. something like: PubKey.Equals(otherpubkey) might be nice |
||
if err2 != nil { | ||
return nil, err2 | ||
} | ||
|
||
if !bytes.Equal(lb, rb) { | ||
return nil, fmt.Errorf("WARNING: PubKey mismatch: %v", npeer.ID.Pretty()) | ||
} | ||
return npeer, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
such a large string!