Skip to content

Commit

Permalink
Merge pull request #2119 from CortexFoundation/dev
Browse files Browse the repository at this point in the history
dial nodes from discv5 dht
  • Loading branch information
ucwong committed Aug 19, 2024
2 parents 105709b + 9674127 commit 20e62d5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
29 changes: 26 additions & 3 deletions ctxc/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/CortexFoundation/CortexTheseus/miner"
"github.com/CortexFoundation/CortexTheseus/node"
"github.com/CortexFoundation/CortexTheseus/p2p"
"github.com/CortexFoundation/CortexTheseus/p2p/dnsdisc"
"github.com/CortexFoundation/CortexTheseus/p2p/enode"
"github.com/CortexFoundation/CortexTheseus/p2p/enr"
"github.com/CortexFoundation/CortexTheseus/params"
Expand All @@ -70,7 +71,7 @@ type Cortex struct {
blockchain *core.BlockChain
protocolManager *ProtocolManager

dialCandidates enode.Iterator
discmix *enode.FairMix

// DB interfaces
chainDb ctxcdb.Database // Block chain database
Expand Down Expand Up @@ -152,6 +153,7 @@ func New(stack *node.Node, config *Config) (*Cortex, error) {
bloomRequests: make(chan chan *bloombits.Retrieval),
bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms),
p2pServer: stack.Server(),
discmix: enode.NewFairMix(0),
shutdownTracker: shutdowncheck.NewShutdownTracker(chainDb),
}

Expand Down Expand Up @@ -252,7 +254,6 @@ func New(stack *node.Node, config *Config) (*Cortex, error) {
gpoParams.Default = config.Miner.GasPrice
}
ctxc.APIBackend.gpo = gasprice.NewOracle(ctxc.APIBackend, gpoParams)
ctxc.dialCandidates, err = ctxc.setupDiscovery()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -569,7 +570,7 @@ func (s *Cortex) Protocols() []p2p.Protocol {
for i, vsn := range ProtocolVersions {
protos[i] = s.protocolManager.makeProtocol(vsn)
protos[i].Attributes = []enr.Entry{s.currentCtxcEntry(s.BlockChain())}
protos[i].DialCandidates = s.dialCandidates
protos[i].DialCandidates = s.discmix
}
return protos
}
Expand All @@ -578,6 +579,7 @@ func (s *Cortex) Protocols() []p2p.Protocol {
// Cortex protocol implementation.
func (s *Cortex) Start(srvr *p2p.Server) error {
s.startCtxcEntryUpdate(srvr.LocalNode())
s.setupDiscovery()
// Start the bloom bits servicing goroutines
s.startBloomHandlers(params.BloomBitsBlocks)

Expand All @@ -594,12 +596,33 @@ func (s *Cortex) Start(srvr *p2p.Server) error {
return nil
}

func (s *Cortex) setupDiscovery() error {
dnsclient := dnsdisc.NewClient(dnsdisc.Config{})
if len(s.config.DiscoveryURLs) > 0 {
iter, err := dnsclient.NewIterator(s.config.DiscoveryURLs...)
if err != nil {
return err
}
s.discmix.AddSource(iter)
}

// Add DHT nodes from discv5.
if s.p2pServer.DiscoveryV5() != nil {
filter := NewNodeFilter(s.blockchain)
iter := enode.Filter(s.p2pServer.DiscoveryV5().RandomNodes(), filter)
s.discmix.AddSource(iter)
}

return nil
}

// Stop implements node.Service, terminating all internal goroutines used by the
// Cortex protocol.
func (s *Cortex) Stop() error {
if s.synapse != nil {
s.synapse.Close()
}
s.discmix.Close()
s.protocolManager.Stop()
// Then stop everything else.
s.bloomIndexer.Close()
Expand Down
18 changes: 11 additions & 7 deletions ctxc/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package ctxc
import (
"github.com/CortexFoundation/CortexTheseus/core"
"github.com/CortexFoundation/CortexTheseus/core/forkid"
"github.com/CortexFoundation/CortexTheseus/p2p/dnsdisc"
"github.com/CortexFoundation/CortexTheseus/p2p/enode"
"github.com/CortexFoundation/CortexTheseus/rlp"
)
Expand Down Expand Up @@ -65,11 +64,16 @@ func (ctxc *Cortex) currentCtxcEntry(chain *core.BlockChain) *ctxcEntry {
}
}

// setupDiscovery creates the node discovery source for the ctxc protocol.
func (ctxc *Cortex) setupDiscovery() (enode.Iterator, error) {
if len(ctxc.config.DiscoveryURLs) == 0 {
return nil, nil
// NewNodeFilter returns a filtering function that returns whether the provided
// enode advertises a forkid compatible with the current chain.
func NewNodeFilter(chain *core.BlockChain) func(*enode.Node) bool {
filter := forkid.NewFilter(chain)
return func(n *enode.Node) bool {
var entry ctxcEntry
if err := n.Load(entry); err != nil {
return false
}
err := filter(entry.ForkID)
return err == nil
}
client := dnsdisc.NewClient(dnsdisc.Config{})
return client.NewIterator(ctxc.config.DiscoveryURLs...)
}
2 changes: 1 addition & 1 deletion nightly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GOFLAGS=-modcacherw make
echo "running cortex..."
#./test.sh > nightly.log 2>&1 &
rm -rf /tmp/.cortex_test
./build/bin/cortex --datadir=/tmp/.cortex_test/ --port=0 --storage.mode=lazy --storage.dht > nightly.log 2>&1 &
./build/bin/cortex --datadir=/tmp/.cortex_test/ --port=0 --storage.mode=lazy --storage.dht --v5disc > nightly.log 2>&1 &

CORTEX_PID=$!

Expand Down

0 comments on commit 20e62d5

Please sign in to comment.