Skip to content

Commit

Permalink
🐛 rare error cases don't lead to aborts
Browse files Browse the repository at this point in the history
🚧 reordering of messages
  • Loading branch information
Robert Kiel committed Mar 14, 2019
1 parent 044c151 commit 92fb03f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/packet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class Packet {
challenge,
message)
)

})
}

Expand Down Expand Up @@ -112,6 +111,8 @@ class Packet {
return cb(Error('General error.'))

record.currentValue = this.transaction.value
log(node.peerInfo.id, `currentValue ${(new BN(record.currentValue)).toString()}`)

record.index = this.transaction.index

return node.paymentChannels.setChannel(record, { channelId: channelId }, cb)
Expand Down
8 changes: 5 additions & 3 deletions src/paymentChannels/eventListeners/close.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = (self) => (err, event) => {
record = _record
counterparty = record.restoreTx.counterparty

const partyA = isPartyA(
partyA = isPartyA(
pubKeyToEthereumAddress(self.node.peerInfo.id.pubKey.marshal()),
pubKeyToEthereumAddress(counterparty)
)
Expand All @@ -36,7 +36,8 @@ module.exports = (self) => (err, event) => {
) {
log(self.node.peerInfo.id, `Found better transaction for payment channel ${channelId.toString('hex')}.`)

self.requestClose(channelId, cb)
self.registerSettlementListener(channelId)
self.requestClose(channelId)
} else {
cb()
}
Expand Down Expand Up @@ -81,11 +82,12 @@ module.exports = (self) => (err, event) => {
cb = receipt
receipt = null
}

const initialValue = new BN(record.restoreTx.value)
receivedMoney = partyA ? amountA.isub(initialValue) : initialValue.isub(amountA)

log(self.node.peerInfo.id, `Closed payment channel \x1b[33m${channelId.toString('hex')}\x1b[0m and ${receivedMoney.isNeg() ? 'spent' : 'received'} \x1b[35m${receivedMoney.abs().toString()} wei\x1b[0m. ${receipt ? ` TxHash \x1b[32m${receipt.transactionHash}\x1b[0m.` : ''}`)
log(self.node.peerInfo.id, `currentValue ${(new BN(record.currentValue)).toString()}`)

self.deleteChannel(channelId, cb)
}
Expand Down
7 changes: 6 additions & 1 deletion src/paymentChannels/rpc/closeChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ module.exports = (self) => (cb) => pull(
(channel, cb) => {
if (parseInt(channel.state) == 0) {
log(self.node.peerInfo.id, `Found orphaned payment channel ${channelId.toString('hex')} inside database. Was the node shut down inappropriately?`)
return self.deleteChannel(channelId, cb)
self.deleteChannel(channelId, (err) => {
if (err)
return cb(err)

return cb(null, new BN(0))
})
}

self.once(`closed ${channelId.toString('base64')}`, (receivedMoney) => {
Expand Down
19 changes: 5 additions & 14 deletions src/paymentChannels/rpc/requestClose.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
'use strict'

const { isPartyA, pubKeyToEthereumAddress, log, bufferToNumber } = require('../../utils')
const { log, bufferToNumber } = require('../../utils')
const { waterfall } = require('neo-async')
const BN = require('bn.js')

module.exports = (self) => (channelId, useRestoreTx = false) => {
if (typeof useRestoreTx === 'function') {
cb = useRestoreTx
useRestoreTx = false
}

let record, lastValue

module.exports = (self) => (channelId, useRestoreTx = false) =>
waterfall([
(cb) => self.getChannel(channelId, cb),
(_record, cb) => {
record = _record
(record, cb) => {
if (typeof record === 'function')
return cb(null, new BN('0'))

const lastTx = useRestoreTx ? record.restoreTx : record.tx

log(self.node.peerInfo.id, `Trying to close payment channel \x1b[33m${channelId.toString('hex')}\x1b[0m. Nonce is ${self.nonce}`)

lastValue = new BN(lastTx.value)
const lastValue = new BN(lastTx.value)

self.contractCall(self.contract.methods.closeChannel(
lastTx.index,
Expand All @@ -42,5 +34,4 @@ module.exports = (self) => (channelId, useRestoreTx = false) => {

self.closingRequests.add(channelId.toString('base64'))
log(self.node.peerInfo.id, `Settled channel \x1b[33m${channelId.toString('hex')}\x1b[0m with txHash \x1b[32m${receipt.transactionHash}\x1b[0m. Nonce is now \x1b[31m${self.nonce}\x1b[0m`)
})
}
})
33 changes: 17 additions & 16 deletions test/demo.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
'use strict'

const { waterfall, timesSeries, times, map } = require('neo-async')
const { waterfall, timesSeries, times, map, parallel } = require('neo-async')
const rlp = require('rlp')
const { resolve } = require('path');

const Web3 = require('web3')
const { toWei } = require('web3-utils')
const BN = require('bn.js')

const { sendTransaction, privKeyToPeerId, log, compileIfNecessary } = require('../src/utils')
const { createFundedNodes } = require('./utils')
const rlp = require('rlp')

const { NETWORK, GAS_PRICE, INFURA_WSS_URL, HARDCODED_ETH_ADDRESS, HARDCODED_PRIV_KEY, CONTRACT_ADDRESS } = require('../src/constants')

const FUNDING_ACCOUNT = HARDCODED_ETH_ADDRESS
const FUNDING_KEY = HARDCODED_PRIV_KEY

const AMOUNT_OF_NODES = 4
const AMOUNT_OF_MESSAGES = 4

const AMOUNT_OF_MESSAGES = 8

const Web3 = require('web3')
let provider, server, web3

console.log(
Expand All @@ -38,7 +40,7 @@ waterfall([
server = require('ganache-core').server({
accounts: [
{
balance: '0xd3c21bcecceda0000000',
balance: `0x${toWei(new BN(100), 'ether').toString('hex')}`,
secretKey: FUNDING_KEY
}
]
Expand All @@ -55,23 +57,22 @@ waterfall([
})
}
},
(cb) => privKeyToPeerId(FUNDING_KEY, cb),
(_fundingPeer, cb) => {
(cb) => {
web3 = new Web3(provider)
fundingPeer = _fundingPeer
web3.eth.getTransactionCount(FUNDING_ACCOUNT, cb)
},
(_index, cb) => {
index = _index

return compileIfNecessary([resolve(__dirname, '../contracts/HoprChannel.sol')], [resolve(__dirname, '../build/contracts/HoprChannel.json')], cb)
parallel({
fundingPeer: (cb) => privKeyToPeerId(FUNDING_KEY, cb),
index: (cb) => web3.eth.getTransactionCount(FUNDING_ACCOUNT, cb),
compiledContract: (cb) => compileIfNecessary([resolve(__dirname, '../contracts/HoprChannel.sol')], [resolve(__dirname, '../build/contracts/HoprChannel.json')], cb)
}, cb)
},
(_, cb) => {
(results, cb) => {
fundingPeer = results.fundingPeer
index = results.index
compiledContract = require('../build/contracts/HoprChannel.json')

if (NETWORK === 'ganache') {
sendTransaction({
to: 0,
gas: 3000333, // 2370333
gasPrice: GAS_PRICE,
nonce: index,
Expand Down
8 changes: 4 additions & 4 deletions test/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { applyEach, times, each, waterfall } = require('neo-async')
const { applyEachSeries, timesSeries, times, each, waterfall } = require('neo-async')
const { createNode } = require('../../src')
const { pubKeyToEthereumAddress, sendTransaction, privKeyToPeerId } = require('../../src/utils')
const { GAS_PRICE, STAKE_GAS_AMOUNT } = require('../../src/constants')
Expand Down Expand Up @@ -91,9 +91,9 @@ module.exports.createFundedNodes = (amountOfNodes, options, peerId, nonce, cb) =
return createNode(opts, cb)
}
], cb), cb),
(nodes, cb) => applyEach([
(nodes, cb) => applyEachSeries([
(cb) => this.warmUpNodes(nodes, cb),
(cb) => times(amountOfNodes, (n, cb) =>
(cb) => timesSeries(amountOfNodes, (n, cb) =>
sendTransaction({
from: pubKeyToEthereumAddress(peerId.pubKey.marshal()),
to: pubKeyToEthereumAddress(nodes[n].peerInfo.id.pubKey.marshal()),
Expand All @@ -114,7 +114,7 @@ module.exports.createFundedNodes = (amountOfNodes, options, peerId, nonce, cb) =

node.paymentChannels.nonce = node.paymentChannels.nonce + 1

cb()
return cb()
}), cb)
], (err) => cb(err, nodes))
], cb)
Expand Down

0 comments on commit 92fb03f

Please sign in to comment.