distinguish between seeds and manual peers in the config/flags

- we only use seeds if we can’t connect to peers in the addrbook.
- we always connect to nodes given in config/flags

Refs #864
This commit is contained in:
Anton Kaliaev
2017-12-28 12:54:39 -06:00
parent 179d6062e4
commit 28fc15028a
28 changed files with 112 additions and 107 deletions

View File

@ -380,40 +380,44 @@ func (n *Node) OnStart() error {
return err
}
err = n.dialSeedsIfAddrBookIsEmptyOrPEXFailedToConnect()
if err != nil {
return err
// Always connect to manual peers
if n.config.P2P.ManualPeers != "" {
err = n.sw.DialPeersAsync(n.addrBook, strings.Split(n.config.P2P.ManualPeers, ","), true)
if err != nil {
return err
}
}
if n.config.P2P.Seeds != "" {
err = n.dialSeedsIfAddrBookIsEmptyOrPEXFailedToConnect(strings.Split(n.config.P2P.Seeds, ","))
if err != nil {
return err
}
}
// start tx indexer
return n.indexerService.Start()
}
func (n *Node) dialSeedsIfAddrBookIsEmptyOrPEXFailedToConnect() error {
if n.config.P2P.Seeds == "" {
return nil
}
seeds := strings.Split(n.config.P2P.Seeds, ",")
func (n *Node) dialSeedsIfAddrBookIsEmptyOrPEXFailedToConnect(seeds []string) error {
// prefer peers from address book
if n.config.P2P.PexReactor && n.addrBook.Size() > 0 {
// give some time to PexReactor to connect us to other peers
const fallbackToSeedsAfterSec = 30 * time.Second
// give some time for PexReactor to connect us to other peers
const fallbackToSeedsAfter = 30 * time.Second
go func() {
time.Sleep(fallbackToSeedsAfterSec)
time.Sleep(fallbackToSeedsAfter)
// fallback to dialing seeds if for some reason we can't connect to any
// peers
outbound, inbound, _ := n.sw.NumPeers()
if n.IsRunning() && outbound+inbound == 0 {
n.DialSeeds(seeds)
// TODO: ignore error?
n.sw.DialPeersAsync(n.addrBook, seeds, false)
}
}()
return nil
}
// add seeds to the address book and dial out
return n.DialSeeds(seeds)
return n.sw.DialPeersAsync(n.addrBook, seeds, false)
}
// OnStop stops the Node. It implements cmn.Service.
@ -599,11 +603,6 @@ func (n *Node) NodeInfo() *p2p.NodeInfo {
return n.sw.NodeInfo()
}
// DialSeeds dials the given seeds on the Switch.
func (n *Node) DialSeeds(seeds []string) error {
return n.sw.DialSeeds(n.addrBook, seeds)
}
//------------------------------------------------------------------------------
var (