-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·321 lines (264 loc) · 8.89 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env node
require('dotenv').config({ path: process.env.SECRETS_PATH || './' })
const http = require('http')
const net = require('net')
const { URL } = require('url')
const express = require('express')
const cors = require('cors')
const helmet = require('helmet')
const WebSocket = require('ws')
const expressWs = require('express-ws')
const zmq = require('zeromq')
const session = require('express-session')
const redis = require('redis')
const jayson = require('jayson/promise')
//const mariadb = require('mariadb')
const yargs = require('yargs/yargs')
const HTTP_PORT = parseInt(process.env.HTTP_PORT || 8095)
const ADDRINDEXRS_URL = new URL(process.env.ADDRINDEXRS_URL || 'tcp://localhost:8435')
const DOGEPARTY_URL = process.env.DOGEPARTY_URL || 'http://rpc:rpc@localhost:4005'
const DOGECOIN_ZMQ_URL = process.env.DOGECOIN_ZMQ_URL || 'tcp://localhost:28835'
const REDIS_URL = process.env.REDIS_URL || 'redis://localhost:6379/8'
const DEFAULT_SESSION_SECRET = 'configure this!'
const SESSION_SECRET = process.env.SESSION_SECRET || DEFAULT_SESSION_SECRET
const INTERVAL_CHECK_DOGEPARTY_PARSED = parseInt(process.env.INTERVAL_CHECK_DOGEPARTY_PARSED || '10000')
const INTERVAL_CHECK_DOGEPARTY_MEMPOOL = parseInt(process.env.INTERVAL_CHECK_DOGEPARTY_MEMPOOL || '10000')
var localMempool = [] //To detect new mempool txs on dogeparty
var firstMempoolCheck = true
var localLastBlock = -1
const xdpClient = jayson.client.http(DOGEPARTY_URL)
async function startZmq(notifiers) {
const sock = new zmq.Subscriber
const sleep = (ms) => new Promise(r => setTimeout(r, ms))
sock.connect(DOGECOIN_ZMQ_URL)
if (notifiers && notifiers.hashtx) {
sock.subscribe('hashtx')
}
if (notifiers && notifiers.hashblock) {
sock.subscribe('hashblock')
}
console.log(`ZMQ connected to ${DOGECOIN_ZMQ_URL}`)
for await (const [topic, msg] of sock) {
const topicName = topic.toString('utf8')
if (topicName === 'hashtx') {
const txid = msg.toString('hex')
notifiers.hashtx(txid)
} else if (topicName === 'hashblock') {
const blockhash = msg.toString('hex')
notifiers.hashblock(blockhash)
if (notifiers.xdp) {
setTimeout(waitForDogepartyBlock(blockhash), INTERVAL_CHECK_DOGEPARTY_PARSED)
}
}
}
}
async function waitForDogepartyBlock(notifiers) {
let found = false
let xdpInfo = await xdpClient.request('get_running_info', [])
let newLastBlock = -1
if (xdpInfo.result && xdpInfo.result.last_block && xdpInfo.result.last_block.block_index) {
newLastBlock = xdpInfo.result.last_block.block_index
if ((localLastBlock >= 0) && (newLastBlock >= 0) && (localLastBlock < newLastBlock)){
let blockIndexes = []
for (var i = localLastBlock+1;i<=newLastBlock;i++){
blockIndexes.push(i)
}
let blocks = await xdpClient.request('get_blocks', {block_indexes: blockIndexes})
let blockMessages = []
for (var nextBlockIndex in blocks.result){
var nextBlock = blocks.result[nextBlockIndex]
let nextBlockMessages = nextBlock._messages.map(x => {
try {
return {
...x,
bindings: JSON.parse(x.bindings)
}
} catch(e) {
return x
}
})
blockMessages.push(...nextBlockMessages)
}
if (blockMessages.length > 0){
notifiers.xdp(blockMessages)
}
localLastBlock = newLastBlock
} else {
localLastBlock = newLastBlock
}
}
}
async function waitForMempool(notifiers){
let found = false
let xdpMempool
while (!found) {
xdpMempoolRequest = await xdpClient.request('get_mempool', [])
if (xdpMempoolRequest.result) {
found = true
xdpMempool = xdpMempoolRequest.result
} else {
await sleep(INTERVAL_CHECK_DOGEPARTY_MEMPOOL)
}
}
//First, checks for txs in local mempool that are not longer in dogeparty mempool and remove them
var nextMempoolTxIndex = 0
while (nextMempoolTxIndex < localMempool.length){
var nextMempoolTx = localMempool[nextMempoolTxIndex]
let index = findMempoolTx(nextMempoolTx.tx_hash, localMempool)
if (index == -1){
localMempool.splice(nextMempoolTxIndex, 1)
} else {
nextMempoolTxIndex++
}
}
//Now checks for new txs in dogeparty mempool
var newMempoolTxs = []
for (var nextMempoolTxIndex in xdpMempool){
var nextMempoolTx = xdpMempool[nextMempoolTxIndex]
let index = findMempoolTx(nextMempoolTx.tx_hash, localMempool)
if (index == -1){
localMempool.push(nextMempoolTx)
newMempoolTxs.push(nextMempoolTx)
}
}
if (!firstMempoolCheck){
if (newMempoolTxs.length > 0){
notifiers.xdp(newMempoolTxs.map(x => {
try {
return {
...x,
bindings: JSON.parse(x.bindings)
}
} catch(e) {
return x
}
}))
}
} else {
firstMempoolCheck = false
}
}
function findMempoolTx(txHash, mempoolArray){
//TODO: binary search
for (var nextMempoolTxIndex in mempoolArray){
var nextMempoolTx = mempoolArray[nextMempoolTxIndex]
if (nextMempoolTx.tx_hash == txHash){
return nextMempoolTxIndex
}
}
return -1
}
async function checkParsedBlocks(notifiers){
await waitForDogepartyBlock(notifiers)
setTimeout(()=>{checkParsedBlocks(notifiers)}, INTERVAL_CHECK_DOGEPARTY_PARSED)
}
async function checkXcpMempool(notifiers){
await waitForMempool(notifiers)
setTimeout(()=>{checkXcpMempool(notifiers)}, INTERVAL_CHECK_DOGEPARTY_MEMPOOL)
}
function startServer() {
const app = express()
const redisClient = redis.createClient(REDIS_URL)
const RedisStore = require('connect-redis')(session)
//const server = http.createServer(app)
const wsInstance = expressWs(app)
if (process.env.HELMET_ON) {
app.use(helmet()) // Protect headers
}
app.use(cors()) // Allow cors
app.use(
session({
store: new RedisStore({ client: redisClient }),
secret: SESSION_SECRET,
resave: false,
})
)
app.use(express.static('static'))
app.get('/api', (req, res) => {
res.json({})
})
const notificationObservers = {
hashtx: [],
hashblock: [],
xdp: []
}
const notifiers = {
hashtx: (data) => {
//notificationObservers.hashtx.forEach(x => x(data))
},
hashblock: (data) => {
//notificationObservers.hashblock.forEach(x => x(data))
},
xdp: (data) => {
notificationObservers.xdp.forEach(x => x(data))
}
}
//const wss = new WebSocket.Server({ clientTracking: false, noServer: true })
//server.on('upgrade', function (request, socket, head) {
/*sessionParser(request, {}, () => {
// Use this code to allow only api calls that are authed
if (!request.session.userId) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
console.log('Session is parsed!');
wss.handleUpgrade(request, socket, head, function (ws) {
wss.emit('connection', ws, request);
});
});*/
/* console.log('User asking upgrade to websocket')
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit('connection', ws, request)
})
})*/
let globalId = 0
//const clients = {}
app.ws('/', (ws, request) => {
const myId = globalId++
console.log(`User ${myId} connected`)
//const userId = request.session.userId;
//clients[myId] = ws
ws.on('message', (message) => {
// no need for these rn
})
ws.on('close', () => {
//delete clients[myId]
})
})
const broadcast = (msg) => {
wsInstance.getWss().clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(msg)
}
})
}
notificationObservers.hashblock.push((data) => {
broadcast(JSON.stringify({ hashblock: data }))
})
notificationObservers.hashtx.push((data) => {
broadcast(JSON.stringify({ hashtx: data }))
})
notificationObservers.xdp.push((data) => {
broadcast(JSON.stringify({ xdp: data }))
})
//server.listen(HTTP_PORT, (err) => {
app.listen(HTTP_PORT, (err) => {
if (err) {
console.log(`Error while listening on port ${HTTP_PORT}`, err)
} else {
console.log(`Listening on port ${HTTP_PORT}`)
//setImmediate(() => startZmq(notifiers))
setImmediate(() => checkParsedBlocks(notifiers))
setImmediate(() => checkXcpMempool(notifiers))
}
})
if (SESSION_SECRET === DEFAULT_SESSION_SECRET) {
console.error(`Using default session secret "${DEFAULT_SESSION_SECRET}", This is very dangerous: pass SESSION_SECRET environment variable to modify it`)
}
}
// Yargs has built in mechanism to handle commands, but it isn't working here
const {argv} = yargs(yargs.hideBin(process.argv))
if (argv._.length > 0 && argv._[0] === 'server') {
startServer()
}