-
Notifications
You must be signed in to change notification settings - Fork 14
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
Conversation
Codecov Report
@@ 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
|
src/new/swapConnection.ts
Outdated
* 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 { |
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.
Why are we extending BaseConnection
when BaseConnection
was meant to be a ws connection with the fullnode?
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.
Good point! Opened #535 to discuss the refactoring possibilities for this, including the Wallet Service websocket that is already implemented using it.
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.
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.
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.
Refactored the AtomicSwapServiceConnection
class to extend directly from EventEmitter
on f440e3c.
src/websocket/atomic-swap.ts
Outdated
*/ | ||
onMessage(evt) { | ||
const message = JSON.parse(evt.data) | ||
const _type = message.type.split(':')[0] |
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.
Why are we splitting the type with :
?
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.
It wasn't necessary. Removed on d1d252f
src/websocket/atomic-swap.ts
Outdated
/** | ||
* Returns a JSON stringified ping message | ||
*/ | ||
getPingMessage() { |
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.
Do you have a pong in the swap service ws?
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.
Yes, we do.
src/websocket/atomic-swap.ts
Outdated
* @class | ||
* @name AtomicSwapWebSocket | ||
*/ | ||
class AtomicSwapWebSocket extends BaseWebSocket { |
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.
Is there any difference between this class and WalletWebSocket
? The only one I've seen is the type split, which could be a parameter.
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.
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?
src/new/swapConnection.ts
Outdated
* 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 { |
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.
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.
src/new/swapConnection.ts
Outdated
@@ -0,0 +1,95 @@ | |||
/** |
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.
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.
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.
Indeed, the scope of this service is greater than the one on the new
folder.
Moved it to /src/swapService
on 13bceac.
AtomicSwapServiceConnection no longer extends BaseConnection
__tests__/new/swapConnection.test.ts
Outdated
@@ -0,0 +1,108 @@ | |||
import { AtomicSwapServiceConnection } from '../../src/swapService/swapConnection'; |
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 folder of the test should change as well?
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.
Fixed on b6207ef
src/swapService/swapConnection.ts
Outdated
|
||
unsubscribeProposal(proposalId: string) { | ||
if (this.websocket) { | ||
const msg = JSON.stringify({type: 'unsubscribe_proposal', proposalId}); |
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.
const msg = JSON.stringify({type: 'unsubscribe_proposal', proposalId}); | |
const msg = JSON.stringify({ type: 'unsubscribe_proposal', proposalId }); |
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.
Fixed on f205976
Acceptance Criteria
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