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

switch to websockets #358

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

switch to websockets #358

wants to merge 11 commits into from

Conversation

panleone
Copy link
Member

@panleone panleone commented Apr 26, 2024

Abstract

Replace the legacy HTTP requests based system with a websocket implementation.


Testing

At the moment this is testable only on mainnet (Duddino's testnet explorer needs to be updated still).
What I suggest for testing:

  • Activate debug mode, so you can see more logs from the console
  • Try to sync your mainnet wallet from zero. How fast it is? Is it faster than the current HTTP version?
  • Test transaction creation, promo codes, etc... (basically everything involving network requests)
  • Try to change explorer from duddino to zkbitcoin or viceversa. verify that nothing gets broken.
  • Finally keep the wallet open for a long period of time (let's say 1 hour). From console see how many times the connection got closed and verify that network is still working

@panleone panleone added the Enhancement New feature or request label Apr 26, 2024
@panleone panleone self-assigned this Apr 26, 2024
Copy link

netlify bot commented Apr 26, 2024

Deploy Preview for cheery-moxie-4f1121 ready!

Name Link
🔨 Latest commit 01b1aa5
🔍 Latest deploy log https://app.netlify.com/sites/cheery-moxie-4f1121/deploys/6646776599c6d50008e02c39
😎 Deploy Preview https://deploy-preview-358--cheery-moxie-4f1121.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@JSKitty JSKitty added Awaiting Review This PR and/or issue is awaiting reviews before continuing. Review Reward: 20 PIV Reviewers of this Pull Request will receive a 20 PIV reward labels May 7, 2024
@@ -107,20 +107,159 @@ export class Network {
}
}

/**
*
*/
export class ExplorerNetwork extends Network {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should keep both classes, for explorers that may not support websockets, or just to give the option to users

scripts/network.js Outdated Show resolved Hide resolved
Comment on lines +194 to +204
async awaitWebSocketStatus(status, errMessage) {
for (let i = 0; i < 100; i++) {
if (this.ws.readyState === status) {
break;
}
await sleep(100);
}
if (this.ws.readyState !== status) {
throw new Error(errMessage);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Use onopen event

Comment on lines +247 to +286
async sendAndWaitForAnswer(method, params) {
let attempt = 1;
const maxAttempts = 10;
// milliseconds
const maxAwaitTime = 600 * 1000;
const frequency = 100;
while (attempt <= maxAttempts) {
let receivedInvalidAnswer = false;
const [id, ws_that_sent] = await this.send(method, params);
for (let i = 0; i < Math.floor(maxAwaitTime / frequency); i++) {
const res = this.cachedResults[id];
if (res !== undefined) {
delete this.cachedResults[id];
if (res.error) {
await sleep(1000);
receivedInvalidAnswer = true;
break;
}
return res;
}
await sleep(frequency);
// If connection got closed while sending do not wait until timeout
if (ws_that_sent !== this.ws) {
break;
}
}
debugWarn(
DebugTopics.NET,
'Failed send attempt for ' + method,
'attempt ' +
attempt +
'/' +
maxAttempts +
' received an invalid answer: ' +
receivedInvalidAnswer
);
attempt += 1;
}
throw new Error('Failed to communicate with the explorer');
}
Copy link
Member

Choose a reason for hiding this comment

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

Instead of making heavy use of sleep, this should use some form of observer/event and be refactored in another class

let attempt = 1;
const maxAttempts = 10;
// milliseconds
const maxAwaitTime = 600 * 1000;
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't have a maxAwaitTime, we should only abort if the websocket closes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting Review This PR and/or issue is awaiting reviews before continuing. Enhancement New feature or request Review Reward: 20 PIV Reviewers of this Pull Request will receive a 20 PIV reward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants