-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(routing): pickup v2 mediator role basic implementation (#975)
Signed-off-by: Ariel Gentile <[email protected]>
- Loading branch information
Showing
38 changed files
with
822 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
packages/core/src/modules/routing/__tests__/pickup.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import type { SubjectMessage } from '../../../../../../tests/transport/SubjectInboundTransport' | ||
|
||
import { Subject } from 'rxjs' | ||
|
||
import { SubjectInboundTransport } from '../../../../../../tests/transport/SubjectInboundTransport' | ||
import { SubjectOutboundTransport } from '../../../../../../tests/transport/SubjectOutboundTransport' | ||
import { getBaseConfig, waitForBasicMessage } from '../../../../tests/helpers' | ||
import { Agent } from '../../../agent/Agent' | ||
import { ConsoleLogger, LogLevel } from '../../../logger' | ||
import { HandshakeProtocol } from '../../connections' | ||
import { MediatorPickupStrategy } from '../MediatorPickupStrategy' | ||
|
||
const logger = new ConsoleLogger(LogLevel.info) | ||
const recipientConfig = getBaseConfig('Mediation: Recipient', { | ||
autoAcceptConnections: true, | ||
indyLedgers: [], | ||
logger, | ||
}) | ||
const mediatorConfig = getBaseConfig('Mediation: Mediator', { | ||
autoAcceptConnections: true, | ||
endpoints: ['rxjs:mediator'], | ||
indyLedgers: [], | ||
logger, | ||
}) | ||
|
||
describe('E2E Pick Up protocol', () => { | ||
let recipientAgent: Agent | ||
let mediatorAgent: Agent | ||
|
||
afterEach(async () => { | ||
await recipientAgent?.shutdown() | ||
await recipientAgent?.wallet.delete() | ||
await mediatorAgent?.shutdown() | ||
await mediatorAgent?.wallet.delete() | ||
}) | ||
|
||
test('E2E Pick Up V1 protocol', async () => { | ||
const mediatorMessages = new Subject<SubjectMessage>() | ||
|
||
const subjectMap = { | ||
'rxjs:mediator': mediatorMessages, | ||
} | ||
|
||
// Initialize mediatorReceived message | ||
mediatorAgent = new Agent(mediatorConfig.config, recipientConfig.agentDependencies) | ||
mediatorAgent.registerOutboundTransport(new SubjectOutboundTransport(subjectMap)) | ||
mediatorAgent.registerInboundTransport(new SubjectInboundTransport(mediatorMessages)) | ||
await mediatorAgent.initialize() | ||
|
||
// Create connection to use for recipient | ||
const mediatorOutOfBandRecord = await mediatorAgent.oob.createInvitation({ | ||
label: 'mediator invitation', | ||
handshake: true, | ||
handshakeProtocols: [HandshakeProtocol.DidExchange], | ||
}) | ||
|
||
// Initialize recipient | ||
recipientAgent = new Agent(recipientConfig.config, recipientConfig.agentDependencies) | ||
recipientAgent.registerOutboundTransport(new SubjectOutboundTransport(subjectMap)) | ||
await recipientAgent.initialize() | ||
|
||
// Connect | ||
const mediatorInvitation = mediatorOutOfBandRecord.outOfBandInvitation | ||
|
||
let { connectionRecord: recipientMediatorConnection } = await recipientAgent.oob.receiveInvitationFromUrl( | ||
mediatorInvitation.toUrl({ domain: 'https://example.com/ssi' }) | ||
) | ||
|
||
recipientMediatorConnection = await recipientAgent.connections.returnWhenIsConnected( | ||
recipientMediatorConnection!.id | ||
) | ||
|
||
let [mediatorRecipientConnection] = await mediatorAgent.connections.findAllByOutOfBandId(mediatorOutOfBandRecord.id) | ||
|
||
mediatorRecipientConnection = await mediatorAgent.connections.returnWhenIsConnected(mediatorRecipientConnection!.id) | ||
|
||
const message = 'hello pickup V1' | ||
await mediatorAgent.basicMessages.sendMessage(mediatorRecipientConnection.id, message) | ||
|
||
await recipientAgent.mediationRecipient.pickupMessages(recipientMediatorConnection) | ||
|
||
const basicMessage = await waitForBasicMessage(recipientAgent, { | ||
content: message, | ||
}) | ||
|
||
expect(basicMessage.content).toBe(message) | ||
}) | ||
|
||
test('E2E Pick Up V2 protocol', async () => { | ||
const mediatorMessages = new Subject<SubjectMessage>() | ||
|
||
const subjectMap = { | ||
'rxjs:mediator': mediatorMessages, | ||
} | ||
|
||
// Initialize mediatorReceived message | ||
mediatorAgent = new Agent(mediatorConfig.config, recipientConfig.agentDependencies) | ||
mediatorAgent.registerOutboundTransport(new SubjectOutboundTransport(subjectMap)) | ||
mediatorAgent.registerInboundTransport(new SubjectInboundTransport(mediatorMessages)) | ||
await mediatorAgent.initialize() | ||
|
||
// Create connection to use for recipient | ||
const mediatorOutOfBandRecord = await mediatorAgent.oob.createInvitation({ | ||
label: 'mediator invitation', | ||
handshake: true, | ||
handshakeProtocols: [HandshakeProtocol.DidExchange], | ||
}) | ||
|
||
// Initialize recipient | ||
recipientAgent = new Agent(recipientConfig.config, recipientConfig.agentDependencies) | ||
recipientAgent.registerOutboundTransport(new SubjectOutboundTransport(subjectMap)) | ||
await recipientAgent.initialize() | ||
|
||
// Connect | ||
const mediatorInvitation = mediatorOutOfBandRecord.outOfBandInvitation | ||
|
||
let { connectionRecord: recipientMediatorConnection } = await recipientAgent.oob.receiveInvitationFromUrl( | ||
mediatorInvitation.toUrl({ domain: 'https://example.com/ssi' }) | ||
) | ||
|
||
recipientMediatorConnection = await recipientAgent.connections.returnWhenIsConnected( | ||
recipientMediatorConnection!.id | ||
) | ||
|
||
let [mediatorRecipientConnection] = await mediatorAgent.connections.findAllByOutOfBandId(mediatorOutOfBandRecord.id) | ||
|
||
mediatorRecipientConnection = await mediatorAgent.connections.returnWhenIsConnected(mediatorRecipientConnection!.id) | ||
|
||
const message = 'hello pickup V2' | ||
await mediatorAgent.basicMessages.sendMessage(mediatorRecipientConnection.id, message) | ||
|
||
await recipientAgent.mediationRecipient.pickupMessages(recipientMediatorConnection, MediatorPickupStrategy.PickUpV2) | ||
|
||
const basicMessage = await waitForBasicMessage(recipientAgent, { | ||
content: message, | ||
}) | ||
|
||
expect(basicMessage.content).toBe(message) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
export * from './ForwardHandler' | ||
export * from './KeylistUpdateHandler' | ||
export * from './BatchHandler' | ||
export * from './BatchPickupHandler' | ||
export * from './KeylistUpdateResponseHandler' | ||
export * from './StatusHandler' | ||
export * from './MessageDeliveryHandler' | ||
export * from './MediationDenyHandler' | ||
export * from './MediationGrantHandler' | ||
export * from './MediationRequestHandler' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,6 @@ | ||
export * from './BatchMessage' | ||
export * from './BatchPickupMessage' | ||
export * from './ForwardMessage' | ||
export * from './KeylistUpdateMessage' | ||
export * from './KeylistUpdateResponseMessage' | ||
export * from './MediationGrantMessage' | ||
export * from './MediationDenyMessage' | ||
export * from './MediationRequestMessage' | ||
export * from './DeliveryRequestMessage' | ||
export * from './StatusMessage' | ||
export * from './StatusRequestMessage' | ||
export * from './MessageDeliveryMessage' | ||
export * from './MessagesReceivedMessage' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './pickup' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './v1' | ||
export * from './v2' |
64 changes: 64 additions & 0 deletions
64
packages/core/src/modules/routing/protocol/pickup/v1/MessagePickupService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { InboundMessageContext } from '../../../../../agent/models/InboundMessageContext' | ||
import type { EncryptedMessage } from '../../../../../types' | ||
import type { BatchPickupMessage } from './messages' | ||
|
||
import { Dispatcher } from '../../../../../agent/Dispatcher' | ||
import { EventEmitter } from '../../../../../agent/EventEmitter' | ||
import { createOutboundMessage } from '../../../../../agent/helpers' | ||
import { InjectionSymbols } from '../../../../../constants' | ||
import { inject, injectable } from '../../../../../plugins' | ||
import { MessageRepository } from '../../../../../storage/MessageRepository' | ||
|
||
import { BatchHandler, BatchPickupHandler } from './handlers' | ||
import { BatchMessage, BatchMessageMessage } from './messages' | ||
|
||
@injectable() | ||
export class MessagePickupService { | ||
private messageRepository: MessageRepository | ||
private dispatcher: Dispatcher | ||
private eventEmitter: EventEmitter | ||
|
||
public constructor( | ||
@inject(InjectionSymbols.MessageRepository) messageRepository: MessageRepository, | ||
dispatcher: Dispatcher, | ||
eventEmitter: EventEmitter | ||
) { | ||
this.messageRepository = messageRepository | ||
this.dispatcher = dispatcher | ||
this.eventEmitter = eventEmitter | ||
|
||
this.registerHandlers() | ||
} | ||
|
||
public async batch(messageContext: InboundMessageContext<BatchPickupMessage>) { | ||
// Assert ready connection | ||
const connection = messageContext.assertReadyConnection() | ||
|
||
const { message } = messageContext | ||
const messages = await this.messageRepository.takeFromQueue(connection.id, message.batchSize) | ||
|
||
// TODO: each message should be stored with an id. to be able to conform to the id property | ||
// of batch message | ||
const batchMessages = messages.map( | ||
(msg) => | ||
new BatchMessageMessage({ | ||
message: msg, | ||
}) | ||
) | ||
|
||
const batchMessage = new BatchMessage({ | ||
messages: batchMessages, | ||
}) | ||
|
||
return createOutboundMessage(connection, batchMessage) | ||
} | ||
|
||
public async queueMessage(connectionId: string, message: EncryptedMessage) { | ||
await this.messageRepository.add(connectionId, message) | ||
} | ||
|
||
protected registerHandlers() { | ||
this.dispatcher.registerHandler(new BatchPickupHandler(this)) | ||
this.dispatcher.registerHandler(new BatchHandler(this.eventEmitter)) | ||
} | ||
} |
10 changes: 5 additions & 5 deletions
10
.../modules/routing/handlers/BatchHandler.ts → ...otocol/pickup/v1/handlers/BatchHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...es/routing/handlers/BatchPickupHandler.ts → .../pickup/v1/handlers/BatchPickupHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/core/src/modules/routing/protocol/pickup/v1/handlers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './BatchHandler' | ||
export * from './BatchPickupHandler' |
2 changes: 2 additions & 0 deletions
2
packages/core/src/modules/routing/protocol/pickup/v1/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './MessagePickupService' | ||
export * from './messages' |
10 changes: 5 additions & 5 deletions
10
.../modules/routing/messages/BatchMessage.ts → ...otocol/pickup/v1/messages/BatchMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...es/routing/messages/BatchPickupMessage.ts → .../pickup/v1/messages/BatchPickupMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/core/src/modules/routing/protocol/pickup/v1/messages/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './BatchMessage' | ||
export * from './BatchPickupMessage' |
Oops, something went wrong.