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