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

feat: Websocket connection to the Atomic Swap Service #533

Merged
merged 16 commits into from
Jun 9, 2023

Conversation

tuliomir
Copy link
Contributor

@tuliomir tuliomir commented May 19, 2023

Acceptance Criteria

  • Establishes the connection with the Atomic Swap Service via a websocket protocol

Note: The subscribing and unsubscribing of proposals will be made at a future PR. This one interacts with the server foundation provided by the backend on HathorNetwork/hathor-atomic-swap-service#5

Security Checklist

  • Make sure you do not include new dependencies in the project unless strictly necessary and do not include dev-dependencies as production ones. More dependencies increase the possibility of one of them being hijacked and affecting us.

@tuliomir tuliomir added the enhancement New feature or request label May 19, 2023
@tuliomir tuliomir requested a review from r4mmer May 19, 2023 16:33
@tuliomir tuliomir self-assigned this May 19, 2023
@codecov
Copy link

codecov bot commented May 19, 2023

Codecov Report

Merging #533 (f205976) into dev (e0c9926) will increase coverage by 0.09%.
The diff coverage is 91.17%.

@@            Coverage Diff             @@
##              dev     #533      +/-   ##
==========================================
+ Coverage   77.57%   77.67%   +0.09%     
==========================================
  Files          66       67       +1     
  Lines        5173     5205      +32     
  Branches     1094     1101       +7     
==========================================
+ Hits         4013     4043      +30     
- Misses       1149     1151       +2     
  Partials       11       11              
Impacted Files Coverage Δ
src/connection.ts 70.58% <ø> (ø)
src/swapService/swapConnection.ts 90.32% <90.32%> (ø)
src/new/connection.ts 90.32% <100.00%> (ø)
src/websocket/index.ts 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

@tuliomir tuliomir marked this pull request as ready for review May 19, 2023 20:14
@tuliomir tuliomir requested a review from pedroferreira1 as a code owner May 19, 2023 20:14
@tuliomir tuliomir marked this pull request as draft May 19, 2023 20:14
@tuliomir tuliomir marked this pull request as ready for review May 20, 2023 00:45
* You can subscribe for the following events:
* - update-atomic-swap-proposal: Fired when the state of a listened proposal changes
**/
export class AtomicSwapServiceConnection extends BaseConnection {
Copy link
Member

Choose a reason for hiding this comment

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

Why are we extending BaseConnection when BaseConnection was meant to be a ws connection with the fullnode?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! Opened #535 to discuss the refactoring possibilities for this, including the Wallet Service websocket that is already implemented using it.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @r4mmer that it's a different use. The BaseConnection is not meant to be used with full node only but with wallet context.

I think it's bad to use this as base class, you should refactor it before merging, it's weird that you have your ConnectionParams options with network and servers, which makes no sense for the swap service.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored the AtomicSwapServiceConnection class to extend directly from EventEmitter on f440e3c.

*/
onMessage(evt) {
const message = JSON.parse(evt.data)
const _type = message.type.split(':')[0]
Copy link
Member

Choose a reason for hiding this comment

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

Why are we splitting the type with :?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It wasn't necessary. Removed on d1d252f

@tuliomir tuliomir requested a review from r4mmer May 30, 2023 14:24
/**
* Returns a JSON stringified ping message
*/
getPingMessage() {
Copy link
Member

Choose a reason for hiding this comment

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

Do you have a pong in the swap service ws?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we do.

* @class
* @name AtomicSwapWebSocket
*/
class AtomicSwapWebSocket extends BaseWebSocket {
Copy link
Member

Choose a reason for hiding this comment

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

Is there any difference between this class and WalletWebSocket? The only one I've seen is the type split, which could be a parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! I refactored all my code to use the WalletWebSocket instead, even the tests.
And since this class no longer serves only the wallet, I renamed it to GenericWebSocket on bb5f334.

Do you agree with these changes?

* You can subscribe for the following events:
* - update-atomic-swap-proposal: Fired when the state of a listened proposal changes
**/
export class AtomicSwapServiceConnection extends BaseConnection {
Copy link
Member

Choose a reason for hiding this comment

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

I agree with @r4mmer that it's a different use. The BaseConnection is not meant to be used with full node only but with wallet context.

I think it's bad to use this as base class, you should refactor it before merging, it's weird that you have your ConnectionParams options with network and servers, which makes no sense for the swap service.

@@ -0,0 +1,95 @@
/**
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the folder new is the right folder for it. I would create a new folder for swapService, or something like that, to keep the swap service files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, the scope of this service is greater than the one on the new folder.
Moved it to /src/swapService on 13bceac.

@tuliomir tuliomir requested review from r4mmer and pedroferreira1 June 7, 2023 15:41
@@ -0,0 +1,108 @@
import { AtomicSwapServiceConnection } from '../../src/swapService/swapConnection';
Copy link
Member

Choose a reason for hiding this comment

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

The folder of the test should change as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed on b6207ef


unsubscribeProposal(proposalId: string) {
if (this.websocket) {
const msg = JSON.stringify({type: 'unsubscribe_proposal', proposalId});
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const msg = JSON.stringify({type: 'unsubscribe_proposal', proposalId});
const msg = JSON.stringify({ type: 'unsubscribe_proposal', proposalId });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed on f205976

@tuliomir tuliomir merged commit 25dcebb into dev Jun 9, 2023
@tuliomir tuliomir deleted the feat/ws-atomic-swap branch June 9, 2023 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants