Skip to content

Commit 8cc9ac5

Browse files
authored
Merge pull request #7 from smartcontractkit/feat/1-gateway-at-a-time
2 parents ce3df4d + 8db60a1 commit 8cc9ac5

File tree

2 files changed

+152
-207
lines changed

2 files changed

+152
-207
lines changed

src/SecretsManager.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,21 @@ export class SecretsManager {
235235
signature: storageSignatureBase64,
236236
}
237237

238-
const gatewayResponses = await this.sendMessageToGateways({
238+
const gatewayResponse = await this.sendMessageToGateways({
239239
gatewayUrls,
240240
method: 'secrets_set',
241241
don_id: this.donId!,
242242
payload,
243243
})
244244

245-
let totalNodeResponses = 0
245+
let totalNodeResponses = gatewayResponse.nodeResponses.length
246246
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++
256253
}
257254
}
258255

@@ -283,8 +280,8 @@ export class SecretsManager {
283280

284281
private async sendMessageToGateways(
285282
gatewayRpcMessageConfig: GatewayMessageConfig,
286-
): Promise<GatewayResponse[]> {
287-
const gatewayResponses: GatewayResponse[] = []
283+
): Promise<GatewayResponse> {
284+
let gatewayResponse: GatewayResponse | undefined
288285
let i = 0
289286
for (const url of gatewayRpcMessageConfig.gatewayUrls) {
290287
i++
@@ -296,22 +293,32 @@ export class SecretsManager {
296293
if (!response.data?.result?.body?.payload?.success) {
297294
throw Error(`Gateway response indicated failure:\n${JSON.stringify(response.data)}`)
298295
}
299-
const gatewayResponse = this.extractNodeResponses(response)
300-
gatewayResponses.push({
296+
const nodeResponses = this.extractNodeResponses(response)
297+
gatewayResponse = {
301298
gatewayUrl: url,
302-
nodeResponses: gatewayResponse,
303-
})
299+
nodeResponses,
300+
}
301+
break // Break after first successful message is sent to a gateway
304302
} catch (e) {
305303
const error = e as any
306304
const errorResponseData = error?.response?.data
307-
throw Error(
305+
console.log(
308306
`Error encountered when attempting to send request to DON gateway URL #${i} of ${
309307
gatewayRpcMessageConfig.gatewayUrls.length
310308
}\n${url}:\n${errorResponseData ? JSON.stringify(errorResponseData) : error}`,
311309
)
312310
}
313311
}
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
315322
}
316323

317324
private async createGatewayMessage({
@@ -402,21 +409,21 @@ export class SecretsManager {
402409

403410
public async listDONHostedEncryptedSecrets(
404411
gatewayUrls: string[],
405-
): Promise<{ result: GatewayResponse[]; error?: string }> {
412+
): Promise<{ result: GatewayResponse; error?: string }> {
406413
this.isInitialized()
407414
this.validateGatewayUrls(gatewayUrls)
408415

409-
const gatewayResponses = await this.sendMessageToGateways({
416+
const gatewayResponse = await this.sendMessageToGateways({
410417
gatewayUrls,
411418
method: 'secrets_list',
412419
don_id: this.donId!,
413420
})
414421

415422
try {
416-
this.verifyDONHostedSecrets(gatewayResponses)
417-
return { result: gatewayResponses }
423+
this.verifyDONHostedSecrets([gatewayResponse])
424+
return { result: gatewayResponse }
418425
} catch (e) {
419-
return { result: gatewayResponses, error: e?.toString() }
426+
return { result: gatewayResponse, error: e?.toString() }
420427
}
421428
}
422429

0 commit comments

Comments
 (0)