-
Notifications
You must be signed in to change notification settings - Fork 802
chore: resubscribe when no result is received #7022
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
Open
ganchoradkov
wants to merge
2
commits into
v2.0
Choose a base branch
from
chore/resubscribe-on-no-result
base: v2.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−12
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ | |
| ErrorResponse, | ||
| getBigIntRpcId, | ||
| } from "@walletconnect/jsonrpc-utils"; | ||
| import { FIVE_MINUTES, ONE_SECOND, toMiliseconds } from "@walletconnect/time"; | ||
| import { FIVE_MINUTES, fromMiliseconds, ONE_SECOND, toMiliseconds } from "@walletconnect/time"; | ||
| import { | ||
| EnginePrivate, | ||
| EngineTypes, | ||
|
|
@@ -122,7 +122,10 @@ | |
| ENGINE_QUEUE_STATES, | ||
| AUTH_PUBLIC_KEY_NAME, | ||
| TVF_METHODS, | ||
| RESUBSCRIBE_INTERVAL, | ||
| MAX_RESUBSCRIBE_ATTEMPTS_ON_PENDING_RESULTS, | ||
| } from "../constants/index.js"; | ||
| import { HEARTBEAT_EVENTS } from "@walletconnect/heartbeat"; | ||
|
|
||
| export class Engine extends IEngine { | ||
| public name = ENGINE_CONTEXT; | ||
|
|
@@ -172,13 +175,26 @@ | |
| } | ||
| > = new Map(); | ||
|
|
||
| // most flows depend on sending a request and receiving a result from the peer sign-client sdk | ||
| // this map is used to store the messages that are waiting for a result | ||
| // the key is the clientRpcId of the request, the value is the topic of the request | ||
| private requestWaitingForResult: Map< | ||
| number, | ||
| { | ||
| topic: string; | ||
| waitingSince: number; | ||
| resubscribeAttempts?: number; | ||
| } | ||
| > = new Map(); | ||
|
|
||
| constructor(client: IEngine["client"]) { | ||
| super(client); | ||
| } | ||
|
|
||
| public init: IEngine["init"] = async () => { | ||
| if (!this.initialized) { | ||
| await this.cleanup(); | ||
| this.registerHeartbeatEvents(); | ||
| this.registerRelayerEvents(); | ||
| this.registerExpirerEvents(); | ||
| this.registerPairingEvents(); | ||
|
|
@@ -365,7 +381,7 @@ | |
| }); | ||
|
|
||
| await this.setProposal(proposal.id, proposal); | ||
|
|
||
| this.requestWaitingForResult.set(proposal.id, { topic, waitingSince: Date.now() }); | ||
| await this.sendProposeSession({ | ||
| proposal, | ||
| publishOpts: { | ||
|
|
@@ -378,6 +394,7 @@ | |
| }, | ||
| }).catch((error) => { | ||
| this.deleteProposal(proposal.id); | ||
| this.requestWaitingForResult.delete(proposal.id); | ||
| throw error; | ||
| }); | ||
|
|
||
|
|
@@ -741,6 +758,8 @@ | |
| chainId, | ||
| }; | ||
|
|
||
| this.requestWaitingForResult.set(clientRpcId, { topic, waitingSince: Date.now() }); | ||
|
|
||
| return await Promise.all([ | ||
| new Promise<void>(async (resolve) => { | ||
| await this.sendRequest({ | ||
|
|
@@ -752,7 +771,10 @@ | |
| expiry, | ||
| throwOnFailedPublish: true, | ||
| tvf: this.getTVFParams(clientRpcId, protocolRequestParams), | ||
| }).catch((error) => reject(error)); | ||
| }).catch((error) => { | ||
| this.requestWaitingForResult.delete(clientRpcId); | ||
| reject(error); | ||
| }); | ||
| this.client.events.emit("session_request_sent", { | ||
| topic, | ||
| request, | ||
|
|
@@ -847,13 +869,20 @@ | |
| else resolve(); | ||
| }); | ||
| await Promise.all([ | ||
| this.sendRequest({ | ||
| topic, | ||
| method: "wc_sessionPing", | ||
| params: {}, | ||
| throwOnFailedPublish: true, | ||
| clientRpcId, | ||
| relayRpcId, | ||
| new Promise<void>(async (resolve) => { | ||
| this.requestWaitingForResult.set(clientRpcId, { topic, waitingSince: Date.now() }); | ||
| await this.sendRequest({ | ||
| topic, | ||
| method: "wc_sessionPing", | ||
| params: {}, | ||
| throwOnFailedPublish: true, | ||
| clientRpcId, | ||
| relayRpcId, | ||
| }).catch((error) => { | ||
| this.requestWaitingForResult.delete(clientRpcId); | ||
| reject(error); | ||
| }); | ||
| resolve(); | ||
| }), | ||
| done(), | ||
| ]); | ||
|
|
@@ -1040,7 +1069,7 @@ | |
| const authenticateEventTarget = engineEvent("session_request", authenticateId); | ||
|
|
||
| // handle fallback session proposal response | ||
| const onSessionConnect = async ({ error, session }: any) => { | ||
| // cleanup listener for authenticate response | ||
| this.events.off(authenticateEventTarget, onAuthenticate); | ||
| if (error) reject(error); | ||
|
|
@@ -1884,6 +1913,47 @@ | |
| await this.client.core.relayer.confirmOnlineStateOrThrow(); | ||
| } | ||
|
|
||
| // ---------- Heartbeat Event ----------------------------------- // | ||
|
|
||
| /** | ||
| * This function is used to register the heartbeat events for the engine | ||
| * It checks if any requests are still waiting for a result and resubscribes to the topic if needed | ||
| * It also logs a warning if the request is still waiting for a result after the maximum number of attempts | ||
| * It also logs a debug message if the request is resubscribed to the topic | ||
| * It also logs a warning if the request is still waiting for a result after the maximum number of attempts | ||
| */ | ||
| private registerHeartbeatEvents() { | ||
| this.client.core.heartbeat.on(HEARTBEAT_EVENTS.pulse, async () => { | ||
| for (const [clientRpcId, request] of this.requestWaitingForResult.entries()) { | ||
| const attempts = (request.resubscribeAttempts || 0) + 1; | ||
| if (fromMiliseconds(Date.now() - request.waitingSince) > attempts * RESUBSCRIBE_INTERVAL) { | ||
| if (attempts <= MAX_RESUBSCRIBE_ATTEMPTS_ON_PENDING_RESULTS) { | ||
| this.client.logger.debug( | ||
| `Resubscribing to topic ${request.topic} for request ${clientRpcId}, attempt ${attempts}`, | ||
| ); | ||
| try { | ||
| this.requestWaitingForResult.set(clientRpcId, { | ||
| ...request, | ||
| resubscribeAttempts: attempts, | ||
| }); | ||
| await this.client.core.relayer.subscribe(request.topic, { | ||
| internal: { throwOnFailedPublish: true }, | ||
| }); | ||
| } catch (error) { | ||
| this.client.logger.warn( | ||
| `Failed to resubscribe to topic ${request.topic} for request ${clientRpcId}, attempt ${attempts}`, | ||
| ); | ||
| } | ||
| } else { | ||
| this.client.logger.warn( | ||
| `Request ${clientRpcId} still hasn't received a result after ${attempts * RESUBSCRIBE_INTERVAL}s, giving up`, | ||
| ); | ||
ganchoradkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // ---------- Relay Events Router ----------------------------------- // | ||
|
|
||
| private registerRelayerEvents() { | ||
|
|
@@ -2020,6 +2090,8 @@ | |
| const record = await this.client.core.history.get(topic, payload.id); | ||
| const resMethod = record.request.method as JsonRpcTypes.WcMethod; | ||
|
|
||
| this.requestWaitingForResult.delete(payload.id); | ||
|
|
||
| switch (resMethod) { | ||
| case "wc_sessionPropose": | ||
| return this.onSessionProposeResponse(topic, payload, transportType); | ||
|
|
||
This file contains hidden or 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 | ||
|---|---|---|---|---|
|
|
@@ -16,13 +16,14 @@ import { describe, it, expect, afterEach } from "vitest"; | |||
| import { SignClient } from "../../src"; | ||||
| import { formatJsonRpcResult } from "@walletconnect/jsonrpc-utils"; | ||||
| import { RELAYER_EVENTS } from "@walletconnect/core"; | ||||
| import { ISignClient } from "@walletconnect/types"; | ||||
|
|
||||
|
Comment on lines
+19
to
20
|
||||
| import { ISignClient } from "@walletconnect/types"; |
ganchoradkov marked this conversation as resolved.
Show resolved
Hide resolved
ganchoradkov marked this conversation as resolved.
Show resolved
Hide resolved
ganchoradkov marked this conversation as resolved.
Show resolved
Hide resolved
ganchoradkov marked this conversation as resolved.
Show resolved
Hide resolved
ganchoradkov marked this conversation as resolved.
Show resolved
Hide resolved
ganchoradkov marked this conversation as resolved.
Show resolved
Hide resolved
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
RESUBSCRIBE_INTERVALconstant lacks a unit suffix or documentation. Consider renaming it toRESUBSCRIBE_INTERVAL_SECONDSor adding a JSDoc comment to clarify it represents seconds, especially since other constants in the file use named constants from@walletconnect/time(e.g.,FIVE_MINUTES,ONE_DAY).