@@ -7,143 +7,158 @@ const dirtyChai = require('dirty-chai')
7
7
const expect = chai . expect
8
8
const waterfall = require ( 'async/waterfall' )
9
9
const parallel = require ( 'async/parallel' )
10
- const isNode = require ( 'detect-node' )
11
10
11
+ const PeerId = require ( 'peer-id' )
12
12
const IPFS = require ( '../../src/core' )
13
- const createTempRepo = require ( '../utils/create-repo-node.js' )
13
+ const createTempRepo = require ( '../utils/create-repo-nodejs.js' )
14
+ const Factory = require ( '../utils/ipfs-factory-daemon' )
15
+ const relayConfig = require ( '../utils/ipfs-factory-daemon/default-config.json' )
14
16
15
17
chai . use ( dirtyChai )
16
18
17
- describe ( 'circuit' , ( ) => {
18
- if ( ! isNode ) {
19
- return
20
- }
21
-
22
- describe ( 'transfer over circuit' , function ( ) {
23
- this . timeout ( 50000 )
24
-
25
- let ipfsRelay
26
- let ipfsDst
27
- let ipfsSrc
28
-
29
- before ( ( done ) => {
30
- ipfsRelay = new IPFS ( {
31
- repo : createTempRepo ( ) ,
32
- config : {
33
- Addresses : {
34
- Swarm : [
35
- '/ip4/0.0.0.0/tcp/9000' ,
36
- '/ip4/0.0.0.0/tcp/9001/ws'
37
- ]
38
- } ,
39
- Bootstrap : [ ] ,
40
- Experimental : {
41
- Relay : {
42
- Circuit : {
43
- Enabled : true ,
44
- Active : false
45
- }
46
- }
47
- } ,
48
- Identity : {
49
- PeerID : 'QmUGuDXBhWKJwoNNMhnsLGSzzDfjdSt81SoTHcMu1dXBrV' ,
50
- PrivKey : 'CAASpgkwggSiAgEAAoIBAQC3ty1UbNy/0jjuJY24/1FvwnGLF6CML++iBQ6LTQdDv6u7mOFX9kzkHxI8JxQrOTLByWkXkJQ+zGKJDaLFpzmwZdWPglpWHS5LeiH7pggUPxPhZ7DkS4p9nWJbQh6h8gZ39zdpEBXWjbsqCYmgjGL0EG5pzN+dLgKnBBBTiEqv8hK9aN5uRvsFxUyhN6J9LJzgh2aGblUG/0rir2Nge1xeFj9sLeHpgPI6wCyqys+nRFJXvGNZ8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kTmbjcz1yZlaX3vYYTZOpL2KrRHDcUX0AazUS9SqJSLvFxvmouwY6wBvBov6tStAZjRJvECgYA5XjWN7tHRwEr+NoAGsSmtsqGCrHrCPdj8SOyKTXRARQKRdvJ0KBJkMmzLdmLVErYjuy9OmwdigWcTZjZ2HhrhXNR95Xepg3I5ylzuouMPZepAacjtGl4oi91rdv6z9hD4/cSB+wIfXG9o0ghgJ/vexzvxFzDnvY+8MqEa2PP6NA=='
19
+ describe ( 'circuit' , function ( ) {
20
+ this . timeout ( 20 * 1000 )
21
+
22
+ let ipfsDst
23
+ let ipfsSrc
24
+ let relayPeer
25
+ let relayAddrs
26
+ let factory = new Factory ( )
27
+
28
+ before ( ( done ) => {
29
+ ipfsDst = new IPFS ( {
30
+ repo : createTempRepo ( ) ,
31
+ start : false ,
32
+ config : {
33
+ Addresses : {
34
+ Swarm : [
35
+ '/ip4/0.0.0.0/tcp/9002'
36
+ ]
37
+ } ,
38
+ Bootstrap : [ ] ,
39
+ EXPERIMENTAL : {
40
+ Relay : {
41
+ Enabled : true
51
42
}
52
43
}
53
- } )
44
+ }
45
+ } )
54
46
55
- ipfsDst = new IPFS ( {
56
- repo : createTempRepo ( ) ,
57
- config : {
58
- Addresses : {
59
- Swarm : [
60
- '/ip4/0.0.0.0/tcp/9002'
61
- ]
62
- } ,
63
- Bootstrap : [ ]
47
+ ipfsSrc = new IPFS ( {
48
+ repo : createTempRepo ( ) ,
49
+ start : false ,
50
+ config : {
51
+ Addresses : {
52
+ Swarm : [
53
+ '/ip4/0.0.0.0/tcp/9003/ws'
54
+ ]
55
+ } ,
56
+ Bootstrap : [ ] ,
57
+ EXPERIMENTAL : {
58
+ Relay : {
59
+ Enabled : true
60
+ }
64
61
}
65
- } )
62
+ }
63
+ } )
66
64
67
- ipfsSrc = new IPFS ( {
68
- repo : createTempRepo ( ) ,
69
- config : {
70
- Addresses : {
71
- Swarm : [
72
- '/ip4/0.0.0.0/tcp/9003/ws'
73
- ]
74
- } ,
75
- Bootstrap : [ ]
76
- }
77
- } )
65
+ waterfall ( [
66
+ ( pCb ) => {
67
+ PeerId . create ( { bits : 1024 } , ( err , id ) => {
68
+ if ( err ) {
69
+ return pCb ( err )
70
+ }
78
71
72
+ const peerId = id . toJSON ( )
73
+ relayConfig . Identity . PeerID = peerId . id
74
+ relayConfig . Identity . PrivKey = peerId . privKey
75
+ relayConfig . Addresses . Swarm = [
76
+ '/ip4/127.0.0.1/tcp/61452/ws' ,
77
+ '/ip4/127.0.0.1/tcp/61453'
78
+ ]
79
+ pCb ( )
80
+ } )
81
+ } ,
82
+ ( pCb ) => {
83
+ factory . spawnNode ( createTempRepo ( ) , Object . assign ( relayConfig , {
84
+ EXPERIMENTAL : {
85
+ Relay : {
86
+ Enabled : true ,
87
+ HOP : {
88
+ Enabled : true ,
89
+ Active : false
90
+ }
91
+ }
92
+ }
93
+ } ) , ( err , node ) => {
94
+ expect ( err ) . to . not . exist ( )
95
+ relayPeer = node
96
+ pCb ( )
97
+ } )
98
+ } ,
99
+ ( pCb ) => {
100
+ relayPeer . swarm . localAddrs ( ( err , addrs ) => {
101
+ expect ( err ) . to . not . exist ( )
102
+ relayAddrs = addrs
103
+ pCb ( )
104
+ } )
105
+ } ,
106
+ ( pCb ) => ipfsSrc . start ( pCb ) ,
107
+ ( pCb ) => ipfsDst . start ( pCb )
108
+ ] , ( err ) => {
109
+ expect ( err ) . to . not . exist ( )
110
+ let addr = relayAddrs . filter ( ( a ) => ! a . toString ( ) . includes ( '/p2p-circuit' ) )
79
111
parallel ( [
80
- ( pCb ) => ipfsRelay . once ( 'start' , pCb ) ,
81
- ( pCb ) => ipfsSrc . once ( 'start' , pCb ) ,
82
- ( pCb ) => ipfsDst . once ( 'start' , pCb )
83
- ] , ( ) => {
84
- waterfall ( [
85
- ( cb ) => ipfsSrc
86
- . swarm
87
- . connect ( `/ip4/0.0.0.0/tcp/9001/ws/ipfs/QmUGuDXBhWKJwoNNMhnsLGSzzDfjdSt81SoTHcMu1dXBrV` , ( ) => cb ( ) ) ,
88
- ( cb ) => setTimeout ( cb , 1000 ) ,
89
- ( cb ) => ipfsDst
90
- . swarm
91
- . connect ( `/ip4/0.0.0.0/tcp/9000/ipfs/QmUGuDXBhWKJwoNNMhnsLGSzzDfjdSt81SoTHcMu1dXBrV` , ( ) => cb ( ) ) ,
92
- ( cb ) => setTimeout ( cb , 1000 ) ,
93
- ( cb ) => ipfsSrc . id ( cb ) ,
94
- ( id , cb ) => ipfsRelay . swarm . connect ( `/ip4/0.0.0.0/tcp/9002/ipfs/${ id . id } ` , ( ) => cb ( ) ) ,
95
- ( cb ) => setTimeout ( cb , 1000 ) ,
96
- ( cb ) => ipfsDst . id ( cb ) ,
97
- ( id , cb ) => ipfsRelay . swarm . connect ( `/ip4/0.0.0.0/tcp/9003/ws/ipfs/${ id . id } ` , ( ) => cb ( ) )
98
- ] , done )
112
+ ( cb ) => ipfsSrc . swarm . connect ( addr [ 0 ] , cb ) ,
113
+ ( cb ) => ipfsDst . swarm . connect ( addr [ 1 ] , cb )
114
+ ] , ( err ) => {
115
+ setTimeout ( ( ) => done ( err ) , 2000 )
99
116
} )
100
117
} )
118
+ } )
101
119
102
- after ( ( done ) => {
103
- waterfall ( [
104
- ( cb ) => ipfsRelay . stop ( ( ) => cb ( ) ) ,
105
- ( cb ) => ipfsSrc . stop ( ( ) => cb ( ) ) ,
106
- ( cb ) => ipfsDst . stop ( ( ) => cb ( ) )
107
- ] , done )
108
- } )
120
+ after ( ( done ) => {
121
+ waterfall ( [
122
+ ( cb ) => ipfsSrc . stop ( ( ) => cb ( ) ) ,
123
+ ( cb ) => ipfsDst . stop ( ( ) => cb ( ) ) ,
124
+ ( cb ) => factory . dismantle ( ( err ) => done ( err ) )
125
+ ] , done )
126
+ } )
109
127
110
- it ( 'should be able to connect over circuit' , ( done ) => {
111
- ipfsSrc . swarm . connect ( ipfsDst . _peerInfo , ( err , conn ) => {
112
- expect ( err ) . to . be . null ( )
113
- expect ( conn ) . to . not . be . null ( )
114
- done ( )
115
- } )
128
+ it ( 'should be able to connect over circuit' , ( done ) => {
129
+ ipfsSrc . swarm . connect ( ipfsDst . _peerInfo , ( err , conn ) => {
130
+ expect ( err ) . to . not . exist ( )
131
+ done ( )
116
132
} )
133
+ } )
117
134
118
- it ( 'should be able to transfer data over circuit' , ( done ) => {
119
- waterfall ( [
120
- // dial destination over WS /ip4/0.0.0.0/tcp/9002/ws
121
- ( cb ) => ipfsSrc . swarm . connect ( ipfsDst . _peerInfo , cb ) ,
122
- ( conn , cb ) => ipfsDst . files . add ( new ipfsDst . types . Buffer ( 'Hello world over circuit!' ) ,
123
- ( err , res ) => {
124
- expect ( err ) . to . be . null ( )
125
- expect ( res [ 0 ] ) . to . not . be . null ( )
126
- cb ( null , res [ 0 ] . hash )
127
- } ) ,
128
- ( fileHash , cb ) => ipfsSrc . files . cat ( fileHash , function ( err , stream ) {
135
+ it ( 'should be able to transfer data over circuit' , ( done ) => {
136
+ waterfall ( [
137
+ ( cb ) => ipfsDst . swarm . connect ( ipfsSrc . _peerInfo , cb ) ,
138
+ ( conn , cb ) => ipfsDst . files . add ( new ipfsDst . types . Buffer ( 'Hello world over circuit!' ) ,
139
+ ( err , res ) => {
129
140
expect ( err ) . to . be . null ( )
141
+ expect ( res [ 0 ] ) . to . not . be . null ( )
142
+ cb ( null , res [ 0 ] . hash )
143
+ } ) ,
144
+ ( hash , cb ) => ipfsSrc . files . cat ( hash , function ( err , stream ) {
145
+ expect ( err ) . to . be . null ( )
130
146
131
- var res = ''
147
+ var res = ''
132
148
133
- stream . on ( 'data' , function ( chunk ) {
134
- res += chunk . toString ( )
135
- } )
149
+ stream . on ( 'data' , function ( chunk ) {
150
+ res += chunk . toString ( )
151
+ } )
136
152
137
- stream . on ( 'error' , function ( err ) {
138
- cb ( err )
139
- } )
153
+ stream . on ( 'error' , function ( err ) {
154
+ cb ( err )
155
+ } )
140
156
141
- stream . on ( 'end' , function ( ) {
142
- expect ( res ) . to . be . equal ( 'Hello world over circuit!' )
143
- cb ( null , res )
144
- } )
157
+ stream . on ( 'end' , function ( ) {
158
+ expect ( res ) . to . be . equal ( 'Hello world over circuit!' )
159
+ cb ( null , res )
145
160
} )
146
- ] , done )
147
- } )
161
+ } )
162
+ ] , done )
148
163
} )
149
164
} )
0 commit comments