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: process incoming requests in order #4638

Merged
merged 5 commits into from
Jul 2, 2024

Conversation

ganchoradkov
Copy link
Member

@ganchoradkov ganchoradkov commented Jun 21, 2024

Description

Updated intake logic of incoming relay messages so they are processed by Sign Client one by one instead of all at the same time. The old approach was resulting in conflicts when messages were dependent on each other such as session_update adding new chain to the session and session_eventinstructing the client to switch to that new chain. In the above case, the session_event must be processed after the session_update has finished the update, else the client has no knowledge of that new chain.

Additionally, implemented relayMessageCache that stores any messages received while the client is still initializing and the messages are processed after init has finished. This allows implementing apps to setup all event listeners and not miss events

Type of change

  • Chore (non-breaking change that addresses non-functional tasks, maintenance, or code quality improvements)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Draft PR (breaking/non-breaking change which needs more work for having a proper functionality [Mark this PR as ready to review only when completely ready])
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

How has this been tested?

dogfooding

Checklist

  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

Additional Information (Optional)

Please include any additional information that may be useful for the reviewer.

@ganchoradkov ganchoradkov marked this pull request as draft June 21, 2024 11:27
@ganchoradkov ganchoradkov marked this pull request as ready for review June 24, 2024 07:40
Copy link
Contributor

@Cali93 Cali93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all tests passing ? Should we add unhappy path cases ?

@@ -1028,6 +1029,23 @@ export class Engine extends IEngine {
return formatMessage(request, iss);
};

public processRelayMessageCache: IEngine["processRelayMessageCache"] = () => {
// process the relay messages cache in the next tick to allow event listeners to be registered by the implementing app
setTimeout(async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's a good idea to mix setTimeout with an async operation.
Unless I misunderstood, I'd rather isolate the setTimeout and do something along the lines of

Suggested change
setTimeout(async () => {
await sleep(50)
if (this.relayMessageCache.length === 0) return;
while (this.relayMessageCache.length > 0) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need that small timeout?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the goal is to process the message after the finish initializing. This allows to setup listeners after the client.init()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but why 50 and not other number? Is 50 enough in a RN environment?

Copy link
Member Author

@ganchoradkov ganchoradkov Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's arbitrary number. We can change it to some other value if you have suggestion

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we use a flag like client.initialized then ?

});
this.client.core.relayer.on(RELAYER_EVENTS.message, (event: RelayerTypes.MessageEvent) => {
// capture any messages that arrive before the client is initialized so we can process them after initialization is complete
if (!this.initialized || this.relayMessageCache.length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a cache or a queue ? Why are we pushing the events in the cache instead of processing them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we put all messages received during init, and process them after init has finished. Otherwise it can lead to missed events by the implementing app

packages/sign-client/src/controllers/engine.ts Outdated Show resolved Hide resolved
packages/sign-client/src/controllers/engine.ts Outdated Show resolved Hide resolved
@ganchoradkov
Copy link
Member Author

ganchoradkov commented Jun 28, 2024

Are all tests passing ? Should we add unhappy path cases ?

Not sure how to slow down the client and force the behaviour thus has been manually dogfooded. Will tinker @Cali93

Copy link
Contributor

@Cali93 Cali93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -1028,6 +1029,23 @@ export class Engine extends IEngine {
return formatMessage(request, iss);
};

public processRelayMessageCache: IEngine["processRelayMessageCache"] = () => {
// process the relay messages cache in the next tick to allow event listeners to be registered by the implementing app
setTimeout(async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we use a flag like client.initialized then ?

@ganchoradkov ganchoradkov merged commit 4f27d4e into v2.0 Jul 2, 2024
9 checks passed
@ganchoradkov ganchoradkov deleted the fix/process-incoming-requests-in-order branch July 2, 2024 12:35
@ganchoradkov ganchoradkov mentioned this pull request Jul 17, 2024
20 tasks
github-merge-queue bot referenced this pull request in valora-inc/wallet Aug 13, 2024
….14.0 (#5765)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[@walletconnect/react-native-compat](https://github.com/walletconnect/walletconnect-monorepo)
| [`^2.13.3` ->
`^2.14.0`](https://renovatebot.com/diffs/npm/@walletconnect%2freact-native-compat/2.13.3/2.14.0)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/@walletconnect%2freact-native-compat/2.14.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@walletconnect%2freact-native-compat/2.14.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@walletconnect%2freact-native-compat/2.13.3/2.14.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@walletconnect%2freact-native-compat/2.13.3/2.14.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>walletconnect/walletconnect-monorepo
(@&#8203;walletconnect/react-native-compat)</summary>

###
[`v2.14.0`](https://github.com/WalletConnect/walletconnect-monorepo/releases/tag/2.14.0)

[Compare
Source](https://github.com/walletconnect/walletconnect-monorepo/compare/2.13.3...2.14.0)

##### What's Changed

- fix: update requester and responder pairing metadata upon
sessionAuthenticate approval by
[@&#8203;quetool](https://github.com/quetool) in
[https://github.com/WalletConnect/walletconnect-monorepo/pull/4614](https://github.com/WalletConnect/walletconnect-monorepo/pull/4614)
- feat: random session identifier by
[@&#8203;ganchoradkov](https://github.com/ganchoradkov) in
[https://github.com/WalletConnect/walletconnect-monorepo/pull/2454](https://github.com/WalletConnect/walletconnect-monorepo/pull/2454)
- fix: process incoming requests in order by
[@&#8203;ganchoradkov](https://github.com/ganchoradkov) in
[https://github.com/WalletConnect/walletconnect-monorepo/pull/4638](https://github.com/WalletConnect/walletconnect-monorepo/pull/4638)
- chore: parsing optional fields in formatMessage function by
[@&#8203;ignaciosantise](https://github.com/ignaciosantise) in
[https://github.com/WalletConnect/walletconnect-monorepo/pull/5114](https://github.com/WalletConnect/walletconnect-monorepo/pull/5114)
- fix: always use fallback verify server by
[@&#8203;chris13524](https://github.com/chris13524) in
[https://github.com/WalletConnect/walletconnect-monorepo/pull/5091](https://github.com/WalletConnect/walletconnect-monorepo/pull/5091)
- refactor: `.org` relay url by
[@&#8203;ganchoradkov](https://github.com/ganchoradkov) in
[https://github.com/WalletConnect/walletconnect-monorepo/pull/5151](https://github.com/WalletConnect/walletconnect-monorepo/pull/5151)
- feat: generic provider by
[@&#8203;ganchoradkov](https://github.com/ganchoradkov) in
[https://github.com/WalletConnect/walletconnect-monorepo/pull/5182](https://github.com/WalletConnect/walletconnect-monorepo/pull/5182)
- feat: verify context session authenticate by
[@&#8203;ganchoradkov](https://github.com/ganchoradkov) in
[https://github.com/WalletConnect/walletconnect-monorepo/pull/5192](https://github.com/WalletConnect/walletconnect-monorepo/pull/5192)
- refactor: reduce reconnect timeout by
[@&#8203;ganchoradkov](https://github.com/ganchoradkov) in
[https://github.com/WalletConnect/walletconnect-monorepo/pull/5194](https://github.com/WalletConnect/walletconnect-monorepo/pull/5194)

##### New Contributors

- [@&#8203;quetool](https://github.com/quetool) made their first
contribution in
[https://github.com/WalletConnect/walletconnect-monorepo/pull/4614](https://github.com/WalletConnect/walletconnect-monorepo/pull/4614)

**Full Changelog**:
WalletConnect/walletconnect-monorepo@2.13.3...2.14.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 5pm,every weekend" in timezone
America/Los_Angeles, Automerge - "after 5pm,every weekend" in timezone
America/Los_Angeles.

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View the
[repository job
log](https://developer.mend.io/github/valora-inc/wallet).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4yMC4xIiwidXBkYXRlZEluVmVyIjoiMzguMjAuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsibnBtIiwicmVub3ZhdGUiXX0=-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: valora-bot <valorabot@valoraapp.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants