diff options
| author | Adam Soltys <[email protected]> | 2018-04-05 03:02:26 +0000 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2018-04-05 03:02:26 +0000 |
| commit | 5846fd948279de7e43d3d8406254286220b716e4 (patch) | |
| tree | 767eda50f9c1ae3ae86415ae2c1e6449e4014a7e | |
| parent | cc8e24b0981a4edaa626a6145df0a4593c7840b6 (diff) | |
| download | coinos-server-5846fd948279de7e43d3d8406254286220b716e4.tar.gz coinos-server-5846fd948279de7e43d3d8406254286220b716e4.zip | |
if peer can't handle multiple channels, try another peer
| -rw-r--r-- | index.js | 48 |
1 files changed, 29 insertions, 19 deletions
@@ -216,27 +216,37 @@ const l = console.log try { let amount = Math.min(req.user.balance, 16777216) - let channel = await lna.openChannel({ - node_pubkey: new Buffer(peer, 'hex'), - local_funding_amount: amount, - }) - - channel.on('data', async data => { - if (sent || !data.chan_pending) return - req.user.channelbalance += amount - req.user.balance -= amount - req.user.channel = reverse(data.chan_pending.txid).toString('hex') - await req.user.save() - res.send(data) - sent = true - }) + let openchannel = async (peer, amount) => { + let channel = await lna.openChannel({ + node_pubkey: new Buffer(peer, 'hex'), + local_funding_amount: amount, + }) + + channel.on('data', async data => { + if (sent || !data.chan_pending) return + req.user.channelbalance += amount + req.user.balance -= amount + req.user.channel = reverse(data.chan_pending.txid).toString('hex') + await req.user.save() + res.send(data) + sent = true + }) - channel.on('error', err => { - l(peer, err) - return res.status(500).send(err.message) - }) + channel.on('error', err => { + l('channel error', peer, err) + if (err.message.startsWith('Multiple')) { + busypeers.push(peer) + peer = lna.channelpeers.find(p => !busypeers.includes(p)) + return openchannel(peer, amount) + } + + return res.status(500).send(err.message) + }) + } + + openchannel(peer, amount) } catch (err) { - l(peer, err) + l('rpc error', peer, err) return res.status(500).send(err) } }) |
