Skip to content

Commit

Permalink
use shared TCP port
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Jan 8, 2025
1 parent 05b3f17 commit ad2c311
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
8 changes: 4 additions & 4 deletions ant.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type AntConfig struct {
PrivateKey crypto.PrivKey
UserAgent string
Port int
PortWSS int
ProtocolPrefix string
BootstrapPeers []peer.AddrInfo
EventsChan chan ants.RequestEvent
Expand Down Expand Up @@ -105,12 +104,12 @@ func SpawnAnt(ctx context.Context, ps peerstore.Peerstore, ds ds.Batching, cfg *
fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic-v1", cfg.Port),
fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic-v1/webtransport", cfg.Port),
fmt.Sprintf("/ip4/0.0.0.0/udp/%d/webrtc-direct", cfg.Port),
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d/tls/sni/*.%s/ws", cfg.PortWSS, forgeDomain), // cert manager websocket multi address
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d/tls/sni/*.%s/ws", cfg.Port, forgeDomain), // cert manager websocket multi address
fmt.Sprintf("/ip6/::/tcp/%d", cfg.Port),
fmt.Sprintf("/ip6/::/udp/%d/quic-v1", cfg.Port),
fmt.Sprintf("/ip6/::/udp/%d/quic-v1/webtransport", cfg.Port),
fmt.Sprintf("/ip6/::/udp/%d/webrtc-direct", cfg.Port),
fmt.Sprintf("/ip6/::/tcp/%d/tls/sni/*.%s/ws", cfg.PortWSS, forgeDomain), // cert manager websocket multi address
fmt.Sprintf("/ip6/::/tcp/%d/tls/sni/*.%s/ws", cfg.Port, forgeDomain), // cert manager websocket multi address
}

opts := []libp2p.Option{
Expand All @@ -120,6 +119,7 @@ func SpawnAnt(ctx context.Context, ps peerstore.Peerstore, ds ds.Batching, cfg *
libp2p.DisableRelay(),
libp2p.ListenAddrStrings(listenAddrs...),
libp2p.DisableMetrics(),
libp2p.ShareTCPListener(),
libp2p.Transport(libp2ptcp.NewTCPTransport),
libp2p.Transport(libp2pquic.NewTransport),
libp2p.Transport(libp2pwebtransport.New),
Expand Down Expand Up @@ -188,7 +188,7 @@ func SpawnAnt(ctx context.Context, ps peerstore.Peerstore, ds ds.Batching, cfg *
default:
continue
}
logger.Infof(" [%d] %s %s", i, actionStr, maddr.Address)
logger.Infof(" [%d] %s %s/p2p/%s", i, actionStr, maddr.Address, h.ID())
}
case event.EvtLocalReachabilityChanged:
logger.Infow("Reachability changed", "ant", h.ID(), "reachability", evt.Reachability)
Expand Down
10 changes: 0 additions & 10 deletions cmd/ants/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var queenConfig = struct {
KeyDBPath string
NumPorts int
FirstPort int
FirstPortWSS int
UPnp bool
BatchSize int
BatchTime time.Duration
Expand Down Expand Up @@ -61,7 +60,6 @@ var queenConfig = struct {
BucketSize: 20,
UserAgent: "celestiant",
QueenID: "",
FirstPortWSS: 7000,
}

func main() {
Expand Down Expand Up @@ -175,13 +173,6 @@ func main() {
Destination: &queenConfig.FirstPort,
Value: queenConfig.FirstPort,
},
&cli.IntFlag{
Name: "first_port_wss",
Usage: "First port ants can listen on for secure websockets",
EnvVars: []string{"ANTS_FIRST_PORT_WSS"},
Destination: &queenConfig.FirstPortWSS,
Value: queenConfig.FirstPortWSS,
},
&cli.IntFlag{
Name: "num_ports",
Usage: "Number of ports ants can listen on",
Expand Down Expand Up @@ -306,7 +297,6 @@ func runQueenCommand(c *cli.Context) error {
KeysDBPath: queenConfig.KeyDBPath,
NPorts: queenConfig.NumPorts,
FirstPort: queenConfig.FirstPort,
FirstPortWSS: queenConfig.FirstPortWSS,
UPnP: queenConfig.UPnp,
BatchSize: queenConfig.BatchSize,
BatchTime: queenConfig.BatchTime,
Expand Down
21 changes: 6 additions & 15 deletions queen.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type QueenConfig struct {
KeysDBPath string
NPorts int
FirstPort int
FirstPortWSS int
UPnP bool
BatchSize int
BatchTime time.Duration
Expand All @@ -48,10 +47,6 @@ type QueenConfig struct {
Telemetry *metrics.Telemetry
}

func (cfg *QueenConfig) RangesOverlap() bool {
return cfg.FirstPort+cfg.NPorts > cfg.FirstPortWSS && cfg.FirstPort < cfg.FirstPortWSS+cfg.NPorts
}

type Queen struct {
cfg *QueenConfig

Expand All @@ -76,10 +71,6 @@ type Queen struct {
}

func NewQueen(clickhouseClient db.Client, cfg *QueenConfig) (*Queen, error) {
if cfg.RangesOverlap() {
return nil, fmt.Errorf("port ranges overlap")
}

ps, err := pstoremem.NewPeerstore()
if err != nil {
return nil, fmt.Errorf("creating peerstore: %w", err)
Expand Down Expand Up @@ -118,20 +109,20 @@ func NewQueen(clickhouseClient db.Client, cfg *QueenConfig) (*Queen, error) {
return queen, nil
}

func (q *Queen) takeAvailablePort() (int, int, error) {
func (q *Queen) takeAvailablePort() (int, error) {
if q.cfg.UPnP {
return 0, 0, nil
return 0, nil
}

for i, occupied := range q.portsOccupancy {
if occupied {
continue
}
q.portsOccupancy[i] = true
return q.cfg.FirstPort + i, q.cfg.FirstPortWSS + i, nil
return q.cfg.FirstPort + i, nil
}

return 0, 0, fmt.Errorf("no available port")
return 0, fmt.Errorf("no available port")
}

func (q *Queen) freePort(port int) {
Expand Down Expand Up @@ -293,6 +284,7 @@ func (q *Queen) routine(ctx context.Context) {

// zones correspond to the prefixes of the tries that must be covered by an ant
zones := trieZones(networkTrie, q.cfg.BucketSize)
zones = zones[0:1]
logger.Debugf("%d zones must be covered by ants", len(zones))

// convert string zone to bitstr.Key
Expand Down Expand Up @@ -341,7 +333,7 @@ func (q *Queen) routine(ctx context.Context) {
// add missing ants
privKeys := q.keysDB.MatchingKeys(missingKeys, returnedKeys)
for _, key := range privKeys {
port, portWSS, err := q.takeAvailablePort()
port, err := q.takeAvailablePort()
if err != nil {
logger.Error("trying to spawn new ant: ", err)
continue
Expand All @@ -351,7 +343,6 @@ func (q *Queen) routine(ctx context.Context) {
PrivateKey: key,
UserAgent: q.cfg.UserAgent,
Port: port,
PortWSS: portWSS,
ProtocolPrefix: fmt.Sprintf("/celestia/%s", celestiaNet), // TODO: parameterize
BootstrapPeers: BootstrapPeers(celestiaNet), // TODO: parameterize
EventsChan: q.antsEvents,
Expand Down

0 comments on commit ad2c311

Please sign in to comment.