@@ -4,7 +4,6 @@ const pull = require('pull-stream')
4
4
const lp = require ( 'pull-length-prefixed' )
5
5
const paramap = require ( 'pull-paramap' )
6
6
7
-
8
7
const { waterfall } = require ( 'neo-async' )
9
8
const { pubKeyToPeerId, pubKeyToEthereumAddress, log } = require ( '../utils' )
10
9
const BN = require ( 'bn.js' )
@@ -19,59 +18,66 @@ module.exports = (self) => (cb) => pull(
19
18
const channelId = data . key . slice ( 17 )
20
19
const { tx, restoreTx, index } = data . value
21
20
22
- self . contract . methods . channels ( channelId ) . call ( {
23
- from : pubKeyToEthereumAddress ( self . node . peerInfo . id . pubKey . marshal ( ) )
24
- } , 'latest' , ( err , channel ) => {
25
- // check whether the channel exists
26
- if ( parseInt ( channel . state ) == 0 ) {
27
- log ( self . node . peerInfo . id , `Found orphaned payment channel ${ channelId . toString ( 'hex' ) } inside database. Was the node shut down inappropriately?` )
28
- return self . deleteChannel ( channelId , cb )
29
- }
30
21
31
- if ( index . compare ( tx . index ) === 1 ) { // index > tx.index ?
32
- waterfall ( [
33
- ( cb ) => pubKeyToPeerId ( restoreTx . counterparty , cb ) ,
34
- ( peerId , cb ) => self . node . peerRouting . findPeer ( peerId , cb ) ,
35
- ( peerInfo , cb ) => self . node . dialProtocol ( peerInfo , c . PROTOCOL_SETTLE_CHANNEL , cb ) ,
36
- ] , ( err , conn ) => {
37
- if ( err )
38
- throw err
22
+ waterfall ( [
23
+ ( cb ) => self . contract . methods . channels ( channelId ) . call ( {
24
+ from : pubKeyToEthereumAddress ( self . node . peerInfo . id . pubKey . marshal ( ) )
25
+ } , 'latest' , cb ) ,
26
+ ( channel , cb ) => {
27
+ if ( parseInt ( channel . state ) == 0 ) {
28
+ log ( self . node . peerInfo . id , `Found orphaned payment channel ${ channelId . toString ( 'hex' ) } inside database. Was the node shut down inappropriately?` )
29
+ return self . deleteChannel ( channelId , cb )
30
+ }
31
+
32
+ if ( index . compare ( tx . index ) === 1 ) { // index > tx.index ?
33
+ // Ask counterparty to settle payment channel because
34
+ // last payment went to that party which means that we
35
+ // have only one signature of the last transaction.
36
+ waterfall ( [
37
+ ( cb ) => pubKeyToPeerId ( restoreTx . counterparty , cb ) ,
38
+ ( peerId , cb ) => self . node . peerRouting . findPeer ( peerId , cb ) ,
39
+ ( peerInfo , cb ) => self . node . dialProtocol ( peerInfo , c . PROTOCOL_SETTLE_CHANNEL , cb ) ,
40
+ ( conn , cb ) => {
41
+ const now = Date . now ( )
39
42
40
- const now = Date . now ( )
43
+ // TODO: Implement proper transaction handling
44
+ const timeout = setTimeout ( self . requestClose , SETTLEMENT_TIMEOUT , channelId , true )
41
45
42
- // TODO: Implement proper transaction handling
43
- const timeout = setTimeout ( self . requestClose , SETTLEMENT_TIMEOUT , channelId , true )
46
+ self . contract . once ( 'ClosedChannel' , {
47
+ topics : [ `0x${ channelId . toString ( 'hex' ) } ` ]
48
+ } , ( err ) => {
49
+ if ( err )
50
+ throw err
44
51
45
- self . contract . once ( 'ClosedChannel' , {
46
- topics : [ `0x${ channelId . toString ( 'hex' ) } ` ]
47
- } , ( err ) => {
48
- if ( err )
49
- throw err
52
+ if ( Date . now ( ) - now < SETTLEMENT_TIMEOUT ) {
53
+ // Prevent node from settling channel itself with a probably
54
+ // outdated transaction
55
+ clearTimeout ( timeout )
56
+ }
57
+ } )
50
58
51
- if ( Date . now ( ) - now < SETTLEMENT_TIMEOUT ) {
52
- // Prevent node from settling channel itself with a probably
53
- // outdated transaction
54
- clearTimeout ( timeout )
59
+ pull (
60
+ pull . once ( channelId ) ,
61
+ lp . encode ( ) ,
62
+ conn
63
+ )
55
64
}
65
+ ] , ( err ) => {
66
+ if ( err )
67
+ console . log ( err )
56
68
} )
69
+ } else {
70
+ self . requestClose ( channelId )
71
+ }
57
72
58
- pull (
59
- pull . once ( channelId ) ,
60
- lp . encode ( ) ,
61
- conn
62
- )
73
+ self . once ( `closed ${ channelId . toString ( 'base64' ) } ` , ( receivedMoney ) => {
74
+ // Callback just when the channel is settled, i.e. the closing listener
75
+ // emits the 'closed <channelId>' event.
76
+
77
+ cb ( null , receivedMoney )
63
78
} )
64
- } else {
65
- self . requestClose ( channelId )
66
79
}
67
-
68
- self . on ( `closed ${ channelId . toString ( 'base64' ) } ` , ( receivedMoney ) => {
69
- // Callback just when the channel is settled, i.e. the closing listener
70
- // emits the 'closed <channelId>' event.
71
-
72
- cb ( null , receivedMoney )
73
- } )
74
- } )
80
+ ] , cb )
75
81
} ) ,
76
82
pull . collect ( ( err , values ) => {
77
83
if ( err )
0 commit comments