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

automatically retries messages to multisig broker #5477

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 30 additions & 1 deletion ironfish-cli/src/multisigBroker/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
IdentityMessage,
IdentitySchema,
JoinSessionMessage,
MultisigBrokerAckSchema,
MultisigBrokerMessage,
MultisigBrokerMessageSchema,
MultisigBrokerMessageWithError,
Expand All @@ -40,6 +41,7 @@ import {
SigningStatusSchema,
} from '../messages'

const RETRY_INTERVAL = 5000
export abstract class MultisigClient {
readonly logger: Logger
readonly version: number
Expand Down Expand Up @@ -68,6 +70,8 @@ export abstract class MultisigClient {
sessionId: string | null = null
passphrase: string

retries: Map<number, NodeJS.Timer> = new Map()

constructor(options: { passphrase: string; logger: Logger }) {
this.logger = options.logger
this.version = 3
Expand Down Expand Up @@ -147,6 +151,10 @@ export abstract class MultisigClient {
if (this.connectTimeout) {
clearTimeout(this.connectTimeout)
}

for (const retryInterval of this.retries.values()) {
clearInterval(retryInterval)
}
}

isConnected(): boolean {
Expand Down Expand Up @@ -215,14 +223,23 @@ export abstract class MultisigClient {
return
}

const messageId = this.nextMessageId++

const message: MultisigBrokerMessage = {
id: this.nextMessageId++,
id: messageId,
method,
sessionId: this.sessionId,
body: this.encryptMessageBody(body),
}

this.writeData(JSON.stringify(message) + '\n')

this.retries.set(
messageId,
setInterval(() => {
this.writeData(JSON.stringify(message) + '\n')
}, RETRY_INTERVAL),
)
}

protected onConnect(): void {
Expand Down Expand Up @@ -275,6 +292,18 @@ export abstract class MultisigClient {
header.result.body = this.decryptMessageBody(header.result.body)

switch (header.result.method) {
case 'ack': {
const body = await YupUtils.tryValidate(MultisigBrokerAckSchema, header.result.body)

if (body.error) {
throw new ServerMessageMalformedError(body.error, header.result.method)
}

const retryInterval = this.retries.get(body.result.messageId)
clearInterval(retryInterval)
this.retries.delete(body.result.messageId)
break
}
case 'identity': {
const body = await YupUtils.tryValidate(IdentitySchema, header.result.body)

Expand Down
10 changes: 10 additions & 0 deletions ironfish-cli/src/multisigBroker/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export interface MultisigBrokerMessageWithError
}
}

export type MultisigBrokerAckMessage = {
messageId: number
}

export type DkgStartSessionMessage = {
minSigners: number
maxSigners: number
Expand Down Expand Up @@ -94,6 +98,12 @@ export const MultisigBrokerMessageWithErrorSchema: yup.ObjectSchema<MultisigBrok
})
.required()

export const MultisigBrokerAckSchema: yup.ObjectSchema<MultisigBrokerAckMessage> = yup
.object({
messageId: yup.number().required(),
})
.required()

export const DkgStartSessionSchema: yup.ObjectSchema<DkgStartSessionMessage> = yup
.object({
minSigners: yup.number().defined(),
Expand Down
8 changes: 8 additions & 0 deletions ironfish-cli/src/multisigBroker/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
DkgStatusMessage,
IdentityMessage,
IdentitySchema,
MultisigBrokerAckMessage,
MultisigBrokerMessage,
MultisigBrokerMessageSchema,
MultisigBrokerMessageWithError,
Expand Down Expand Up @@ -179,6 +180,7 @@ export class MultisigServer {
}

this.logger.debug(`Client ${client.id} sent ${message.method} message`)
this.send(client.socket, 'ack', message.sessionId, { messageId: message.id })

if (message.method === 'dkg.start_session') {
await this.handleDkgStartSessionMessage(client, message)
Expand Down Expand Up @@ -320,6 +322,12 @@ export class MultisigServer {
body: SigningStatusMessage,
): void
send(socket: net.Socket, method: 'connected', sessionId: string, body: ConnectedMessage): void
send(
socket: net.Socket,
method: 'ack',
sessionId: string,
body: MultisigBrokerAckMessage,
): void
send(socket: net.Socket, method: string, sessionId: string, body?: unknown): void {
const message: MultisigBrokerMessage = {
id: this.nextMessageId++,
Expand Down