Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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