-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
170 lines (137 loc) · 4.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const leveldb = require('level')
const async = require('async')
const semaphore = require('semaphore')
const vapUtils = require('vaporyjs-util')
const BN = vapUtils.BN
const Blockchain = require('vaporyjs-blockchain')
const Block = require('vaporyjs-block')
const Network = require('@vaporyjs/devp2p')
const genesisHash = require('vapory-common').genesisHash.v.slice(2)
const VapPeerManager = require('devp2p-vap')
const SyncManager = require('./syncManager.js')
var db = leveldb('./blockchaindb')
var blockchain = new Blockchain(db, false)
var self = this
var port = 30304
doTheTango()
function doTheTango(){
var network = new Network({
address: '0.0.0.0',
publicIp: '127.0.0.1',
port: port,
// peerDB: peerDB,
secretKey: new Buffer('a153387bcc66f16b6aeaed404d3c3e2ec04f3a85c6942e9de107fb8b2f71e322', 'hex'),
capabilities: {
vap: 61
}
})
var syncManager = new SyncManager(blockchain)
network.on('connection', function (peer) {
var stream = peer.createStream()
var head
var headDetails
var readyLock = semaphore(1)
stream.on('end', function () {
console.log('stream ended')
})
var peerMan = new VapPeerManager(stream)
setupPeerHandlers()
// initialize
// lock until initialization complete
readyLock.take(function(){
async.series([
getHead,
getDetails,
], function () {
var td = new Buffer(new BN(headDetails.td).toArray())
console.log( 'peerMan.sendStatus:', td, head.hash() )
// process.exit()
peerMan.sendStatus(new Buffer([01]), td, head.hash(), new Buffer(genesisHash, 'hex'))
readyLock.leave()
})
})
function setupPeerHandlers(){
peerMan.on('status', function (status) {
// wait until intialization is complete before processing
console.log('got status');
readyLock.take(function(){
readyLock.leave()
console.log('prcoessing status');
//dissconect if wrong genesis
if (genesisHash !== status.genesisHash.toString('hex')) {
peer.sendDisconnect(peer.DISCONNECT_REASONS.SUBPROTOCOL_REASON)
} else {
console.log('td: ' + status.td.toString('hex'));
console.log('headDetails: ' + headDetails);
syncManager.sync(headDetails.td, head.header.number, peerMan, function(){
console.log('done syncing');
})
}
})
})
peerMan.on('blockHashesFromNumber', function (data) {
console.log(data.startNumber.toString('hex'));
console.log('exiting....');
process.exit()
})
}
function getHead(done) {
console.log('getHead - before')
blockchain.getHead(function (err, h) {
console.log('getHead - after')
head = h
console.log('blockchain.meta:')
console.log(blockchain.meta)
done()
})
}
function getDetails(done) {
console.log('getDetails - before')
blockchain.getDetails(head.hash(), function (err, d) {
console.log('getDetails - after', d)
headDetails = d
done()
})
}
})
async.series([
function (done) {
network.listen(port, '0.0.0.0', done)
},
function (done) {
network.connect({
address: '127.0.0.1',
port: 30303
}, done)
}
])
network.on('disconnect', function (dis) {
console.log('networking', 'dissconect: ' + dis.reason)
})
var dpt = network.dpt
dpt.socket.on('message', function (msg, rinfo) {
console.log('server got msg from ' + rinfo.address + ":" + rinfo.port)
})
dpt.on('ping', function (ping, peer) {
console.log('got ping ---- ')
})
dpt.on('pong', function (pong, peer) {
console.log('got pong----')
})
dpt.on('findNode', function (findNode, peer) {
console.log('findNode----')
// console.log(findNode.id.toString('hex'))
})
dpt.on('neighbors', function (neighbors, peer) {
console.log('neighbors----')
// neighbors.forEach(function(n) {
// console.log('adding: ' + n.id.toString('hex'))
// })
})
dpt.on('newPeer', function (peer) {
console.log('newPeer')
})
dpt.on('error', function () {
console.log('eeeerorrr')
})
}