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(State Channels): Ping every 10 seconds to persist connection #324

Merged
merged 50 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
077a6ff
Improve channel rpc usage
mpowaga Mar 5, 2019
6e4b3b4
Fix lint error
mpowaga Mar 5, 2019
528b6c3
Remove unreachable code
mpowaga Mar 5, 2019
cb53c32
Make sure that sign function is correctly called
mpowaga Mar 5, 2019
28d3423
Merge branch 'develop' into feature/improve-channel-tests
nduchak Mar 6, 2019
228f65d
Merge branch 'develop' into feature/channel-rpc-improvement
nduchak Mar 6, 2019
8184360
Improve error handling for update method
mpowaga Mar 6, 2019
81d4f43
Add missing channel tx serializations
mpowaga Mar 7, 2019
dbb8fa5
Add channel close solo and settle tx serialization
mpowaga Mar 12, 2019
ec9a12b
Add channel slash tx serialization
mpowaga Mar 26, 2019
e3bc811
Add proof of inclusion tx serialization
mpowaga Mar 27, 2019
27b94ba
Merge branch 'feature/channel-rpc-improvement' into feature/channel-t…
mpowaga Mar 27, 2019
83b2495
Add basic merkle patricia tree implementation
mpowaga Mar 28, 2019
6dab142
Add merkle patricia tree serialization and verify function
mpowaga Mar 29, 2019
415751e
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Mar 30, 2019
dec757b
fix(schema.js): Fix linter error
Mar 30, 2019
cf9bcc5
Improve channel tests and error handling (#276)
mpowaga Apr 1, 2019
1d2e3a6
Improve state channel params handling. Fixes #299 (#300)
mpowaga Apr 1, 2019
8e01600
Compiler improvements (#303)
nduchak Apr 1, 2019
988f089
Channel contracts (#279)
mpowaga Apr 2, 2019
28ba57e
Merge branch 'develop' into feature/channel-tx-serializations
mpowaga Apr 2, 2019
3924b8f
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 5, 2019
c58af67
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 9, 2019
a25de21
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 9, 2019
504f03b
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 9, 2019
69bcf3c
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 10, 2019
01b004a
Fix channel tests
mpowaga Apr 10, 2019
99a0730
feat(State Channels): Ping every 10 seconds to persist connection
mpowaga Apr 10, 2019
823c204
Register pong timeout after ping has been sent
mpowaga Apr 11, 2019
a97e30f
Add contract call tx serialization
mpowaga Apr 12, 2019
6822692
Add channel tx serialization
mpowaga Apr 12, 2019
fb70fb1
Add missing tree tx serializations
mpowaga Apr 12, 2019
c86bd4b
Add channel snapshot solo tx serialization
mpowaga Apr 12, 2019
182d459
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 12, 2019
5fce39c
Merge branch 'develop' into feature/channel-ping
nduchak Apr 16, 2019
01f7ef7
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 16, 2019
e843b35
Merge branch 'develop' into feature/channel-tx-serializations
nduchak Apr 16, 2019
23edb7b
feat(State Channels): Ping every 10 seconds to persist connection
mpowaga Apr 10, 2019
e3ad5a1
Register pong timeout after ping has been sent
mpowaga Apr 11, 2019
b2b87a7
Merge branch 'develop' into feature/channel-ping
mpowaga Apr 16, 2019
9ce8ef0
Merge branch 'feature/channel-ping' of https://github.com/aeternity/a…
mpowaga Apr 16, 2019
149038c
Handle unexpected messages
mpowaga Apr 16, 2019
f2a46ba
Fix lint error
mpowaga Apr 16, 2019
5531c18
Merge branch 'develop' into feature/channel-ping
nduchak Apr 17, 2019
6bdbe88
Merge branch 'fix/channel-fsm' into feature/channel-ping
mpowaga Apr 18, 2019
15832ca
Merge branch 'develop' into feature/channel-ping
nduchak Apr 18, 2019
67da9b7
Merge branch 'develop' into feature/channel-ping
mpowaga Apr 23, 2019
d4ae6d8
Merge branch 'feature/channel-ping' of https://github.com/aeternity/a…
mpowaga Apr 23, 2019
d7bccbe
Merge branch 'develop' into feature/channel-ping
nduchak Apr 24, 2019
f54dafe
Merge branch 'develop' into feature/channel-ping
nduchak Apr 24, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion es/channel/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
changeStatus,
changeState,
send,
emit
emit,
disconnect
} from './internal'
import { unpackTx } from '../tx/builder'

Expand Down Expand Up @@ -225,6 +226,7 @@ export function awaitingShutdownOnChainTx (channel, message, state) {
export function awaitingLeave (channel, message, state) {
if (message.method === 'channels.leave') {
state.resolve({ channelId: message.params.channel_id, signedTx: message.params.data.state })
disconnect(channel)
return { handler: channelClosed }
}
if (message.method === 'channels.error') {
Expand Down
13 changes: 11 additions & 2 deletions es/channel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import {
initialize,
enqueueAction,
send,
call
call,
disconnect as channelDisconnect
} from './internal'
import * as R from 'ramda'

Expand All @@ -52,6 +53,13 @@ function on (event, callback) {
eventEmitters.get(this).on(event, callback)
}

/**
* Close the connection
*/
function disconnect () {
return channelDisconnect(this)
}

/**
* Get current status
*
Expand Down Expand Up @@ -558,7 +566,8 @@ const Channel = AsyncInit.compose({
callContract,
callContractStatic,
getContractCall,
getContractState
getContractState,
disconnect
}
})

Expand Down
37 changes: 34 additions & 3 deletions es/channel/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import * as R from 'ramda'
import { pascalToSnake } from '../utils/string'
import { awaitingConnection } from './handlers'

const PING_TIMEOUT_MS = 10000
const PONG_TIMEOUT_MS = 5000

const options = new WeakMap()
const status = new WeakMap()
const state = new WeakMap()
Expand All @@ -33,6 +36,8 @@ const actionQueue = new WeakMap()
const actionQueueLocked = new WeakMap()
const sequence = new WeakMap()
const rpcCallbacks = new WeakMap()
const pingTimeoutId = new WeakMap()
const pongTimeoutId = new WeakMap()

function channelURL (url, params, endpoint = 'channel') {
const paramString = R.join('&', R.values(R.mapObjIndexed((value, key) =>
Expand Down Expand Up @@ -147,6 +152,24 @@ function call (channel, method, params) {
})
}

function ping (channel) {
const ws = websockets.get(channel)
if (ws.readyState === ws.OPEN) {
ws._connection.ping()
clearTimeout(pongTimeoutId.get(channel))
pongTimeoutId.set(channel, setTimeout(() => ws._connection.drop(), PONG_TIMEOUT_MS))
}
}

function disconnect (channel) {
const ws = websockets.get(channel)
if (ws.readyState === ws.OPEN) {
ws._connection.close()
clearTimeout(pongTimeoutId.get(channel))
clearTimeout(pingTimeoutId.get(channel))
}
}

function WebSocket (url, callbacks) {
function fireOnce (target, key, always) {
target[key] = (...args) => {
Expand Down Expand Up @@ -178,11 +201,18 @@ async function initialize (channel, channelOptions) {
eventEmitters.set(channel, new EventEmitter())
sequence.set(channel, 0)
rpcCallbacks.set(channel, new Map())
websockets.set(channel, await WebSocket(wsUrl, {
const ws = await WebSocket(wsUrl, {
onopen: () => changeStatus(channel, 'connected'),
onclose: () => changeStatus(channel, 'disconnected'),
onmessage: ({ data }) => onMessage(channel, data)
}))
})
ws._connection.on('pong', () => {
clearTimeout(pongTimeoutId.get(channel))
clearTimeout(pingTimeoutId.get(channel))
pingTimeoutId.set(channel, setTimeout(() => ping(channel), PING_TIMEOUT_MS))
})
pingTimeoutId.set(channel, setTimeout(() => ping(channel), PING_TIMEOUT_MS))
websockets.set(channel, ws)
}

export {
Expand All @@ -196,5 +226,6 @@ export {
changeState,
send,
enqueueAction,
call
call,
disconnect
}
8 changes: 7 additions & 1 deletion test/integration/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ describe('Channel', function () {
await initiator.spend('6000000000000000', await responder.address())
})

after(() => {
initiatorCh.disconnect()
responderCh.disconnect()
})

beforeEach(() => {
responderShouldRejectUpdate = false
})
Expand Down Expand Up @@ -301,7 +306,8 @@ describe('Channel', function () {
await Promise.all([waitForChannel(initiatorCh), waitForChannel(responderCh)])
sinon.assert.notCalled(initiatorSign)
sinon.assert.notCalled(responderSign)
await initiatorCh.leave()
initiatorCh.disconnect()
responderCh.disconnect()
})

it('can create a contract and accept', async () => {
Expand Down