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