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 membersList: Record<string, Member> = {};
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() {}
@ -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> {
console.log('Opening new websocket connection');
await initWebsocket(url);
@ -116,9 +134,9 @@ export default class Services {
* @param wsurl - The WebSocket URL (key).
* @param spAddress - The SP Address (value).
*/
public updateRelay(wsurl: string, spAddress: string): void {
this.relayAddresses[wsurl] = spAddress;
console.log(`Updated: ${wsurl} -> ${spAddress}`);
public updateRelay(url: string, spAddress: string) {
console.log(`✅ Updating relay ${url} with spAddress ${spAddress}`);
this.relayAddresses[url] = spAddress;
}
/**
@ -381,16 +399,15 @@ export default class Services {
roles: Record<string, RoleDefinition>,
): Promise<ApiReturn> {
let relayAddress = this.getAllRelays()[0]?.spAddress;
if (!relayAddress || relayAddress === '') {
console.log('No relay address found, connecting to relays...');
await this.connectAllRelays();
// After connectAllRelays completes, relay addresses should be updated
if (!relayAddress) {
console.log('⏳ Waiting for relays to be ready...');
await this.getRelayReadyPromise();
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;
@ -409,9 +426,13 @@ export default class Services {
...this.sdkClient.encode_binary(publicSplitData.binaryData)
};
console.log('encoded data:', encodedPrivateData);
console.log('encoded data:', encodedPublicData);
// console.log('encodedPrivateData:', encodedPrivateData);
// 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 (
encodedPrivateData,
roles,
@ -1365,7 +1386,12 @@ export default class Services {
public async handleHandshakeMsg(url: string, parsedMsg: any) {
try {
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);
this.currentBlockHeight = handshakeMsg.chain_tip;
console.log('this.currentBlockHeight:', this.currentBlockHeight);