Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: no return routing and wait for ping #946

Merged
merged 3 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/core/src/agent/MessageSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ export class MessageSender {
// as the `from` field in a received message will identity the did used so we don't have to store all keys in tags to be able to find the connections associated with
// an incoming message.
const [firstOurAuthenticationKey] = ourAuthenticationKeys
const shouldUseReturnRoute = !this.transportService.hasInboundEndpoint(ourDidDocument)
// If the returnRoute is already set we won't override it. This allows to set the returnRoute manually if this is desired.
const shouldAddReturnRoute =
payload.transport?.returnRoute === undefined && !this.transportService.hasInboundEndpoint(ourDidDocument)

// Loop trough all available services and try to send the message
for await (const service of services) {
Expand All @@ -237,7 +239,7 @@ export class MessageSender {
message: payload,
service,
senderKey: firstOurAuthenticationKey,
returnRoute: shouldUseReturnRoute,
returnRoute: shouldAddReturnRoute,
connectionId: connection.id,
})
return
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/modules/connections/ConnectionsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AgentConfig } from '../../agent/AgentConfig'
import { Dispatcher } from '../../agent/Dispatcher'
import { MessageSender } from '../../agent/MessageSender'
import { createOutboundMessage } from '../../agent/helpers'
import { ReturnRouteTypes } from '../../decorators/transport/TransportDecorator'
import { AriesFrameworkError } from '../../error'
import { injectable, module } from '../../plugins'
import { DidResolverService } from '../dids'
Expand Down Expand Up @@ -165,11 +166,17 @@ export class ConnectionsModule {
)
}
const message = await this.didExchangeProtocol.createComplete(connectionRecord, outOfBandRecord)
// Disable return routing as we don't want to receive a response for this message over the same channel
// This has led to long timeouts as not all clients actually close an http socket if there is no response message
message.setReturnRouting(ReturnRouteTypes.none)
outboundMessage = createOutboundMessage(connectionRecord, message)
} else {
const { message } = await this.connectionService.createTrustPing(connectionRecord, {
responseRequested: false,
})
// Disable return routing as we don't want to receive a response for this message over the same channel
// This has led to long timeouts as not all clients actually close an http socket if there is no response message
message.setReturnRouting(ReturnRouteTypes.none)
outboundMessage = createOutboundMessage(connectionRecord, message)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { OutOfBandService } from '../../oob/OutOfBandService'
import type { ConnectionService } from '../services/ConnectionService'

import { createOutboundMessage } from '../../../agent/helpers'
import { ReturnRouteTypes } from '../../../decorators/transport/TransportDecorator'
import { AriesFrameworkError } from '../../../error'
import { ConnectionResponseMessage } from '../messages'

Expand Down Expand Up @@ -73,6 +74,10 @@ export class ConnectionResponseHandler implements Handler {
// if auto accept is enable
if (connection.autoAcceptConnection ?? this.agentConfig.autoAcceptConnections) {
const { message } = await this.connectionService.createTrustPing(connection, { responseRequested: false })

// Disable return routing as we don't want to receive a response for this message over the same channel
// This has led to long timeouts as not all clients actually close an http socket if there is no response message
message.setReturnRouting(ReturnRouteTypes.none)
return createOutboundMessage(connection, message)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { DidExchangeProtocol } from '../DidExchangeProtocol'
import type { ConnectionService } from '../services'

import { createOutboundMessage } from '../../../agent/helpers'
import { ReturnRouteTypes } from '../../../decorators/transport/TransportDecorator'
import { AriesFrameworkError } from '../../../error'
import { OutOfBandState } from '../../oob/domain/OutOfBandState'
import { DidExchangeResponseMessage } from '../messages'
Expand Down Expand Up @@ -97,6 +98,10 @@ export class DidExchangeResponseHandler implements Handler {
// if auto accept is enable
if (connection.autoAcceptConnection ?? this.agentConfig.autoAcceptConnections) {
const message = await this.didExchangeProtocol.createComplete(connection, outOfBandRecord)
// Disable return routing as we don't want to receive a response for this message over the same channel
// This has led to long timeouts as not all clients actually close an http socket if there is no response message
message.setReturnRouting(ReturnRouteTypes.none)

if (!outOfBandRecord.reusable) {
await this.outOfBandService.updateState(outOfBandRecord, OutOfBandState.Done)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class ConnectionService {
messageContext: InboundMessageContext<ConnectionRequestMessage>,
outOfBandRecord: OutOfBandRecord
): Promise<ConnectionRecord> {
this.logger.debug(`Process message ${ConnectionRequestMessage.type} start`, messageContext)
this.logger.debug(`Process message ${ConnectionRequestMessage.type.messageTypeUri} start`, messageContext)
outOfBandRecord.assertRole(OutOfBandRole.Sender)
outOfBandRecord.assertState(OutOfBandState.AwaitResponse)

Expand Down Expand Up @@ -183,7 +183,7 @@ export class ConnectionService {
await this.connectionRepository.update(connectionRecord)
this.emitStateChangedEvent(connectionRecord, null)

this.logger.debug(`Process message ${ConnectionRequestMessage.type} end`, connectionRecord)
this.logger.debug(`Process message ${ConnectionRequestMessage.type.messageTypeUri} end`, connectionRecord)
return connectionRecord
}

Expand Down Expand Up @@ -259,7 +259,7 @@ export class ConnectionService {
messageContext: InboundMessageContext<ConnectionResponseMessage>,
outOfBandRecord: OutOfBandRecord
): Promise<ConnectionRecord> {
this.logger.debug(`Process message ${ConnectionResponseMessage.type} start`, messageContext)
this.logger.debug(`Process message ${ConnectionResponseMessage.type.messageTypeUri} start`, messageContext)
const { connection: connectionRecord, message, recipientKey, senderKey } = messageContext

if (!recipientKey || !senderKey) {
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/transport/HttpOutboundTransport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Agent } from '../agent/Agent'
import type { AgentMessageReceivedEvent } from '../agent/Events'
import type { Logger } from '../logger'
import type { OutboundPackage } from '../types'
import type { OutboundTransport } from './OutboundTransport'
Expand All @@ -7,6 +8,7 @@ import type fetch from 'node-fetch'
import { AbortController } from 'abort-controller'

import { AgentConfig } from '../agent/AgentConfig'
import { AgentEventTypes } from '../agent/Events'
import { AriesFrameworkError } from '../error/AriesFrameworkError'
import { isValidJweStructure, JsonEncoder } from '../utils'

Expand Down Expand Up @@ -84,7 +86,13 @@ export class HttpOutboundTransport implements OutboundTransport {
)
return
}
await this.agent.receiveMessage(encryptedMessage)
// Emit event with the received agent message.
this.agent.events.emit<AgentMessageReceivedEvent>({
type: AgentEventTypes.AgentMessageReceived,
payload: {
message: encryptedMessage,
},
})
} catch (error) {
this.logger.debug('Unable to parse response message')
}
Expand Down