Add some more logging
This commit is contained in:
@@ -242,12 +242,16 @@ export class Libp2pComponent implements ILibp2pComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async start(): Promise<void> {
|
async start(): Promise<void> {
|
||||||
|
logger.info('[TypeScript] start() called, _started:', this._started, '_starting:', this._starting);
|
||||||
|
|
||||||
if (this._started || this._starting) {
|
if (this._started || this._starting) {
|
||||||
|
logger.info('[TypeScript] Already started or starting, returning early');
|
||||||
return; // Already started or starting
|
return; // Already started or starting
|
||||||
}
|
}
|
||||||
|
|
||||||
this._starting = true;
|
this._starting = true;
|
||||||
this._error = null;
|
this._error = null;
|
||||||
|
logger.info('[TypeScript] Setting _starting to true, proceeding with start');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Register protocols if any
|
// Register protocols if any
|
||||||
@@ -281,7 +285,18 @@ export class Libp2pComponent implements ILibp2pComponent {
|
|||||||
config.privateKey = Buffer.from(privateKeyBytes).toString('base64');
|
config.privateKey = Buffer.from(privateKeyBytes).toString('base64');
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await this.nativeModule.startLibp2p(config);
|
logger.debug('[Libp2pComponent] About to call nativeModule.startLibp2p with config:', config);
|
||||||
|
logger.debug('[Libp2pComponent] nativeModule exists:', !!this.nativeModule);
|
||||||
|
logger.debug('[Libp2pComponent] nativeModule.startLibp2p exists:', !!this.nativeModule?.startLibp2p);
|
||||||
|
|
||||||
|
let result;
|
||||||
|
try {
|
||||||
|
result = await this.nativeModule.startLibp2p(config);
|
||||||
|
logger.info('[Libp2pComponent] startLibp2p returned:', result);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('[Libp2pComponent] startLibp2p failed:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
// Update internal state from result
|
// Update internal state from result
|
||||||
if (result.peerId) {
|
if (result.peerId) {
|
||||||
@@ -348,22 +363,46 @@ export class Libp2pComponent implements ILibp2pComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async dial(multiaddr: string): Promise<Connection> {
|
async dial(multiaddr: string): Promise<Connection> {
|
||||||
await this.nativeModule.connectToPeer(multiaddr);
|
console.log(`[Libp2pComponent] dial() called with multiaddr: ${multiaddr}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await this.nativeModule.connectToPeer(multiaddr);
|
||||||
|
console.log('[Libp2pComponent] connectToPeer result:', result);
|
||||||
|
|
||||||
// Create connection object
|
// Create connection object
|
||||||
const peerId = multiaddr.match(/\/p2p\/([^/]+)/)?.[1] || '';
|
// Try to extract peer ID from result or multiaddr
|
||||||
return {
|
let peerId = result?.peer_id || result?.peerId || '';
|
||||||
|
if (!peerId) {
|
||||||
|
// Try to extract from multiaddr if not in result
|
||||||
|
peerId = multiaddr.match(/\/p2p\/([^/]+)/)?.[1] || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// If still no peer ID, this might be a connection without known peer ID
|
||||||
|
// The real peer ID will be determined during handshake
|
||||||
|
if (!peerId || peerId === 'pending') {
|
||||||
|
console.log('[Libp2pComponent] Dialing without known peer ID, will be determined during handshake');
|
||||||
|
peerId = 'pending';
|
||||||
|
}
|
||||||
|
|
||||||
|
const connection = {
|
||||||
id: `${peerId}-${Date.now()}`,
|
id: `${peerId}-${Date.now()}`,
|
||||||
remotePeer: new SimplePeerId(peerId),
|
remotePeer: new SimplePeerId(peerId),
|
||||||
remoteAddr: new SimpleMultiaddr(multiaddr),
|
remoteAddr: new SimpleMultiaddr(multiaddr),
|
||||||
stat: {
|
stat: {
|
||||||
direction: 'outbound',
|
direction: 'outbound' as const,
|
||||||
status: 'open',
|
status: 'open' as const,
|
||||||
timeline: {
|
timeline: {
|
||||||
open: Date.now(),
|
open: Date.now(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log('[Libp2pComponent] Returning connection:', connection);
|
||||||
|
return connection;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[Libp2pComponent] dial error:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async hangUp(peerId: string): Promise<void> {
|
async hangUp(peerId: string): Promise<void> {
|
||||||
|
|||||||
Reference in New Issue
Block a user