allow specifying some protected peers in the config file

This commit is contained in:
whyrusleeping 2019-12-16 21:37:31 -08:00
parent 4ace1fe436
commit eb4077b1af
3 changed files with 16 additions and 5 deletions

View File

@ -36,6 +36,7 @@ type API struct {
type Libp2p struct { type Libp2p struct {
ListenAddresses []string ListenAddresses []string
BootstrapPeers []string BootstrapPeers []string
ProtectedPeers []string
} }
// // Full Node // // Full Node

View File

@ -38,7 +38,7 @@ type Message struct {
type NewStreamFunc func(context.Context, peer.ID, ...protocol.ID) (inet.Stream, error) type NewStreamFunc func(context.Context, peer.ID, ...protocol.ID) (inet.Stream, error)
type Service struct { type Service struct {
newStream NewStreamFunc h host.Host
cs *store.ChainStore cs *store.ChainStore
syncer *chain.Syncer syncer *chain.Syncer
@ -51,7 +51,7 @@ func NewHelloService(h host.Host, cs *store.ChainStore, syncer *chain.Syncer, pm
} }
return &Service{ return &Service{
newStream: h.NewStream, h: h,
cs: cs, cs: cs,
syncer: syncer, syncer: syncer,
@ -99,6 +99,8 @@ func (hs *Service) HandleStream(s inet.Stream) {
} }
if ts.TipSet().Height() > 0 { if ts.TipSet().Height() > 0 {
hs.h.ConnManager().TagPeer(s.Conn().RemotePeer(), "fcpeer", 1)
// don't bother informing about genesis // don't bother informing about genesis
log.Infof("Got new tipset through Hello: %s from %s", ts.Cids(), s.Conn().RemotePeer()) log.Infof("Got new tipset through Hello: %s from %s", ts.Cids(), s.Conn().RemotePeer())
hs.syncer.InformNewHead(s.Conn().RemotePeer(), ts) 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 { 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 { if err != nil {
return err return err
} }

View File

@ -63,9 +63,17 @@ func genLibp2pKey() (crypto.PrivKey, error) {
// Misc options // Misc options
func ConnectionManager(low, high int, grace time.Duration) func() (opts Libp2pOpts, err error) { func ConnectionManager(low, high int, grace time.Duration, protected []string) func() (opts Libp2pOpts, err error) {
return func() (opts Libp2pOpts, err error) { return func() (Libp2pOpts, error) {
cm := connmgr.NewConnManager(low, high, grace) 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)) opts.Opts = append(opts.Opts, libp2p.ConnectionManager(cm))
return return
} }