github.com/cryptogateway/go-paymex@v0.0.0-20210204174735-96277fb1e602/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/cryptogateway/go-paymex/core/forkid"
    21  	"github.com/cryptogateway/go-paymex/p2p"
    22  	"github.com/cryptogateway/go-paymex/p2p/dnsdisc"
    23  	"github.com/cryptogateway/go-paymex/p2p/enode"
    24  	"github.com/cryptogateway/go-paymex/rlp"
    25  )
    26  
    27  // lesEntry is the "les" ENR entry. This is set for LES servers only.
    28  type lesEntry struct {
    29  	// Ignore additional fields (for forward compatibility).
    30  	_ []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  	_      []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(cfg *p2p.Config) (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 cfg.DiscoveryV5 && eth.p2pServer.DiscV5 != nil {
    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  }