enr_entry.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package les
  17. import (
  18. "github.com/ethereum/go-ethereum/core/forkid"
  19. "github.com/ethereum/go-ethereum/p2p/dnsdisc"
  20. "github.com/ethereum/go-ethereum/p2p/enode"
  21. "github.com/ethereum/go-ethereum/rlp"
  22. )
  23. // lesEntry is the "les" ENR entry. This is set for LES servers only.
  24. type lesEntry struct {
  25. // Ignore additional fields (for forward compatibility).
  26. VfxVersion uint
  27. Rest []rlp.RawValue `rlp:"tail"`
  28. }
  29. func (lesEntry) ENRKey() string { return "les" }
  30. // ethEntry is the "eth" ENR entry. This is redeclared here to avoid depending on package eth.
  31. type ethEntry struct {
  32. ForkID forkid.ID
  33. Tail []rlp.RawValue `rlp:"tail"`
  34. }
  35. func (ethEntry) ENRKey() string { return "eth" }
  36. // setupDiscovery creates the node discovery source for the eth protocol.
  37. func (eth *LightEthereum) setupDiscovery() (enode.Iterator, error) {
  38. it := enode.NewFairMix(0)
  39. // Enable DNS discovery.
  40. if len(eth.config.EthDiscoveryURLs) != 0 {
  41. client := dnsdisc.NewClient(dnsdisc.Config{})
  42. dns, err := client.NewIterator(eth.config.EthDiscoveryURLs...)
  43. if err != nil {
  44. return nil, err
  45. }
  46. it.AddSource(dns)
  47. }
  48. // Enable DHT.
  49. if eth.udpEnabled {
  50. it.AddSource(eth.p2pServer.DiscV5.RandomNodes())
  51. }
  52. forkFilter := forkid.NewFilter(eth.blockchain)
  53. iterator := enode.Filter(it, func(n *enode.Node) bool { return nodeIsServer(forkFilter, n) })
  54. return iterator, nil
  55. }
  56. // nodeIsServer checks whether n is an LES server node.
  57. func nodeIsServer(forkFilter forkid.Filter, n *enode.Node) bool {
  58. var les lesEntry
  59. var eth ethEntry
  60. return n.Load(&les) == nil && n.Load(&eth) == nil && forkFilter(eth.ForkID) == nil
  61. }