unaiverse 0.1.11__cp311-cp311-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of unaiverse might be problematic. Click here for more details.

Files changed (50) hide show
  1. unaiverse/__init__.py +19 -0
  2. unaiverse/agent.py +2090 -0
  3. unaiverse/agent_basics.py +1948 -0
  4. unaiverse/clock.py +221 -0
  5. unaiverse/dataprops.py +1236 -0
  6. unaiverse/hsm.py +1892 -0
  7. unaiverse/modules/__init__.py +18 -0
  8. unaiverse/modules/cnu/__init__.py +17 -0
  9. unaiverse/modules/cnu/cnus.py +536 -0
  10. unaiverse/modules/cnu/layers.py +261 -0
  11. unaiverse/modules/cnu/psi.py +60 -0
  12. unaiverse/modules/hl/__init__.py +15 -0
  13. unaiverse/modules/hl/hl_utils.py +411 -0
  14. unaiverse/modules/networks.py +1509 -0
  15. unaiverse/modules/utils.py +710 -0
  16. unaiverse/networking/__init__.py +16 -0
  17. unaiverse/networking/node/__init__.py +18 -0
  18. unaiverse/networking/node/connpool.py +1308 -0
  19. unaiverse/networking/node/node.py +2499 -0
  20. unaiverse/networking/node/profile.py +446 -0
  21. unaiverse/networking/node/tokens.py +79 -0
  22. unaiverse/networking/p2p/__init__.py +187 -0
  23. unaiverse/networking/p2p/go.mod +127 -0
  24. unaiverse/networking/p2p/go.sum +548 -0
  25. unaiverse/networking/p2p/golibp2p.py +18 -0
  26. unaiverse/networking/p2p/golibp2p.pyi +135 -0
  27. unaiverse/networking/p2p/lib.go +2662 -0
  28. unaiverse/networking/p2p/lib.go.sha256 +1 -0
  29. unaiverse/networking/p2p/lib_types.py +312 -0
  30. unaiverse/networking/p2p/message_pb2.py +50 -0
  31. unaiverse/networking/p2p/messages.py +362 -0
  32. unaiverse/networking/p2p/mylogger.py +77 -0
  33. unaiverse/networking/p2p/p2p.py +871 -0
  34. unaiverse/networking/p2p/proto-go/message.pb.go +846 -0
  35. unaiverse/networking/p2p/unailib.cpython-311-darwin.so +0 -0
  36. unaiverse/stats.py +1481 -0
  37. unaiverse/streamlib/__init__.py +15 -0
  38. unaiverse/streamlib/streamlib.py +210 -0
  39. unaiverse/streams.py +776 -0
  40. unaiverse/utils/__init__.py +16 -0
  41. unaiverse/utils/lone_wolf.json +24 -0
  42. unaiverse/utils/misc.py +310 -0
  43. unaiverse/utils/sandbox.py +293 -0
  44. unaiverse/utils/server.py +435 -0
  45. unaiverse/world.py +335 -0
  46. unaiverse-0.1.11.dist-info/METADATA +367 -0
  47. unaiverse-0.1.11.dist-info/RECORD +50 -0
  48. unaiverse-0.1.11.dist-info/WHEEL +6 -0
  49. unaiverse-0.1.11.dist-info/licenses/LICENSE +43 -0
  50. unaiverse-0.1.11.dist-info/top_level.txt +1 -0
@@ -0,0 +1,2662 @@
1
+ // lib.go
2
+ // This Go program compiles into a C shared library (.so file on Linux/macOS, .dll on Windows)
3
+ // exposing libp2p functionalities (host creation, peer connection, pubsub, direct messaging)
4
+ // for use by other languages, primarily Python via CFFI or ctypes.
5
+ package main
6
+
7
+ /*
8
+ #include <stdlib.h>
9
+ */
10
+ import "C" // Enables CGo features, allowing Go to call C code and vice-versa.
11
+
12
+ import (
13
+ // Standard Go libraries
14
+ "bytes" // For byte buffer manipulations (e.g., encoding/decoding, separators)
15
+ "errors" // For handling some types of errors
16
+ "container/list" // For an efficient ordered list (doubly-linked list for queues)
17
+ "context" // For managing cancellation signals and deadlines across API boundaries and goroutines
18
+ "crypto/rand" // For generating identity keys
19
+ "crypto/tls" // For TLS configuration and certificates
20
+ "encoding/base64" // For encoding binary message data into JSON-safe strings
21
+ "encoding/binary" // For encoding/decoding length prefixes in stream communication
22
+ "encoding/json" // For marshalling/unmarshalling data structures to/from JSON (used for C API communication)
23
+ "fmt" // For formatted string creation and printing
24
+ "io" // For input/output operations (e.g., reading from streams)
25
+ "log" // For logging information, warnings, and errors
26
+ "net" // For network-related errors and interfaces
27
+ "os" // For interacting with the operating system (e.g., Stdout)
28
+ "path/filepath" // For file path manipulations (e.g., saving/loading identity keys)
29
+ "strings" // For string manipulations (e.g., trimming, splitting)
30
+ "sync" // For synchronization primitives like Mutexes and RWMutexes to protect shared data
31
+ "time" // For time-related functions (e.g., timeouts, timestamps)
32
+ "unsafe" // For using Go pointers with C code (specifically C.free)
33
+
34
+ // Core libp2p libraries
35
+ libp2p "github.com/libp2p/go-libp2p" // Main libp2p package for creating a host
36
+ dht "github.com/libp2p/go-libp2p-kad-dht" // Kademlia DHT implementation for peer discovery and routing
37
+ "github.com/libp2p/go-libp2p/core/crypto" // Defines cryptographic primitives (keys, signatures)
38
+ "github.com/libp2p/go-libp2p/core/event" // Event bus for subscribing to libp2p events (connections, reachability changes)
39
+ "github.com/libp2p/go-libp2p/core/host" // Defines the main Host interface, representing a libp2p node
40
+ "github.com/libp2p/go-libp2p/core/network" // Defines network interfaces like Stream and Connection
41
+ "github.com/libp2p/go-libp2p/core/peer" // Defines Peer ID and AddrInfo types
42
+ "github.com/libp2p/go-libp2p/core/peerstore" // Defines the Peerstore interface for storing peer metadata (addresses, keys)
43
+ "github.com/libp2p/go-libp2p/core/routing" // Defines the Routing interface for peer routing (e.g., DHT)
44
+ rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager" // For managing resources (bandwidth, memory) for libp2p hosts
45
+ "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client" // For establishing outbound relayed connections (acting as a client)
46
+ rc "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay" // Import for relay service options
47
+ autorelay "github.com/libp2p/go-libp2p/p2p/host/autorelay"
48
+
49
+ // transport protocols for libp2p
50
+ quic "github.com/libp2p/go-libp2p/p2p/transport/quic" // QUIC transport for peer-to-peer connections (e.g., for mobile devices)
51
+ "github.com/libp2p/go-libp2p/p2p/transport/tcp" // TCP transport for peer-to-peer connections (most common)
52
+ webrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc" // WebRTC transport for peer-to-peer connections (e.g., for browsers or mobile devices)
53
+ ws "github.com/libp2p/go-libp2p/p2p/transport/websocket" // WebSocket transport for peer-to-peer connections (e.g., for browsers)
54
+ webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport" // WebTransport transport for peer-to-peer connections (e.g., for browsers)
55
+
56
+ // --- AutoTLS Imports ---
57
+ "github.com/caddyserver/certmagic" // Automatic TLS certificate management (used by p2p-forge)
58
+ golog "github.com/ipfs/go-log/v2" // IPFS logging library for structured logging
59
+ p2pforge "github.com/ipshipyard/p2p-forge/client" // p2p-forge library for automatic TLS and domain management
60
+
61
+ // protobuf
62
+ pg "unailib/proto-go" // Generated Protobuf code for our message formats
63
+
64
+ "google.golang.org/protobuf/proto" // Core Protobuf library for marshalling/unmarshalling messages
65
+
66
+ // PubSub library
67
+ pubsub "github.com/libp2p/go-libp2p-pubsub" // GossipSub implementation for publish/subscribe messaging
68
+
69
+ // Multiaddr libraries (libp2p's addressing format)
70
+ ma "github.com/multiformats/go-multiaddr" // Core multiaddr parsing and manipulation
71
+ manet "github.com/multiformats/go-multiaddr/net" // Utilities for working with multiaddrs and net interfaces (checking loopback, etc.)
72
+ )
73
+
74
+ // ChatProtocol defines the protocol ID string used for direct peer-to-peer messaging streams.
75
+ // This ensures that both peers understand how to interpret the data on the stream.
76
+ // const UnaiverseChatProtocol = "/unaiverse-chat-protocol/1.0.0"
77
+ const UnaiverseChatProtocol = "/unaiverse/chat/1.0.0"
78
+ const UnaiverseUserAgent = "go-libp2p/example/autotls"
79
+
80
+ // ExtendedPeerInfo holds information about a connected peer.
81
+ type ExtendedPeerInfo struct {
82
+ ID peer.ID `json:"id"` // the Peer ID of the connected peer.
83
+ Addrs []ma.Multiaddr `json:"addrs"` // the Multiaddr(s) associated with the peer.
84
+ ConnectedAt time.Time `json:"connected_at"` // Timestamp when the connection was established.
85
+ Direction string `json:"direction"` // Direction of the connection: "inbound" or "outbound".
86
+ Misc int `json:"misc"` // Misc information (integer), custom usage
87
+ }
88
+
89
+ // RendezvousState holds the discovered peers from a rendezvous topic,
90
+ // along with metadata about the freshness of the data.
91
+ type RendezvousState struct {
92
+ Peers map[peer.ID]ExtendedPeerInfo `json:"peers"`
93
+ UpdateCount int64 `json:"update_count"`
94
+ }
95
+
96
+ // QueuedMessage represents a message received either directly or via PubSub.
97
+ //
98
+ // This lightweight version stores the binary payload in the `Data` field,
99
+ // while the `From` field contains the Peer ID of the sender for security reasons.
100
+ // It has to match with the 'sender' field in the ProtoBuf payload of the message.
101
+ type QueuedMessage struct {
102
+ From peer.ID `json:"from"` // The VERIFIED peer ID of the sender from the network layer.
103
+ Data []byte `json:"-"` // The raw data payload (Protobuf encoded).
104
+ }
105
+
106
+ // MessageStore holds the QueuedMessages for each channel in separate FIFO queues.
107
+ // It has a maximum number of channels and a maximum queue length per channel.
108
+ type MessageStore struct {
109
+ mu sync.Mutex // protects the message store from concurrent access.
110
+ messagesByChannel map[string]*list.List // stores a FIFO queue of messages for each channel
111
+ }
112
+
113
+ // NodeConfig contains the parameters to initialize a node
114
+ type NodeConfig struct {
115
+ IdentityDir string `json:"identity_dir"`
116
+ PredefinedPort int `json:"predefined_port"`
117
+ ListenIPs []string `json:"listen_ips"`
118
+ MaxConnections int `json:"max_connections"`
119
+
120
+ // Group Relay Logic
121
+ Relay struct {
122
+ EnableClient bool `json:"enable_client"`
123
+ EnableService bool `json:"enable_service"`
124
+ } `json:"relay"`
125
+
126
+ // Group TLS Logic (Mutually exclusive logic becomes clear here)
127
+ TLS struct {
128
+ AutoTLS bool `json:"auto_tls"`
129
+ Domain string `json:"domain"`
130
+ CertPath string `json:"cert_path"`
131
+ KeyPath string `json:"key_path"`
132
+ } `json:"tls"`
133
+
134
+ // explicit configuration for network environment
135
+ Network struct {
136
+ ForcePublic bool `json:"force_public"` // Replaces knowsIsPublic
137
+ } `json:"network"`
138
+
139
+ // Group DHT logic
140
+ DHT struct {
141
+ Enabled bool `json:"enabled"`
142
+ Mode string `json:"mode"` // "client", "server"
143
+ Keep bool `json:"keep"` // to keep it running after init
144
+ } `json:"dht"`
145
+ }
146
+
147
+ // CreateNodeResponse defines the structure of our success message.
148
+ type CreateNodeResponse struct {
149
+ Addresses []string `json:"addresses"`
150
+ IsPublic bool `json:"isPublic"`
151
+ }
152
+
153
+ // NodeInstance holds ALL state for a single libp2p node.
154
+ type NodeInstance struct {
155
+ // Core Components
156
+ host host.Host
157
+ pubsub *pubsub.PubSub
158
+ dht *dht.IpfsDHT
159
+ ctx context.Context
160
+ cancel context.CancelFunc
161
+ certManager *p2pforge.P2PForgeCertMgr
162
+ messageStore *MessageStore
163
+
164
+ // Address Cache
165
+ addrMutex sync.RWMutex
166
+ localAddrs []ma.Multiaddr
167
+ relayAddrs []ma.Multiaddr
168
+
169
+ // PubSub State
170
+ pubsubMutex sync.RWMutex
171
+ topics map[string]*pubsub.Topic
172
+ subscriptions map[string]*pubsub.Subscription
173
+
174
+ // Peer State
175
+ peersMutex sync.RWMutex
176
+ connectedPeers map[peer.ID]ExtendedPeerInfo
177
+
178
+ // Stream State
179
+ streamsMutex sync.Mutex
180
+ persistentChatStreams map[peer.ID]network.Stream
181
+
182
+ // Rendezvous State
183
+ rendezvousMutex sync.RWMutex
184
+ rendezvousState *RendezvousState
185
+
186
+ // a copy of its own index for logging
187
+ instanceIndex int
188
+ }
189
+
190
+ // --- Create a package-level logger ---
191
+ var logger = golog.Logger("unailib")
192
+
193
+ // --- Multi-Instance State Management ---
194
+ var (
195
+ // Set the libp2p configuration parameters.
196
+ maxInstances int
197
+ maxChannelQueueLen int
198
+ maxUniqueChannels int
199
+ MaxMessageSize uint32
200
+
201
+ // A single slice to hold all our instances.
202
+ allInstances []*NodeInstance
203
+ // A SINGLE mutex to protect the allInstances slice itself (during create/close).
204
+ globalInstanceMutex sync.RWMutex
205
+ )
206
+
207
+ // --- Helper Functions ---
208
+ // jsonErrorResponse creates a JSON string representing an error state.
209
+ // It takes a base message and an optional error, formats them, escapes the message
210
+ // for JSON embedding, and returns a C string pointer (`*C.char`).
211
+ // The caller (usually C/Python) is responsible for freeing this C string using FreeString.
212
+ func jsonErrorResponse(
213
+ message string,
214
+ err error,
215
+ ) *C.char {
216
+
217
+ errMsg := message
218
+ if err != nil {
219
+ errMsg = fmt.Sprintf("%s: %s", message, err.Error())
220
+ }
221
+ logger.Errorf("[GO] ❌ Error: %s", errMsg)
222
+ // Ensure error messages are escaped properly for JSON embedding
223
+ escapedErrMsg := escapeStringForJSON(errMsg)
224
+ // Format into a standard {"state": "Error", "message": "..."} JSON structure.
225
+ jsonError := fmt.Sprintf(`{"state":"Error","message":"%s"}`, escapedErrMsg)
226
+ // Convert the Go string to a C string (allocates memory in C heap).
227
+ return C.CString(jsonError)
228
+ }
229
+
230
+ // jsonSuccessResponse creates a JSON string representing a success state.
231
+ // It takes an arbitrary Go object (`message`), marshals it into JSON, wraps it
232
+ // in a standard {"state": "Success", "message": {...}} structure, and returns
233
+ // a C string pointer (`*C.char`).
234
+ // The caller (usually C/Python) is responsible for freeing this C string using FreeString.
235
+ func jsonSuccessResponse(
236
+ message interface{},
237
+ ) *C.char {
238
+
239
+ // Marshal the provided Go data structure into JSON bytes.
240
+ jsonData, err := json.Marshal(message)
241
+ if err != nil {
242
+ // If marshalling fails, return a JSON error response instead.
243
+ return jsonErrorResponse("Failed to marshal success response", err)
244
+ }
245
+ // Format into the standard success structure.
246
+ jsonSuccess := fmt.Sprintf(`{"state":"Success","message":%s}`, string(jsonData))
247
+ // Convert the Go string to a C string (allocates memory in C heap).
248
+ return C.CString(jsonSuccess)
249
+ }
250
+
251
+ // escapeStringForJSON performs basic escaping of characters (like double quotes and backslashes)
252
+ // within a string to ensure it's safe to embed within a JSON string value.
253
+ // It uses Go's standard JSON encoder for robust escaping.
254
+ func escapeStringForJSON(
255
+ s string,
256
+ ) string {
257
+
258
+ var buf bytes.Buffer
259
+ // Encode the string using Go's JSON encoder, which handles escaping.
260
+ json.NewEncoder(&buf).Encode(s)
261
+ // The encoder adds surrounding quotes and a trailing newline, which we remove.
262
+ res := buf.String()
263
+ // Check bounds before slicing to avoid panic.
264
+ if len(res) > 2 && res[0] == '"' && res[len(res)-2] == '"' {
265
+ return res[1 : len(res)-2] // Trim surrounding quotes and newline
266
+ }
267
+ // Fallback if encoding behaves unexpectedly (e.g., empty string).
268
+ return s
269
+ }
270
+
271
+ // getInstance is a new helper to safely retrieve a node instance.
272
+ // It handles bounds checking, nil checks, and locking.
273
+ func getInstance(instanceIndex int) (*NodeInstance, error) {
274
+ if instanceIndex < 0 || instanceIndex >= maxInstances {
275
+ return nil, fmt.Errorf("invalid instance index: %d. Must be between 0 and %d", instanceIndex, maxInstances-1)
276
+ }
277
+
278
+ // Use a Read Lock, which is fast and allows concurrent reads.
279
+ globalInstanceMutex.RLock()
280
+ instance := allInstances[instanceIndex]
281
+ globalInstanceMutex.RUnlock()
282
+
283
+ if instance == nil {
284
+ return nil, fmt.Errorf("instance %d is not initialized or has been closed", instanceIndex)
285
+ }
286
+
287
+ // Check host as a proxy for *full* initialization,
288
+ // as it's set late in CreateNode
289
+ if instance.host == nil {
290
+ return nil, fmt.Errorf("instance %d is not fully initialized (host is nil)", instanceIndex)
291
+ }
292
+
293
+ return instance, nil
294
+ }
295
+
296
+ // newMessageStore initializes a new MessageStore.
297
+ func newMessageStore() *MessageStore {
298
+ return &MessageStore{
299
+ messagesByChannel: make(map[string]*list.List),
300
+ }
301
+ }
302
+
303
+ func loadOrCreateIdentity(keyPath string) (crypto.PrivKey, error) {
304
+ // Check if key file already exists.
305
+ if _, err := os.Stat(keyPath); err == nil {
306
+ // Key file exists, read and unmarshal it.
307
+ bytes, err := os.ReadFile(keyPath)
308
+ if err != nil {
309
+ return nil, fmt.Errorf("failed to read existing key file: %w", err)
310
+ }
311
+ // load the key
312
+ privKey, err := crypto.UnmarshalPrivateKey(bytes)
313
+ if err != nil {
314
+ return nil, fmt.Errorf("failed to unmarshal corrupt private key: %w", err)
315
+ }
316
+ return privKey, nil
317
+
318
+ } else if os.IsNotExist(err) {
319
+ // Key file does not exist, generate a new one.
320
+ logger.Infof("[GO] 💎 Generating new persistent peer identity in %s\n", keyPath)
321
+ privKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
322
+ if err != nil {
323
+ return nil, fmt.Errorf("failed to generate new key: %w", err)
324
+ }
325
+
326
+ // Marshal the new key to bytes.
327
+ bytes, err := crypto.MarshalPrivateKey(privKey)
328
+ if err != nil {
329
+ return nil, fmt.Errorf("failed to marshal new private key: %w", err)
330
+ }
331
+
332
+ // Write the new key to a file.
333
+ if err := os.WriteFile(keyPath, bytes, 0400); err != nil {
334
+ return nil, fmt.Errorf("failed to write new key file: %w", err)
335
+ }
336
+ return privKey, nil
337
+
338
+ } else {
339
+ // Another error occurred (e.g., permissions).
340
+ return nil, fmt.Errorf("failed to stat key file: %w", err)
341
+ }
342
+ }
343
+
344
+ func getListenAddrs(ips []string, tcpPort int, tlsMode string) ([]ma.Multiaddr, error) {
345
+ if len(ips) == 0 {
346
+ ips = []string{"0.0.0.0"}
347
+ }
348
+
349
+ var listenAddrs []ma.Multiaddr
350
+ quicPort := 0
351
+ // webrtcPort := 0
352
+ webtransPort := 0
353
+ if tcpPort != 0 {
354
+ quicPort = tcpPort + 1
355
+ // webrtcPort = tcpPort + 2
356
+ webtransPort = tcpPort + 3
357
+ }
358
+
359
+ // --- Create Multiaddrs for both protocols from the single IP list ---
360
+ for _, ip := range ips {
361
+ // TCP
362
+ tcpMaddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, tcpPort))
363
+ // QUIC
364
+ quicMaddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/udp/%d/quic-v1", ip, quicPort))
365
+ // WebTransport
366
+ webtransMaddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/udp/%d/quic-v1/webtransport", ip, webtransPort))
367
+ // // WebRTC
368
+ // webrtcMaddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/udp/%d/webrtc-direct", ip, webrtcPort))
369
+
370
+ // listenAddrs = append(listenAddrs, tcpMaddr, quicMaddr, webrtcMaddr, webtransMaddr)
371
+ listenAddrs = append(listenAddrs, tcpMaddr, quicMaddr, webtransMaddr)
372
+
373
+ switch tlsMode {
374
+ case "autotls":
375
+ // This is the special multiaddr that triggers AutoTLS
376
+ wssMaddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/tls/sni/*.%s/ws", ip, tcpPort, p2pforge.DefaultForgeDomain))
377
+ listenAddrs = append(listenAddrs, wssMaddr)
378
+ case "domain":
379
+ // This is the standard secure WebSocket address with provided domain
380
+ wssMaddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/tls/ws", ip, tcpPort))
381
+ listenAddrs = append(listenAddrs, wssMaddr)
382
+ default:
383
+ // Fallback to a standard, non-secure WebSocket address
384
+ wsMaddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d/ws", ip, tcpPort))
385
+ listenAddrs = append(listenAddrs, wsMaddr)
386
+ }
387
+ }
388
+
389
+ logger.Debugf("[GO] 🔧 Prepared Listen Addresses: %v\n", listenAddrs)
390
+
391
+ return listenAddrs, nil
392
+ }
393
+
394
+ func createResourceManager(maxConnections int) (network.ResourceManager, error) {
395
+ limits := rcmgr.DefaultLimits
396
+ libp2p.SetDefaultServiceLimits(&limits)
397
+ myLimits := rcmgr.PartialLimitConfig{
398
+ System: rcmgr.ResourceLimits{Conns: rcmgr.LimitVal(maxConnections)},
399
+ }
400
+ concreteLimits := myLimits.Build(limits.AutoScale())
401
+ return rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(concreteLimits))
402
+ }
403
+
404
+ func setupPubSub(ni *NodeInstance) error {
405
+ psOptions := []pubsub.Option{
406
+ // pubsub.WithFloodPublish(true),
407
+ pubsub.WithMaxMessageSize(int(MaxMessageSize)),
408
+ }
409
+ ps, err := pubsub.NewGossipSub(ni.ctx, ni.host, psOptions...)
410
+ if err != nil {
411
+ return err
412
+ }
413
+ ni.pubsub = ps // Set the pubsub field on the instance
414
+ return nil
415
+ }
416
+
417
+ func setupNotifiers(ni *NodeInstance) {
418
+ ni.host.Network().Notify(&network.NotifyBundle{
419
+ ConnectedF: func(_ network.Network, conn network.Conn) {
420
+ logger.Debugf("[GO] 🔔 Instance %d: Event - Connected to %s (Direction: %s)\n", ni.instanceIndex, conn.RemotePeer(), conn.Stat().Direction)
421
+ // the old logic to add the peer to the connectedPeers map was moved inside addPeerToTrackedMap
422
+ },
423
+ DisconnectedF: func(_ network.Network, conn network.Conn) {
424
+ logger.Debugf("[GO] 🔔 Instance %d: Event - Disconnected from %s\n", ni.instanceIndex, conn.RemotePeer())
425
+ remotePeerID := conn.RemotePeer()
426
+
427
+ // Get the host for this instance to query its network state.
428
+ if ni.host == nil {
429
+ // This shouldn't happen if the notifier is active, but a safe check.
430
+ logger.Warnf("[GO] âš ī¸ Instance %d: DisconnectedF: Host is nil, cannot perform connection check.\n", ni.instanceIndex)
431
+ return
432
+ }
433
+
434
+ // --- Check if this is the LAST connection to this peer ---
435
+ // libp2p can have multiple connections to a single peer (e.g., TCP, QUIC).
436
+ // We only want to consider the peer fully disconnected when ALL connections are gone.
437
+ if len(ni.host.Network().ConnsToPeer(remotePeerID)) == 0 {
438
+ logger.Debugf("[GO] Instance %d: Last connection to %s closed. Removing from tracked peers.\n", ni.instanceIndex, remotePeerID)
439
+
440
+ // Handle disconnection for ConnectedPeers
441
+ ni.peersMutex.Lock()
442
+ if _, exists := ni.connectedPeers[remotePeerID]; exists {
443
+ delete(ni.connectedPeers, remotePeerID)
444
+ logger.Debugf("[GO] Instance %d: Removed %s from ConnectedPeers via DisconnectedF notifier.\n", ni.instanceIndex, remotePeerID)
445
+ //peerRemoved = true
446
+ }
447
+ ni.peersMutex.Unlock()
448
+
449
+ // Also clean up persistent stream if one existed for this peer
450
+ ni.streamsMutex.Lock()
451
+ if stream, ok := ni.persistentChatStreams[remotePeerID]; ok {
452
+ logger.Debugf("[GO] Instance %d: Cleaning up persistent stream for disconnected peer %s via DisconnectedF notifier.\n", ni.instanceIndex, remotePeerID)
453
+ _ = stream.Close() // Attempt graceful close
454
+ delete(ni.persistentChatStreams, remotePeerID)
455
+ }
456
+ ni.streamsMutex.Unlock()
457
+ } else {
458
+ logger.Debugf("[GO] Instance %d: DisconnectedF: Still have %d active connections to %s, not removing from tracked peers.\n", ni.instanceIndex, len(ni.host.Network().ConnsToPeer(remotePeerID)), remotePeerID)
459
+ }
460
+ },
461
+ })
462
+ }
463
+
464
+ // handleAddressUpdateEvents listens for libp2p address changes and updates the local cache.
465
+ func handleAddressUpdateEvents(ni *NodeInstance, sub event.Subscription) {
466
+ defer sub.Close()
467
+
468
+ // Initialize cache immediately with current state to avoid race conditions at startup
469
+ ni.addrMutex.Lock()
470
+ ni.localAddrs = ni.host.Addrs()
471
+ ni.addrMutex.Unlock()
472
+
473
+ for {
474
+ select {
475
+ case <-ni.ctx.Done():
476
+ return
477
+ case _, ok := <-sub.Out():
478
+ if !ok {
479
+ return
480
+ }
481
+ // We only use the event as a trigger but we take the addresses from the Host
482
+ allAddresses := ni.host.Addrs()
483
+ ni.addrMutex.Lock()
484
+ ni.localAddrs = allAddresses
485
+ ni.addrMutex.Unlock()
486
+
487
+ // Log addresses to verify
488
+ addrsStr := make([]string, len(allAddresses))
489
+ for i, a := range allAddresses {
490
+ addrsStr[i] = a.String()
491
+ }
492
+ logger.Debugf("[GO] 🔄 Instance %d: Updated local addresses (updating cache). Addrs: %v", ni.instanceIndex, addrsStr)
493
+ }
494
+ }
495
+ }
496
+
497
+ // Helper to filter peers for PeerSource
498
+ func (ni *NodeInstance) isSuitableForPeerSource(pid peer.ID) bool {
499
+ ps := ni.host.Peerstore()
500
+
501
+ // 1. Check for Relay Hop Protocol (NEEDED)
502
+ protocols, err := ps.GetProtocols(pid)
503
+ if err != nil {
504
+ return false
505
+ }
506
+ isRelay := false
507
+ for _, proto := range protocols {
508
+ if proto == "/libp2p/circuit/relay/0.2.0/hop" {
509
+ isRelay = true
510
+ break
511
+ }
512
+ }
513
+ if !isRelay {
514
+ return false
515
+ }
516
+
517
+ // 2. By now we only check for WebRTC-Direct Address (Transport Layer)
518
+ // We iterate addresses to find one that contains the webrtc-direct component.
519
+ addrs := ps.Addrs(pid)
520
+ isSuitableWRTC := false
521
+ isSuitableWSS := false
522
+ isSuitable := false
523
+ for _, addr := range addrs {
524
+ _, err := addr.ValueForProtocol(ma.P_WEBRTC_DIRECT)
525
+ if err == nil {
526
+ isSuitableWRTC = true
527
+ }
528
+ _, err = addr.ValueForProtocol(ma.P_WS)
529
+ if err == nil {
530
+ _, err := addr.ValueForProtocol(ma.P_TLS)
531
+ if err == nil {
532
+ isSuitableWSS = true
533
+ }
534
+ }
535
+ }
536
+ isSuitable = isSuitableWRTC && isSuitableWSS
537
+
538
+ return isSuitable
539
+ }
540
+
541
+ // PeerSource acts as the peer discovery backend for AutoRelay.
542
+ // It combines a local cache lookup (fast/free) with a DHT random walk (slow/expensive).
543
+ func (ni *NodeInstance) PeerSource(ctx context.Context, numPeers int) <-chan peer.AddrInfo {
544
+ out := make(chan peer.AddrInfo)
545
+
546
+ go func() {
547
+ defer close(out)
548
+
549
+ // Safety checks: Ensure host and DHT are fully initialized
550
+ if ni.host == nil || ni.dht == nil {
551
+ return
552
+ }
553
+
554
+ // Keep track of peers we've already sent in this batch
555
+ sentPeers := make(map[peer.ID]struct{})
556
+ peersFound := 0
557
+
558
+ // --- PHASE 1: Scavenge Local Peerstore ---
559
+ localPeers := ni.host.Peerstore().Peers()
560
+ for _, pid := range localPeers {
561
+ if peersFound >= numPeers {
562
+ return
563
+ }
564
+ if pid == ni.host.ID() {
565
+ continue
566
+ }
567
+
568
+ // Add it if it meets our criteria
569
+ if ni.isSuitableForPeerSource(pid) {
570
+ info := ni.host.Peerstore().PeerInfo(pid)
571
+ if len(info.Addrs) == 0 {
572
+ continue
573
+ }
574
+
575
+ select {
576
+ case out <- info:
577
+ sentPeers[pid] = struct{}{}
578
+ peersFound++
579
+ case <-ctx.Done():
580
+ return
581
+ }
582
+ }
583
+ }
584
+
585
+ // --- PHASE 2: DHT Random Walk ---
586
+ if peersFound < numPeers {
587
+ logger.Debugf("[GO] âš ī¸ Instance %d: Local peerstore insufficient (%d/%d). Starting DHT walk...",
588
+ ni.instanceIndex, peersFound, numPeers)
589
+
590
+ for peersFound < numPeers {
591
+ randomKey := make([]byte, 32)
592
+ rand.Read(randomKey)
593
+ randomKeyStr := string(randomKey)
594
+
595
+ candidatePIDs, err := ni.dht.GetClosestPeers(ctx, randomKeyStr)
596
+ if err != nil {
597
+ select {
598
+ case <-ctx.Done():
599
+ return
600
+ case <-time.After(2 * time.Second):
601
+ continue
602
+ }
603
+ }
604
+
605
+ for _, pid := range candidatePIDs {
606
+ if peersFound >= numPeers {
607
+ return
608
+ }
609
+ if pid == ni.host.ID() {
610
+ continue
611
+ }
612
+ if _, alreadySent := sentPeers[pid]; alreadySent {
613
+ continue
614
+ }
615
+
616
+ info := ni.host.Peerstore().PeerInfo(pid)
617
+ if len(info.Addrs) > 0 {
618
+ select {
619
+ case out <- info:
620
+ sentPeers[pid] = struct{}{}
621
+ peersFound++
622
+ case <-ctx.Done():
623
+ return
624
+ }
625
+ }
626
+ }
627
+ }
628
+ }
629
+ }()
630
+
631
+ return out
632
+ }
633
+
634
+ // --- Core Logic Functions ---
635
+
636
+ // storeReceivedMessage processes a raw message received either from a direct stream
637
+ // or a PubSub topic. The sender peerID and the channel to store are retrieved in handleStream and readFromSubscription
638
+ func storeReceivedMessage(
639
+ ni *NodeInstance,
640
+ from peer.ID,
641
+ channel string,
642
+ data []byte,
643
+ ) {
644
+ // Get the message store for this instance
645
+ store := ni.messageStore
646
+ if store == nil {
647
+ logger.Errorf("[GO] ❌ storeReceivedMessage: Message store not initialized for instance %d\n", ni.instanceIndex)
648
+ return // Cannot process message if store is nil
649
+ }
650
+
651
+ // Create the minimal message envelope.
652
+ newMessage := &QueuedMessage{
653
+ From: from,
654
+ Data: data,
655
+ }
656
+
657
+ // Lock the store mutex before accessing the shared maps.
658
+ store.mu.Lock()
659
+ defer store.mu.Unlock()
660
+
661
+ // Check if this channel already has a message list.
662
+ messageList, channelExists := store.messagesByChannel[channel]
663
+ if !channelExists {
664
+ // If the channel does not exist, check if we can create a new message queue.
665
+ if len(store.messagesByChannel) >= maxUniqueChannels {
666
+ logger.Warnf("[GO] đŸ—‘ī¸ Instance %d: Message store full. Discarding message for new channel '%s'.\n", ni.instanceIndex, channel)
667
+ return
668
+ }
669
+ messageList = list.New()
670
+ store.messagesByChannel[channel] = messageList
671
+ logger.Debugf("[GO] ✨ Instance %d: Created new channel queue '%s'. Total channels: %d\n", ni.instanceIndex, channel, len(store.messagesByChannel))
672
+ }
673
+
674
+ // If the channel already has a message list, check its length.
675
+ if messageList.Len() >= maxChannelQueueLen {
676
+ logger.Warnf("[GO] đŸ—‘ī¸ Instance %d: Queue for channel '%s' full. Discarding message.\n", ni.instanceIndex, channel)
677
+ return
678
+ }
679
+
680
+ messageList.PushBack(newMessage)
681
+ logger.Debugf("[GO] đŸ“Ĩ Instance %d: Queued message on channel '%s' from %s. New queue length: %d\n", ni.instanceIndex, channel, from, messageList.Len())
682
+ }
683
+
684
+ // readFromSubscription runs as a dedicated goroutine for each active PubSub subscription for a specific instance.
685
+ // It continuously waits for new messages on the subscription's channel (`sub.Next(ctx)`),
686
+ // routes them to `storeReceivedMessage`, and handles errors and context cancellation gracefully.
687
+ // You need to provide the full Channel to uniquely identify the subscription.
688
+ func readFromSubscription(
689
+ ni *NodeInstance,
690
+ sub *pubsub.Subscription,
691
+ ) {
692
+ // Get the topic string directly from the subscription object.
693
+ topic := sub.Topic()
694
+
695
+ if ni.ctx == nil || ni.host == nil {
696
+ logger.Errorf("[GO] ❌ readFromSubscription: Context or Host not initialized for instance %d. Exiting goroutine.\n", ni.instanceIndex)
697
+ return
698
+ }
699
+
700
+ logger.Infof("[GO] 👂 Instance %d: Started listener goroutine for topic: %s\n", ni.instanceIndex, topic)
701
+ defer logger.Infof("[GO] 👂 Instance %d: Exiting listener goroutine for topic: %s\n", ni.instanceIndex, topic) // Log when goroutine exits
702
+
703
+ for {
704
+ // Check if the main context has been cancelled (e.g., during node shutdown).
705
+ if ni.ctx.Err() != nil {
706
+ logger.Debugf("[GO] 👂 Instance %d: Context cancelled, stopping listener goroutine for topic: %s\n", ni.instanceIndex, topic)
707
+ return // Exit the goroutine.
708
+ }
709
+
710
+ // Wait for the next message from the subscription. This blocks until a message
711
+ // arrives, the context is cancelled, or an error occurs.
712
+ msg, err := sub.Next(ni.ctx)
713
+ if err != nil {
714
+ // Check for expected errors during shutdown or cancellation.
715
+ if err == context.Canceled || err == context.DeadlineExceeded || err == pubsub.ErrSubscriptionCancelled || ni.ctx.Err() != nil {
716
+ logger.Debugf("[GO] 👂 Instance %d: Subscription listener for topic '%s' stopping gracefully: %v\n", ni.instanceIndex, topic, err)
717
+ return // Exit goroutine cleanly.
718
+ }
719
+ // Handle EOF, which can sometimes occur. Treat it as a reason to stop.
720
+ if err == io.EOF {
721
+ logger.Debugf("[GO] 👂 Instance %d: Subscription listener for topic '%s' encountered EOF, stopping: %v\n", ni.instanceIndex, topic, err)
722
+ return // Exit goroutine.
723
+ }
724
+ // Log other errors but attempt to continue (they might be transient).
725
+ logger.Errorf("[GO] ❌ Instance %d: Error reading from subscription '%s': %v. Continuing...\n", ni.instanceIndex, topic, err)
726
+ // Pause briefly to avoid busy-looping on persistent errors.
727
+ time.Sleep(1 * time.Second)
728
+ continue // Continue the loop to try reading again.
729
+ }
730
+
731
+ logger.Infof("[GO] đŸ“Ŧ Instance %d (id: %s): Received new PubSub message on topic '%s' from %s\n", ni.instanceIndex, ni.host.ID().String(), topic, msg.GetFrom())
732
+
733
+ // Ignore messages published by the local node itself.
734
+ if msg.GetFrom() == ni.host.ID() {
735
+ continue // Skip processing self-sent messages.
736
+ }
737
+
738
+ // Handle Rendezvous or Standard Messages
739
+ if strings.HasSuffix(topic, ":rv") {
740
+ // This is a rendezvous update.
741
+ // 1. First, unmarshal the outer Protobuf message.
742
+ var protoMsg pg.Message
743
+ if err := proto.Unmarshal(msg.Data, &protoMsg); err != nil {
744
+ logger.Warnf("âš ī¸ Instance %d: Could not decode Protobuf message on topic '%s': %v\n", ni.instanceIndex, topic, err)
745
+ continue
746
+ }
747
+
748
+ // 2. The actual payload is a JSON string within the 'json_content' field.
749
+ jsonPayload := protoMsg.GetJsonContent()
750
+ if jsonPayload == "" {
751
+ logger.Warnf("âš ī¸ Instance %d: Rendezvous message on topic '%s' has empty JSON content.\n", ni.instanceIndex, topic)
752
+ continue
753
+ }
754
+
755
+ // 3. Now, unmarshal the inner JSON payload.
756
+ var updatePayload struct {
757
+ Peers []ExtendedPeerInfo `json:"peers"`
758
+ UpdateCount int64 `json:"update_count"`
759
+ }
760
+ if err := json.Unmarshal([]byte(jsonPayload), &updatePayload); err != nil {
761
+ logger.Warnf("[GO] âš ī¸ Instance %d: Could not decode rendezvous update payload on topic '%s': %v\n", ni.instanceIndex, topic, err)
762
+ continue // Skip this malformed message.
763
+ }
764
+
765
+ // 4. Create a new map from the decoded peer list.
766
+ newPeerMap := make(map[peer.ID]ExtendedPeerInfo)
767
+ for _, peerInfo := range updatePayload.Peers {
768
+ newPeerMap[peerInfo.ID] = peerInfo
769
+ }
770
+
771
+ // 5. Safely replace the old map with the new one.
772
+ ni.rendezvousMutex.Lock()
773
+ // If this is the first update for this instance, initialize the state struct.
774
+ if ni.rendezvousState == nil {
775
+ ni.rendezvousState = &RendezvousState{}
776
+ }
777
+ rendezvousState := ni.rendezvousState
778
+ rendezvousState.Peers = newPeerMap
779
+ rendezvousState.UpdateCount = updatePayload.UpdateCount
780
+ ni.rendezvousMutex.Unlock()
781
+
782
+ logger.Debugf("[GO] ✅ Instance %d: Updated rendezvous peers from topic '%s'. Found %d peers. Update count: %d.\n", ni.instanceIndex, topic, len(newPeerMap), updatePayload.UpdateCount)
783
+ } else {
784
+ // This is a standard message. Queue it as before.
785
+ logger.Debugf("[GO] 📝 Instance %d: Storing new pubsub message from topic '%s'.\n", ni.instanceIndex, topic)
786
+ storeReceivedMessage(ni, msg.GetFrom(), topic, msg.Data)
787
+ }
788
+ }
789
+ }
790
+
791
+ // handleStream reads from a direct message stream using the new framing protocol.
792
+ // It expects the stream to start with a 4-byte length prefix, followed by a 1-byte channel name length,
793
+ // the channel name itself, and finally the Protobuf-encoded payload.
794
+ func handleStream(ni *NodeInstance, s network.Stream) {
795
+ senderPeerID := s.Conn().RemotePeer()
796
+ ni.peersMutex.Lock()
797
+ existing, exists := ni.connectedPeers[senderPeerID]
798
+
799
+ // 1. Gather fresh info (Addresses & Direction)
800
+ direction := "incoming"
801
+ if s.Stat().Direction == network.DirOutbound {
802
+ direction = "outgoing"
803
+ }
804
+ knownAddrs := ni.host.Peerstore().Addrs(senderPeerID)
805
+ if len(knownAddrs) == 0 {
806
+ knownAddrs = []ma.Multiaddr{s.Conn().RemoteMultiaddr()}
807
+ }
808
+
809
+ if !exists {
810
+ // CASE A: New Application Peer
811
+ ni.connectedPeers[senderPeerID] = ExtendedPeerInfo{
812
+ ID: senderPeerID,
813
+ Addrs: knownAddrs,
814
+ ConnectedAt: time.Now(),
815
+ Direction: direction,
816
+ }
817
+ logger.Infof("[GO] ➕ Instance %d: Peer %s promoted to Application Peer (Incoming Stream).", ni.instanceIndex, senderPeerID)
818
+ } else {
819
+ // CASE B: Existing Peer - Update Addresses
820
+ // We keep ConnectedAt and Direction from the original session start.
821
+ existing.Addrs = knownAddrs
822
+ ni.connectedPeers[senderPeerID] = existing
823
+ logger.Debugf("[GO] 🔄 Instance %d: Refreshed addresses for existing Application Peer %s.", ni.instanceIndex, senderPeerID)
824
+ }
825
+ ni.peersMutex.Unlock()
826
+ logger.Debugf("[GO] đŸ“Ĩ Instance %d: Accepted new INCOMING stream from %s, storing for duplex communication.\n", ni.instanceIndex, senderPeerID)
827
+
828
+ // This defer block ensures cleanup happens when the stream is closed by either side.
829
+ defer func() {
830
+ logger.Debugf("[GO] 🧹 Instance %d: Inbound stream from %s closed. Removing from persistent map.\n", ni.instanceIndex, senderPeerID)
831
+ ni.streamsMutex.Lock()
832
+ delete(ni.persistentChatStreams, senderPeerID)
833
+ ni.streamsMutex.Unlock()
834
+ s.Close() // Ensure the stream is fully closed.
835
+ }()
836
+
837
+ // Store the newly accepted stream so we can use it to send messages back to this peer.
838
+ ni.streamsMutex.Lock()
839
+ ni.persistentChatStreams[senderPeerID] = s
840
+ ni.streamsMutex.Unlock()
841
+
842
+ for {
843
+ // 1. Read the 4-byte total length prefix.
844
+ var totalLen uint32
845
+ if err := binary.Read(s, binary.BigEndian, &totalLen); err != nil {
846
+ if err == io.EOF {
847
+ logger.Debugf("[GO] 🔌 Instance %d: Direct stream with peer %s closed (EOF).\n", ni.instanceIndex, senderPeerID)
848
+ } else if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
849
+ logger.Warnf("[GO] âŗ Instance %d: Timeout reading length from direct stream with %s: %v\n", ni.instanceIndex, senderPeerID, err)
850
+ } else if errors.Is(err, network.ErrReset) {
851
+ // network.ErrReset indicates the stream was closed gracefully/reset by the peer or locally.
852
+ logger.Warnf("[GO] âš™ī¸ Instance %d: Direct stream with peer %s reset (graceful teardown/libp2p).", ni.instanceIndex, senderPeerID)
853
+ } else {
854
+ logger.Errorf("[GO] ❌ Instance %d: Unexpected error reading length from direct stream with %s: %v\n", ni.instanceIndex, senderPeerID, err)
855
+ }
856
+ return // Exit handler for any read error on length.
857
+ }
858
+
859
+ // --- Check the message size ---
860
+ if totalLen > MaxMessageSize {
861
+ logger.Errorf("[GO] ❌ Instance %d: Received message length %d exceeds limit (%d) from %s. Resetting stream.\n", ni.instanceIndex, totalLen, MaxMessageSize, senderPeerID)
862
+ s.Reset() // Forcefully close the stream due to protocol violation.
863
+ return
864
+ }
865
+ if totalLen == 0 {
866
+ logger.Warnf("[GO] âš ī¸ Instance %d: Received zero length message frame from %s, continuing loop.\n", ni.instanceIndex, senderPeerID)
867
+ continue
868
+ }
869
+
870
+ // 2. Read the 1-byte channel name length.
871
+ var channelLen uint8
872
+ if err := binary.Read(s, binary.BigEndian, &channelLen); err != nil {
873
+ if err == io.EOF {
874
+ logger.Debugf("[GO] 🔌 Instance %d: Direct stream with peer %s closed (EOF).\n", ni.instanceIndex, senderPeerID)
875
+ } else if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
876
+ logger.Warnf("[GO] âŗ Instance %d: Timeout reading channel-length from direct stream with %s: %v\n", ni.instanceIndex, senderPeerID, err)
877
+ } else {
878
+ logger.Errorf("[GO] ❌ Instance %d: Unexpected error reading channel-length from direct stream with %s: %v\n", ni.instanceIndex, senderPeerID, err)
879
+ }
880
+ return // Exit handler for any read error on length.
881
+ }
882
+
883
+ // 3. Read the channel name string.
884
+ channelBytes := make([]byte, channelLen)
885
+ if _, err := io.ReadFull(s, channelBytes); err != nil {
886
+ if err == io.EOF {
887
+ logger.Debugf("[GO] 🔌 Instance %d: Direct stream with peer %s closed (EOF).\n", ni.instanceIndex, senderPeerID)
888
+ } else if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
889
+ logger.Warnf("[GO] âŗ Instance %d: Timeout reading channel from direct stream with %s: %v\n", ni.instanceIndex, senderPeerID, err)
890
+ } else {
891
+ logger.Errorf("[GO] ❌ Instance %d: Unexpected error reading channel from direct stream with %s: %v\n", ni.instanceIndex, senderPeerID, err)
892
+ }
893
+ return // Exit handler for any read error on length.
894
+ }
895
+ channel := string(channelBytes)
896
+
897
+ // 4. Read the Protobuf payload.
898
+ payloadLen := totalLen - uint32(channelLen) - 1 // Subtract channel len byte and channel string
899
+ payload := make([]byte, payloadLen)
900
+ if _, err := io.ReadFull(s, payload); err != nil {
901
+ if err == io.EOF {
902
+ logger.Debugf("[GO] 🔌 Instance %d: Direct stream with peer %s closed (EOF).\n", ni.instanceIndex, senderPeerID)
903
+ } else if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
904
+ logger.Warnf("[GO] âŗ Instance %d: Timeout reading payload from direct stream with %s: %v\n", ni.instanceIndex, senderPeerID, err)
905
+ } else {
906
+ logger.Errorf("[GO] ❌ Instance %d: Unexpected error reading payload from direct stream with %s: %v\n", ni.instanceIndex, senderPeerID, err)
907
+ }
908
+ return // Exit handler for any read error on length.
909
+ }
910
+
911
+ // 5. Store the message.
912
+ logger.Infof("[GO] 📨 Instance %d: Received direct message on channel '%s' from %s, storing.\n", ni.instanceIndex, channel, senderPeerID)
913
+ storeReceivedMessage(ni, senderPeerID, channel, payload)
914
+ }
915
+ }
916
+
917
+ // setupDirectMessageHandler configures the libp2p host for a specific instance
918
+ // to listen for incoming streams using the custom ChatProtocol.
919
+ // When a peer opens a stream with this protocol ID, the provided handler function
920
+ // is invoked to manage communication on that stream.
921
+ func setupDirectMessageHandler(
922
+ ni *NodeInstance,
923
+ ) {
924
+ if ni.host == nil {
925
+ logger.Errorf("[GO] ❌ Instance %d: Cannot setup direct message handler: Host not initialized\n", ni.instanceIndex)
926
+ return
927
+ }
928
+
929
+ // Set a handler function for the UnaiverseChatProtocol. This function will be called
930
+ // automatically by libp2p whenever a new incoming stream for this protocol is accepted.
931
+ // Use a closure to capture the NodeInstance pointer.
932
+ ni.host.SetStreamHandler(UnaiverseChatProtocol, func(s network.Stream) {
933
+ handleStream(ni, s)
934
+ })
935
+ }
936
+
937
+ // This function constructs and writes a message using our new framing protocol for direct messages.
938
+ // It takes a writer (e.g., a network stream), the channel name, and the payload data.
939
+ // The message format is:
940
+ // - 4-byte total length (including all the following parts)
941
+ // - 1-byte channel name length
942
+ // - channel name (as a UTF-8 string)
943
+ // - payload (Protobuf-encoded data).
944
+ func writeDirectMessageFrame(w io.Writer, channel string, payload []byte) error {
945
+ channelBytes := []byte(channel)
946
+ channelLen := uint8(len(channelBytes))
947
+
948
+ // Check if channel name is too long for our 1-byte length prefix.
949
+ if len(channelBytes) > 255 {
950
+ return fmt.Errorf("channel name exceeds 255 bytes limit: %s", channel)
951
+ }
952
+
953
+ // Total length = 1 (for channel len) + len(channel) + len(payload)
954
+ totalLength := uint32(1 + len(channelBytes) + len(payload))
955
+
956
+ // --- Add size check before writing ---
957
+ if totalLength > MaxMessageSize {
958
+ return fmt.Errorf("outgoing message size (%d) exceeds limit (%d)", totalLength, MaxMessageSize)
959
+ }
960
+
961
+ buf := new(bytes.Buffer)
962
+
963
+ // Write total length (4 bytes)
964
+ if err := binary.Write(buf, binary.BigEndian, totalLength); err != nil {
965
+ return fmt.Errorf("failed to write total length: %w", err)
966
+ }
967
+ // Write channel length (1 byte)
968
+ if err := binary.Write(buf, binary.BigEndian, channelLen); err != nil {
969
+ return fmt.Errorf("failed to write channel length: %w", err)
970
+ }
971
+ // Write channel name
972
+ if _, err := buf.Write(channelBytes); err != nil {
973
+ return fmt.Errorf("failed to write channel name: %w", err)
974
+ }
975
+ // Write payload
976
+ if _, err := buf.Write(payload); err != nil {
977
+ return fmt.Errorf("failed to write payload: %w", err)
978
+ }
979
+
980
+ // Write the entire frame to the stream.
981
+ if _, err := w.Write(buf.Bytes()); err != nil {
982
+ return fmt.Errorf("failed to write framed message to stream: %w", err)
983
+ }
984
+ return nil
985
+ }
986
+
987
+ // goGetNodeAddresses is the internal Go function that performs the core logic
988
+ // of fetching and formatting node addresses.
989
+ // It takes a pointer to a NodeInstance and a targetPID. If targetPID is empty (peer.ID("")),
990
+ // it fetches addresses for the local node of the given instance.
991
+ // It returns a slice of fully formatted multiaddress strings and an error if one occurs.
992
+ func goGetNodeAddresses(
993
+ ni *NodeInstance,
994
+ targetPID peer.ID,
995
+ ) ([]string, error) {
996
+ if ni.host == nil {
997
+ errMsg := fmt.Sprintf("Instance %d: Host not initialized", ni.instanceIndex)
998
+ logger.Errorf("[GO] ❌ goGetNodeAddresses: %s\n", errMsg)
999
+ return nil, fmt.Errorf("%s", errMsg)
1000
+ }
1001
+
1002
+ // Determine the actual Peer ID to resolve addresses for.
1003
+ resolvedPID := targetPID
1004
+ isThisNode := false
1005
+ if targetPID == "" || targetPID == ni.host.ID() {
1006
+ resolvedPID = ni.host.ID()
1007
+ isThisNode = true
1008
+ }
1009
+
1010
+ // --- 1. Gather all candidate addresses from the host and peerstore ---
1011
+ var candidateAddrs []ma.Multiaddr
1012
+ if isThisNode {
1013
+ ni.addrMutex.RLock()
1014
+ candidateAddrs = append(candidateAddrs, ni.localAddrs...)
1015
+ candidateAddrs = append(candidateAddrs, ni.relayAddrs...)
1016
+ ni.addrMutex.RUnlock()
1017
+ } else {
1018
+ // --- Remote Peer Addresses ---
1019
+ ni.peersMutex.RLock()
1020
+ if epi, exists := ni.connectedPeers[resolvedPID]; exists {
1021
+ candidateAddrs = append(candidateAddrs, epi.Addrs...)
1022
+ }
1023
+ ni.peersMutex.RUnlock()
1024
+ candidateAddrs = append(candidateAddrs, ni.host.Peerstore().Addrs(resolvedPID)...)
1025
+ }
1026
+
1027
+ // --- 2. Process and filter candidate addresses ---
1028
+ addrSet := make(map[string]struct{})
1029
+ for _, addr := range candidateAddrs {
1030
+ // if addr == nil || manet.IsIPLoopback(addr) || manet.IsIPUnspecified(addr) {
1031
+ // continue
1032
+ // }
1033
+
1034
+ // Use the idiomatic `peer.SplitAddr` to check if the address already includes a Peer ID.
1035
+ var finalAddr ma.Multiaddr
1036
+ transportAddr, idInAddr := peer.SplitAddr(addr)
1037
+ if transportAddr == nil {
1038
+ continue
1039
+ }
1040
+
1041
+ // handle cases for different transport protocols
1042
+ if strings.HasPrefix(transportAddr.String(), "/p2p-circuit/") {
1043
+ continue
1044
+ }
1045
+ if strings.Contains(transportAddr.String(), "*") {
1046
+ continue
1047
+ }
1048
+
1049
+ // handle cases based on presence and correctness of Peer ID in the address
1050
+ switch {
1051
+ case idInAddr == resolvedPID:
1052
+ // Case A: The address is already perfect and has the correct Peer ID. Use it as is.
1053
+ finalAddr = addr
1054
+
1055
+ case idInAddr == "":
1056
+ // Case B: The address is missing a Peer ID. This is common for addresses from the
1057
+ // peerstore and for relayed addresses like `/p2p/RELAY_ID/p2p-circuit`. We must append ours.
1058
+ p2pComponent, _ := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", resolvedPID.String()))
1059
+ finalAddr = addr.Encapsulate(p2pComponent)
1060
+
1061
+ case idInAddr != resolvedPID:
1062
+ // Case C: The address has the WRONG Peer ID. This is stale or incorrect data. Discard it.
1063
+ logger.Warnf("[GO] âš ī¸ Instance %d: Discarding stale address for peer %s: %s\n", ni.instanceIndex, resolvedPID, addr)
1064
+ continue
1065
+ }
1066
+ addrSet[finalAddr.String()] = struct{}{}
1067
+ }
1068
+
1069
+ // --- 4. Convert the final set of unique addresses to a slice for returning. ---
1070
+ result := make([]string, 0, len(addrSet))
1071
+ for addr := range addrSet {
1072
+ result = append(result, addr)
1073
+ }
1074
+
1075
+ if len(result) == 0 {
1076
+ logger.Warnf("[GO] âš ī¸ goGetNodeAddresses: No suitable addresses found for peer %s.", resolvedPID)
1077
+ }
1078
+
1079
+ return result, nil
1080
+ }
1081
+
1082
+ // Close gracefully shuts down all components of this node instance.
1083
+ // This REPLACES the old `closeSingleInstance` function.
1084
+ func (ni *NodeInstance) Close() error {
1085
+ logger.Infof("[GO] 🛑 Instance %d: Closing node...", ni.instanceIndex)
1086
+
1087
+ // --- Stop Cert Manager FIRST ---
1088
+ if ni.certManager != nil {
1089
+ logger.Debugf("[GO] - Instance %d: Stopping AutoTLS cert manager...\n", ni.instanceIndex)
1090
+ ni.certManager.Stop()
1091
+ }
1092
+
1093
+ // --- Cancel Main Context ---
1094
+ if ni.cancel != nil {
1095
+ logger.Debugf("[GO] - Instance %d: Cancelling main context...\n", ni.instanceIndex)
1096
+ ni.cancel()
1097
+ }
1098
+
1099
+ // Give goroutines time to react to context cancellation
1100
+ time.Sleep(200 * time.Millisecond)
1101
+
1102
+ // --- Close DHT Client ---
1103
+ if ni.dht != nil {
1104
+ logger.Debugf("[GO] - Instance %d: Closing DHT...\n", ni.instanceIndex)
1105
+ if err := ni.dht.Close(); err != nil {
1106
+ logger.Warnf("[GO] âš ī¸ Instance %d: Error closing DHT: %v\n", ni.instanceIndex, err)
1107
+ }
1108
+ ni.dht = nil
1109
+ }
1110
+
1111
+ // --- Close Persistent Outgoing Streams ---
1112
+ ni.streamsMutex.Lock()
1113
+ if len(ni.persistentChatStreams) > 0 {
1114
+ logger.Debugf("[GO] - Instance %d: Closing %d persistent outgoing streams...\n", ni.instanceIndex, len(ni.persistentChatStreams))
1115
+ for pid, stream := range ni.persistentChatStreams {
1116
+ logger.Debugf("[GO] - Instance %d: Closing stream to %s\n", ni.instanceIndex, pid)
1117
+ _ = stream.Close() // Attempt graceful close
1118
+ }
1119
+ }
1120
+ ni.persistentChatStreams = make(map[peer.ID]network.Stream) // Clear the map
1121
+ ni.streamsMutex.Unlock()
1122
+
1123
+ // --- Clean Up PubSub State ---
1124
+ ni.pubsubMutex.Lock()
1125
+ if len(ni.subscriptions) > 0 {
1126
+ logger.Debugf("[GO] - Instance %d: Ensuring PubSub subscriptions (%d) are cancelled...\n", ni.instanceIndex, len(ni.subscriptions))
1127
+ for channel, sub := range ni.subscriptions {
1128
+ logger.Debugf("[GO] - Instance %d: Cancelling subscription to topic: %s\n", ni.instanceIndex, channel)
1129
+ sub.Cancel()
1130
+ }
1131
+ }
1132
+ ni.subscriptions = make(map[string]*pubsub.Subscription) // Clear the map
1133
+ ni.topics = make(map[string]*pubsub.Topic) // Clear the map
1134
+ ni.pubsubMutex.Unlock()
1135
+
1136
+ // --- Close Host Instance ---
1137
+ var hostErr error
1138
+ if ni.host != nil {
1139
+ logger.Debugf("[GO] - Instance %d: Closing host instance...\n", ni.instanceIndex)
1140
+ hostErr = ni.host.Close()
1141
+ if hostErr != nil {
1142
+ logger.Warnf("[GO] âš ī¸ %s (proceeding with cleanup)\n", hostErr)
1143
+ } else {
1144
+ logger.Debugf("[GO] - Instance %d: Host closed successfully.\n", ni.instanceIndex)
1145
+ }
1146
+ }
1147
+
1148
+ // --- Clear Remaining State for this instance ---
1149
+ ni.peersMutex.Lock()
1150
+ ni.connectedPeers = make(map[peer.ID]ExtendedPeerInfo) // Clear the map
1151
+ ni.peersMutex.Unlock()
1152
+
1153
+ // Clear also the addresses
1154
+ ni.addrMutex.Lock()
1155
+ ni.localAddrs = nil
1156
+ ni.relayAddrs = nil
1157
+ ni.addrMutex.Unlock()
1158
+
1159
+ // Clear the MessageStore for this instance
1160
+ if ni.messageStore != nil {
1161
+ ni.messageStore.mu.Lock()
1162
+ ni.messageStore.messagesByChannel = make(map[string]*list.List) // Clear the message store
1163
+ ni.messageStore.mu.Unlock()
1164
+ }
1165
+ logger.Debugf("[GO] - Instance %d: Cleared connected peers map and message buffer.\n", ni.instanceIndex)
1166
+
1167
+ // Clear the rendezvous state for this instance
1168
+ ni.rendezvousMutex.Lock()
1169
+ ni.rendezvousState = nil // Clear the state
1170
+ ni.rendezvousMutex.Unlock()
1171
+
1172
+ // Nil out components to signify the instance is fully closed
1173
+ ni.host = nil
1174
+ ni.pubsub = nil
1175
+ ni.ctx = nil
1176
+ ni.cancel = nil
1177
+ ni.certManager = nil
1178
+ ni.messageStore = nil
1179
+
1180
+ if hostErr != nil {
1181
+ return hostErr
1182
+ }
1183
+
1184
+ logger.Infof("[GO] ✅ Instance %d: Node closed successfully.\n", ni.instanceIndex)
1185
+ return nil
1186
+ }
1187
+
1188
+ // --- Exported C Functions ---
1189
+ // These functions are callable from C (and thus Python). They act as the API boundary.
1190
+
1191
+ // This function MUST be called once from Python before any other library function.
1192
+ //
1193
+ //export InitializeLibrary
1194
+ func InitializeLibrary(
1195
+ maxInstancesC C.int,
1196
+ maxUniqueChannelsC C.int,
1197
+ maxChannelQueueLenC C.int,
1198
+ maxMessageSizeC C.int,
1199
+ logConfigJSONC *C.char,
1200
+ ) {
1201
+ // --- Configure Logging FIRST ---
1202
+ log.SetFlags(log.LstdFlags | log.Lmicroseconds)
1203
+ configStr := C.GoString(logConfigJSONC)
1204
+ golog.SetAllLoggers(golog.LevelFatal)
1205
+ if configStr != "" {
1206
+ var logLevels map[string]string
1207
+ if err := json.Unmarshal([]byte(configStr), &logLevels); err != nil {
1208
+ log.Printf("[GO] âš ī¸ Invalid log config JSON: %v. Using defaults.\n", err)
1209
+ } else {
1210
+ for logger, levelStr := range logLevels {
1211
+ if err := golog.SetLogLevel(logger, levelStr); err != nil {
1212
+ log.Printf("[GO] âš ī¸ Failed to set log level for '%s': %v\n", logger, err)
1213
+ }
1214
+ }
1215
+ }
1216
+ }
1217
+
1218
+ maxInstances = int(maxInstancesC)
1219
+ maxUniqueChannels = int(maxUniqueChannelsC)
1220
+ maxChannelQueueLen = int(maxChannelQueueLenC)
1221
+ MaxMessageSize = uint32(maxMessageSizeC)
1222
+
1223
+ // Initialize the *single* global slice.
1224
+ allInstances = make([]*NodeInstance, maxInstances)
1225
+ logger.Infof("[GO] ✅ Go library initialized with MaxInstances=%d, MaxUniqueChannels=%d and MaxChannelQueueLen=%d\n", maxInstances, maxUniqueChannels, maxChannelQueueLen)
1226
+ }
1227
+
1228
+ // CreateNode initializes and starts a new libp2p host (node) for a specific instance.
1229
+ // It configures the node based on the provided parameters (port, relay capabilities, UPnP).
1230
+ // Parameters:
1231
+ // - instanceIndexC (C.int): The index for this node instance (0 to maxInstances-1).
1232
+ // - predefinedPortC (C.int): The TCP port to listen on (0 for random).
1233
+ // - enableRelayClientC (C.int): 1 if this node should enable relay communications (client mode)
1234
+ // - enableRelayServiceC (C.int): 1 to set this node as a relay service (server mode),
1235
+ // - knowsIsPublicC (C.int): 1 to assume public reachability, 0 otherwise (-> tries to assess it in any possible way).
1236
+ // - maxConnectionsC (C.int): The maximum number of connections this node can maintain.
1237
+ //
1238
+ // Returns:
1239
+ // - *C.char: A JSON string indicating success (with node addresses) or failure (with an error message).
1240
+ // The structure is `{"state":"Success", "message": ["/ip4/.../p2p/...", ...]}` or `{"state":"Error", "message":"..."}`.
1241
+ // - IMPORTANT: The caller (C/Python) MUST free the returned C string using the `FreeString` function
1242
+ // exported by this library to avoid memory leaks. Returns NULL only on catastrophic failure before JSON creation.
1243
+ //
1244
+ //export CreateNode
1245
+ func CreateNode(
1246
+ instanceIndexC C.int,
1247
+ configJSONC *C.char,
1248
+ ) (ret *C.char) {
1249
+
1250
+ instanceIndex := int(instanceIndexC)
1251
+
1252
+ if instanceIndex < 0 || instanceIndex >= maxInstances {
1253
+ errMsg := fmt.Errorf("invalid instance index: %d. Must be between 0 and %d", instanceIndex, maxInstances-1)
1254
+ return jsonErrorResponse("Invalid instance index", errMsg)
1255
+ }
1256
+
1257
+ // --- Instance Creation and State Check ---
1258
+ globalInstanceMutex.Lock()
1259
+ if allInstances[instanceIndex] != nil {
1260
+ globalInstanceMutex.Unlock()
1261
+ msg := fmt.Sprintf("Instance %d is already initialized. Please call CloseNode first.", instanceIndex)
1262
+ return jsonErrorResponse(msg, nil)
1263
+ }
1264
+
1265
+ // --- Create the new instance object ---
1266
+ ni := &NodeInstance{
1267
+ instanceIndex: instanceIndex,
1268
+ topics: make(map[string]*pubsub.Topic),
1269
+ subscriptions: make(map[string]*pubsub.Subscription),
1270
+ connectedPeers: make(map[peer.ID]ExtendedPeerInfo),
1271
+ persistentChatStreams: make(map[peer.ID]network.Stream),
1272
+ messageStore: newMessageStore(),
1273
+ }
1274
+ ni.ctx, ni.cancel = context.WithCancel(context.Background())
1275
+ isPublic := false
1276
+
1277
+ // Store it in the global slice
1278
+ allInstances[instanceIndex] = ni
1279
+ globalInstanceMutex.Unlock()
1280
+
1281
+ logger.Infof("[GO] 🚀 Instance %d: Starting CreateNode...", instanceIndex)
1282
+ // --- Centralized Cleanup on Failure ---
1283
+ var success bool = false
1284
+ defer func() {
1285
+ if !success {
1286
+ // If `success` is still false when CreateNode exits, an error
1287
+ // must have occurred. We call Close() and remove the instance.
1288
+ logger.Warnf("[GO] âš ī¸ Instance %d: CreateNode failed, cleaning up...", instanceIndex)
1289
+ ni.Close() // Call the new method!
1290
+ globalInstanceMutex.Lock()
1291
+ allInstances[instanceIndex] = nil // Remove it from the global list
1292
+ globalInstanceMutex.Unlock()
1293
+ }
1294
+ }()
1295
+
1296
+ // 1. Parse Configuration
1297
+ configJSON := C.GoString(configJSONC)
1298
+ var cfg NodeConfig
1299
+ if err := json.Unmarshal([]byte(configJSON), &cfg); err != nil {
1300
+ return jsonErrorResponse("Invalid Configuration JSON", err)
1301
+ }
1302
+
1303
+ // --- Sanity checks on the config ---
1304
+ // If one of the three parameters for custom certificates is specified, all three are required.
1305
+ if cfg.TLS.Domain != "" || cfg.TLS.CertPath != "" || cfg.TLS.KeyPath != "" {
1306
+ if cfg.TLS.Domain == "" || cfg.TLS.CertPath == "" || cfg.TLS.KeyPath == "" {
1307
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Missing at least one of 'Domain', 'CertPath' or 'KeyPath'.", instanceIndex), nil)
1308
+ }
1309
+ } // in the following, cfg.TLS.Domain != "" will be used as flag for useCustomTLS
1310
+
1311
+ // Having both customTLS and autoTLS is not allowed
1312
+ if cfg.TLS.Domain != "" && cfg.TLS.AutoTLS {
1313
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Cannot specify both a 'Domain' and 'AutoTLS'.", instanceIndex), nil)
1314
+ }
1315
+
1316
+ // If we use AutoTLS we need the DHT on
1317
+ if cfg.TLS.AutoTLS && !cfg.DHT.Enabled {
1318
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Using TLS requires DHT 'Enabled'.", instanceIndex), nil)
1319
+ }
1320
+
1321
+ // Check the DHT mode
1322
+ if cfg.DHT.Enabled {
1323
+ if cfg.DHT.Mode != "server" && cfg.DHT.Mode != "client" {
1324
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: DHT 'Mode' must be one of 'client' or 'server'.", instanceIndex), nil)
1325
+ }
1326
+ }
1327
+
1328
+ // If we want RelayService we must be public (either forced or via AutoNat)
1329
+ if cfg.Relay.EnableService {
1330
+ if !(cfg.DHT.Enabled || cfg.Network.ForcePublic) {
1331
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: A relay needs to be publicly reachable (forced or discovered).", instanceIndex), nil)
1332
+ }
1333
+ }
1334
+
1335
+ // --- Load or Create Persistent Identity ---
1336
+ keyPath := filepath.Join(cfg.IdentityDir, "identity.key")
1337
+ privKey, err := loadOrCreateIdentity(keyPath)
1338
+ if err != nil {
1339
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Failed to prepare identity", instanceIndex), err)
1340
+ }
1341
+
1342
+ // --- AutoTLS Cert Manager Setup (if enabled) ---
1343
+ var certManager *p2pforge.P2PForgeCertMgr
1344
+ if cfg.TLS.AutoTLS {
1345
+ logger.Debugf("[GO] - Instance %d: AutoTLS is ENABLED. Setting up certificate manager...\n", instanceIndex)
1346
+ certManager, err = p2pforge.NewP2PForgeCertMgr(
1347
+ p2pforge.WithCAEndpoint(p2pforge.DefaultCAEndpoint),
1348
+ p2pforge.WithCertificateStorage(&certmagic.FileStorage{Path: filepath.Join(cfg.IdentityDir, "p2p-forge-certs")}),
1349
+ p2pforge.WithUserAgent(UnaiverseUserAgent),
1350
+ p2pforge.WithRegistrationDelay(10*time.Second),
1351
+ )
1352
+ if err != nil {
1353
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Failed to create AutoTLS cert manager", instanceIndex), err)
1354
+ }
1355
+ certManager.Start()
1356
+ ni.certManager = certManager
1357
+ }
1358
+
1359
+ // --- 4. Libp2p Options Assembly ---
1360
+ tlsMode := "none"
1361
+ if cfg.TLS.AutoTLS {
1362
+ tlsMode = "autotls"
1363
+ } else if cfg.TLS.Domain != "" {
1364
+ tlsMode = "domain"
1365
+ }
1366
+ listenAddrs, err := getListenAddrs(cfg.ListenIPs, cfg.PredefinedPort, tlsMode)
1367
+ if err != nil {
1368
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Failed to create multiaddrs", instanceIndex), err)
1369
+ }
1370
+
1371
+ // Setup Resource Manager
1372
+ limiter, err := createResourceManager(cfg.PredefinedPort)
1373
+ if err != nil {
1374
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Failed to create resource manager", instanceIndex), err)
1375
+ }
1376
+
1377
+ options := []libp2p.Option{
1378
+ libp2p.Identity(privKey),
1379
+ libp2p.ListenAddrs(listenAddrs...),
1380
+ libp2p.DefaultSecurity,
1381
+ libp2p.DefaultMuxers,
1382
+ libp2p.Transport(tcp.NewTCPTransport),
1383
+ libp2p.ShareTCPListener(),
1384
+ libp2p.Transport(quic.NewTransport),
1385
+ libp2p.Transport(webtransport.New),
1386
+ libp2p.Transport(webrtc.New),
1387
+ libp2p.ResourceManager(limiter),
1388
+ libp2p.UserAgent(UnaiverseUserAgent),
1389
+ libp2p.NATPortMap(),
1390
+ libp2p.EnableHolePunching(),
1391
+ }
1392
+
1393
+ // Add WebSocket transport, with or without TLS based on cert availability
1394
+ if cfg.TLS.Domain != "" {
1395
+ // We already have certificates, use them
1396
+ logger.Debugf("[GO] - Instance %d: Certificates provided, setting up secure WebSocket (WSS).\n", instanceIndex)
1397
+ cert, err := tls.LoadX509KeyPair(cfg.TLS.CertPath, cfg.TLS.KeyPath)
1398
+ if err != nil {
1399
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Failed to load Custom TLS certificate and key", instanceIndex), err)
1400
+ }
1401
+ tlsConfig := &tls.Config{Certificates: []tls.Certificate{cert}}
1402
+ // let's also create a custom address factory to ensure we always advertise the correct domain name
1403
+ domainAddressFactory := func(addrs []ma.Multiaddr) []ma.Multiaddr {
1404
+ // Replace the IP part of the WSS address with our domain.
1405
+ result := make([]ma.Multiaddr, 0, len(addrs))
1406
+ for _, addr := range addrs {
1407
+ if strings.Contains(addr.String(), "/tls/ws") || strings.Contains(addr.String(), "/wss") {
1408
+ // This is our WSS listener. Create the public /dns4 version.
1409
+ portStr, err := addr.ValueForProtocol(ma.P_TCP)
1410
+ if err != nil {
1411
+ // Should not happen for a TCP/WS address, but safe fallback
1412
+ result = append(result, addr)
1413
+ continue
1414
+ }
1415
+ dnsAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/dns4/%s/tcp/%s/tls/ws", cfg.TLS.Domain, portStr))
1416
+ result = append(result, dnsAddr)
1417
+ } else {
1418
+ // Keep other addresses (like QUIC) as they are.
1419
+ result = append(result, addr)
1420
+ }
1421
+ }
1422
+ return result
1423
+ }
1424
+ options = append(options,
1425
+ libp2p.Transport(ws.New, ws.WithTLSConfig(tlsConfig)),
1426
+ libp2p.AddrsFactory(domainAddressFactory),
1427
+ )
1428
+ logger.Debugf("[GO] - Instance %d: Loaded custom TLS certificate and key for WSS.\n", instanceIndex)
1429
+ } else if cfg.TLS.AutoTLS {
1430
+ // No certificates, create them automatically
1431
+ options = append(options,
1432
+ libp2p.Transport(ws.New, ws.WithTLSConfig(certManager.TLSConfig())),
1433
+ libp2p.AddrsFactory(certManager.AddressFactory()),
1434
+ )
1435
+ } else {
1436
+ // No certificates, use plain WS
1437
+ logger.Debugf("[GO] - Instance %d: No certificates found, setting up non-secure WebSocket.\n", instanceIndex)
1438
+ options = append(options, libp2p.Transport(ws.New))
1439
+ }
1440
+
1441
+ // EnableRelay (the ability to *use* relays) is default, we can explicitly disable it if needed.
1442
+ if !cfg.Relay.EnableClient {
1443
+ options = append(options, libp2p.DisableRelay()) // Explicitly disable using relays.
1444
+ logger.Debugf("[GO] - Instance %d: Relay client is DISABLED.\n", instanceIndex)
1445
+ }
1446
+
1447
+ // Configure Relay Service (ability to *be* a relay)
1448
+ if cfg.Relay.EnableService {
1449
+ // limit := rc.DefaultLimit() // open this to see the default limits
1450
+ resources := rc.DefaultResources() // open this to see the default resource limits
1451
+ // Set the duration for relayed connections. 0 means infinite.
1452
+ ttl := 2 * time.Hour // reduced to 2 hours, it will be the node's duty to refresh the reservation if needed.
1453
+ // limit.Duration = ttl
1454
+ // resources.Limit = limit
1455
+ resources.Limit = nil // same as setting rc.WithInfiniteLimits()
1456
+ resources.ReservationTTL = ttl
1457
+
1458
+ // This single option enables the node to act as a relay for others, including hopping,
1459
+ // with our custom resource limits.
1460
+ options = append(options, libp2p.EnableRelayService(rc.WithResources(resources)), libp2p.EnableNATService())
1461
+ logger.Debugf("[GO] - Instance %d: Relay service is ENABLED with custom resource configuration.\n", instanceIndex)
1462
+ }
1463
+
1464
+ // Prepare discovering the bootstrap peers
1465
+ if cfg.DHT.Enabled {
1466
+ // Add any possible option to be publicly reachable
1467
+ discoveryOpts := []libp2p.Option{
1468
+ libp2p.EnableAutoNATv2(),
1469
+ libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
1470
+ bootstrapAddrInfos := dht.GetDefaultBootstrapPeerAddrInfos()
1471
+ // Define the DHT options
1472
+ var dhtMode dht.ModeOpt
1473
+ if cfg.DHT.Mode == "server" {
1474
+ dhtMode = dht.ModeAutoServer
1475
+ } else {
1476
+ dhtMode = dht.ModeClient
1477
+ }
1478
+ dhtOptions := []dht.Option{
1479
+ dht.Mode(dhtMode),
1480
+ dht.BootstrapPeers(bootstrapAddrInfos...),
1481
+ }
1482
+ var err error
1483
+ ni.dht, err = dht.New(ni.ctx, h, dhtOptions...)
1484
+ return ni.dht, err
1485
+ }),
1486
+ }
1487
+
1488
+ if cfg.Relay.EnableClient && !cfg.Relay.EnableService && cfg.DHT.Keep {
1489
+ // Enable AutoRelay. This uses the services above (DHT, AutoNAT)
1490
+ // to find relays and bind to one if we are private.
1491
+ discoveryOpts = append(discoveryOpts, libp2p.EnableAutoRelayWithPeerSource(ni.PeerSource, autorelay.WithBootDelay(time.Second*10)))
1492
+ logger.Debugf("[GO] - Instance %d: AutoRelay client ENABLED.\n", instanceIndex)
1493
+ }
1494
+ options = append(options, discoveryOpts...)
1495
+ logger.Debugf("[GO] - Instance %d: Trying to be publicly reachable.\n", instanceIndex)
1496
+ }
1497
+
1498
+ if cfg.Network.ForcePublic {
1499
+ // Force public reachability to test local relays
1500
+ options = append(options, libp2p.ForceReachabilityPublic())
1501
+ isPublic = true
1502
+ }
1503
+
1504
+ // Create the libp2p Host instance with the configured options for this instance.
1505
+ host, err := libp2p.New(options...)
1506
+ if err != nil {
1507
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Failed to create host", instanceIndex), err)
1508
+ }
1509
+ ni.host = host
1510
+ logger.Infof("[GO] ✅ Instance %d: Host created with ID: %s\n", instanceIndex, ni.host.ID())
1511
+
1512
+ // --- Link Host to Cert Manager ---
1513
+ if cfg.TLS.AutoTLS {
1514
+ certManager.ProvideHost(ni.host)
1515
+ logger.Debugf("[GO] - Instance %d: Provided host to AutoTLS cert manager.\n", instanceIndex)
1516
+ }
1517
+
1518
+ // --- Start Address Reporting & Caching ---
1519
+ cacheSub, err := ni.host.EventBus().Subscribe(new(event.EvtLocalAddressesUpdated))
1520
+ if err != nil {
1521
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Failed to create address cache subscription", instanceIndex), err)
1522
+ }
1523
+ go handleAddressUpdateEvents(ni, cacheSub)
1524
+ logger.Debugf("[GO] 🧠 Instance %d: Address cache background listener started.", instanceIndex)
1525
+
1526
+ // --- Wait for Reachability ---
1527
+ // Subscribe to reachability events
1528
+ reachSub, err := ni.host.EventBus().Subscribe(new(event.EvtLocalReachabilityChanged))
1529
+ if err != nil {
1530
+ return jsonErrorResponse("Failed to subscribe to reachability events", err)
1531
+ }
1532
+ defer reachSub.Close()
1533
+
1534
+ timeoutCtx, timeoutCancel := context.WithTimeout(ni.ctx, 30*time.Second)
1535
+ defer timeoutCancel()
1536
+ logger.Debugf("[GO] âŗ Instance %d: Waiting for reachability update.", instanceIndex)
1537
+
1538
+ WAIT_LOOP:
1539
+ for {
1540
+ select {
1541
+ case evt := <-reachSub.Out():
1542
+ rEvt := evt.(event.EvtLocalReachabilityChanged)
1543
+ if rEvt.Reachability == network.ReachabilityPublic {
1544
+ logger.Debugf("[GO] đŸ“ļ Instance %d: Reachability -> PUBLIC", instanceIndex)
1545
+ isPublic = true
1546
+ } else {
1547
+ // If config forced us public, we ignore AutoNAT saying private.
1548
+ // Otherwise, we accept reality.
1549
+ if !cfg.Network.ForcePublic {
1550
+ isPublic = false
1551
+ }
1552
+ }
1553
+ break WAIT_LOOP
1554
+
1555
+ case <-timeoutCtx.Done():
1556
+ logger.Warnf("[GO] âš ī¸ Instance %d: Timeout. Proceeding with best effort. (Public: %t)", instanceIndex, isPublic)
1557
+ break WAIT_LOOP
1558
+
1559
+ // 4. Node Shutdown
1560
+ case <-ni.ctx.Done():
1561
+ return jsonErrorResponse("Context cancelled during init", nil)
1562
+ }
1563
+ }
1564
+
1565
+ // --- PubSub Initialization ---
1566
+ if err := setupPubSub(ni); err != nil {
1567
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Failed to create PubSub", instanceIndex), err)
1568
+ }
1569
+ logger.Debugf("[GO] ✅ Instance %d: PubSub (GossipSub) initialized.\n", instanceIndex)
1570
+
1571
+ // --- Setup Notifiers and Handlers ---
1572
+ setupNotifiers(ni)
1573
+ logger.Debugf("[GO] 🔔 Instance %d: Registered network event notifier.\n", instanceIndex)
1574
+
1575
+ setupDirectMessageHandler(ni)
1576
+ logger.Debugf("[GO] ✅ Instance %d: Direct message handler set up.\n", instanceIndex)
1577
+
1578
+ // --- Close DHT if needed ---
1579
+ if !cfg.DHT.Keep {
1580
+ if ni.dht != nil {
1581
+ logger.Debugf("[GO] - Instance %d: Closing DHT client...\n", instanceIndex)
1582
+ if err := ni.dht.Close(); err != nil {
1583
+ logger.Warnf("[GO] âš ī¸ Instance %d: Error closing DHT: %v\n", instanceIndex, err)
1584
+ }
1585
+ ni.dht = nil
1586
+ }
1587
+ }
1588
+
1589
+ // --- Get Final Addresses ---
1590
+ nodeAddresses, err := goGetNodeAddresses(ni, "")
1591
+ if err != nil {
1592
+ return jsonErrorResponse(
1593
+ fmt.Sprintf("Instance %d: Failed to obtain node addresses after waiting for reachability", instanceIndex),
1594
+ err,
1595
+ )
1596
+ }
1597
+
1598
+ // --- Build and return the new structured response ---
1599
+ response := CreateNodeResponse{
1600
+ Addresses: nodeAddresses,
1601
+ IsPublic: isPublic,
1602
+ }
1603
+
1604
+ logger.Infof("[GO] 🌐 Instance %d: Node addresses: %v\n", instanceIndex, nodeAddresses)
1605
+ reachabilityStatus := map[bool]string{true: "Public", false: "Private"}[isPublic]
1606
+ logger.Infof("[GO] 🎉 Instance %d: Node creation complete. Reachability status: %s\n", instanceIndex, reachabilityStatus)
1607
+ success = true // Mark success to avoid cleanup in defer.
1608
+ return jsonSuccessResponse(response)
1609
+ }
1610
+
1611
+ // ConnectTo attempts to establish a connection with a remote peer given its multiaddress for a specific instance.
1612
+ // Parameters:
1613
+ // - instanceIndexC (C.int): The index of the node instance.
1614
+ // - addrsJSONC (*C.char): Pointer to a JSON string containing the list of addresses that can be dialed.
1615
+ //
1616
+ // Returns:
1617
+ // - *C.char: A JSON string indicating success (with peer AddrInfo of the winning connection) or failure (with an error message).
1618
+ // Structure: `{"state":"Success", "message": {"ID": "...", "Addrs": ["...", ...]}}` or `{"state":"Error", "message":"..."}`.
1619
+ // - IMPORTANT: The caller MUST free the returned C string using `FreeString`.
1620
+ //
1621
+ //export ConnectTo
1622
+ func ConnectTo(
1623
+ instanceIndexC C.int,
1624
+ addrsJSONC *C.char,
1625
+ ) *C.char {
1626
+
1627
+ ni, err := getInstance(int(instanceIndexC))
1628
+ if err != nil {
1629
+ return jsonErrorResponse("Invalid instance", err)
1630
+ }
1631
+
1632
+ goAddrsJSON := C.GoString(addrsJSONC)
1633
+ logger.Debugf("[GO] 📞 Instance %d: Attempting to connect to peer with addresses: %s\n", ni.instanceIndex, goAddrsJSON)
1634
+
1635
+ // --- Unmarshal Address List from JSON ---
1636
+ var addrStrings []string
1637
+ if err := json.Unmarshal([]byte(goAddrsJSON), &addrStrings); err != nil {
1638
+ return jsonErrorResponse("Failed to parse addresses JSON", err)
1639
+ }
1640
+ if len(addrStrings) == 0 {
1641
+ return jsonErrorResponse("Address list is empty", nil)
1642
+ }
1643
+
1644
+ // --- Create AddrInfo from the list ---
1645
+ addrInfo, err := peer.AddrInfoFromString(addrStrings[0])
1646
+ if err != nil {
1647
+ return jsonErrorResponse("Invalid first multiaddress in list", err)
1648
+ }
1649
+
1650
+ // Add the rest of the addresses to the AddrInfo struct
1651
+ for i := 1; i < len(addrStrings); i++ {
1652
+ maddr, err := ma.NewMultiaddr(addrStrings[i])
1653
+ if err != nil {
1654
+ logger.Warnf("[GO] âš ī¸ Instance %d: Skipping invalid multiaddress '%s' in list: %v\n", ni.instanceIndex, addrStrings[i], err)
1655
+ continue
1656
+ }
1657
+ // You might want to add a check here to ensure subsequent addresses are for the same peer ID
1658
+ addrInfo.Addrs = append(addrInfo.Addrs, maddr)
1659
+ }
1660
+
1661
+ // Check if attempting to connect to the local node itself.
1662
+ if addrInfo.ID == ni.host.ID() {
1663
+ logger.Debugf("[GO] â„šī¸ Instance %d: Attempting to connect to self (%s), skipping explicit connection.\n", ni.instanceIndex, addrInfo.ID)
1664
+ // Connecting to self is usually not necessary or meaningful in libp2p.
1665
+ // Return success, indicating the "connection" is implicitly present.
1666
+ return jsonSuccessResponse(addrInfo) // Caller frees.
1667
+ }
1668
+
1669
+ // --- 1. ESTABLISH CONNECTION ---
1670
+ // Use a context with a timeout for the connection attempt to prevent blocking indefinitely.
1671
+ connCtx, cancel := context.WithTimeout(ni.ctx, 30*time.Second) // 30-second timeout.
1672
+ defer cancel() // Ensure context is cancelled eventually.
1673
+
1674
+ // Add the peer's address(es) to the local peerstore for this instance. This helps libp2p find the peer.
1675
+ // ConnectedAddrTTL suggests the address is likely valid for a short time after connection.
1676
+ // Use PermanentAddrTTL if the address is known to be stable.
1677
+ ni.host.Peerstore().AddAddrs(addrInfo.ID, addrInfo.Addrs, peerstore.ConnectedAddrTTL)
1678
+
1679
+ // Initiate the connection attempt. libp2p will handle dialing and negotiation.
1680
+ logger.Debugf("[GO] - Instance %d: Attempting host.Connect to %s...\n", ni.instanceIndex, addrInfo.ID)
1681
+ if err := ni.host.Connect(connCtx, *addrInfo); err != nil {
1682
+ // Check if the error was due to the connection timeout.
1683
+ if connCtx.Err() == context.DeadlineExceeded {
1684
+ errMsg := fmt.Sprintf("Instance %d: Connection attempt to %s timed out after 30s", ni.instanceIndex, addrInfo.ID)
1685
+ logger.Errorf("[GO] ❌ %s\n", errMsg)
1686
+ return jsonErrorResponse(errMsg, nil) // Return specific timeout error (caller frees).
1687
+ }
1688
+ // Handle other connection errors.
1689
+ errMsg := fmt.Sprintf("Instance %d: Failed to connect to peer %s", ni.instanceIndex, addrInfo.ID)
1690
+ // Example: Check for specific common errors if needed
1691
+ // if strings.Contains(err.Error(), "no route to host") { ... }
1692
+ return jsonErrorResponse(errMsg, err) // Return generic connection error (caller frees).
1693
+ }
1694
+
1695
+ // --- 2. FIND THE WINNING ADDRESS ---
1696
+ // After a successful connection, query the host's network for active connections to the peer.
1697
+ // This is where you find the 'winning' address.
1698
+ conns := ni.host.Network().ConnsToPeer(addrInfo.ID)
1699
+ var winningAddr string
1700
+ if len(conns) > 0 {
1701
+ winningAddr = fmt.Sprintf("%s/p2p/%s", conns[0].RemoteMultiaddr().String(), addrInfo.ID.String())
1702
+ logger.Debugf("[GO] ✅ Instance %d: Successfully connected to peer %s via: %s\n", ni.instanceIndex, addrInfo.ID, winningAddr)
1703
+ } else {
1704
+ logger.Warnf("[GO] âš ī¸ Instance %d: Connect succeeded for %s, but no active connection found immediately. It may be pending.\n", ni.instanceIndex, addrInfo.ID)
1705
+ }
1706
+
1707
+ // Success: log the successful connection and return the response.
1708
+ logger.Infof("[GO] ✅ Instance %d: Successfully initiated connection to multiaddress: %s\n", ni.instanceIndex, winningAddr)
1709
+ winningAddrInfo, err := peer.AddrInfoFromString(winningAddr)
1710
+ if err != nil {
1711
+ return jsonErrorResponse("Invalid winner multiaddress.", err)
1712
+ }
1713
+ return jsonSuccessResponse(winningAddrInfo) // Caller frees.
1714
+ }
1715
+
1716
+ // ReserveOnRelay attempts to reserve a slot on a specified relay node for a specific instance.
1717
+ // This allows the local node to be reachable via that relay, even if behind NAT/firewall.
1718
+ // The first connection with the relay node should be done in advance using ConnectTo.
1719
+ // Parameters:
1720
+ // - instanceIndexC (C.int): The index of the node instance.
1721
+ // - relayPeerIDC (*C.char): The peerID of the relay node.
1722
+ //
1723
+ // Returns:
1724
+ // - *C.char: A JSON string indicating success or failure.
1725
+ // On success, the `message` contains the expiration date of the reservation (ISO 8601).
1726
+ // Structure (Success): `{"state":"Success", "message": "2024-12-31T23:59:59Z"}`
1727
+ // Structure (Error): `{"state":"Error", "message":"..."}`
1728
+ // - IMPORTANT: The caller MUST free the returned C string using `FreeString`.
1729
+ //
1730
+ //export ReserveOnRelay
1731
+ func ReserveOnRelay(
1732
+ instanceIndexC C.int,
1733
+ relayPeerIDC *C.char,
1734
+ ) *C.char {
1735
+
1736
+ ni, err := getInstance(int(instanceIndexC))
1737
+ if err != nil {
1738
+ return jsonErrorResponse("Invalid instance", err)
1739
+ }
1740
+
1741
+ // Convert C string input to Go string.
1742
+ goRelayPeerID := C.GoString(relayPeerIDC)
1743
+ logger.Debugf("[GO] đŸ…ŋī¸ Instance %d: Attempting to reserve slot on relay with Peer ID: %s\n", ni.instanceIndex, goRelayPeerID)
1744
+
1745
+ // --- Decode Peer ID and build AddrInfo from Peerstore ---
1746
+ relayPID, err := peer.Decode(goRelayPeerID)
1747
+ if err != nil {
1748
+ return jsonErrorResponse("Failed to decode relay Peer ID string", err)
1749
+ }
1750
+
1751
+ // Construct the AddrInfo using the ID and the addresses we know from the peerstore.
1752
+ relayInfo := peer.AddrInfo{
1753
+ ID: relayPID,
1754
+ Addrs: ni.host.Peerstore().Addrs(relayPID),
1755
+ }
1756
+
1757
+ // Ensure the node is not trying to reserve a slot on itself.
1758
+ if relayInfo.ID == ni.host.ID() {
1759
+ return jsonErrorResponse(
1760
+ fmt.Sprintf("Instance %d: Cannot reserve slot on self", ni.instanceIndex), nil,
1761
+ ) // Caller frees.
1762
+ }
1763
+
1764
+ // --- VERIFY CONNECTION TO RELAY ---
1765
+ if len(ni.host.Network().ConnsToPeer(relayInfo.ID)) == 0 {
1766
+ errMsg := fmt.Sprintf("Instance %d: Not connected to relay %s. Must connect before reserving.", ni.instanceIndex, relayInfo.ID)
1767
+ return jsonErrorResponse(errMsg, nil)
1768
+ }
1769
+ logger.Debugf("[GO] - Instance %d: Verified connection to relay: %s\n", ni.instanceIndex, relayInfo.ID)
1770
+
1771
+ // --- Attempt Reservation ---
1772
+ // Use a separate context with potentially longer timeout for the reservation itself.
1773
+ resCtx, resCancel := context.WithTimeout(ni.ctx, 60*time.Second) // 60-second timeout for reservation.
1774
+ defer resCancel()
1775
+ // Call the circuitv2 client function to request a reservation.
1776
+ // This performs the RPC communication with the relay.
1777
+ reservation, err := client.Reserve(resCtx, ni.host, relayInfo)
1778
+ if err != nil {
1779
+ errMsg := fmt.Sprintf("Instance %d: Failed to reserve slot on relay %s", ni.instanceIndex, relayInfo.ID)
1780
+ // Handle reservation timeout specifically.
1781
+ if resCtx.Err() == context.DeadlineExceeded {
1782
+ errMsg = fmt.Sprintf("Instance %d: Reservation attempt on relay %s timed out", ni.instanceIndex, relayInfo.ID)
1783
+ return jsonErrorResponse(errMsg, nil) // Caller frees.
1784
+ }
1785
+ return jsonErrorResponse(errMsg, err) // Caller frees.
1786
+ }
1787
+
1788
+ // Although Reserve usually errors out if it fails, double-check if the reservation object is nil.
1789
+ if reservation == nil {
1790
+ errMsg := fmt.Sprintf("Instance %d: Reservation on relay %s returned nil voucher, but no error", ni.instanceIndex, relayInfo.ID)
1791
+ return jsonErrorResponse(errMsg, nil) // Caller frees.
1792
+ }
1793
+
1794
+ // --- Construct Relayed Addresses and Update Local Peerstore ---
1795
+ // We construct a relayed address for each public address of the relay to maximize reachability.
1796
+ var constructedAddrs []ma.Multiaddr
1797
+ for _, relayAddr := range reservation.Addrs {
1798
+ // We only want to use public, usable addresses for the circuit
1799
+ if manet.IsIPLoopback(relayAddr) || manet.IsIPUnspecified(relayAddr) {
1800
+ continue
1801
+ }
1802
+
1803
+ // Ensure the relay's address in the peerstore includes its own Peer ID
1804
+ baseRelayAddrStr := relayAddr.String()
1805
+ if _, idInAddr := peer.SplitAddr(relayAddr); idInAddr == "" {
1806
+ baseRelayAddrStr = fmt.Sprintf("%s/p2p/%s", relayAddr.String(), relayInfo.ID.String())
1807
+ }
1808
+
1809
+ constructedAddr, err := ma.NewMultiaddr(fmt.Sprintf("%s/p2p-circuit", baseRelayAddrStr))
1810
+ if err == nil {
1811
+ constructedAddrs = append(constructedAddrs, constructedAddr)
1812
+ }
1813
+ }
1814
+
1815
+ if len(constructedAddrs) == 0 {
1816
+ return jsonErrorResponse("Reservation succeeded but failed to construct any valid relayed multiaddr", nil)
1817
+ }
1818
+
1819
+ logger.Debugf("[GO] 🔗 Instance %d: Constructed %d Relayed Addresses:", ni.instanceIndex, len(constructedAddrs))
1820
+ for _, addr := range constructedAddrs {
1821
+ logger.Debugf("[GO] -> %s", addr.String())
1822
+ }
1823
+ // Tell the Network to start listening on these addresses.
1824
+ // This activates the circuit transport for this specific relay, updates host.Addrs(),
1825
+ // and triggers the EvtLocalAddressesUpdated event automatically.
1826
+ if err := ni.host.Network().Listen(constructedAddrs...); err != nil {
1827
+ // If listening fails, it's not fatal for the reservation (we still have the slot),
1828
+ // but it implies we might not be reachable via those specific paths.
1829
+ logger.Warnf("[GO] âš ī¸ Instance %d: Failed to start listener on some relayed addresses: %v", ni.instanceIndex, err)
1830
+ } else {
1831
+ logger.Debugf("[GO] ✅ Instance %d: Successfully started listening on relayed addresses.", ni.instanceIndex)
1832
+ }
1833
+ // Also add them directly to the istance
1834
+ ni.addrMutex.Lock()
1835
+ ni.relayAddrs = constructedAddrs
1836
+ ni.addrMutex.Unlock()
1837
+
1838
+ logger.Infof("[GO] ✅ Instance %d: Reservation successful on relay: %s.\n", ni.instanceIndex, relayInfo.ID)
1839
+
1840
+ // Return the expiration time of the reservation as confirmation.
1841
+ return jsonSuccessResponse(reservation.Expiration)
1842
+ }
1843
+
1844
+ // DisconnectFrom attempts to close any active connections to a specified peer
1845
+ // and removes the peer from the internally tracked list for a specific instance.
1846
+ // Parameters:
1847
+ // - instanceIndexC (C.int): The index of the node instance.
1848
+ // - peerIDC (*C.char): The Peer ID string of the peer to disconnect from.
1849
+ //
1850
+ // Returns:
1851
+ // - *C.char: A JSON string indicating success or failure.
1852
+ // Structure: `{"state":"Success", "message":"Disconnected from peer ..."}` or `{"state":"Error", "message":"..."}`.
1853
+ // - IMPORTANT: The caller MUST free the returned C string using `FreeString`.
1854
+ //
1855
+ //export DisconnectFrom
1856
+ func DisconnectFrom(
1857
+ instanceIndexC C.int,
1858
+ peerIDC *C.char,
1859
+ ) *C.char {
1860
+
1861
+ ni, err := getInstance(int(instanceIndexC))
1862
+ if err != nil {
1863
+ return jsonErrorResponse("Invalid instance", err)
1864
+ }
1865
+
1866
+ goPeerID := C.GoString(peerIDC)
1867
+ logger.Debugf("[GO] 🔌 Instance %d: Attempting to disconnect from peer: %s\n", ni.instanceIndex, goPeerID)
1868
+
1869
+ pid, err := peer.Decode(goPeerID)
1870
+ if err != nil {
1871
+ return jsonErrorResponse(
1872
+ fmt.Sprintf("Instance %d: Failed to decode peer ID", ni.instanceIndex), err,
1873
+ )
1874
+ }
1875
+
1876
+ if pid == ni.host.ID() {
1877
+ logger.Debugf("[GO] â„šī¸ Instance %d: Attempting to disconnect from self (%s), skipping.\n", ni.instanceIndex, pid)
1878
+ return jsonSuccessResponse("Cannot disconnect from self")
1879
+ }
1880
+
1881
+ // --- Close Persistent Outgoing Stream (if exists) for this instance ---
1882
+ ni.streamsMutex.Lock()
1883
+ stream, exists := ni.persistentChatStreams[pid]
1884
+ if exists {
1885
+ logger.Debugf("[GO] â†ŗ Instance %d: Closing persistent outgoing stream to %s\n", ni.instanceIndex, pid)
1886
+ _ = stream.Close() // Attempt graceful close
1887
+ delete(ni.persistentChatStreams, pid)
1888
+ }
1889
+ ni.streamsMutex.Unlock() // Unlock before potentially blocking network call
1890
+
1891
+ // --- Close Network Connections ---
1892
+ conns := ni.host.Network().ConnsToPeer(pid)
1893
+ closedNetworkConn := false
1894
+ if len(conns) > 0 {
1895
+ logger.Debugf("[GO] - Instance %d: Closing %d active network connection(s) to peer %s...\n", ni.instanceIndex, len(conns), pid)
1896
+ err = ni.host.Network().ClosePeer(pid) // This closes the underlying connection(s)
1897
+ if err != nil {
1898
+ logger.Warnf("[GO] âš ī¸ Instance %d: Error closing network connection(s) to peer %s: %v (proceeding with cleanup)\n", ni.instanceIndex, pid, err)
1899
+ } else {
1900
+ logger.Debugf("[GO] - Instance %d: Closed network connection(s) to peer: %s\n", ni.instanceIndex, pid)
1901
+ closedNetworkConn = true
1902
+ }
1903
+ } else {
1904
+ logger.Debugf("[GO] â„šī¸ Instance %d: No active network connections found to peer %s.\n", ni.instanceIndex, pid)
1905
+ }
1906
+
1907
+ // --- Remove from Tracking Map for this instance ---
1908
+ ni.peersMutex.Lock()
1909
+ delete(ni.connectedPeers, pid)
1910
+ ni.peersMutex.Unlock()
1911
+
1912
+ logMsg := fmt.Sprintf("Instance %d: Disconnected from peer %s", ni.instanceIndex, goPeerID)
1913
+ if !exists && !closedNetworkConn && len(conns) == 0 {
1914
+ logMsg = fmt.Sprintf("Instance %d: Disconnected from peer %s (not connected or tracked)", ni.instanceIndex, goPeerID)
1915
+ }
1916
+ logger.Infof("[GO] 🔌 %s\n", logMsg)
1917
+
1918
+ return jsonSuccessResponse(logMsg)
1919
+ }
1920
+
1921
+ // GetConnectedPeers returns a list of peers currently tracked as connected for a specific instance.
1922
+ // Note: This relies on the internal `connectedPeersInstances` map which is updated during
1923
+ // connect/disconnect operations and incoming streams. It may optionally perform
1924
+ // a liveness check.
1925
+ // Parameters:
1926
+ // - instanceIndexC (C.int): The index of the node instance.
1927
+ //
1928
+ // Returns:
1929
+ // - *C.char: A JSON string containing a list of connected peers' information.
1930
+ // Structure: `{"state":"Success", "message": [ExtendedPeerInfo, ...]}` or `{"state":"Error", "message":"..."}`.
1931
+ // Each `ExtendedPeerInfo` object has `addr_info` (ID, Addrs), `connected_at`, `direction`, and `misc`.
1932
+ // - IMPORTANT: The caller MUST free the returned C string using `FreeString`.
1933
+ //
1934
+ //export GetConnectedPeers
1935
+ func GetConnectedPeers(
1936
+ instanceIndexC C.int,
1937
+ ) *C.char {
1938
+
1939
+ ni, err := getInstance(int(instanceIndexC))
1940
+ if err != nil {
1941
+ // If getInstance errors, it means the host isn't ready.
1942
+ // Return success with an empty list, as it's a query, not an operation.
1943
+ logger.Warnf("[GO] âš ī¸ Instance %d: GetConnectedPeers called but instance is not ready: %v\n", ni.instanceIndex, err)
1944
+ return jsonSuccessResponse([]ExtendedPeerInfo{})
1945
+ }
1946
+
1947
+ // Use a Write Lock for the entire critical section to avoid mixing RLock and Lock.
1948
+ ni.peersMutex.RLock()
1949
+ defer ni.peersMutex.RUnlock() // Ensure lock is released.
1950
+
1951
+ // Create a slice to hold the results directly from the map.
1952
+ peersList := make([]ExtendedPeerInfo, 0, len(ni.connectedPeers))
1953
+
1954
+ for _, peerInfo := range ni.connectedPeers {
1955
+ peersList = append(peersList, peerInfo)
1956
+ }
1957
+
1958
+ logger.Debugf("[GO] â„šī¸ Instance %d: Reporting %d currently tracked and active peers.\n", ni.instanceIndex, len(peersList))
1959
+
1960
+ // Return the list of active peers as a JSON success response.
1961
+ return jsonSuccessResponse(peersList) // Caller frees.
1962
+ }
1963
+
1964
+ // GetRendezvousPeers returns a list of peers currently tracked as part of the world for a specific instance.
1965
+ // Note: This relies on the internal `rendezvousDiscoveredPeersInstances` map which is updated by pubsub
1966
+ // Parameters:
1967
+ // - instanceIndexC (C.int): The index of the node instance.
1968
+ //
1969
+ // Returns:
1970
+ // - *C.char: A JSON string containing a list of connected peers' information.
1971
+ // Structure: `{"state":"Success", "message": [ExtendedPeerInfo, ...]}` or `{"state":"Error", "message":"..."}`.
1972
+ // Each `ExtendedPeerInfo` object has `addr_info` (ID, Addrs), `connected_at`, `direction`, and `misc`.
1973
+ // - IMPORTANT: The caller MUST free the returned C string using `FreeString`.
1974
+ //
1975
+ //export GetRendezvousPeers
1976
+ func GetRendezvousPeers(
1977
+ instanceIndexC C.int,
1978
+ ) *C.char {
1979
+
1980
+ ni, err := getInstance(int(instanceIndexC))
1981
+ if err != nil {
1982
+ // If instance isn't ready, we definitely don't have rendezvous peers.
1983
+ return C.CString(`{"state":"Empty"}`)
1984
+ }
1985
+
1986
+ ni.rendezvousMutex.RLock()
1987
+ rendezvousState := ni.rendezvousState
1988
+ ni.rendezvousMutex.RUnlock()
1989
+
1990
+ // If the state pointer is nil, it means we haven't received the first update yet.
1991
+ if rendezvousState == nil {
1992
+ return C.CString(`{"state":"Empty"}`)
1993
+ }
1994
+
1995
+ // Extract the list of extendedPeerInfo to return it
1996
+ peersList := make([]ExtendedPeerInfo, 0, len(rendezvousState.Peers))
1997
+ for _, peerInfo := range rendezvousState.Peers {
1998
+ peersList = append(peersList, peerInfo)
1999
+ }
2000
+
2001
+ // This struct will be marshaled to JSON with exactly the fields you want.
2002
+ responsePayload := struct {
2003
+ Peers []ExtendedPeerInfo `json:"peers"`
2004
+ UpdateCount int64 `json:"update_count"`
2005
+ }{
2006
+ Peers: peersList,
2007
+ UpdateCount: rendezvousState.UpdateCount,
2008
+ }
2009
+
2010
+ // The state exists, so return the whole struct.
2011
+ logger.Debugf("[GO] â„šī¸ Instance %d: Reporting %d rendezvous peers (UpdateCount: %d).\n", ni.instanceIndex, len(rendezvousState.Peers), rendezvousState.UpdateCount)
2012
+ return jsonSuccessResponse(responsePayload) // Caller frees.
2013
+ }
2014
+
2015
+ // GetNodeAddresses is the C-exported wrapper for goGetNodeAddresses.
2016
+ // It handles C-Go type conversions and JSON marshaling.
2017
+ //
2018
+ //export GetNodeAddresses
2019
+ func GetNodeAddresses(
2020
+ instanceIndexC C.int,
2021
+ peerIDC *C.char,
2022
+ ) *C.char {
2023
+
2024
+ ni, err := getInstance(int(instanceIndexC))
2025
+ if err != nil {
2026
+ return jsonErrorResponse("Invalid instance", err)
2027
+ }
2028
+ peerIDStr := C.GoString(peerIDC) // Raw string from C
2029
+
2030
+ var pidForInternalCall peer.ID // This will be peer.ID("") for local
2031
+
2032
+ if peerIDStr == "" || peerIDStr == ni.host.ID().String() {
2033
+ // Convention: Empty peer.ID ("") passed to goGetNodeAddresses means "local node".
2034
+ pidForInternalCall = "" // This is peer.ID("")
2035
+ } else {
2036
+ pidForInternalCall, err = peer.Decode(peerIDStr)
2037
+ if err != nil {
2038
+ errMsg := fmt.Sprintf("Instance %d: Failed to decode peer ID '%s'", ni.instanceIndex, peerIDStr)
2039
+ return jsonErrorResponse(errMsg, err)
2040
+ }
2041
+ }
2042
+
2043
+ // Call the internal Go function with the resolved peer.ID or empty peer.ID for local
2044
+ addresses, err := goGetNodeAddresses(ni, pidForInternalCall)
2045
+ if err != nil {
2046
+ return jsonErrorResponse(err.Error(), nil)
2047
+ }
2048
+
2049
+ return jsonSuccessResponse(addresses)
2050
+ }
2051
+
2052
+ // SendMessageToPeer sends a message either directly to a specific peer or broadcasts it via PubSub for a specific instance.
2053
+ // Parameters:
2054
+ // - instanceIndexC (C.int): The index of the node instance.
2055
+ // - channelC (*C.char): Use the unique channel as defined above in the Message struct.
2056
+ // - dataC (*C.char): A pointer to the raw byte data of the message payload.
2057
+ // - lengthC (C.int): The length of the data buffer pointed to by `data`.
2058
+ //
2059
+ // Returns:
2060
+ // - *C.char: A JSON string with {"state": "Success/Error", "message": "..."}.
2061
+ // - IMPORTANT: The caller MUST free this string using FreeString.
2062
+ //
2063
+ //export SendMessageToPeer
2064
+ func SendMessageToPeer(
2065
+ instanceIndexC C.int,
2066
+ channelC *C.char,
2067
+ dataC *C.char,
2068
+ lengthC C.int,
2069
+ ) *C.char {
2070
+
2071
+ ni, err := getInstance(int(instanceIndexC))
2072
+ if err != nil {
2073
+ return jsonErrorResponse("Invalid instance", err)
2074
+ }
2075
+
2076
+ // Convert C inputs
2077
+ goChannel := C.GoString(channelC)
2078
+ goData := C.GoBytes(unsafe.Pointer(dataC), C.int(lengthC))
2079
+
2080
+ // --- Branch: Broadcast or Direct Send ---
2081
+ if strings.Contains(goChannel, "::ps:") {
2082
+ // --- Broadcast via specific PubSub Topic ---
2083
+ instancePubsub := ni.pubsub
2084
+ if instancePubsub == nil {
2085
+ // PubSub not initialized, cannot broadcast
2086
+ return jsonErrorResponse("PubSub not initialized, cannot broadcast", nil)
2087
+ }
2088
+
2089
+ ni.pubsubMutex.Lock()
2090
+ topic, exists := ni.topics[goChannel]
2091
+ if !exists {
2092
+ var err error
2093
+ logger.Debugf("[GO] - Instance %d: Joining PubSub topic '%s' for sending.\n", ni.instanceIndex, goChannel)
2094
+ topic, err = instancePubsub.Join(goChannel) // ps is instancePubsub
2095
+ if err != nil {
2096
+ ni.pubsubMutex.Unlock()
2097
+ // Failed to join PubSub topic
2098
+ return jsonErrorResponse(fmt.Sprintf("Failed to join PubSub topic '%s'", goChannel), err)
2099
+ }
2100
+ ni.topics[goChannel] = topic
2101
+ logger.Debugf("[GO] ✅ Instance %d: Joined PubSub topic: %s for publishing.\n", ni.instanceIndex, goChannel)
2102
+ }
2103
+ ni.pubsubMutex.Unlock()
2104
+
2105
+ // Directly publish the raw Protobuf payload.
2106
+ if err := topic.Publish(ni.ctx, goData); err != nil {
2107
+ // Failed to publish to topic
2108
+ return jsonErrorResponse(fmt.Sprintf("Failed to publish to topic '%s'", goChannel), err)
2109
+ }
2110
+ logger.Infof("[GO] 🌍 Instance %d: Broadcast to topic '%s' (%d bytes)\n", ni.instanceIndex, goChannel, len(goData))
2111
+ return jsonSuccessResponse(fmt.Sprintf("Message broadcast to topic %s", goChannel))
2112
+
2113
+ } else if strings.Contains(goChannel, "::dm:") {
2114
+ // --- Direct Peer-to-Peer Message Sending (Persistent Stream Logic) ---
2115
+ receiverChannelIDStr := strings.Split(goChannel, "::dm:")[1] // Extract the receiver's channel ID from the format "dm:<peerID>-<channelSpecifier>"
2116
+ peerIDStr := strings.Split(receiverChannelIDStr, "-")[0]
2117
+ pid, err := peer.Decode(peerIDStr)
2118
+ if err != nil {
2119
+ // Invalid peer ID format
2120
+ return jsonErrorResponse("Invalid peer ID format in channel string", err)
2121
+ }
2122
+
2123
+ if pid == ni.host.ID() {
2124
+ // Attempt to send direct message to self
2125
+ return jsonErrorResponse("Attempt to send direct message to self is invalid", nil)
2126
+ }
2127
+
2128
+ instancePersistentChatStreamsMutex := &ni.streamsMutex
2129
+ instancePersistentChatStreamsMutex.Lock()
2130
+ stream, exists := ni.persistentChatStreams[pid]
2131
+
2132
+ // If stream exists, try writing to it
2133
+ if exists {
2134
+ logger.Debugf("[GO] â†ŗ Instance %d: Reusing existing stream to %s\n", ni.instanceIndex, pid)
2135
+ err = writeDirectMessageFrame(stream, goChannel, goData)
2136
+ if err == nil {
2137
+ // Success writing to existing stream
2138
+ instancePersistentChatStreamsMutex.Unlock() // Unlock before returning
2139
+ logger.Infof("[GO] 📤 Instance %d: Sent direct message to %s (on existing stream)\n", ni.instanceIndex, pid)
2140
+ return jsonSuccessResponse(fmt.Sprintf("Direct message sent to %s (reused stream).", pid))
2141
+ }
2142
+ // Write failed on existing stream - assume it's broken
2143
+ logger.Warnf("[GO] âš ī¸ Instance %d: Failed to write to existing stream for %s: %v. Closing and removing stream.", ni.instanceIndex, pid, err)
2144
+ // Close the stream (Reset is more abrupt, Close attempts graceful)
2145
+ _ = stream.Close() // Ignore error during close, as we're removing it anyway
2146
+ // Remove from map
2147
+ delete(ni.persistentChatStreams, pid)
2148
+ // Unlock and return specific error
2149
+ instancePersistentChatStreamsMutex.Unlock()
2150
+ return jsonErrorResponse(fmt.Sprintf("Failed to write to existing stream for %s. Closing and removing stream.", pid), err)
2151
+ } else {
2152
+ // Stream does not exist, need to create a new one
2153
+ instancePersistentChatStreamsMutex.Unlock()
2154
+
2155
+ logger.Debugf("[GO] â†ŗ Instance %d: No existing stream to %s, creating new one...\n", ni.instanceIndex, pid)
2156
+ streamCtx, cancel := context.WithTimeout(ni.ctx, 20*time.Second)
2157
+ defer cancel()
2158
+
2159
+ newStream, err := ni.host.NewStream(
2160
+ network.WithAllowLimitedConn(streamCtx, UnaiverseChatProtocol),
2161
+ pid,
2162
+ UnaiverseChatProtocol,
2163
+ )
2164
+
2165
+ // Re-acquire lock *after* NewStream finishes or errors
2166
+ instancePersistentChatStreamsMutex.Lock()
2167
+ defer instancePersistentChatStreamsMutex.Unlock()
2168
+
2169
+ if err != nil {
2170
+ // Failed to open a *new* stream
2171
+ if streamCtx.Err() == context.DeadlineExceeded || err == context.DeadlineExceeded {
2172
+ return jsonErrorResponse(fmt.Sprintf("Failed to open new stream to %s: Timeout", pid), err)
2173
+ }
2174
+ return jsonErrorResponse(fmt.Sprintf("Failed to open new stream to %s.", pid), err)
2175
+ }
2176
+
2177
+ // --- RACE CONDITION HANDLING ---
2178
+ // Double-check if another goroutine created a stream while we were unlocked
2179
+ existingStream, existsNow := ni.persistentChatStreams[pid]
2180
+ if existsNow {
2181
+ logger.Warnf("[GO] âš ī¸ Instance %d: Race condition: Another stream to %s was created. Using existing one and closing the new one.", ni.instanceIndex, pid)
2182
+ _ = newStream.Close() // Close the redundant stream we just created.
2183
+ stream = existingStream
2184
+ } else {
2185
+ logger.Debugf("[GO] ✅ Instance %d: Opened and stored new persistent stream to %s\n", ni.instanceIndex, pid)
2186
+ ni.persistentChatStreams[pid] = newStream
2187
+ stream = newStream
2188
+ go handleStream(ni, newStream)
2189
+ }
2190
+
2191
+ // --- Write message to the determined stream ---
2192
+ err = writeDirectMessageFrame(stream, goChannel, goData)
2193
+ if err != nil {
2194
+ logger.Errorf("[GO] ❌ Instance %d: Failed to write initial message to stream for %s: %v. Closing and removing.", ni.instanceIndex, pid, err)
2195
+ _ = stream.Close()
2196
+ if currentStream, ok := ni.persistentChatStreams[pid]; ok && currentStream == stream {
2197
+ delete(ni.persistentChatStreams, pid)
2198
+ }
2199
+ return jsonErrorResponse(fmt.Sprintf("Failed to write to new stream to '%s' (needs reconnect).", pid), err)
2200
+ }
2201
+
2202
+ logger.Infof("[GO] 📤 Instance %d: Sent direct message to %s (on NEW stream)\n", ni.instanceIndex, pid)
2203
+ return jsonSuccessResponse(fmt.Sprintf("Direct message sent to %s (new stream).", pid))
2204
+ }
2205
+ } else {
2206
+ // Invalid channel format
2207
+ return jsonErrorResponse(fmt.Sprintf("Invalid channel format '%s'", goChannel), nil)
2208
+ }
2209
+ }
2210
+
2211
+ // SubscribeToTopic joins a PubSub topic and starts listening for messages for a specific instance.
2212
+ // Parameters:
2213
+ // - instanceIndexC (C.int): The index of the node instance.
2214
+ // - channelC (*C.char): The Channel associated to the topic to subscribe to.
2215
+ //
2216
+ // Returns:
2217
+ // - *C.char: A JSON string indicating success or failure.
2218
+ // Structure: `{"state":"Success", "message":"Subscribed to topic ..."}` or `{"state":"Error", "message":"..."}`.
2219
+ // - IMPORTANT: The caller MUST free the returned C string using `FreeString`.
2220
+ //
2221
+ //export SubscribeToTopic
2222
+ func SubscribeToTopic(
2223
+ instanceIndexC C.int,
2224
+ channelC *C.char,
2225
+ ) *C.char {
2226
+
2227
+ ni, err := getInstance(int(instanceIndexC))
2228
+ if err != nil {
2229
+ return jsonErrorResponse("Invalid instance", err)
2230
+ }
2231
+
2232
+ // Convert C string input to Go string.
2233
+ channel := C.GoString(channelC)
2234
+ logger.Debugf("[GO] <sub> Instance %d: Attempting to subscribe to topic: %s\n", ni.instanceIndex, channel)
2235
+
2236
+ // Get instance-specific state and mutex
2237
+ instancePubsub := ni.pubsub
2238
+ if ni.host == nil || instancePubsub == nil {
2239
+ return jsonErrorResponse(
2240
+ fmt.Sprintf("Instance %d: Host or PubSub not initialized", ni.instanceIndex), nil,
2241
+ )
2242
+ }
2243
+
2244
+ // Lock the mutex for safe access to the shared topics and subscriptions maps for this instance.
2245
+ ni.pubsubMutex.Lock()
2246
+ defer ni.pubsubMutex.Unlock() // Ensure mutex is unlocked when function returns.
2247
+
2248
+ // Check if already subscribed to this topic for this instance.
2249
+ if _, exists := ni.subscriptions[channel]; exists {
2250
+ logger.Debugf("[GO] <sub> Instance %d: Already subscribed to topic: %s\n", ni.instanceIndex, channel)
2251
+ // Return success, indicating the desired state is already met.
2252
+ return jsonSuccessResponse(
2253
+ fmt.Sprintf("Instance %d: Already subscribed to topic %s", ni.instanceIndex, channel),
2254
+ ) // Caller frees.
2255
+ }
2256
+
2257
+ // If the channel ends with ":rv", it indicates a rendezvous topic, so we remove other ones
2258
+ // from the instanceTopics and instanceSubscriptions list, and we clean the rendezvousDiscoveredPeersInstances.
2259
+ if strings.HasSuffix(channel, ":rv") {
2260
+ logger.Debugf(" - Instance %d: Joining rendezvous topic '%s'. Cleaning up previous rendezvous state.\n", ni.instanceIndex, channel)
2261
+ // Remove all existing rendezvous topics and subscriptions for this instance.
2262
+ for existingChannel := range ni.topics {
2263
+ if strings.HasSuffix(existingChannel, ":rv") {
2264
+ logger.Debugf(" - Instance %d: Removing existing rendezvous topic '%s' from instance state.\n", ni.instanceIndex, existingChannel)
2265
+
2266
+ // Close the topic handle if it exists.
2267
+ if topic, exists := ni.topics[existingChannel]; exists {
2268
+ if err := topic.Close(); err != nil {
2269
+ logger.Warnf("âš ī¸ Instance %d: Error closing topic handle for '%s': %v (proceeding with map cleanup)\n", ni.instanceIndex, existingChannel, err)
2270
+ }
2271
+ delete(ni.topics, existingChannel)
2272
+ }
2273
+
2274
+ // Remove the subscription if it exists.
2275
+ if sub, exists := ni.subscriptions[existingChannel]; exists {
2276
+ sub.Cancel() // Cancel the subscription
2277
+ delete(ni.subscriptions, existingChannel) // Remove from map
2278
+ }
2279
+
2280
+ // Also clean up rendezvous discovered peers for this instance.
2281
+ logger.Debugf(" - Instance %d: Resetting rendezvous state for new topic '%s'.\n", ni.instanceIndex, channel)
2282
+ ni.rendezvousMutex.Lock()
2283
+ ni.rendezvousState = nil
2284
+ ni.rendezvousMutex.Unlock()
2285
+ }
2286
+ }
2287
+ logger.Debugf(" - Instance %d: Cleaned up previous rendezvous state.\n", ni.instanceIndex)
2288
+ }
2289
+
2290
+ // --- Join the Topic ---
2291
+ // Get a handle for the topic. `Join` creates the topic if it doesn't exist locally
2292
+ // and returns a handle. It's safe to call Join multiple times; it's idempotent.
2293
+ // We store the handle primarily for potential future publishing from this node.
2294
+ topic, err := instancePubsub.Join(channel)
2295
+ if err != nil {
2296
+ errMsg := fmt.Sprintf("Instance %d: Failed to join topic '%s'", ni.instanceIndex, channel)
2297
+ return jsonErrorResponse(errMsg, err) // Caller frees.
2298
+ }
2299
+ // Store the topic handle in the map for this instance.
2300
+ ni.topics[channel] = topic
2301
+ logger.Debugf("[GO] - Instance %d: Obtained topic handle for: %s\n", ni.instanceIndex, channel)
2302
+
2303
+ // --- Subscribe to the Topic ---
2304
+ // Create an actual subscription to receive messages from the topic.
2305
+ sub, err := topic.Subscribe()
2306
+ if err != nil {
2307
+ // Close the newly created topic handle.
2308
+ err := topic.Close()
2309
+ if err != nil {
2310
+ // Log error but proceed with cleanup.
2311
+ logger.Warnf("[GO] âš ī¸ Instance %d: Error closing topic handle for '%s': %v (proceeding with map cleanup)\n", ni.instanceIndex, channel, err)
2312
+ }
2313
+ // Remove the topic handle from our local map for this instance.
2314
+ delete(ni.topics, channel)
2315
+ errMsg := fmt.Sprintf("Instance %d: Failed to subscribe to topic '%s' after joining", ni.instanceIndex, channel)
2316
+ return jsonErrorResponse(errMsg, err) // Caller frees.
2317
+ }
2318
+ // Store the subscription object in the map for this instance.
2319
+ ni.subscriptions[channel] = sub
2320
+ logger.Debugf("[GO] - Instance %d: Created subscription object for: %s\n", ni.instanceIndex, channel)
2321
+
2322
+ // --- Start Listener Goroutine ---
2323
+ // Launch a background goroutine that will continuously read messages
2324
+ // from this new subscription and add them to the message buffer for this instance.
2325
+ // Pass the instance index, subscription object, and topic name (for logging).
2326
+ go readFromSubscription(ni, sub)
2327
+
2328
+ logger.Debugf("[GO] ✅ Instance %d: Subscribed successfully to topic: %s and started listener.\n", ni.instanceIndex, channel)
2329
+ return jsonSuccessResponse(
2330
+ fmt.Sprintf("Instance %d: Subscribed to topic %s", ni.instanceIndex, channel),
2331
+ ) // Caller frees.
2332
+ }
2333
+
2334
+ // UnsubscribeFromTopic cancels an active PubSub subscription and cleans up related resources for a specific instance.
2335
+ // Parameters:
2336
+ // - instanceIndexC (C.int): The index of the node instance.
2337
+ // - channelC (*C.char): The Channel associated to the topic to unsubscribe from.
2338
+ //
2339
+ // Returns:
2340
+ // - *C.char: A JSON string indicating success or failure.
2341
+ // Structure: `{"state":"Success", "message":"Unsubscribed from topic ..."}` or `{"state":"Error", "message":"..."}`.
2342
+ // - IMPORTANT: The caller MUST free the returned C string using `FreeString`.
2343
+ //
2344
+ //export UnsubscribeFromTopic
2345
+ func UnsubscribeFromTopic(
2346
+ instanceIndexC C.int,
2347
+ channelC *C.char,
2348
+ ) *C.char {
2349
+
2350
+ ni, err := getInstance(int(instanceIndexC))
2351
+ if err != nil {
2352
+ // If instance is already gone, we can consider it "unsubscribed"
2353
+ logger.Warnf("[GO] âš ī¸ Instance %d: Unsubscribe called but instance is not ready: %v\n", ni.instanceIndex, err)
2354
+ return jsonSuccessResponse(fmt.Sprintf("Instance %d: Not subscribed (instance not running)", ni.instanceIndex))
2355
+ }
2356
+
2357
+ // Convert C string input to Go string.
2358
+ channel := C.GoString(channelC)
2359
+ logger.Debugf("[GO] </sub> Instance %d: Attempting to unsubscribe from topic: %s\n", ni.instanceIndex, channel)
2360
+
2361
+ // Lock the mutex for write access to shared maps for this instance.
2362
+ ni.pubsubMutex.Lock()
2363
+ defer ni.pubsubMutex.Unlock()
2364
+
2365
+ // --- Cancel the Subscription ---
2366
+ // Find the subscription object in the map for this instance.
2367
+ sub, subExists := ni.subscriptions[channel]
2368
+ if !subExists {
2369
+ logger.Warnf("[GO] </sub> Instance %d: Not currently subscribed to topic: %s (or already unsubscribed)\n", ni.instanceIndex, channel)
2370
+ // Also remove potential stale topic handle if subscription is gone.
2371
+ delete(ni.topics, channel)
2372
+ return jsonSuccessResponse(
2373
+ fmt.Sprintf("Instance %d: Not currently subscribed to topic %s", ni.instanceIndex, channel),
2374
+ ) // Caller frees.
2375
+ }
2376
+
2377
+ // Cancel the subscription. This signals the associated `readFromSubscription` goroutine
2378
+ // (waiting on `sub.Next()`) to stop by causing `sub.Next()` to return an error (usually `ErrSubscriptionCancelled`).
2379
+ // It also cleans up internal PubSub resources related to this subscription.
2380
+ sub.Cancel()
2381
+ // Remove the subscription entry from our local map for this instance.
2382
+ delete(ni.subscriptions, channel)
2383
+ logger.Debugf("[GO] - Instance %d: Cancelled subscription object for topic: %s\n", ni.instanceIndex, channel)
2384
+
2385
+ // --- Close the Topic Handle ---
2386
+ // Find the corresponding topic handle for this instance. It's good practice to close this as well,
2387
+ // although PubSub might manage its lifecycle internally based on subscriptions.
2388
+ // Explicit closing ensures resources related to the *handle* (like internal routing state) are released.
2389
+ topic, topicExists := ni.topics[channel]
2390
+ if topicExists {
2391
+ logger.Debugf("[GO] - Instance %d: Closing topic handle for: %s\n", ni.instanceIndex, channel)
2392
+ // Close the topic handle.
2393
+ err := topic.Close()
2394
+ if err != nil {
2395
+ // Log error but proceed with cleanup.
2396
+ logger.Warnf("[GO] âš ī¸ Instance %d: Error closing topic handle for '%s': %v (proceeding with map cleanup)\n", ni.instanceIndex, channel, err)
2397
+ }
2398
+ // Remove the topic handle from our local map for this instance.
2399
+ delete(ni.topics, channel)
2400
+ logger.Debugf("[GO] - Instance %d: Removed topic handle from local map for topic: %s\n", ni.instanceIndex, channel)
2401
+ } else {
2402
+ logger.Debugf("[GO] - Instance %d: No topic handle found in local map for '%s' to close (already removed or possibly never stored?).\n", ni.instanceIndex, channel)
2403
+ // Ensure removal from map even if handle wasn't found (e.g., inconsistent state).
2404
+ delete(ni.topics, channel)
2405
+ }
2406
+
2407
+ // If the channel ends with ":rv", it indicates a rendezvous topic, so we have closed the topic and the sub
2408
+ // but we also need to clean the rendezvousDiscoveredPeersInstances.
2409
+ if strings.HasSuffix(channel, ":rv") {
2410
+ logger.Debugf(" - Instance %d: Unsubscribing from rendezvous topic. Clearing state.\n", ni.instanceIndex)
2411
+ ni.rendezvousMutex.Lock()
2412
+ ni.rendezvousState = nil
2413
+ ni.rendezvousMutex.Unlock()
2414
+ }
2415
+ logger.Debugf("[GO] - Instance %d: Cleaned up previous rendezvous state.\n", ni.instanceIndex)
2416
+
2417
+ logger.Infof("[GO] ✅ Instance %d: Unsubscribed successfully from topic: %s\n", ni.instanceIndex, channel)
2418
+ return jsonSuccessResponse(
2419
+ fmt.Sprintf("Instance %d: Unsubscribed from topic %s", ni.instanceIndex, channel),
2420
+ ) // Caller frees.
2421
+ }
2422
+
2423
+ // MessageQueueLength returns the total number of messages waiting across all channel queues for a specific instance.
2424
+ // Parameters:
2425
+ // - instanceIndexC (C.int): The index of the node instance.
2426
+ //
2427
+ // Returns:
2428
+ // - C.int: The total number of messages. Returns -1 if instance index is invalid.
2429
+ //
2430
+ //export MessageQueueLength
2431
+ func MessageQueueLength(
2432
+ instanceIndexC C.int,
2433
+ ) C.int {
2434
+
2435
+ ni, err := getInstance(int(instanceIndexC))
2436
+ if err != nil {
2437
+ logger.Errorf("[GO] ❌ MessageQueueLength: %v\n", err)
2438
+ return -1 // Return -1 if instance isn't valid
2439
+ }
2440
+
2441
+ // Get the message store for this instance
2442
+ store := ni.messageStore
2443
+ if store == nil {
2444
+ logger.Errorf("[GO] ❌ Instance %d: Message store not initialized.\n", ni.instanceIndex)
2445
+ return 0 // Return 0 if store is nil (effectively empty)
2446
+ }
2447
+
2448
+ store.mu.Lock()
2449
+ defer store.mu.Unlock()
2450
+
2451
+ totalLength := 0
2452
+ // TODO: this makes sense but not for the check we are doing from python, think about it
2453
+ for _, messageList := range store.messagesByChannel {
2454
+ totalLength += messageList.Len()
2455
+ }
2456
+
2457
+ return C.int(totalLength)
2458
+ }
2459
+
2460
+ // PopMessages retrieves the oldest message from each channel's queue for a specific instance.
2461
+ // This function always pops one message per channel that has messages.
2462
+ // Parameters:
2463
+ // - instanceIndexC (C.int): The index of the node instance.
2464
+ //
2465
+ // Returns:
2466
+ // - *C.char: A JSON string representing a list of the popped messages.
2467
+ // Returns `{"state":"Empty"}` if no messages were available in any queue.
2468
+ // Returns `{"state":"Error", "message":"..."}` on failure.
2469
+ // - IMPORTANT: The caller MUST free the returned C string using `FreeString`.
2470
+ //
2471
+ //export PopMessages
2472
+ func PopMessages(
2473
+ instanceIndexC C.int,
2474
+ ) *C.char {
2475
+
2476
+ ni, err := getInstance(int(instanceIndexC))
2477
+ if err != nil {
2478
+ return jsonErrorResponse("Invalid instance", err)
2479
+ }
2480
+
2481
+ // Get the message store for this instance
2482
+ store := ni.messageStore
2483
+ if store == nil {
2484
+ logger.Errorf("[GO] ❌ Instance %d: PopMessages: Message store not initialized.\n", ni.instanceIndex)
2485
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Message store not initialized", ni.instanceIndex), nil)
2486
+ }
2487
+
2488
+ store.mu.Lock() // Lock for the entire operation
2489
+ defer store.mu.Unlock()
2490
+
2491
+ if len(store.messagesByChannel) == 0 {
2492
+ return C.CString(`{"state":"Empty"}`)
2493
+ }
2494
+
2495
+ // Create a slice to hold the popped messages. Capacity is the number of channels.
2496
+ var poppedMessages []*QueuedMessage
2497
+ for channel, messageList := range store.messagesByChannel {
2498
+ if messageList.Len() > 0 {
2499
+ element := messageList.Front()
2500
+ msg := element.Value.(*QueuedMessage)
2501
+ poppedMessages = append(poppedMessages, msg)
2502
+ messageList.Remove(element)
2503
+ }
2504
+ // if the queue is now empty, we can delete it from the map to save space
2505
+ if messageList.Len() == 0 {
2506
+ delete(store.messagesByChannel, channel)
2507
+ }
2508
+ }
2509
+
2510
+ // After iterating, check if we actually popped anything
2511
+ if len(poppedMessages) == 0 {
2512
+ return C.CString(`{"state":"Empty"}`)
2513
+ }
2514
+
2515
+ // Marshal the slice of popped messages into a JSON array.
2516
+ // We create a temporary structure for JSON marshalling to include the base64-encoded data.
2517
+ payloads := make([]map[string]interface{}, len(poppedMessages))
2518
+ for i, msg := range poppedMessages {
2519
+ payloads[i] = map[string]interface{}{
2520
+ "from": msg.From,
2521
+ "data": base64.StdEncoding.EncodeToString(msg.Data),
2522
+ }
2523
+ }
2524
+
2525
+ jsonBytes, err := json.Marshal(payloads)
2526
+ if err != nil {
2527
+ logger.Errorf("[GO] ❌ Instance %d: PopMessages: Failed to marshal messages to JSON: %v\n", ni.instanceIndex, err)
2528
+ // Messages have already been popped from the queue at this point.
2529
+ // Returning an error is the best we can do.
2530
+ return jsonErrorResponse(
2531
+ fmt.Sprintf("Instance %d: Failed to marshal popped messages", ni.instanceIndex), err,
2532
+ )
2533
+ }
2534
+
2535
+ return C.CString(string(jsonBytes))
2536
+ }
2537
+
2538
+ // CloseNode gracefully shuts down the libp2p host, cancels subscriptions, closes connections,
2539
+ // and cleans up all associated resources.
2540
+ // Parameters:
2541
+ // - instanceIndexC (C.int): The index of the node instance. If -1, closes all initialized instances.
2542
+ //
2543
+ // Returns:
2544
+ // - *C.char: A JSON string indicating the result of the closure attempt.
2545
+ // Structure: `{"state":"Success", "message":"Node closed successfully"}` or `{"state":"Error", "message":"Error closing host: ..."}`.
2546
+ // If closing all, the message will summarize the results.
2547
+ // - IMPORTANT: The caller MUST free the returned C string using `FreeString`.
2548
+ //
2549
+ //export CloseNode
2550
+ func CloseNode(
2551
+ instanceIndexC C.int,
2552
+ ) *C.char {
2553
+
2554
+ instanceIndex := int(instanceIndexC)
2555
+
2556
+ if instanceIndex == -1 {
2557
+ logger.Debugf("[GO] 🛑 Closing all initialized instances of this node...")
2558
+ successCount := 0
2559
+ errorCount := 0
2560
+ var errorMessages []string
2561
+
2562
+ // acquire the global lock
2563
+ globalInstanceMutex.Lock()
2564
+ defer globalInstanceMutex.Unlock()
2565
+
2566
+ for i, ni := range allInstances {
2567
+ if ni != nil {
2568
+ logger.Debugf("[GO] 🛑 Attempting to close instance %d...\n", i)
2569
+
2570
+ err := ni.Close() // Call the new method
2571
+ allInstances[i] = nil // Remove from slice
2572
+
2573
+ if err != nil {
2574
+ errorCount++
2575
+ errorMessages = append(errorMessages, fmt.Sprintf("Instance %d: %v", i, err))
2576
+ logger.Errorf("[GO] ❌ Instance %d: Close failed: %v\n", i, err)
2577
+ } else {
2578
+ successCount++
2579
+ logger.Debugf("[GO] ✅ Instance %d: Closed successfully.\n", i)
2580
+ }
2581
+ }
2582
+ }
2583
+
2584
+ summaryMsg := fmt.Sprintf("Closed %d nodes successfully, %d failed.", successCount, errorCount)
2585
+ if errorCount > 0 {
2586
+ logger.Errorf("[GO] ❌ Errors encountered during batch close:\n")
2587
+ for _, msg := range errorMessages {
2588
+ logger.Errorf(msg)
2589
+ }
2590
+ return jsonErrorResponse(summaryMsg, fmt.Errorf("details: %v", errorMessages))
2591
+ }
2592
+
2593
+ logger.Infof("[GO] 🛑 All initialized nodes closed.")
2594
+ return jsonSuccessResponse(summaryMsg)
2595
+
2596
+ } else {
2597
+ if instanceIndex < 0 || instanceIndex >= maxInstances {
2598
+ err := fmt.Errorf("invalid instance index: %d. Must be between 0 and %d", instanceIndex, maxInstances-1)
2599
+ return jsonErrorResponse("Invalid instance index for single close", err) // Caller frees.
2600
+ }
2601
+
2602
+ globalInstanceMutex.Lock()
2603
+ defer globalInstanceMutex.Unlock()
2604
+
2605
+ instance := allInstances[instanceIndex]
2606
+ if instance == nil {
2607
+ logger.Debugf("[GO] â„šī¸ Instance %d: Node was already closed.\n", instanceIndex)
2608
+ return jsonSuccessResponse(fmt.Sprintf("Instance %d: Node was already closed", instanceIndex))
2609
+ }
2610
+
2611
+ err := instance.Close()
2612
+ allInstances[instanceIndex] = nil
2613
+
2614
+ if err != nil {
2615
+ return jsonErrorResponse(fmt.Sprintf("Instance %d: Error closing host", instanceIndex), err)
2616
+ }
2617
+
2618
+ logger.Infof("[GO] 🛑 Instance %d: Node closed successfully.\n", instanceIndex)
2619
+ return jsonSuccessResponse(fmt.Sprintf("Instance %d: Node closed successfully", instanceIndex))
2620
+ }
2621
+ }
2622
+
2623
+ // FreeString is called from the C/Python side to release the memory allocated by Go
2624
+ // when returning a `*C.char` (via `C.CString`).
2625
+ // Parameters:
2626
+ // - s (*C.char): The pointer to the C string previously returned by an exported Go function.
2627
+ //
2628
+ //export FreeString
2629
+ func FreeString(
2630
+ s *C.char,
2631
+ ) {
2632
+
2633
+ // Check for NULL pointer before attempting to free.
2634
+ if s != nil {
2635
+ C.free(unsafe.Pointer(s)) // Use C.free via unsafe.Pointer to release the memory.
2636
+ }
2637
+ }
2638
+
2639
+ // FreeInt is provided for completeness but is generally **NOT** needed if Go functions
2640
+ // only return `C.int` (by value). It would only be necessary if a Go function manually
2641
+ // allocated memory for a C integer (`*C.int`) and returned the pointer, which is uncommon.
2642
+ // Parameters:
2643
+ // - i (*C.int): The pointer to the C integer previously allocated and returned by Go.
2644
+ //
2645
+ //export FreeInt
2646
+ func FreeInt(
2647
+ i *C.int,
2648
+ ) {
2649
+
2650
+ // Check for NULL pointer.
2651
+ if i != nil {
2652
+ logger.Warnf("[GO] âš ī¸ FreeInt called - Ensure a *C.int pointer was actually allocated and returned from Go (this is unusual).")
2653
+ C.free(unsafe.Pointer(i)) // Free the memory if it was indeed allocated.
2654
+ }
2655
+ }
2656
+
2657
+ // main is the entry point for a Go executable.
2658
+ func main() {
2659
+ // This message will typically only be seen if you run `go run lib.go`
2660
+ // or build and run as a standard executable, NOT when used as a shared library.
2661
+ logger.Debugf("[GO] libp2p Go library main function (not executed in c-shared library mode)")
2662
+ }