diff --git a/app/app.go b/app/app.go index a85334ab45..227885c9c0 100644 --- a/app/app.go +++ b/app/app.go @@ -404,7 +404,6 @@ func wireP2P(ctx context.Context, life *lifecycle.Manager, conf Config, life.RegisterStart(lifecycle.AsyncAppCtx, lifecycle.StartP2PPing, p2p.NewPingService(p2pNode, peerIDs, conf.TestConfig.TestPingConfig)) life.RegisterStart(lifecycle.AsyncAppCtx, lifecycle.StartP2PEventCollector, p2p.NewEventCollector(p2pNode)) life.RegisterStart(lifecycle.AsyncAppCtx, lifecycle.StartP2PRouters, p2p.NewRelayRouter(p2pNode, peerIDs, relays)) - life.RegisterStart(lifecycle.AsyncAppCtx, lifecycle.StartForceDirectConns, p2p.ForceDirectConnections(p2pNode, peerIDs)) life.RegisterStart(lifecycle.AsyncAppCtx, lifecycle.StartForceQUICConns, p2p.UpgradeToQUICConnections(p2pNode, peerIDs)) return p2pNode, nil diff --git a/app/lifecycle/order.go b/app/lifecycle/order.go index 4969432e7a..2cc2fbb53d 100644 --- a/app/lifecycle/order.go +++ b/app/lifecycle/order.go @@ -23,7 +23,6 @@ const ( StartValidatorAPI StartP2PPing StartP2PRouters - StartForceDirectConns StartForceQUICConns StartP2PConsensus StartSimulator diff --git a/app/lifecycle/orderstart_string.go b/app/lifecycle/orderstart_string.go index 8f402f0b10..42031b8a68 100644 --- a/app/lifecycle/orderstart_string.go +++ b/app/lifecycle/orderstart_string.go @@ -20,26 +20,24 @@ func _() { _ = x[StartValidatorAPI-7] _ = x[StartP2PPing-8] _ = x[StartP2PRouters-9] - _ = x[StartForceDirectConns-10] - _ = x[StartForceQUICConns-11] - _ = x[StartP2PConsensus-12] - _ = x[StartSimulator-13] - _ = x[StartScheduler-14] - _ = x[StartBuilderRegWatcher-15] - _ = x[StartP2PEventCollector-16] - _ = x[StartPeerInfo-17] - _ = x[StartParSigDB-18] - _ = x[StartStackSnipe-19] + _ = x[StartForceQUICConns-10] + _ = x[StartP2PConsensus-11] + _ = x[StartSimulator-12] + _ = x[StartScheduler-13] + _ = x[StartBuilderRegWatcher-14] + _ = x[StartP2PEventCollector-15] + _ = x[StartPeerInfo-16] + _ = x[StartParSigDB-17] + _ = x[StartStackSnipe-18] } -const _OrderStart_name = "TrackerPrivkeyLockEth1ClientAggSigDBRelayMonitoringAPIDebugAPIValidatorAPIP2PPingP2PRoutersForceDirectConnsForceQUICConnsP2PConsensusSimulatorSchedulerBuilderRegWatcherP2PEventCollectorPeerInfoParSigDBStackSnipe" +const _OrderStart_name = "TrackerPrivkeyLockEth1ClientAggSigDBRelayMonitoringAPIDebugAPIValidatorAPIP2PPingP2PRoutersForceQUICConnsP2PConsensusSimulatorSchedulerBuilderRegWatcherP2PEventCollectorPeerInfoParSigDBStackSnipe" -var _OrderStart_index = [...]uint8{0, 7, 18, 28, 36, 41, 54, 62, 74, 81, 91, 107, 121, 133, 142, 151, 168, 185, 193, 201, 211} +var _OrderStart_index = [...]uint8{0, 7, 18, 28, 36, 41, 54, 62, 74, 81, 91, 105, 117, 126, 135, 152, 169, 177, 185, 195} func (i OrderStart) String() string { - idx := int(i) - 0 - if i < 0 || idx >= len(_OrderStart_index)-1 { + if i < 0 || i >= OrderStart(len(_OrderStart_index)-1) { return "OrderStart(" + strconv.FormatInt(int64(i), 10) + ")" } - return _OrderStart_name[_OrderStart_index[idx]:_OrderStart_index[idx+1]] + return _OrderStart_name[_OrderStart_index[i]:_OrderStart_index[i+1]] } diff --git a/app/lifecycle/orderstop_string.go b/app/lifecycle/orderstop_string.go index 97b41a26fa..6cf1f1c103 100644 --- a/app/lifecycle/orderstop_string.go +++ b/app/lifecycle/orderstop_string.go @@ -28,9 +28,8 @@ const _OrderStop_name = "SchedulerPrivkeyLockRetryerDutyDBBeaconMockValidatorAPI var _OrderStop_index = [...]uint8{0, 9, 20, 27, 33, 43, 55, 62, 71, 78, 86, 99} func (i OrderStop) String() string { - idx := int(i) - 0 - if i < 0 || idx >= len(_OrderStop_index)-1 { + if i < 0 || i >= OrderStop(len(_OrderStop_index)-1) { return "OrderStop(" + strconv.FormatInt(int64(i), 10) + ")" } - return _OrderStop_name[_OrderStop_index[idx]:_OrderStop_index[idx+1]] + return _OrderStop_name[_OrderStop_index[i]:_OrderStop_index[i+1]] } diff --git a/p2p/p2p.go b/p2p/p2p.go index 999ff7685b..ce6f5ed8d7 100644 --- a/p2p/p2p.go +++ b/p2p/p2p.go @@ -106,6 +106,7 @@ func NewNode(ctx context.Context, cfg Config, key *k1.PrivateKey, connGater Conn libp2p.ConnectionGater(connGater), // Enable Autonat (required for hole punching) libp2p.EnableNATService(), + libp2p.EnableHolePunching(), libp2p.AddrsFactory(func(internalAddrs []ma.Multiaddr) []ma.Multiaddr { return filterAdvertisedAddrs(externalAddrs, internalAddrs, filterPrivateAddrs) }), @@ -292,49 +293,6 @@ func (f peerRoutingFunc) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo return f(ctx, p) } -// ForceDirectConnections attempts to establish a direct connection if there is an existing relay connection to the peer. -// The idea is to enable switching to a direct connection as soon as the host has a connection to the peer. -func ForceDirectConnections(p2pNode host.Host, peerIDs []peer.ID) lifecycle.HookFuncCtx { - forceDirectConn := func(ctx context.Context) { - for _, p := range peerIDs { - if p2pNode.ID() == p { - continue // Skip self - } - - conns := p2pNode.Network().ConnsToPeer(p) - if len(conns) == 0 { - // Skip if there isn't any existing connection to peer. Note that we only force direct connection - // if there is already an existing relay connection between the host and peer. - continue - } - - if isDirectConnAvailable(conns) { - continue - } - - // All existing connections are through relays, so we can try force dialing a direct connection. - err := p2pNode.Connect(network.WithForceDirectDial(ctx, "relay_to_direct"), peer.AddrInfo{ID: p}) - if err == nil { - log.Debug(ctx, "Forced direct connection to peer successful", z.Str("peer", PeerName(p))) - } - } - } - - return func(ctx context.Context) { - ticker := time.NewTicker(1 * time.Minute) - defer ticker.Stop() - - for { - select { - case <-ctx.Done(): - return - case <-ticker.C: - forceDirectConn(ctx) - } - } - } -} - // isQUICEnabled returns true if the host has an address or listening address on QUIC. func isQUICEnabled(h host.Host) bool { if slices.ContainsFunc(h.Network().ListenAddresses(), isQUICAddr) { @@ -350,19 +308,6 @@ func isQUICEnabled(h host.Host) bool { return false } -// isDirectConnAvailable returns true if direct connection is available in the given set of connections. -func isDirectConnAvailable(conns []network.Conn) bool { - for _, conn := range conns { - if IsRelayAddr(conn.RemoteMultiaddr()) { - continue - } - - return true - } - - return false -} - // UpgradeToQUICConnections tries to upgrade a direct TCP connection to a direct QUIC connection // if there is known QUIC addresses from the peerstore. func UpgradeToQUICConnections(p2pNode host.Host, peerIDs []peer.ID) lifecycle.HookFuncCtx { @@ -457,7 +402,7 @@ func UpgradeToQUICConnections(p2pNode host.Host, peerIDs []peer.ID) lifecycle.Ho if !hasDirectTCPConn(conns) { log.Debug(ctx, "No direct connection via TCP to peer", z.Str("peer", PeerName(p)), z.Any("conns", conns)) - continue // no direct TPC connection to upgrade to QUIC, ForceDirectConnections shall upgrade to direct + continue // no direct TCP connection to upgrade to QUIC, hole punching shall upgrade to direct } // Get known QUIC addrs from peerstore