-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
548 lines (450 loc) · 18.4 KB
/
server.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
const request = require('request');
const util = require('util');
require("./config.js")();
var database = require('./database');
var alias_database= new database.alias_database();
var events = require('events');
var eventEmitter = new events.EventEmitter();
let username = cnf_username;
let password = cnf_password;
let walletHost = "http://" + cnf_host + ":" + cnf_port;
var counter=0;
var current_block_height=0;
var read_block_height=cnf_read_block_height;
var process_read_blocks=false;
var process_rewind_blocks=false;
var accept_new_syncing_wallet=false;
var process_syncing_to_wallets_array={};
var startup=true;
var compare_blocks=[];//
var orphan_read_start=-1;
var orphan_read_current=-1;
var orphan_read_end=-1;
var rewind_list=[];
//mempool
var global_mempool_reading=false;
var global_mempool_ids=[];
var global_mempool_output_list=[];
var global_mempool_output_list_string="";
var global_mempool_i=0;
var global_mempool_j=0;
//currencies/Alias
var alias_prices=null;
//Create an event handler:
var mainloop = async function () {
//sync
if(startup){
startup=false;
var db_height=await alias_database.get_current_db_blockheight();
console.log(db_height);
if(db_height!=undefined){
read_block_height=db_height.blockheight+1; // +1 do not read twice
console.log(read_block_height);
rewind_list= await alias_database.get_rewinds(); //set rewind array
//initial rewind check
process_read_blocks=false;
process_rewind_blocks= true;
orphan_read_start = read_block_height - 1000 - 1;
orphan_read_current = orphan_read_start;
orphan_read_end = read_block_height - 1;
rewind_blocks_check();
}
}
if(Object.keys(process_syncing_to_wallets_array).length>0){
setTimeout(function(){
eventEmitter.emit('next');
},500);
return;
}else{
request_rpc("getblockcount",null,"getblockcount");
}
//main running every 15 sec
console.log("log main: "+new Date().toLocaleString());
setTimeout(function(){
eventEmitter.emit('next');
},15000);
}
//Assign the event handler to an event:
eventEmitter.on('next', mainloop);
//Fire the 'scream' event:
eventEmitter.emit('next');
//mempool read interval
var read_raw_mempool = function (error, data) {
if(error){
console.error("Could not read mempool!");
global_mempool_reading=false;
return;
}
try {
// console.log("read_raw_mempool: ", data);
var j_dat = JSON.parse(data);
global_mempool_ids=j_dat.result;
// console.log(global_mempool_ids);
global_mempool_i=0;
global_mempool_j=0;
global_mempool_output_list=[];
if(global_mempool_ids!=null && global_mempool_i<global_mempool_ids.length){
request_rpc("gettxout",[global_mempool_ids[global_mempool_i],global_mempool_j,true],"event_read_next_mempool_output");
}
else{
global_mempool_reading=false;
}
} catch (e) {
console.error(e);
global_mempool_reading=false;
}
}
var read_next_mempool_output = function (error, data) {
try {
if (error) {
console.error("Could not read mempool OUTPUT" + global_mempool_i + "/" + global_mempool_j + "!");
global_mempool_reading = false;
return;
}
var j_dat = JSON.parse(data);
var res=j_dat.result;
// console.log(res);
if(res!=null){
res.tx=global_mempool_ids[global_mempool_i];
res.num=global_mempool_j;
if (res.value == 0 && res.scriptPubKey.hex.startsWith("6a026e706a") && global_mempool_output_list.length>0){
if(global_mempool_output_list[global_mempool_output_list.length-1].scriptPubKey.addresses!=undefined){
res.scriptPubKey.addresses=global_mempool_output_list[global_mempool_output_list.length-1].scriptPubKey.addresses;}
}
global_mempool_output_list.push(res);
global_mempool_j++;
}
else{
global_mempool_i++;
global_mempool_j=0;
}
if (global_mempool_i < global_mempool_ids.length) {
request_rpc("gettxout",[global_mempool_ids[global_mempool_i],global_mempool_j,true],"event_read_next_mempool_output");
} else {
// console.log("global_mempool_output_list:\n",global_mempool_output_list);
global_mempool_output_list_string=JSON.stringify(global_mempool_output_list);
global_mempool_reading = false;
}
} catch (e) {
console.error(e);
global_mempool_reading = false;
}
}
eventEmitter.on('event_read_raw_mempool', read_raw_mempool);
eventEmitter.on('event_read_next_mempool_output', read_next_mempool_output);
//mempool interval
setInterval(async function(){
if(!global_mempool_reading && !process_read_blocks && !process_rewind_blocks){
global_mempool_reading=true;
request_rpc("getrawmempool",null,"event_read_raw_mempool");
}
},500);
//
if (cnf_get_alias_prices) {
set_alias_prices();
setInterval(async function () {
set_alias_prices();
}, (180 * 1000));
}
//**********************************************
//start the server for light wallet
const io = require('socket.io')(3000,{pingTimeout: 20000, pingInterval: 2500});
io.on('connection', socket => {
var address_list=[];
//this will be replaced by the POW Ddos token when switched to TOR
var socket_id = socket.id;
// console.log(socket);
console.log('New connection ID: ' + socket_id);
// either with send()
socket.send('You\'re connected to the Aliwa server.');
// handle the event sent with socket.send()
socket.on('message', (data) => {
console.log(data);
});
// handle the event sent with socket.emit()
socket.on('sync_from', async (from,list,last_rewind,sync_id) => {
address_list=list;
if(accept_new_syncing_wallet){
process_syncing_to_wallets_array[socket_id]=1; //server can not sync while array not empty
setTimeout(function(){
if(process_syncing_to_wallets_array[socket_id]!=undefined){
delete process_syncing_to_wallets_array[socket_id];} //guarantee deleting after 5 seconds
}
,5000);
var result={};
if(last_rewind==null || last_rewind==undefined){last_rewind={time:0};}
if(last_rewind.time < ((Date.now()/1000))- (179*24*3600)){ //last sync/rewind older than 180 days --> sync from 0
result = await get_tx_data_by_addresses(0,list);
result.from=0;
result.to=read_block_height-1;
if(rewind_list!=null && rewind_list.length>0){
result.last_rewind=rewind_list[rewind_list.length-1];}
else{
result.last_rewind={time:(Date.now()/1000),block_height:read_block_height-1};
}
}
else{
var new_rewind={time:last_rewind.time,block_height:last_rewind.block_height};
var found_lower=false;
for(var i=rewind_list.length-1; i>=0 && rewind_list[i].time>new_rewind.time; i--){
if(rewind_list[i].block_height <= from){
new_rewind.block_height=rewind_list[i].block_height;
found_lower=true;
}
}
result = await get_tx_data_by_addresses((found_lower ? new_rewind.block_height : (from+1)),list);
result.from=(found_lower ? new_rewind.block_height : (from+1));
result.to=read_block_height-1;
if(rewind_list!=null && rewind_list.length>0){
result.last_rewind=rewind_list[rewind_list.length-1];}
else{
result.last_rewind={time:(Date.now()/1000),block_height:read_block_height-1};
}
}
result.sync_id=sync_id;
result.alias_prices=alias_prices;
result.server_donation_address=cnf_donation_address;
result.server_mode=cnf_server_mode;
socket.emit("server_respond_sync_data",result);
}
else{
console.log("not accepted - waiting for server sync");
socket.emit("server_respond_sync_data",{message:"wait for syncing"});
}
//delete to allow server sync again
if(process_syncing_to_wallets_array[socket_id]!=undefined){
delete process_syncing_to_wallets_array[socket_id];}
});
var last_mempool_string="";
var mempool_interval= setInterval(async function(){
//global mempool changed?
if (last_mempool_string != global_mempool_output_list_string) {
last_mempool_string = global_mempool_output_list_string;
var mempool_output = [];
for (var i = 0; i < global_mempool_output_list.length; i++) {
if (global_mempool_output_list[i].scriptPubKey != undefined && global_mempool_output_list[i].scriptPubKey != null)
{
// console.log(global_mempool_output_list[i].scriptPubKey.addresses);
if (global_mempool_output_list[i].scriptPubKey.addresses != undefined && global_mempool_output_list[i].scriptPubKey.addresses != null && global_mempool_output_list[i].scriptPubKey.addresses.length > 0)
{
for (var j = 0, len = address_list.length; j < len; j++) {
if (address_list[j] == global_mempool_output_list[i].scriptPubKey.addresses[0]) {
mempool_output.push(global_mempool_output_list[i]);
}
}
}
}
}
if(mempool_output.length>0){
// console.log("mempool_output:\n",mempool_output);
socket.emit("server_mempool_txs", {message: mempool_output});
}
}
// console.log("mempool push @ "+new Date().toLocaleString());
},1500);
socket.on("disconnect",function(){
clearInterval(mempool_interval);
address_list=undefined;
console.log("client ["+socket_id+"] disconnected");
});
socket.on("send_raw_tx",function(raw_tx,tx_object){
console.log("NEW TRANSACTION INCOMING: "+raw_tx);
request_rpc("sendrawtransaction",raw_tx,"socket_event",socket,tx_object);
});
});
//functions
/*
* ***********************************
*/
function request_rpc(method,params,event,socket,socket_data) {
let options = {
url: walletHost,
method: "post",
headers:
{
"content-type": "application/json"
},
auth: {
user: username,
pass: password
},
body: JSON.stringify({"jsonrpc": "1.0", "id": (method + "_"), "method": method, "params": (params != null ? (Array.isArray(params) ? params : [params]) : [])})
};
request(options, (error, response, body) => {
if (error) {
console.error("request method: " + method+" | "+params);
console.error('An error has occurred: ', error);
if(socket!=undefined){
socket.emit("server_respond_send_raw_tx",{message:false});
return;
}
eventEmitter.emit(event,true,error);
process.exit();
} else {
// console.log("request method: " + method+" | "+params);
// console.log('Post successful: body: ', body);
if(socket!=undefined){
socket.emit("server_respond_send_raw_tx",{message:body,data:socket_data});
return;
}
eventEmitter.emit(event,false,body);
// console.log('Post successful: response: ', response);
}
});
}
function set_alias_prices() {
let options = {
url: "https://api.coingecko.com/api/v3/simple/price?ids=spectrecoin&vs_currencies=btc%2Ceth%2Cltc%2Cbch%2Cbnb%2Ceos%2Cxrp%2Cxlm%2Clink%2Cdot%2Cyfi%2Cusd%2Caed%2Cars%2Caud%2Cbdt%2Cbhd%2Cbmd%2Cbrl%2Ccad%2Cchf%2Cclp%2Ccny%2Cczk%2Cdkk%2Ceur%2Cgbp%2Chkd%2Chuf%2Cidr%2Cils%2Cinr%2Cjpy%2Ckrw%2Ckwd%2Clkr%2Cmmk%2Cmxn%2Cmyr%2Cngn%2Cnok%2Cnzd%2Cphp%2Cpkr%2Cpln%2Crub%2Csar%2Csek%2Csgd%2Cthb%2Ctry%2Ctwd%2Cuah%2Cvef%2Cvnd%2Czar%2Cxdr%2Cxag%2Cxau%2Cbits%2Csats",
method: "get",
headers:
{
"content-type": "application/json"
}
};
request(options, (error, response, body) => {
if (error) {
console.error('An error has occurred: ', error);
} else {
console.log("set alias_prices",body);
var result=JSON.parse(body);
if(result.spectrecoin!=null){alias_prices=result.spectrecoin;}
}
});
}
var set_current_blockheight= function(error,data){
if(error){
console.error("can't get current blockheight!");
return;
}
var obj=JSON.parse(data);
// console.log(obj.result);
if(current_block_height!=obj.result){
console.log("set_current_blockheight:"+obj.result);}
current_block_height=obj.result;
if(process_read_blocks==false && read_block_height<=current_block_height && process_rewind_blocks==false){
process_read_blocks=true;
accept_new_syncing_wallet=false;
request_rpc("getblockbynumber",[read_block_height,true],"getblockbynumber");
}else if(process_read_blocks==false && read_block_height>current_block_height && process_rewind_blocks==false)
{accept_new_syncing_wallet=true;}
};
eventEmitter.on('getblockcount', set_current_blockheight);
var read_block = function (error,data){
if(error){
console.error("can't read block!");
console.error(error);
return;
}
var obj=JSON.parse(data);
if(obj.result.height%100==0){console.log("read block ("+obj.result.height+"):");}
write_block(obj.result);
// console.log(util.inspect(obj.result, {showHidden: false, depth: null}));
// console.log("++++++++++++++++++++++++++++++++++++++");
// for(var i=0;i<obj.result.tx.length;i++){
// console.log("TX--------- "+i);
// console.log(util.inspect(obj.result.tx[i], {showHidden: false, depth: null}));
// console.log("***************************************************");
// }
};
var fill_compare = function(error,data){
if(error){
console.error("can't read block for orphan scan!");
console.error(error);
return;
}
var obj=JSON.parse(data);
// if(obj.result.height%100==0){console.log("read block ("+obj.result.height+"):");}
compare_blocks.push({height:obj.result.height,hash:obj.result.hash});
orphan_read_current++;
rewind_blocks_check();
}
eventEmitter.on('getblockbynumber', read_block);
eventEmitter.on('fill_compare_blocks', fill_compare);
async function write_block(data){
//write to DB ....
//async write_block (hash,height,time,num_transactions,difficulty,flags)
var write_result=await alias_database.write_blocks(data.height,data.hash,data.time,data.tx,data.difficulty,data.flags,(read_block_height==current_block_height));
// console.log(write_result);
//read more if not synced yet
read_block_height++;
if(read_block_height<=current_block_height){
request_rpc("getblockbynumber",[read_block_height,true],"getblockbynumber");}
else{
process_rewind_blocks=true;
process_read_blocks=false;
orphan_read_start=current_block_height-50; //only check the last 50 blocks for orphans on every new block to save time -> if the 50ths block back is still orphaned it will rewind in steps of 1000 until equal
orphan_read_current=orphan_read_start;
orphan_read_end=current_block_height;
rewind_blocks_check();
}
}
async function rewind_blocks_check(){
//don't rewind bewlow 0
if(orphan_read_current<0){
compare_blocks=[];
orphan_read_start= -1;
orphan_read_current = -1;
orphan_read_end = -1;
process_rewind_blocks=false;
accept_new_syncing_wallet=true;
return;
}
if(orphan_read_current<=orphan_read_end){
request_rpc("getblockbynumber",[orphan_read_current,true],"fill_compare_blocks");
}
else{
// console.log("rewind_blocks_check() "+orphan_read_start+" -> "+orphan_read_end);
var read_list=[];
for(var i=orphan_read_start;i<=orphan_read_end;i++){
read_list.push(i);
}
var rpc_block_list=await get_blockhash_by_blockheight(read_list);
// console.log(rpc_block_list.length);
for(var i=0,len=compare_blocks.length;i<len;i++){
if(compare_blocks[i].hash!=rpc_block_list[i].blockhash)
{
if(i==0){ //rewind more
orphan_read_start=orphan_read_start-1000;
orphan_read_current=orphan_read_start;
orphan_read_end=orphan_read_end-1000;
rewind_blocks_check();
return;
}
else{
console.log("FOUND ORPHAN BLOCK --> rewind to "+compare_blocks[i].height);
await alias_database.rewind_blocks(compare_blocks[i].height); // delete blocks and add new rewind point
rewind_list= await alias_database.get_rewinds(); //update rewind array
setTimeout(function(){
process_rewind_blocks=false;
read_block_height=compare_blocks[i].height;
compare_blocks=[];
orphan_read_start=-1;
orphan_read_current=-1;
orphan_read_end=-1;
//sync from new start
process_read_blocks=true;
request_rpc("getblockbynumber",[read_block_height,true],"getblockbynumber");
},1501); // --> time of rewind must be a unique utc second
return;
}
}
}
//if no more orphans found-->
compare_blocks=[];
orphan_read_start= -1;
orphan_read_current = -1;
orphan_read_end = -1;
process_rewind_blocks=false;
accept_new_syncing_wallet=true;
}
}
async function get_tx_data_by_addresses(from,list){
console.log("-------------get_tx_data_by_addresses");
var result= await alias_database.get_tx_data_by_addresses(from,list);
return result;
}
async function get_blockhash_by_blockheight(list){
console.log("-------------get_blockhash_by_blockheight");
var result= await alias_database.get_blockhash_by_blockheight(list);
return result;
}