From f3dc113dbaa18ab5998637cfc54d53c56ce40306 Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Thu, 5 Mar 2020 08:49:26 -0600 Subject: [PATCH] add multi-options (#5012) --- beacon-chain/p2p/discovery.go | 35 ++++++++++++++++++++++++++--------- beacon-chain/p2p/service.go | 2 +- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/beacon-chain/p2p/discovery.go b/beacon-chain/p2p/discovery.go index d17f37ee4..a1709b456 100644 --- a/beacon-chain/p2p/discovery.go +++ b/beacon-chain/p2p/discovery.go @@ -109,6 +109,14 @@ func startDHTDiscovery(host core.Host, bootstrapAddr string) error { } func parseBootStrapAddrs(addrs []string) (discv5Nodes []string, kadDHTNodes []string) { + discv5Nodes, kadDHTNodes = parseGenericAddrs(addrs) + if len(discv5Nodes) == 0 && len(kadDHTNodes) == 0 { + log.Warn("No bootstrap addresses supplied") + } + return discv5Nodes, kadDHTNodes +} + +func parseGenericAddrs(addrs []string) (enodeString []string, multiAddrString []string) { for _, addr := range addrs { if addr == "" { // Ignore empty entries @@ -116,20 +124,17 @@ func parseBootStrapAddrs(addrs []string) (discv5Nodes []string, kadDHTNodes []st } _, err := enode.Parse(enode.ValidSchemes, addr) if err == nil { - discv5Nodes = append(discv5Nodes, addr) + enodeString = append(enodeString, addr) continue } _, err = multiAddrFromString(addr) if err == nil { - kadDHTNodes = append(kadDHTNodes, addr) + multiAddrString = append(multiAddrString, addr) continue } - log.Errorf("Invalid bootstrap address of %s provided", addr) + log.Errorf("Invalid address of %s provided", addr) } - if len(discv5Nodes) == 0 && len(kadDHTNodes) == 0 { - log.Warn("No bootstrap addresses supplied") - } - return discv5Nodes, kadDHTNodes + return enodeString, multiAddrString } func convertToMultiAddr(nodes []*enode.Node) []ma.Multiaddr { @@ -168,15 +173,27 @@ func convertToSingleMultiAddr(node *enode.Node) (ma.Multiaddr, error) { return multiAddr, nil } -func manyMultiAddrsFromString(addrs []string) ([]ma.Multiaddr, error) { +func peersFromStringAddrs(addrs []string) ([]ma.Multiaddr, error) { var allAddrs []ma.Multiaddr - for _, stringAddr := range addrs { + enodeString, multiAddrString := parseGenericAddrs(addrs) + for _, stringAddr := range multiAddrString { addr, err := multiAddrFromString(stringAddr) if err != nil { return nil, errors.Wrapf(err, "Could not get multiaddr from string") } allAddrs = append(allAddrs, addr) } + for _, stringAddr := range enodeString { + enodeAddr, err := enode.Parse(enode.ValidSchemes, stringAddr) + if err != nil { + return nil, errors.Wrapf(err, "Could not get enode from string") + } + addr, err := convertToSingleMultiAddr(enodeAddr) + if err != nil { + return nil, errors.Wrapf(err, "Could not get multiaddr") + } + allAddrs = append(allAddrs, addr) + } return allAddrs, nil } diff --git a/beacon-chain/p2p/service.go b/beacon-chain/p2p/service.go index 91039acb3..ad44990e2 100644 --- a/beacon-chain/p2p/service.go +++ b/beacon-chain/p2p/service.go @@ -198,7 +198,7 @@ func (s *Service) Start() { s.started = true if len(s.cfg.StaticPeers) > 0 { - addrs, err := manyMultiAddrsFromString(s.cfg.StaticPeers) + addrs, err := peersFromStringAddrs(s.cfg.StaticPeers) if err != nil { log.Errorf("Could not connect to static peer: %v", err) }