p2p: allow better dial source diagnostics

Adding an optional name to the dial sources allows better
diagnostics of FairMix and the whole dial process.

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2025-03-31 09:45:07 +02:00
parent d2176f463b
commit 7f2db0e32e
No known key found for this signature in database
GPG key ID: 0FE274EE8C95166E
4 changed files with 14 additions and 10 deletions

View file

@ -478,7 +478,7 @@ func (s *Ethereum) setupDiscovery() error {
if err != nil { if err != nil {
return err return err
} }
s.discmix.AddSource(iter) s.discmix.AddSource(iter, "DNSdiscEth")
} }
// Add snap nodes from DNS. // Add snap nodes from DNS.
@ -487,14 +487,14 @@ func (s *Ethereum) setupDiscovery() error {
if err != nil { if err != nil {
return err return err
} }
s.discmix.AddSource(iter) s.discmix.AddSource(iter, "DNSdiscSnap")
} }
// Add DHT nodes from discv5. // Add DHT nodes from discv5.
if s.p2pServer.DiscoveryV5() != nil { if s.p2pServer.DiscoveryV5() != nil {
filter := eth.NewNodeFilter(s.blockchain) filter := eth.NewNodeFilter(s.blockchain)
iter := enode.Filter(s.p2pServer.DiscoveryV5().RandomNodes(), filter) iter := enode.Filter(s.p2pServer.DiscoveryV5().RandomNodes(), filter)
s.discmix.AddSource(iter) s.discmix.AddSource(iter, "DiscoveryV5")
} }
return nil return nil

View file

@ -149,6 +149,7 @@ type mixSource struct {
it Iterator it Iterator
next chan *Node next chan *Node
timeout time.Duration timeout time.Duration
name string
} }
// NewFairMix creates a mixer. // NewFairMix creates a mixer.
@ -167,7 +168,7 @@ func NewFairMix(timeout time.Duration) *FairMix {
} }
// AddSource adds a source of nodes. // AddSource adds a source of nodes.
func (m *FairMix) AddSource(it Iterator) { func (m *FairMix) AddSource(it Iterator, name ...string) {
m.mu.Lock() m.mu.Lock()
defer m.mu.Unlock() defer m.mu.Unlock()
@ -175,7 +176,10 @@ func (m *FairMix) AddSource(it Iterator) {
return return
} }
m.wg.Add(1) m.wg.Add(1)
source := &mixSource{it, make(chan *Node), m.timeout} source := &mixSource{it, make(chan *Node), m.timeout, ""}
if len(name) > 0 {
source.name = name[0]
}
m.sources = append(m.sources, source) m.sources = append(m.sources, source)
go m.runSource(m.closed, source) go m.runSource(m.closed, source)
} }

View file

@ -99,9 +99,9 @@ func TestFairMix(t *testing.T) {
func testMixerFairness(t *testing.T) { func testMixerFairness(t *testing.T) {
mix := NewFairMix(1 * time.Second) mix := NewFairMix(1 * time.Second)
mix.AddSource(&genIter{index: 1}) mix.AddSource(&genIter{index: 1}, "1")
mix.AddSource(&genIter{index: 2}) mix.AddSource(&genIter{index: 2}, "2")
mix.AddSource(&genIter{index: 3}) mix.AddSource(&genIter{index: 3}, "3")
defer mix.Close() defer mix.Close()
nodes := ReadNodes(mix, 500) nodes := ReadNodes(mix, 500)

View file

@ -479,7 +479,7 @@ func (srv *Server) setupDiscovery() error {
return err return err
} }
srv.discv4 = ntab srv.discv4 = ntab
srv.discmix.AddSource(ntab.RandomNodes()) srv.discmix.AddSource(ntab.RandomNodes(), "DiscoveryV4")
} }
if srv.Config.DiscoveryV5 { if srv.Config.DiscoveryV5 {
cfg := discover.Config{ cfg := discover.Config{
@ -498,7 +498,7 @@ func (srv *Server) setupDiscovery() error {
added := make(map[string]bool) added := make(map[string]bool)
for _, proto := range srv.Protocols { for _, proto := range srv.Protocols {
if proto.DialCandidates != nil && !added[proto.Name] { if proto.DialCandidates != nil && !added[proto.Name] {
srv.discmix.AddSource(proto.DialCandidates) srv.discmix.AddSource(proto.DialCandidates, "DialCandidates-"+proto.Name)
added[proto.Name] = true added[proto.Name] = true
} }
} }