(pool)
| 7 | var shutdown = false; |
| 8 | var p = false; |
| 9 | var findPeer = function findPeer(pool) { |
| 10 | if (pool.length == 0) { |
| 11 | console.log('No more potential peers...'); |
| 12 | return; |
| 13 | } |
| 14 | |
| 15 | console.log('Finding new peer from pool of '+pool.length+' potential peers'); |
| 16 | p = new Peer(pool.shift()); |
| 17 | |
| 18 | var connectTimeout = setTimeout(function() { // Give them a few seconds to respond, otherwise close the connection automatically |
| 19 | console.log('Peer never connected; hanging up'); |
| 20 | p.destroy(); |
| 21 | }, 5*1000); |
| 22 | connectTimeout.unref(); |
| 23 | var verackTimeout = false; |
| 24 | |
| 25 | p.on('connect', function(d) { |
| 26 | console.log('connect'); |
| 27 | clearTimeout(connectTimeout); |
| 28 | |
| 29 | // Send VERSION message |
| 30 | var m = new Message(p.magicBytes, true); |
| 31 | m.putInt32(70000); // version |
| 32 | m.putInt64(1); // services |
| 33 | m.putInt64(Math.round(new Date().getTime()/1000)); // timestamp |
| 34 | m.pad(26); // addr_me |
| 35 | m.pad(26); // addr_you |
| 36 | m.putInt64(0x1234); // nonce |
| 37 | m.putVarString('Node.js lite peer'); |
| 38 | m.putInt32(10); // start_height |
| 39 | |
| 40 | //console.log(m.raw().toString('hex')); |
| 41 | console.log('Sending VERSION message'); |
| 42 | verackTimeout = setTimeout(function() { |
| 43 | console.log('No VERACK received; disconnect'); |
| 44 | p.destroy(); |
| 45 | }, 10000); |
| 46 | verackTimeout.unref(); |
| 47 | p.once('verackMessage', function() { |
| 48 | console.log('VERACK received; this peer is active now'); |
| 49 | clearTimeout(verackTimeout); |
| 50 | }); |
| 51 | p.send('version', m.raw()); |
| 52 | }); |
| 53 | p.on('end', function(d) { |
| 54 | console.log('end'); |
| 55 | }); |
| 56 | p.on('error', function(d) { |
| 57 | console.log('error', d); |
| 58 | d.peer.destroy(); |
| 59 | }); |
| 60 | p.on('close', function(d) { |
| 61 | console.log('close', d); |
| 62 | if (shutdown === false) { |
| 63 | console.log('Connection closed, trying next...'); |
| 64 | setImmediate(function() { |
| 65 | clearTimeout(connectTimeout); |
| 66 | clearTimeout(verackTimeout); |
no outgoing calls
no test coverage detected