mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
eth: integrate p2p/dnsdisc
This commit is contained in:
parent
9368368b87
commit
db705bb45d
3 changed files with 41 additions and 0 deletions
|
|
@ -47,6 +47,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/miner"
|
"github.com/ethereum/go-ethereum/miner"
|
||||||
"github.com/ethereum/go-ethereum/node"
|
"github.com/ethereum/go-ethereum/node"
|
||||||
"github.com/ethereum/go-ethereum/p2p"
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
|
|
@ -74,6 +75,7 @@ type Ethereum struct {
|
||||||
blockchain *core.BlockChain
|
blockchain *core.BlockChain
|
||||||
protocolManager *ProtocolManager
|
protocolManager *ProtocolManager
|
||||||
lesServer LesServer
|
lesServer LesServer
|
||||||
|
dialCandiates enode.Iterator
|
||||||
|
|
||||||
// DB interfaces
|
// DB interfaces
|
||||||
chainDb ethdb.Database // Block chain database
|
chainDb ethdb.Database // Block chain database
|
||||||
|
|
@ -220,6 +222,11 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||||
}
|
}
|
||||||
eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams)
|
eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, gpoParams)
|
||||||
|
|
||||||
|
eth.dialCandiates, err = eth.setupDiscovery(&ctx.Config.P2P)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return eth, nil
|
return eth, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -510,6 +517,7 @@ func (s *Ethereum) Protocols() []p2p.Protocol {
|
||||||
for i, vsn := range ProtocolVersions {
|
for i, vsn := range ProtocolVersions {
|
||||||
protos[i] = s.protocolManager.makeProtocol(vsn)
|
protos[i] = s.protocolManager.makeProtocol(vsn)
|
||||||
protos[i].Attributes = []enr.Entry{s.currentEthEntry()}
|
protos[i].Attributes = []enr.Entry{s.currentEthEntry()}
|
||||||
|
protos[i].DialCandidates = s.dialCandiates
|
||||||
}
|
}
|
||||||
if s.lesServer != nil {
|
if s.lesServer != nil {
|
||||||
protos = append(protos, s.lesServer.Protocols()...)
|
protos = append(protos, s.lesServer.Protocols()...)
|
||||||
|
|
|
||||||
|
|
@ -95,6 +95,10 @@ type Config struct {
|
||||||
NetworkId uint64 // Network ID to use for selecting peers to connect to
|
NetworkId uint64 // Network ID to use for selecting peers to connect to
|
||||||
SyncMode downloader.SyncMode
|
SyncMode downloader.SyncMode
|
||||||
|
|
||||||
|
// Discovery options
|
||||||
|
DisableDNSDiscovery bool
|
||||||
|
DiscoveryURLs []string
|
||||||
|
|
||||||
NoPruning bool // Whether to disable pruning and flush everything to disk
|
NoPruning bool // Whether to disable pruning and flush everything to disk
|
||||||
NoPrefetch bool // Whether to disable prefetching and only load state on demand
|
NoPrefetch bool // Whether to disable prefetching and only load state on demand
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,12 +17,23 @@
|
||||||
package eth
|
package eth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
"github.com/ethereum/go-ethereum/core/forkid"
|
"github.com/ethereum/go-ethereum/core/forkid"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/dnsdisc"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var knownDNSNetworks = map[common.Hash]string{
|
||||||
|
params.MainnetGenesisHash: "enrtree://AMBMWDM3J6UY3M32TMMROUNLX6Y3YTLVC3DC6HN2AVG5NHNSAXDW6@all.mainnet.nodes.ethflare.xyz",
|
||||||
|
params.TestnetGenesisHash: "enrtree://AMBMWDM3J6UY3M32TMMROUNLX6Y3YTLVC3DC6HN2AVG5NHNSAXDW6@all.ropsten.nodes.ethflare.xyz",
|
||||||
|
params.RinkebyGenesisHash: "enrtree://AMBMWDM3J6UY3M32TMMROUNLX6Y3YTLVC3DC6HN2AVG5NHNSAXDW6@all.rinkeby.nodes.ethflare.xyz",
|
||||||
|
params.GoerliGenesisHash: "enrtree://AMBMWDM3J6UY3M32TMMROUNLX6Y3YTLVC3DC6HN2AVG5NHNSAXDW6@all.goerli.nodes.ethflare.xyz",
|
||||||
|
}
|
||||||
|
|
||||||
// ethEntry is the "eth" ENR entry which advertises eth protocol
|
// ethEntry is the "eth" ENR entry which advertises eth protocol
|
||||||
// on the discovery network.
|
// on the discovery network.
|
||||||
type ethEntry struct {
|
type ethEntry struct {
|
||||||
|
|
@ -37,6 +48,7 @@ func (e ethEntry) ENRKey() string {
|
||||||
return "eth"
|
return "eth"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// startEthEntryUpdate starts the ENR updater loop.
|
||||||
func (eth *Ethereum) startEthEntryUpdate(ln *enode.LocalNode) {
|
func (eth *Ethereum) startEthEntryUpdate(ln *enode.LocalNode) {
|
||||||
var newHead = make(chan core.ChainHeadEvent, 10)
|
var newHead = make(chan core.ChainHeadEvent, 10)
|
||||||
sub := eth.blockchain.SubscribeChainHeadEvent(newHead)
|
sub := eth.blockchain.SubscribeChainHeadEvent(newHead)
|
||||||
|
|
@ -59,3 +71,20 @@ func (eth *Ethereum) startEthEntryUpdate(ln *enode.LocalNode) {
|
||||||
func (eth *Ethereum) currentEthEntry() *ethEntry {
|
func (eth *Ethereum) currentEthEntry() *ethEntry {
|
||||||
return ðEntry{ForkID: forkid.NewID(eth.blockchain)}
|
return ðEntry{ForkID: forkid.NewID(eth.blockchain)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setupDiscovery creates the node discovery source for the eth protocol.
|
||||||
|
func (eth *Ethereum) setupDiscovery(cfg *p2p.Config) (enode.Iterator, error) {
|
||||||
|
if cfg.NoDiscovery || eth.config.DisableDNSDiscovery {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
known := knownDNSNetworks[eth.blockchain.Genesis().Hash()]
|
||||||
|
urls := eth.config.DiscoveryURLs
|
||||||
|
if len(urls) == 0 {
|
||||||
|
if known == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
urls = []string{known}
|
||||||
|
}
|
||||||
|
client := dnsdisc.NewClient(dnsdisc.Config{})
|
||||||
|
return client.NewIterator(urls...)
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue