github.com/jimmyx0x/go-ethereum@v1.10.28/les/enr_entry.go (about)

     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  
    17  package les
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/core/forkid"
    21  	"github.com/ethereum/go-ethereum/p2p/dnsdisc"
    22  	"github.com/ethereum/go-ethereum/p2p/enode"
    23  	"github.com/ethereum/go-ethereum/rlp"
    24  )
    25  
    26  // lesEntry is the "les" ENR entry. This is set for LES servers only.
    27  type lesEntry struct {
    28  	// Ignore additional fields (for forward compatibility).
    29  	VfxVersion uint
    30  	Rest       []rlp.RawValue `rlp:"tail"`
    31  }
    32  
    33  func (lesEntry) ENRKey() string { return "les" }
    34  
    35  // ethEntry is the "eth" ENR entry. This is redeclared here to avoid depending on package eth.
    36  type ethEntry struct {
    37  	ForkID forkid.ID
    38  	Tail   []rlp.RawValue `rlp:"tail"`
    39  }
    40  
    41  func (ethEntry) ENRKey() string { return "eth" }
    42  
    43  // setupDiscovery creates the node discovery source for the eth protocol.
    44  func (eth *LightEthereum) setupDiscovery() (enode.Iterator, error) {
    45  	it := enode.NewFairMix(0)
    46  
    47  	// Enable DNS discovery.
    48  	if len(eth.config.EthDiscoveryURLs) != 0 {
    49  		client := dnsdisc.NewClient(dnsdisc.Config{})
    50  		dns, err := client.NewIterator(eth.config.EthDiscoveryURLs...)
    51  		if err != nil {
    52  			return nil, err
    53  		}
    54  		it.AddSource(dns)
    55  	}
    56  
    57  	// Enable DHT.
    58  	if eth.udpEnabled {
    59  		it.AddSource(eth.p2pServer.DiscV5.RandomNodes())
    60  	}
    61  
    62  	forkFilter := forkid.NewFilter(eth.blockchain)
    63  	iterator := enode.Filter(it, func(n *enode.Node) bool { return nodeIsServer(forkFilter, n) })
    64  	return iterator, nil
    65  }
    66  
    67  // nodeIsServer checks whether n is an LES server node.
    68  func nodeIsServer(forkFilter forkid.Filter, n *enode.Node) bool {
    69  	var les lesEntry
    70  	var eth ethEntry
    71  	return n.Load(&les) == nil && n.Load(&eth) == nil && forkFilter(eth.ForkID) == nil
    72  }