diff --git a/node/config/def.go b/node/config/def.go index 23644f799..446eb9080 100644 --- a/node/config/def.go +++ b/node/config/def.go @@ -36,6 +36,7 @@ type API struct { type Libp2p struct { ListenAddresses []string BootstrapPeers []string + ProtectedPeers []string } // // Full Node diff --git a/node/hello/hello.go b/node/hello/hello.go index 916236a8a..c3415f926 100644 --- a/node/hello/hello.go +++ b/node/hello/hello.go @@ -38,7 +38,7 @@ type Message struct { type NewStreamFunc func(context.Context, peer.ID, ...protocol.ID) (inet.Stream, error) type Service struct { - newStream NewStreamFunc + h host.Host cs *store.ChainStore syncer *chain.Syncer @@ -51,7 +51,7 @@ func NewHelloService(h host.Host, cs *store.ChainStore, syncer *chain.Syncer, pm } return &Service{ - newStream: h.NewStream, + h: h, cs: cs, syncer: syncer, @@ -99,6 +99,8 @@ func (hs *Service) HandleStream(s inet.Stream) { } if ts.TipSet().Height() > 0 { + hs.h.ConnManager().TagPeer(s.Conn().RemotePeer(), "fcpeer", 1) + // don't bother informing about genesis log.Infof("Got new tipset through Hello: %s from %s", ts.Cids(), s.Conn().RemotePeer()) hs.syncer.InformNewHead(s.Conn().RemotePeer(), ts) @@ -110,7 +112,7 @@ func (hs *Service) HandleStream(s inet.Stream) { } func (hs *Service) SayHello(ctx context.Context, pid peer.ID) error { - s, err := hs.newStream(ctx, pid, ProtocolID) + s, err := hs.h.NewStream(ctx, pid, ProtocolID) if err != nil { return err } diff --git a/node/modules/lp2p/libp2p.go b/node/modules/lp2p/libp2p.go index 511b5c775..e50c8a922 100644 --- a/node/modules/lp2p/libp2p.go +++ b/node/modules/lp2p/libp2p.go @@ -63,9 +63,17 @@ func genLibp2pKey() (crypto.PrivKey, error) { // Misc options -func ConnectionManager(low, high int, grace time.Duration) func() (opts Libp2pOpts, err error) { - return func() (opts Libp2pOpts, err error) { +func ConnectionManager(low, high int, grace time.Duration, protected []string) func() (opts Libp2pOpts, err error) { + return func() (Libp2pOpts, error) { cm := connmgr.NewConnManager(low, high, grace) + for _, p := range protected { + pid, err := peer.IDFromString(p) + if err != nil { + return nil, xerrors.Errorf("failed to parse peer ID in protected peers array: %w", err) + } + + cm.Protect(pid, "config-prot") + } opts.Opts = append(opts.Opts, libp2p.ConnectionManager(cm)) return }