Skip to content

Commit

Permalink
add option to disable signed addrs for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
yusefnapora committed Nov 27, 2019
1 parent 3fa6e4f commit 028fe98
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
35 changes: 22 additions & 13 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ type IDService struct {
emitters struct {
evtPeerProtocolsUpdated event.Emitter
}

useSignedAddrs bool
}

// NewIDService constructs a new *IDService and activates it by
Expand All @@ -107,9 +109,10 @@ func NewIDService(ctx context.Context, h host.Host, opts ...Option) *IDService {
Host: h,
UserAgent: userAgent,

ctx: ctx,
currid: make(map[network.Conn]chan struct{}),
observedAddrs: NewObservedAddrSet(ctx),
ctx: ctx,
currid: make(map[network.Conn]chan struct{}),
observedAddrs: NewObservedAddrSet(ctx),
useSignedAddrs: !cfg.disableSignedAddrSupport,
}

// handle local protocol handler updates, and push deltas to peers.
Expand Down Expand Up @@ -319,15 +322,17 @@ func (ids *IDService) populateMessage(mes *pb.Identify, c network.Conn) {

// Generate a signed routing record containing our listen addresses and
// send it along with the unsigned addrs
signedState, err := host.SignedRoutingStateFromHost(ids.Host)
if err != nil {
log.Warningf("error generating signed routing state: %v", err)
} else {
envelopeBytes, err := signedState.Marshal()
if ids.useSignedAddrs {
signedState, err := host.SignedRoutingStateFromHost(ids.Host)
if err != nil {
log.Warningf("error marshaling signed routing state: %v", err)
log.Warningf("error generating signed routing state: %v", err)
} else {
mes.SignedRoutingState = envelopeBytes
envelopeBytes, err := signedState.Marshal()
if err != nil {
log.Warningf("error marshaling signed routing state: %v", err)
} else {
mes.SignedRoutingState = envelopeBytes
}
}
}

Expand Down Expand Up @@ -391,9 +396,13 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c network.Conn) {

// add certified addresses for the peer, if they sent us a signed routing
// state record
routingState, err := signedRoutingStateFromMsg(mes)
if err != nil {
log.Warningf("error getting routing state from Identify message: %v", err)
var routingState *routing.SignedRoutingState
if ids.useSignedAddrs {
var err error
routingState, err = signedRoutingStateFromMsg(mes)
if err != nil {
log.Warningf("error getting routing state from Identify message: %v", err)
}
}

// Extend the TTLs on the known (probably) good addresses.
Expand Down
43 changes: 43 additions & 0 deletions p2p/protocol/identify/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,46 @@ func TestUserAgent(t *testing.T) {
t.Errorf("expected agent version %q, got %q", "bar", av)
}
}

// make sure that we still use the unsigned listen addresses if the remote peer
// does not send us a signed address record
func TestCompatibilityWithPeersThatDoNotSupportSignedAddrs(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

h1 := blhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
h2 := blhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
defer h2.Close()
defer h1.Close()

h1p := h1.ID()
h2p := h2.ID()
ids1 := identify.NewIDService(ctx, h1)
ids2 := identify.NewIDService(ctx, h2, identify.DisableSignedAddrSupport())

h2pi := h2.Peerstore().PeerInfo(h2p)
if err := h1.Connect(ctx, h2pi); err != nil {
t.Fatal(err)
}

h1t2c := h1.Network().ConnsToPeer(h2p)
if len(h1t2c) == 0 {
t.Fatal("should have a conn here")
}

ids1.IdentifyConn(h1t2c[0])

// 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
testHasCertifiedAddrs(t, h1, h2p, []ma.Multiaddr{}) // should not have signed addrs

c := h2.Network().ConnsToPeer(h1.ID())
if len(c) < 1 {
t.Fatal("should have connection by now at least.")
}
ids2.IdentifyConn(c[0])
testKnowsAddrs(t, h2, h1p, h1.Peerstore().Addrs(h1p)) // has them
testHasCertifiedAddrs(t, h2, h1p, []ma.Multiaddr{}) // no signed addrs
}
12 changes: 11 additions & 1 deletion p2p/protocol/identify/opts.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package identify

type config struct {
userAgent string
userAgent string
disableSignedAddrSupport bool
}

// Option is an option function for identify.
Expand All @@ -13,3 +14,12 @@ func UserAgent(ua string) Option {
cfg.userAgent = ua
}
}

// DisableSignedAddrSupport prevents the identify service from sending or parsing
// routing.SignedRoutingState messages during the exchange. Used for testing
// compatibility with older versions that do not support signed addresses.
func DisableSignedAddrSupport() Option {
return func(cfg *config) {
cfg.disableSignedAddrSupport = true
}
}

0 comments on commit 028fe98

Please sign in to comment.