Skip to content
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

add remote multiaddr from active connection to peerstore #15

Merged
merged 2 commits into from
Feb 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ install: true
script:
- make deps
- go test ./p2p/...

cache:
directories:
- $GOPATH/src/gx
28 changes: 15 additions & 13 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,21 @@ func (ids *IDService) IdentifyConn(c inet.Conn) {
log.Event(context.TODO(), "IdentifyOpenFailed", c.RemotePeer())
c.Close()
return
} else {
bwc := ids.Host.GetBandwidthReporter()
s = mstream.WrapStream(s, ID, bwc)

// ok give the response to our handler.
if err := msmux.SelectProtoOrFail(ID, s); err != nil {
log.Debugf("error writing stream header for %s", ID)
log.Event(context.TODO(), "IdentifyOpenFailed", c.RemotePeer())
s.Close()
return
} else {
ids.ResponseHandler(s)
}
}

bwc := ids.Host.GetBandwidthReporter()
s = mstream.WrapStream(s, ID, bwc)

// ok give the response to our handler.
if err := msmux.SelectProtoOrFail(ID, s); err != nil {
log.Debugf("error writing stream header for %s", ID)
log.Event(context.TODO(), "IdentifyOpenFailed", c.RemotePeer())
s.Close()
return
}

ids.ResponseHandler(s)

ids.currmu.Lock()
ch, found := ids.currid[c]
delete(ids.currid, c)
Expand Down Expand Up @@ -190,6 +190,8 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c inet.Conn) {
lmaddrs = append(lmaddrs, maddr)
}

lmaddrs = append(lmaddrs, c.RemoteMultiaddr())

// update our peerstore with the addresses. here, we SET the addresses, clearing old ones.
// We are receiving from the peer itself. this is current address ground truth.
ids.Host.Peerstore().SetAddrs(p, lmaddrs, peer.ConnectedAddrTTL)
Expand Down
11 changes: 9 additions & 2 deletions p2p/protocol/identify/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func subtestIDService(t *testing.T, postDialWait time.Duration) {

// the IDService should be opened automatically, by the network.
// what we should see now is that both peers know about each others listen addresses.
t.Log("test peer1 has peer2 addrs correctly")
testKnowsAddrs(t, h1, h2p, h2.Peerstore().Addrs(h2p)) // has them
testHasProtocolVersions(t, h1, h2p)

Expand All @@ -49,16 +50,22 @@ func subtestIDService(t *testing.T, postDialWait time.Duration) {
}
<-h2.IDService().IdentifyWait(c[0])

addrs := h1.Peerstore().Addrs(h1p)
addrs = append(addrs, c[0].RemoteMultiaddr())

// and the protocol versions.
testKnowsAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) // has them
t.Log("test peer2 has peer1 addrs correctly")
testKnowsAddrs(t, h2, h1p, addrs) // has them
testHasProtocolVersions(t, h2, h1p)
}

func testKnowsAddrs(t *testing.T, h host.Host, p peer.ID, expected []ma.Multiaddr) {
actual := h.Peerstore().Addrs(p)

if len(actual) != len(expected) {
t.Error("dont have the same addresses")
t.Errorf("expected: %s", expected)
t.Errorf("actual: %s", actual)
t.Fatal("dont have the same addresses")
}

have := map[string]struct{}{}
Expand Down