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

Disable generation of Signed peer record for Mockenets #934

Merged
merged 1 commit into from
May 19, 2020
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
95 changes: 54 additions & 41 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ type BasicHost struct {

addrChangeChan chan struct{}

signKey crypto.PrivKey
caBook peerstore.CertifiedAddrBook
disableSignedPeerRecord bool
signKey crypto.PrivKey
caBook peerstore.CertifiedAddrBook
}

var _ host.Host = (*BasicHost)(nil)
Expand Down Expand Up @@ -138,22 +139,26 @@ type HostOpts struct {

// UserAgent sets the user-agent for the host. Defaults to ClientVersion.
UserAgent string

// DisableSignedPeerRecord disables the generation of Signed Peer Records on this host.
DisableSignedPeerRecord bool
}

// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHost, error) {
hostCtx, cancel := context.WithCancel(ctx)

h := &BasicHost{
network: net,
mux: msmux.NewMultistreamMuxer(),
negtimeout: DefaultNegotiationTimeout,
AddrsFactory: DefaultAddrsFactory,
maResolver: madns.DefaultResolver,
eventbus: eventbus.NewBus(),
addrChangeChan: make(chan struct{}, 1),
ctx: hostCtx,
ctxCancel: cancel,
network: net,
mux: msmux.NewMultistreamMuxer(),
negtimeout: DefaultNegotiationTimeout,
AddrsFactory: DefaultAddrsFactory,
maResolver: madns.DefaultResolver,
eventbus: eventbus.NewBus(),
addrChangeChan: make(chan struct{}, 1),
ctx: hostCtx,
ctxCancel: cancel,
disableSignedPeerRecord: opts.DisableSignedPeerRecord,
}

var err error
Expand All @@ -164,23 +169,39 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo
return nil, err
}

cab, ok := peerstore.GetCertifiedAddrBook(net.Peerstore())
if !ok {
return nil, errors.New("peerstore should also be a certified address book")
}
h.caBook = cab
if !h.disableSignedPeerRecord {
cab, ok := peerstore.GetCertifiedAddrBook(net.Peerstore())
if !ok {
return nil, errors.New("peerstore should also be a certified address book")
}
h.caBook = cab

h.signKey = h.Peerstore().PrivKey(h.ID())
if h.signKey == nil {
return nil, errors.New("unable to access host key")
h.signKey = h.Peerstore().PrivKey(h.ID())
if h.signKey == nil {
return nil, errors.New("unable to access host key")
}

// persist a signed peer record for self to the peerstore.
rec := peer.PeerRecordFromAddrInfo(peer.AddrInfo{h.ID(), h.Addrs()})
ev, err := record.Seal(rec, h.signKey)
if err != nil {
return nil, fmt.Errorf("failed to create signed record for self: %w", err)
}
if _, err := cab.ConsumePeerRecord(ev, peerstore.PermanentAddrTTL); err != nil {
return nil, fmt.Errorf("failed to persist signed record to peerstore: %w", err)
}
}

if opts.MultistreamMuxer != nil {
h.mux = opts.MultistreamMuxer
}

// we can't set this as a default above because it depends on the *BasicHost.
h.ids = identify.NewIDService(h, identify.UserAgent(opts.UserAgent))
if h.disableSignedPeerRecord {
h.ids = identify.NewIDService(h, identify.UserAgent(opts.UserAgent), identify.DisableSignedPeerRecord())
} else {
h.ids = identify.NewIDService(h, identify.UserAgent(opts.UserAgent))
}

if uint64(opts.NegotiationTimeout) != 0 {
h.negtimeout = opts.NegotiationTimeout
Expand Down Expand Up @@ -211,16 +232,6 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo

net.SetStreamHandler(h.newStreamHandler)

// persist a signed peer record for self to the peerstore.
rec := peer.PeerRecordFromAddrInfo(peer.AddrInfo{h.ID(), h.Addrs()})
ev, err := record.Seal(rec, h.signKey)
if err != nil {
return nil, fmt.Errorf("failed to create signed record for self: %w", err)
}
if _, err := cab.ConsumePeerRecord(ev, peerstore.PermanentAddrTTL); err != nil {
return nil, fmt.Errorf("failed to persist signed record to peerstore: %w", err)
}

return h, nil
}

Expand Down Expand Up @@ -384,18 +395,20 @@ func (h *BasicHost) background() {
return
}

// add signed peer record to the event
sr, err := h.makeSignedPeerRecord(changeEvt)
if err != nil {
log.Errorf("error creating a signed peer record from the set of current addresses, err=%s", err)
return
}
changeEvt.SignedPeerRecord = sr
if !h.disableSignedPeerRecord {
// add signed peer record to the event
sr, err := h.makeSignedPeerRecord(changeEvt)
if err != nil {
log.Errorf("error creating a signed peer record from the set of current addresses, err=%s", err)
return
}
changeEvt.SignedPeerRecord = sr

// persist the signed record to the peerstore
if _, err := h.caBook.ConsumePeerRecord(sr, peerstore.PermanentAddrTTL); err != nil {
log.Errorf("failed to persist signed peer record in peer store, err=%s", err)
return
// persist the signed record to the peerstore
if _, err := h.caBook.ConsumePeerRecord(sr, peerstore.PermanentAddrTTL); err != nil {
log.Errorf("failed to persist signed peer record in peer store, err=%s", err)
return
}
}

// emit addr change event on the bus
Expand Down
3 changes: 2 additions & 1 deletion p2p/net/mock/mock_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps peerstore.Peerstore) (host
}

opts := &bhost.HostOpts{
NegotiationTimeout: -1,
NegotiationTimeout: -1,
DisableSignedPeerRecord: true,
}

h, err := bhost.NewHost(mn.ctx, n, opts)
Expand Down
28 changes: 18 additions & 10 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ type IDService struct {
// track resources that need to be shut down before we shut down
refCount sync.WaitGroup

disableSignedPeerRecord bool

// Identified connections (finished and in progress).
connsMu sync.RWMutex
conns map[network.Conn]chan struct{}
Expand Down Expand Up @@ -129,6 +131,8 @@ func NewIDService(h host.Host, opts ...Option) *IDService {
conns: make(map[network.Conn]chan struct{}),
observedAddrs: NewObservedAddrManager(hostCtx, h),

disableSignedPeerRecord: cfg.disableSignedPeerRecord,

addPeerHandlerCh: make(chan addPeerHandlerReq),
rmPeerHandlerCh: make(chan rmPeerHandlerReq),
}
Expand Down Expand Up @@ -421,10 +425,12 @@ func (ids *IDService) handleIdentifyResponse(s network.Stream) {

func (ids *IDService) getSnapshot() *identifySnapshot {
snapshot := new(identifySnapshot)
if cab, ok := peerstore.GetCertifiedAddrBook(ids.Host.Peerstore()); ok {
snapshot.record = cab.GetPeerRecord(ids.Host.ID())
if snapshot.record == nil {
log.Errorf("latest peer record does not exist. identify message incomplete!")
if !ids.disableSignedPeerRecord {
if cab, ok := peerstore.GetCertifiedAddrBook(ids.Host.Peerstore()); ok {
snapshot.record = cab.GetPeerRecord(ids.Host.ID())
if snapshot.record == nil {
log.Errorf("latest peer record does not exist. identify message incomplete!")
}
}
}
snapshot.addrs = ids.Host.Addrs()
Expand Down Expand Up @@ -459,12 +465,14 @@ func (ids *IDService) populateMessage(
mes.ListenAddrs = append(mes.ListenAddrs, addr.Bytes())
}

recBytes, err := snapshot.record.Marshal()
if err != nil {
log.Errorf("error marshaling peer record: %v", err)
} else {
mes.SignedPeerRecord = recBytes
log.Debugf("%s sent peer record to %s", ids.Host.ID(), conn.RemotePeer())
if !ids.disableSignedPeerRecord {
recBytes, err := snapshot.record.Marshal()
if err != nil {
log.Errorf("error marshaling peer record: %v", err)
} else {
mes.SignedPeerRecord = recBytes
log.Debugf("%s sent peer record to %s", ids.Host.ID(), conn.RemotePeer())
}
}

// set our public key
Expand Down
11 changes: 10 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
disableSignedPeerRecord bool
}

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

// DisableSignedPeerRecord disables populating signed peer records on the outgoing Identify response
// and ONLY sends the unsigned addresses.
func DisableSignedPeerRecord() Option {
return func(cfg *config) {
cfg.disableSignedPeerRecord = true
}
}