Add promise to wait for relay availability to make createProcess functional
All checks were successful
Build and Push to Registry / build-and-push (push) Successful in 2m23s

This commit is contained in:
NicolasCantu 2025-10-08 11:10:03 +02:00
parent d9daa00b32
commit 412c855777

View File

@ -32,6 +32,8 @@ export default class Services {
private relayAddresses: { [wsurl: string]: string } = {}; private relayAddresses: { [wsurl: string]: string } = {};
private membersList: Record<string, Member> = {}; private membersList: Record<string, Member> = {};
private currentBlockHeight: number = -1; private currentBlockHeight: number = -1;
private relayReadyResolver: (() => void) | null = null;
private relayReadyPromise: Promise<void> | null = null;
// Private constructor to prevent direct instantiation from outside // Private constructor to prevent direct instantiation from outside
private constructor() {} private constructor() {}
@ -106,6 +108,22 @@ export default class Services {
} }
} }
private getRelayReadyPromise(): Promise<void> {
if (!this.relayReadyPromise) {
this.relayReadyPromise = new Promise<void>((resolve) => {
this.relayReadyResolver = resolve;
});
}
return this.relayReadyPromise;
}
private resolveRelayReady(): void {
if (this.relayReadyResolver) {
this.relayReadyResolver();
this.relayReadyResolver = null;
this.relayReadyPromise = null;
}
public async addWebsocketConnection(url: string): Promise<void> { public async addWebsocketConnection(url: string): Promise<void> {
console.log('Opening new websocket connection'); console.log('Opening new websocket connection');
await initWebsocket(url); await initWebsocket(url);
@ -116,9 +134,9 @@ export default class Services {
* @param wsurl - The WebSocket URL (key). * @param wsurl - The WebSocket URL (key).
* @param spAddress - The SP Address (value). * @param spAddress - The SP Address (value).
*/ */
public updateRelay(wsurl: string, spAddress: string): void { public updateRelay(url: string, spAddress: string) {
this.relayAddresses[wsurl] = spAddress; console.log(`✅ Updating relay ${url} with spAddress ${spAddress}`);
console.log(`Updated: ${wsurl} -> ${spAddress}`); this.relayAddresses[url] = spAddress;
} }
/** /**
@ -381,16 +399,15 @@ export default class Services {
roles: Record<string, RoleDefinition>, roles: Record<string, RoleDefinition>,
): Promise<ApiReturn> { ): Promise<ApiReturn> {
let relayAddress = this.getAllRelays()[0]?.spAddress; let relayAddress = this.getAllRelays()[0]?.spAddress;
if (!relayAddress || relayAddress === '') { if (!relayAddress) {
console.log('No relay address found, connecting to relays...'); console.log('⏳ Waiting for relays to be ready...');
await this.connectAllRelays(); await this.getRelayReadyPromise();
// After connectAllRelays completes, relay addresses should be updated
relayAddress = this.getAllRelays()[0]?.spAddress; relayAddress = this.getAllRelays()[0]?.spAddress;
if (!relayAddress || relayAddress === '') { }
throw new Error('No relay address available after connecting to relays');
} if (!relayAddress) {
throw new Error('❌ No relay address available after waiting');
} }
const feeRate = 1; const feeRate = 1;
@ -409,9 +426,13 @@ export default class Services {
...this.sdkClient.encode_binary(publicSplitData.binaryData) ...this.sdkClient.encode_binary(publicSplitData.binaryData)
}; };
console.log('encoded data:', encodedPrivateData); // console.log('encodedPrivateData:', encodedPrivateData);
console.log('encoded data:', encodedPublicData); // console.log('encodedPublicData:', encodedPublicData);
// console.log('roles:', roles);
// console.log('members:', this.getAllMembers());
// console.log('relayAddress:', relayAddress, 'feeRate:', feeRate);
await this.getTokensFromFaucet();
const result = this.sdkClient.create_new_process ( const result = this.sdkClient.create_new_process (
encodedPrivateData, encodedPrivateData,
roles, roles,
@ -1365,7 +1386,12 @@ export default class Services {
public async handleHandshakeMsg(url: string, parsedMsg: any) { public async handleHandshakeMsg(url: string, parsedMsg: any) {
try { try {
const handshakeMsg: HandshakeMessage = JSON.parse(parsedMsg); const handshakeMsg: HandshakeMessage = JSON.parse(parsedMsg);
this.updateRelay(url, handshakeMsg.sp_address); if (handshakeMsg.sp_address) {
this.updateRelay(url, handshakeMsg.sp_address);
this.relayAddresses[url] = handshakeMsg.sp_address;
this.resolveRelayReady();
}
console.log('handshakeMsg:', handshakeMsg); console.log('handshakeMsg:', handshakeMsg);
this.currentBlockHeight = handshakeMsg.chain_tip; this.currentBlockHeight = handshakeMsg.chain_tip;
console.log('this.currentBlockHeight:', this.currentBlockHeight); console.log('this.currentBlockHeight:', this.currentBlockHeight);