@olane/o-node 0.7.12-alpha.5 → 0.7.12-alpha.51

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.
Files changed (60) hide show
  1. package/dist/src/connection/interfaces/o-node-connection-manager.config.d.ts +1 -0
  2. package/dist/src/connection/interfaces/o-node-connection-manager.config.d.ts.map +1 -1
  3. package/dist/src/connection/interfaces/o-node-connection.config.d.ts +1 -0
  4. package/dist/src/connection/interfaces/o-node-connection.config.d.ts.map +1 -1
  5. package/dist/src/connection/o-node-connection.d.ts +5 -2
  6. package/dist/src/connection/o-node-connection.d.ts.map +1 -1
  7. package/dist/src/connection/o-node-connection.js +86 -26
  8. package/dist/src/connection/o-node-connection.manager.d.ts +18 -4
  9. package/dist/src/connection/o-node-connection.manager.d.ts.map +1 -1
  10. package/dist/src/connection/o-node-connection.manager.js +79 -65
  11. package/dist/src/connection/o-stream.request.d.ts +11 -0
  12. package/dist/src/connection/o-stream.request.d.ts.map +1 -0
  13. package/dist/src/connection/o-stream.request.js +7 -0
  14. package/dist/src/index.d.ts +2 -1
  15. package/dist/src/index.d.ts.map +1 -1
  16. package/dist/src/index.js +2 -1
  17. package/dist/src/interfaces/i-heartbeatable-node.d.ts +49 -0
  18. package/dist/src/interfaces/i-heartbeatable-node.d.ts.map +1 -0
  19. package/dist/src/interfaces/i-heartbeatable-node.js +1 -0
  20. package/dist/src/interfaces/i-reconnectable-node.d.ts +46 -0
  21. package/dist/src/interfaces/i-reconnectable-node.d.ts.map +1 -0
  22. package/dist/src/interfaces/i-reconnectable-node.js +1 -0
  23. package/dist/src/interfaces/o-node.config.d.ts +43 -0
  24. package/dist/src/interfaces/o-node.config.d.ts.map +1 -1
  25. package/dist/src/managers/o-connection-heartbeat.manager.d.ts +63 -0
  26. package/dist/src/managers/o-connection-heartbeat.manager.d.ts.map +1 -0
  27. package/dist/src/managers/o-connection-heartbeat.manager.js +227 -0
  28. package/dist/src/managers/o-reconnection.manager.d.ts +51 -0
  29. package/dist/src/managers/o-reconnection.manager.d.ts.map +1 -0
  30. package/dist/src/managers/o-reconnection.manager.js +266 -0
  31. package/dist/src/o-node.d.ts +30 -2
  32. package/dist/src/o-node.d.ts.map +1 -1
  33. package/dist/src/o-node.js +245 -33
  34. package/dist/src/o-node.notification-manager.d.ts +52 -0
  35. package/dist/src/o-node.notification-manager.d.ts.map +1 -0
  36. package/dist/src/o-node.notification-manager.js +188 -0
  37. package/dist/src/o-node.tool.d.ts.map +1 -1
  38. package/dist/src/o-node.tool.js +33 -22
  39. package/dist/src/router/o-node.router.d.ts +1 -0
  40. package/dist/src/router/o-node.router.d.ts.map +1 -1
  41. package/dist/src/router/o-node.router.js +62 -9
  42. package/dist/src/router/o-node.routing-policy.d.ts.map +1 -1
  43. package/dist/src/router/o-node.routing-policy.js +7 -2
  44. package/dist/src/router/resolvers/o-node.resolver.d.ts.map +1 -1
  45. package/dist/src/router/resolvers/o-node.resolver.js +5 -1
  46. package/dist/src/router/resolvers/o-node.search-resolver.d.ts.map +1 -1
  47. package/dist/src/router/resolvers/o-node.search-resolver.js +34 -9
  48. package/dist/src/utils/index.d.ts +3 -0
  49. package/dist/src/utils/index.d.ts.map +1 -0
  50. package/dist/src/utils/index.js +2 -0
  51. package/dist/src/utils/stream.utils.d.ts +6 -0
  52. package/dist/src/utils/stream.utils.d.ts.map +1 -0
  53. package/dist/src/utils/stream.utils.js +31 -0
  54. package/dist/test/helpers/test-node.tool.d.ts +15 -0
  55. package/dist/test/helpers/test-node.tool.d.ts.map +1 -0
  56. package/dist/test/helpers/test-node.tool.js +27 -0
  57. package/package.json +6 -6
  58. package/dist/src/router/resolvers/o-node.child-resolver.d.ts +0 -11
  59. package/dist/src/router/resolvers/o-node.child-resolver.d.ts.map +0 -1
  60. package/dist/src/router/resolvers/o-node.child-resolver.js +0 -58
@@ -0,0 +1,63 @@
1
+ import { oObject } from '@olane/o-core';
2
+ import { oNodeAddress } from '../router/o-node.address.js';
3
+ import { IHeartbeatableNode } from '../interfaces/i-heartbeatable-node.js';
4
+ export interface HeartbeatConfig {
5
+ enabled: boolean;
6
+ intervalMs: number;
7
+ timeoutMs: number;
8
+ failureThreshold: number;
9
+ checkChildren: boolean;
10
+ checkParent: boolean;
11
+ checkLeader: boolean;
12
+ }
13
+ export interface ConnectionHealth {
14
+ address: oNodeAddress;
15
+ peerId: string;
16
+ lastSuccessfulPing: number;
17
+ consecutiveFailures: number;
18
+ averageLatency: number;
19
+ status: 'healthy' | 'degraded' | 'dead';
20
+ }
21
+ /**
22
+ * Connection Heartbeat Manager
23
+ *
24
+ * Uses libp2p's native ping service to detect dead connections early.
25
+ * Continuously pings parent and children to ensure they're alive.
26
+ *
27
+ * How it works:
28
+ * - Every `intervalMs`, pings all tracked connections
29
+ * - If ping fails, increments failure counter
30
+ * - After `failureThreshold` failures, marks connection as dead
31
+ * - Emits events for degraded/recovered/dead connections
32
+ * - Automatically removes dead children from hierarchy
33
+ * - Emits ParentDisconnectedEvent when parent dies (triggers reconnection)
34
+ */
35
+ export declare class oConnectionHeartbeatManager extends oObject {
36
+ private node;
37
+ private config;
38
+ private heartbeatInterval?;
39
+ private healthMap;
40
+ private isRunning;
41
+ constructor(node: IHeartbeatableNode, config: HeartbeatConfig);
42
+ start(): Promise<void>;
43
+ stop(): Promise<void>;
44
+ private performHeartbeatCycle;
45
+ private doPing;
46
+ private pingTarget;
47
+ private handleConnectionDead;
48
+ private emitConnectionDegradedEvent;
49
+ private emitConnectionRecoveredEvent;
50
+ /**
51
+ * Get current health status of all connections
52
+ */
53
+ getHealthStatus(): ConnectionHealth[];
54
+ /**
55
+ * Get health status for specific address
56
+ */
57
+ getConnectionHealth(address: oNodeAddress): ConnectionHealth | undefined;
58
+ /**
59
+ * Get current configuration
60
+ */
61
+ getConfig(): HeartbeatConfig;
62
+ }
63
+ //# sourceMappingURL=o-connection-heartbeat.manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"o-connection-heartbeat.manager.d.ts","sourceRoot":"","sources":["../../../src/managers/o-connection-heartbeat.manager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EAOR,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAE3E,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB,EAAE,MAAM,CAAC;IACzB,aAAa,EAAE,OAAO,CAAC;IACvB,WAAW,EAAE,OAAO,CAAC;IACrB,WAAW,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,YAAY,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,kBAAkB,EAAE,MAAM,CAAC;IAC3B,mBAAmB,EAAE,MAAM,CAAC;IAC5B,cAAc,EAAE,MAAM,CAAC;IACvB,MAAM,EAAE,SAAS,GAAG,UAAU,GAAG,MAAM,CAAC;CACzC;AAED;;;;;;;;;;;;;GAaG;AACH,qBAAa,2BAA4B,SAAQ,OAAO;IAMpD,OAAO,CAAC,IAAI;IACZ,OAAO,CAAC,MAAM;IANhB,OAAO,CAAC,iBAAiB,CAAC,CAAiB;IAC3C,OAAO,CAAC,SAAS,CAAuC;IACxD,OAAO,CAAC,SAAS,CAAS;gBAGhB,IAAI,EAAE,kBAAkB,EACxB,MAAM,EAAE,eAAe;IAK3B,KAAK;IAqBL,IAAI;YAQI,qBAAqB;IAgDnC,OAAO,CAAC,MAAM;YAcA,UAAU;IAuFxB,OAAO,CAAC,oBAAoB;IAyD5B,OAAO,CAAC,2BAA2B;IAmBnC,OAAO,CAAC,4BAA4B;IAiBpC;;OAEG;IACH,eAAe,IAAI,gBAAgB,EAAE;IAIrC;;OAEG;IACH,mBAAmB,CAAC,OAAO,EAAE,YAAY,GAAG,gBAAgB,GAAG,SAAS;IAIxE;;OAEG;IACH,SAAS,IAAI,eAAe;CAG7B"}
@@ -0,0 +1,227 @@
1
+ import { oObject, ChildLeftEvent, ParentDisconnectedEvent, LeaderDisconnectedEvent, ConnectionDegradedEvent, ConnectionRecoveredEvent, oAddress, } from '@olane/o-core';
2
+ /**
3
+ * Connection Heartbeat Manager
4
+ *
5
+ * Uses libp2p's native ping service to detect dead connections early.
6
+ * Continuously pings parent and children to ensure they're alive.
7
+ *
8
+ * How it works:
9
+ * - Every `intervalMs`, pings all tracked connections
10
+ * - If ping fails, increments failure counter
11
+ * - After `failureThreshold` failures, marks connection as dead
12
+ * - Emits events for degraded/recovered/dead connections
13
+ * - Automatically removes dead children from hierarchy
14
+ * - Emits ParentDisconnectedEvent when parent dies (triggers reconnection)
15
+ */
16
+ export class oConnectionHeartbeatManager extends oObject {
17
+ constructor(node, config) {
18
+ super();
19
+ this.node = node;
20
+ this.config = config;
21
+ this.healthMap = new Map();
22
+ this.isRunning = false;
23
+ }
24
+ async start() {
25
+ if (!this.config.enabled) {
26
+ this.logger.debug('Connection heartbeat disabled');
27
+ return;
28
+ }
29
+ this.logger.info(`Starting connection heartbeat: interval=${this.config.intervalMs}ms, ` +
30
+ `timeout=${this.config.timeoutMs}ms, threshold=${this.config.failureThreshold}`);
31
+ // Immediate first check
32
+ await this.performHeartbeatCycle();
33
+ // Schedule recurring checks
34
+ this.heartbeatInterval = setInterval(() => this.performHeartbeatCycle(), this.config.intervalMs);
35
+ }
36
+ async stop() {
37
+ if (this.heartbeatInterval) {
38
+ clearInterval(this.heartbeatInterval);
39
+ this.heartbeatInterval = undefined;
40
+ }
41
+ this.healthMap.clear();
42
+ }
43
+ async performHeartbeatCycle() {
44
+ if (!this.isRunning) {
45
+ return;
46
+ }
47
+ this.isRunning = true;
48
+ const targets = [];
49
+ // Check if this is a leader node (no leader in hierarchy = we are leader)
50
+ const isLeaderNode = this.node.getLeaders().length === 0;
51
+ // Collect leader (if enabled and we're not the leader)
52
+ if (!isLeaderNode) {
53
+ const leaders = this.node.getLeaders();
54
+ for (const leader of leaders) {
55
+ targets.push({ address: leader, role: 'leader' });
56
+ }
57
+ }
58
+ // Collect parent
59
+ if (this.config.checkParent && !isLeaderNode) {
60
+ // Use this.node.parent getter to get the current parent address with transports
61
+ // rather than getParents() which may have a stale reference
62
+ const parent = this.node.parent;
63
+ // make sure that we don't double ping the leader
64
+ if (parent && parent?.toString() !== oAddress.leader().toString()) {
65
+ targets.push({ address: parent, role: 'parent' });
66
+ }
67
+ }
68
+ // Collect children
69
+ if (this.config.checkChildren) {
70
+ const children = this.node.getChildren();
71
+ for (const child of children) {
72
+ targets.push({ address: child, role: 'child' });
73
+ }
74
+ }
75
+ // Ping all targets in parallel
76
+ await Promise.allSettled(targets.map((target) => this.pingTarget(target.address, target.role)));
77
+ this.isRunning = false;
78
+ }
79
+ doPing(address) {
80
+ if (address.toString() === this.node.address.toString()) {
81
+ return Promise.resolve();
82
+ }
83
+ const transport = address.libp2pTransports[0].toMultiaddr();
84
+ if (transport.toString().indexOf('p2p-circuit') > -1) {
85
+ return this.node.use(address, {
86
+ method: 'ping',
87
+ params: {},
88
+ });
89
+ }
90
+ return this.node.p2pNode.services.ping.ping(transport);
91
+ }
92
+ async pingTarget(address, role) {
93
+ if (!address.libp2pTransports.length) {
94
+ this.logger.debug(`${role} has no transports, skipping ping`, address);
95
+ return;
96
+ }
97
+ const key = address.toString();
98
+ let health = this.healthMap.get(key);
99
+ if (!health) {
100
+ health = {
101
+ address,
102
+ peerId: 'unknown',
103
+ lastSuccessfulPing: 0,
104
+ consecutiveFailures: 0,
105
+ averageLatency: 0,
106
+ status: 'healthy',
107
+ };
108
+ this.healthMap.set(key, health);
109
+ }
110
+ try {
111
+ // Use libp2p's native ping service
112
+ const startTime = Date.now();
113
+ // Create timeout promise
114
+ const timeoutPromise = new Promise((_, reject) => {
115
+ setTimeout(() => reject(new Error('Ping timeout')), this.config.timeoutMs);
116
+ });
117
+ // Race between ping and timeout
118
+ // The ping service accepts PeerId as string or object
119
+ await Promise.race([this.doPing(address), timeoutPromise]);
120
+ const latency = Date.now() - startTime;
121
+ // Success - update health
122
+ health.lastSuccessfulPing = Date.now();
123
+ health.consecutiveFailures = 0;
124
+ health.averageLatency =
125
+ health.averageLatency === 0
126
+ ? latency
127
+ : health.averageLatency * 0.7 + latency * 0.3; // Exponential moving average
128
+ const previousStatus = health.status;
129
+ health.status = 'healthy';
130
+ // Emit recovery event if was degraded
131
+ if (previousStatus === 'degraded') {
132
+ this.logger.info(`Connection recovered: ${address} (latency: ${latency}ms)`);
133
+ this.emitConnectionRecoveredEvent(address, role);
134
+ }
135
+ // this.logger.debug(`Ping successful: ${address} (${latency}ms)`);
136
+ }
137
+ catch (error) {
138
+ health.consecutiveFailures++;
139
+ this.logger.warn(`Ping failed: ${address} (failures: ${health.consecutiveFailures}/${this.config.failureThreshold})`, error);
140
+ // Update status based on failure count
141
+ if (health.consecutiveFailures >= this.config.failureThreshold) {
142
+ this.handleConnectionDead(address, role, health);
143
+ }
144
+ else if (health.consecutiveFailures >=
145
+ Math.ceil(this.config.failureThreshold / 2)) {
146
+ health.status = 'degraded';
147
+ this.emitConnectionDegradedEvent(address, role, health.consecutiveFailures);
148
+ }
149
+ }
150
+ }
151
+ handleConnectionDead(address, role, health) {
152
+ health.status = 'dead';
153
+ this.logger.error(`Connection dead after ${health.consecutiveFailures} failures: ${address} (role: ${role})`);
154
+ // Remove from health tracking
155
+ this.healthMap.delete(address.toString());
156
+ // Emit events based on role
157
+ if (role === 'child') {
158
+ // Remove dead child from hierarchy
159
+ this.node.removeChild(address);
160
+ // Emit child left event
161
+ this.node.notificationManager.emit(new ChildLeftEvent({
162
+ source: this.node.address,
163
+ childAddress: address,
164
+ parentAddress: this.node.address,
165
+ reason: `heartbeat_failed_${health.consecutiveFailures}_times`,
166
+ }));
167
+ this.logger.warn(`Removed dead child: ${address}`);
168
+ }
169
+ else if (role === 'parent') {
170
+ // Emit parent disconnected event
171
+ this.node.notificationManager.emit(new ParentDisconnectedEvent({
172
+ source: this.node.address,
173
+ parentAddress: address,
174
+ reason: `heartbeat_failed_${health.consecutiveFailures}_times`,
175
+ }));
176
+ this.logger.error(`Parent connection dead: ${address}`);
177
+ // Reconnection manager will handle this event
178
+ }
179
+ else if (role === 'leader') {
180
+ // Emit leader disconnected event
181
+ this.node.notificationManager.emit(new LeaderDisconnectedEvent({
182
+ source: this.node.address,
183
+ leaderAddress: address,
184
+ reason: `heartbeat_failed_${health.consecutiveFailures}_times`,
185
+ }));
186
+ this.logger.error(`Leader connection dead: ${address}`);
187
+ // Reconnection manager will handle this event
188
+ }
189
+ }
190
+ emitConnectionDegradedEvent(address, role, failures) {
191
+ // ConnectionDegradedEvent only supports parent/child, so we map leader to parent
192
+ const eventRole = role === 'leader' ? 'parent' : role === 'child' ? 'child' : 'parent';
193
+ this.node.notificationManager.emit(new ConnectionDegradedEvent({
194
+ source: this.node.address,
195
+ targetAddress: address,
196
+ role: eventRole,
197
+ consecutiveFailures: failures,
198
+ }));
199
+ }
200
+ emitConnectionRecoveredEvent(address, role) {
201
+ // ConnectionRecoveredEvent only supports parent/child, so we map leader to parent
202
+ const eventRole = role === 'leader' ? 'parent' : role === 'child' ? 'child' : 'parent';
203
+ this.node.notificationManager.emit(new ConnectionRecoveredEvent({
204
+ source: this.node.address,
205
+ targetAddress: address,
206
+ role: eventRole,
207
+ }));
208
+ }
209
+ /**
210
+ * Get current health status of all connections
211
+ */
212
+ getHealthStatus() {
213
+ return Array.from(this.healthMap.values());
214
+ }
215
+ /**
216
+ * Get health status for specific address
217
+ */
218
+ getConnectionHealth(address) {
219
+ return this.healthMap.get(address.toString());
220
+ }
221
+ /**
222
+ * Get current configuration
223
+ */
224
+ getConfig() {
225
+ return { ...this.config };
226
+ }
227
+ }
@@ -0,0 +1,51 @@
1
+ import { oObject } from '@olane/o-core';
2
+ import { IReconnectableNode } from '../interfaces/i-reconnectable-node.js';
3
+ export interface ReconnectionConfig {
4
+ enabled: boolean;
5
+ maxAttempts: number;
6
+ baseDelayMs: number;
7
+ maxDelayMs: number;
8
+ useLeaderFallback: boolean;
9
+ parentDiscoveryIntervalMs: number;
10
+ parentDiscoveryMaxDelayMs: number;
11
+ }
12
+ /**
13
+ * Reconnection Manager
14
+ *
15
+ * Automatically attempts to reconnect to parent when connection is lost.
16
+ *
17
+ * Strategy:
18
+ * 1. Listen for ParentDisconnectedEvent (from heartbeat or libp2p)
19
+ * 2. Attempt direct reconnection with exponential backoff
20
+ * 3. If direct reconnection fails, query leader for new parent
21
+ * 4. Register with new parent and continue operation
22
+ * 5. If all attempts fail, transition node to ERROR state
23
+ */
24
+ export declare class oReconnectionManager extends oObject {
25
+ private node;
26
+ private config;
27
+ private reconnecting;
28
+ constructor(node: IReconnectableNode, config: ReconnectionConfig);
29
+ private setupEventListeners;
30
+ handleNodeConnected(event: any): Promise<void>;
31
+ private handleConnectionDegraded;
32
+ private handleLeaderDisconnected;
33
+ private handleParentDisconnected;
34
+ attemptReconnection(): Promise<void>;
35
+ private tryDirectParentReconnection;
36
+ private tryLeaderFallback;
37
+ /**
38
+ * Wait for leader to become available and reconnect
39
+ * Leader transports are static (configured), so we just need to detect when it's back
40
+ */
41
+ private waitForLeaderAndReconnect;
42
+ /**
43
+ * Wait for non-leader parent to appear in registry and reconnect
44
+ */
45
+ waitForParentAndReconnect(): Promise<void>;
46
+ private handleReconnectionFailure;
47
+ private calculateNodeLevel;
48
+ private calculateBackoffDelay;
49
+ private sleep;
50
+ }
51
+ //# sourceMappingURL=o-reconnection.manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"o-reconnection.manager.d.ts","sourceRoot":"","sources":["../../../src/managers/o-reconnection.manager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EASR,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAI3E,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,iBAAiB,EAAE,OAAO,CAAC;IAC3B,yBAAyB,EAAE,MAAM,CAAC;IAClC,yBAAyB,EAAE,MAAM,CAAC;CACnC;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,oBAAqB,SAAQ,OAAO;IAI7C,OAAO,CAAC,IAAI;IACZ,OAAO,CAAC,MAAM;IAJhB,OAAO,CAAC,YAAY,CAAS;gBAGnB,IAAI,EAAE,kBAAkB,EACxB,MAAM,EAAE,kBAAkB;IAMpC,OAAO,CAAC,mBAAmB;IAyBrB,mBAAmB,CAAC,KAAK,EAAE,GAAG;YAatB,wBAAwB;YAaxB,wBAAwB;YAexB,wBAAwB;IAehC,mBAAmB;YAgDX,2BAA2B;YAiB3B,iBAAiB;IAkB/B;;;OAGG;YACW,yBAAyB;IAiFvC;;OAEG;IACG,yBAAyB;IAkG/B,OAAO,CAAC,yBAAyB;IAajC,OAAO,CAAC,kBAAkB;IAI1B,OAAO,CAAC,qBAAqB;IAK7B,OAAO,CAAC,KAAK;CAGd"}
@@ -0,0 +1,266 @@
1
+ import { oObject, oAddress, NodeState, } from '@olane/o-core';
2
+ import { oNodeAddress } from '../router/o-node.address.js';
3
+ import { oNodeTransport } from '../router/o-node.transport.js';
4
+ /**
5
+ * Reconnection Manager
6
+ *
7
+ * Automatically attempts to reconnect to parent when connection is lost.
8
+ *
9
+ * Strategy:
10
+ * 1. Listen for ParentDisconnectedEvent (from heartbeat or libp2p)
11
+ * 2. Attempt direct reconnection with exponential backoff
12
+ * 3. If direct reconnection fails, query leader for new parent
13
+ * 4. Register with new parent and continue operation
14
+ * 5. If all attempts fail, transition node to ERROR state
15
+ */
16
+ export class oReconnectionManager extends oObject {
17
+ constructor(node, config) {
18
+ super();
19
+ this.node = node;
20
+ this.config = config;
21
+ this.reconnecting = false;
22
+ this.setupEventListeners();
23
+ }
24
+ setupEventListeners() {
25
+ // Listen for parent disconnection (from heartbeat or libp2p)
26
+ this.node.notificationManager.on('parent:disconnected', this.handleParentDisconnected.bind(this));
27
+ // Listen for leader disconnection (from heartbeat)
28
+ this.node.notificationManager.on('leader:disconnected', this.handleLeaderDisconnected.bind(this));
29
+ // Listen for connection degradation as early warning
30
+ this.node.notificationManager.on('connection:degraded', this.handleConnectionDegraded.bind(this));
31
+ this.node.notificationManager.on('node:connected', this.handleNodeConnected.bind(this));
32
+ }
33
+ async handleNodeConnected(event) {
34
+ const connectedEvent = event;
35
+ if (connectedEvent.nodeAddress.toString() === oAddress.leader().toString()) {
36
+ // the leader is back online, let's re-register & tell sub-graphs to re-register
37
+ await this.node.useSelf({
38
+ method: 'register_leader',
39
+ params: {},
40
+ });
41
+ }
42
+ }
43
+ async handleConnectionDegraded(event) {
44
+ const degradedEvent = event;
45
+ if (degradedEvent.role !== 'parent')
46
+ return;
47
+ this.logger.warn(`Parent connection degraded: ${degradedEvent.targetAddress} ` +
48
+ `(failures: ${degradedEvent.consecutiveFailures})`);
49
+ // Could implement pre-emptive parent discovery here
50
+ // For now, just log the warning and wait for full disconnection
51
+ }
52
+ async handleLeaderDisconnected(event) {
53
+ const disconnectEvent = event;
54
+ this.logger.warn(`Leader disconnected: ${disconnectEvent.leaderAddress} (reason: ${disconnectEvent.reason})`);
55
+ // Don't attempt reconnection for leader - the LeaderRequestWrapper
56
+ // will handle retries automatically when we make requests
57
+ // Just log the event for observability
58
+ this.logger.info('Leader requests will use automatic retry mechanism (LeaderRequestWrapper)');
59
+ }
60
+ async handleParentDisconnected(event) {
61
+ const disconnectEvent = event;
62
+ if (this.reconnecting) {
63
+ this.logger.debug('Already reconnecting, ignoring duplicate event');
64
+ return;
65
+ }
66
+ this.logger.warn(`Parent disconnected: ${disconnectEvent.parentAddress} (reason: ${disconnectEvent.reason})`);
67
+ await this.attemptReconnection();
68
+ }
69
+ async attemptReconnection() {
70
+ if (!this.config.enabled) {
71
+ this.logger.warn('Reconnection disabled - node will remain disconnected');
72
+ return;
73
+ }
74
+ this.reconnecting = true;
75
+ let attempt = 0;
76
+ while (attempt < this.config.maxAttempts) {
77
+ attempt++;
78
+ this.logger.info(`Reconnection attempt ${attempt}/${this.config.maxAttempts} to parent: ${this.node.config.parent}`);
79
+ try {
80
+ // Strategy 1: Try direct parent reconnection
81
+ await this.tryDirectParentReconnection();
82
+ // Success!
83
+ this.reconnecting = false;
84
+ this.logger.info(`Successfully reconnected to parent after ${attempt} attempts`);
85
+ return;
86
+ }
87
+ catch (error) {
88
+ this.logger.warn(`Reconnection attempt ${attempt} failed:`, error instanceof Error ? error.message : error);
89
+ if (attempt < this.config.maxAttempts) {
90
+ const delay = this.calculateBackoffDelay(attempt);
91
+ this.logger.debug(`Waiting ${delay}ms before next attempt...`);
92
+ await this.sleep(delay);
93
+ }
94
+ }
95
+ }
96
+ // All direct attempts failed - try leader fallback
97
+ if (this.config.useLeaderFallback) {
98
+ await this.tryLeaderFallback();
99
+ }
100
+ else {
101
+ this.handleReconnectionFailure();
102
+ }
103
+ }
104
+ async tryDirectParentReconnection() {
105
+ if (!this.node.config.parent) {
106
+ throw new Error('No parent configured');
107
+ }
108
+ // Re-register with parent (might have new transports)
109
+ await this.node.registerParent();
110
+ // Verify connection works with a ping
111
+ await this.node.use(this.node.config.parent, {
112
+ method: 'ping',
113
+ params: {},
114
+ });
115
+ this.logger.info('Direct parent reconnection successful');
116
+ }
117
+ async tryLeaderFallback() {
118
+ // Check if parent is the leader - special case
119
+ const parentIsLeader = this.node.config.parent?.toString() === oAddress.leader().toString();
120
+ if (parentIsLeader) {
121
+ this.logger.info('Parent is the leader - waiting for leader to become available');
122
+ await this.waitForLeaderAndReconnect();
123
+ }
124
+ else {
125
+ this.logger.info('Starting infinite parent discovery via leader registry');
126
+ await this.waitForParentAndReconnect();
127
+ }
128
+ }
129
+ /**
130
+ * Wait for leader to become available and reconnect
131
+ * Leader transports are static (configured), so we just need to detect when it's back
132
+ */
133
+ async waitForLeaderAndReconnect() {
134
+ const startTime = Date.now();
135
+ let attempt = 0;
136
+ let currentDelay = this.config.parentDiscoveryIntervalMs;
137
+ // Infinite retry loop - keep trying until leader is back
138
+ while (true) {
139
+ attempt++;
140
+ const elapsedMinutes = Math.floor((Date.now() - startTime) / 60000);
141
+ this.logger.info(`Leader discovery attempt ${attempt} (elapsed: ${elapsedMinutes}m)`);
142
+ try {
143
+ // Try to ping the leader using its configured transports
144
+ // The leader address should already have transports configured
145
+ await this.node.use(this.node.config.parent, {
146
+ method: 'ping',
147
+ params: {},
148
+ });
149
+ // Leader is back! Now re-register with parent and registry
150
+ this.logger.info(`Leader is back online after ${elapsedMinutes}m, re-registering...`);
151
+ try {
152
+ // Register with parent (leader)
153
+ await this.node.registerParent();
154
+ // Force re-registration with registry by resetting the flag
155
+ this.node.didRegister = false;
156
+ await this.node.register();
157
+ // Success!
158
+ this.reconnecting = false;
159
+ this.logger.info(`Successfully reconnected to leader and re-registered after ${elapsedMinutes}m`);
160
+ return;
161
+ }
162
+ catch (registrationError) {
163
+ this.logger.warn('Leader online but registration failed, will retry:', registrationError instanceof Error
164
+ ? registrationError.message
165
+ : registrationError);
166
+ // Fall through to retry with backoff
167
+ }
168
+ }
169
+ catch (error) {
170
+ // Leader not yet available
171
+ this.logger.debug(`Leader not yet available (will retry): ${error instanceof Error ? error.message : error}`);
172
+ }
173
+ // Calculate backoff delay
174
+ const delay = Math.min(currentDelay, this.config.parentDiscoveryMaxDelayMs);
175
+ // Log periodic status updates (every 5 minutes)
176
+ if (attempt % 30 === 0) {
177
+ this.logger.info(`Still waiting for leader to come back online... (${elapsedMinutes}m elapsed, ${attempt} attempts)`);
178
+ }
179
+ this.logger.debug(`Waiting ${delay}ms before next discovery attempt...`);
180
+ await this.sleep(delay);
181
+ // Exponential backoff for next iteration
182
+ currentDelay = Math.min(currentDelay * 2, this.config.parentDiscoveryMaxDelayMs);
183
+ }
184
+ }
185
+ /**
186
+ * Wait for non-leader parent to appear in registry and reconnect
187
+ */
188
+ async waitForParentAndReconnect() {
189
+ const startTime = Date.now();
190
+ let attempt = 0;
191
+ let currentDelay = this.config.parentDiscoveryIntervalMs;
192
+ // Infinite retry loop - keep trying until parent is found
193
+ while (true) {
194
+ attempt++;
195
+ const elapsedMinutes = Math.floor((Date.now() - startTime) / 60000);
196
+ this.logger.info(`Parent discovery attempt ${attempt} (elapsed: ${elapsedMinutes}m)`);
197
+ try {
198
+ // Query registry for parent by its known address
199
+ const response = await this.node.use(oAddress.registry(), {
200
+ method: 'find_available_parent',
201
+ params: {
202
+ parentAddress: this.node.config.parent?.toString(),
203
+ },
204
+ });
205
+ const { parentAddress, parentTransports } = response.result.data;
206
+ // Check if parent was found in registry
207
+ if (parentAddress && parentTransports && parentTransports.length > 0) {
208
+ this.logger.info(`Parent found in registry: ${parentAddress} with ${parentTransports.length} transports`);
209
+ // Update parent reference with fresh transports
210
+ this.node.config.parent = new oNodeAddress(parentAddress, parentTransports.map((t) => new oNodeTransport(t.value)));
211
+ // Attempt to register with parent and re-register with registry
212
+ try {
213
+ await this.tryDirectParentReconnection();
214
+ // Force re-registration with registry by resetting the flag
215
+ this.node.didRegister = false;
216
+ await this.node.register();
217
+ // Success!
218
+ this.reconnecting = false;
219
+ this.logger.info(`Successfully reconnected to parent and re-registered after ${elapsedMinutes}m of discovery attempts`);
220
+ return;
221
+ }
222
+ catch (registrationError) {
223
+ this.logger.warn('Parent found but registration failed, will retry:', registrationError instanceof Error
224
+ ? registrationError.message
225
+ : registrationError);
226
+ // Fall through to retry with backoff
227
+ }
228
+ }
229
+ else {
230
+ this.logger.debug(`Parent not yet available in registry: ${this.node.config.parent?.toString()}`);
231
+ }
232
+ }
233
+ catch (error) {
234
+ // Network error communicating with leader/registry
235
+ this.logger.warn('Error querying registry for parent (will retry):', error instanceof Error ? error.message : error);
236
+ }
237
+ // Calculate backoff delay with exponential increase, capped at max
238
+ const delay = Math.min(currentDelay, this.config.parentDiscoveryMaxDelayMs);
239
+ // Log periodic status updates (every 5 minutes)
240
+ if (attempt % 30 === 0) {
241
+ this.logger.info(`Still waiting for parent to appear in registry... (${elapsedMinutes}m elapsed, ${attempt} attempts)`);
242
+ }
243
+ this.logger.debug(`Waiting ${delay}ms before next discovery attempt...`);
244
+ await this.sleep(delay);
245
+ // Exponential backoff for next iteration
246
+ currentDelay = Math.min(currentDelay * 2, this.config.parentDiscoveryMaxDelayMs);
247
+ }
248
+ }
249
+ handleReconnectionFailure() {
250
+ this.reconnecting = false;
251
+ this.logger.error('Failed to reconnect to parent after all attempts - node in ERROR state');
252
+ // Transition to error state
253
+ this.node.state = NodeState.ERROR;
254
+ // Could emit custom event here for monitoring
255
+ }
256
+ calculateNodeLevel() {
257
+ return this.node.address.paths.length;
258
+ }
259
+ calculateBackoffDelay(attempt) {
260
+ const delay = this.config.baseDelayMs * Math.pow(2, attempt - 1);
261
+ return Math.min(delay, this.config.maxDelayMs);
262
+ }
263
+ sleep(ms) {
264
+ return new Promise((resolve) => setTimeout(resolve, ms));
265
+ }
266
+ }