@@ -235,24 +235,21 @@ export class SecretsManager {
235
235
signature : storageSignatureBase64 ,
236
236
}
237
237
238
- const gatewayResponses = await this . sendMessageToGateways ( {
238
+ const gatewayResponse = await this . sendMessageToGateways ( {
239
239
gatewayUrls,
240
240
method : 'secrets_set' ,
241
241
don_id : this . donId ! ,
242
242
payload,
243
243
} )
244
244
245
- let totalNodeResponses = 0
245
+ let totalNodeResponses = gatewayResponse . nodeResponses . length
246
246
let totalErrorCount = 0
247
- for ( const gatewayResponse of gatewayResponses ) {
248
- totalNodeResponses += gatewayResponse . nodeResponses . length
249
- for ( const nodeResponse of gatewayResponse . nodeResponses ) {
250
- if ( ! nodeResponse . success ) {
251
- console . log (
252
- `WARNING: Node connected to gateway URL ${ gatewayResponse . gatewayUrl } failed to store the encrypted secrets:\n${ nodeResponse } ` ,
253
- )
254
- totalErrorCount ++
255
- }
247
+ for ( const nodeResponse of gatewayResponse . nodeResponses ) {
248
+ if ( ! nodeResponse . success ) {
249
+ console . log (
250
+ `WARNING: Node connected to gateway URL ${ gatewayResponse . gatewayUrl } failed to store the encrypted secrets:\n${ nodeResponse } ` ,
251
+ )
252
+ totalErrorCount ++
256
253
}
257
254
}
258
255
@@ -283,8 +280,8 @@ export class SecretsManager {
283
280
284
281
private async sendMessageToGateways (
285
282
gatewayRpcMessageConfig : GatewayMessageConfig ,
286
- ) : Promise < GatewayResponse [ ] > {
287
- const gatewayResponses : GatewayResponse [ ] = [ ]
283
+ ) : Promise < GatewayResponse > {
284
+ let gatewayResponse : GatewayResponse | undefined
288
285
let i = 0
289
286
for ( const url of gatewayRpcMessageConfig . gatewayUrls ) {
290
287
i ++
@@ -296,22 +293,32 @@ export class SecretsManager {
296
293
if ( ! response . data ?. result ?. body ?. payload ?. success ) {
297
294
throw Error ( `Gateway response indicated failure:\n${ JSON . stringify ( response . data ) } ` )
298
295
}
299
- const gatewayResponse = this . extractNodeResponses ( response )
300
- gatewayResponses . push ( {
296
+ const nodeResponses = this . extractNodeResponses ( response )
297
+ gatewayResponse = {
301
298
gatewayUrl : url ,
302
- nodeResponses : gatewayResponse ,
303
- } )
299
+ nodeResponses,
300
+ }
301
+ break // Break after first successful message is sent to a gateway
304
302
} catch ( e ) {
305
303
const error = e as any
306
304
const errorResponseData = error ?. response ?. data
307
- throw Error (
305
+ console . log (
308
306
`Error encountered when attempting to send request to DON gateway URL #${ i } of ${
309
307
gatewayRpcMessageConfig . gatewayUrls . length
310
308
} \n${ url } :\n${ errorResponseData ? JSON . stringify ( errorResponseData ) : error } `,
311
309
)
312
310
}
313
311
}
314
- return gatewayResponses
312
+
313
+ if ( ! gatewayResponse ) {
314
+ throw Error (
315
+ `Failed to send request to any of the DON gateway URLs:\n${ JSON . stringify (
316
+ gatewayRpcMessageConfig . gatewayUrls ,
317
+ ) } `,
318
+ )
319
+ }
320
+
321
+ return gatewayResponse
315
322
}
316
323
317
324
private async createGatewayMessage ( {
@@ -402,21 +409,21 @@ export class SecretsManager {
402
409
403
410
public async listDONHostedEncryptedSecrets (
404
411
gatewayUrls : string [ ] ,
405
- ) : Promise < { result : GatewayResponse [ ] ; error ?: string } > {
412
+ ) : Promise < { result : GatewayResponse ; error ?: string } > {
406
413
this . isInitialized ( )
407
414
this . validateGatewayUrls ( gatewayUrls )
408
415
409
- const gatewayResponses = await this . sendMessageToGateways ( {
416
+ const gatewayResponse = await this . sendMessageToGateways ( {
410
417
gatewayUrls,
411
418
method : 'secrets_list' ,
412
419
don_id : this . donId ! ,
413
420
} )
414
421
415
422
try {
416
- this . verifyDONHostedSecrets ( gatewayResponses )
417
- return { result : gatewayResponses }
423
+ this . verifyDONHostedSecrets ( [ gatewayResponse ] )
424
+ return { result : gatewayResponse }
418
425
} catch ( e ) {
419
- return { result : gatewayResponses , error : e ?. toString ( ) }
426
+ return { result : gatewayResponse , error : e ?. toString ( ) }
420
427
}
421
428
}
422
429
0 commit comments