/
readAllBlocksToDatabase.js
1146 lines (1068 loc) · 32 KB
/
readAllBlocksToDatabase.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var assert = require("assert");
var Web3 = require("web3");
var web3FusionExtend = require("../../index.js");
var mysql = require("promise-mysql");
const fetch = require("node-fetch");
const CryptoJS = require("crypto-js");
const rp = require("request-promise");
let version = 1.0;
let inHere;
let counter;
let timerSet;
/* Remember to set your environment variables to run this test
e.g. CONNECT_STRING="ws://3.16.110.25:9001" DB_CONNECT_STRING="{'host':'localhost','user':'root','password':'password','database':'db1','connectionLimit':10}" node ./examples/readAllBlocksToADatabase
*/
// console.log("CONNECT_STRING ==> ", process.env.CONNECT_STRING);
// console.log("DATABASE_CONNECT ==> ", process.env.DB_CONNECT_STRING);
var dbConnect = JSON.parse(process.env.DB_CONNECT_STRING.replace(/'/g, '"'));
var web3;
var _pool;
var _masterConnection;
const INFO_ID = "INFO_ID";
const VERSION_ID = "VERSION_ID";
let glb_highestBlockOnChain;
let balancesReturned = {};
let buildTheSystem = [
{
txt: "Build Blocks",
sql:
"CREATE TABLE IF NOT EXISTS blocks (\n" +
" hash VARCHAR(68) NOT NULL UNIQUE,\n" +
" height BIGINT NOT NULL UNIQUE,\n" +
" recCreated DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
" recEdited DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
" timeStamp BIGINT UNSIGNED,\n" +
" miner VARCHAR(68),\n" +
" numberOfTransactions int,\n" +
" ticketSelected VARCHAR(128),\n" +
" block json,\n" +
" tickInfo json,\n" +
" PRIMARY KEY (hash),\n" +
" INDEX `ticketSelected` (`ticketSelected`),\n" +
" INDEX `recCreated` (`recCreated`),\n" +
" INDEX `timestamp` (`timeStamp`),\n" +
" INDEX `heightDesc` (`height` DESC),\n" +
" INDEX `miner` (`miner`),\n" +
" INDEX `minerTimeStamp` (`miner`,`timeStamp`),\n" +
" INDEX `numberOfTransactions` (`numberOfTransactions`)\n" +
") ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"
},
{
txt: "Build Transactions",
sql:
"BEGIN;\n" +
"CREATE TABLE IF NOT EXISTS transactions (\n" +
" hash VARCHAR(68) NOT NULL UNIQUE,\n" +
" height BIGINT NOT NULL,\n" +
" timeStamp BIGINT UNSIGNED,\n" +
" recCreated DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
" recEdited DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
" fromAddress VARCHAR(68),\n" +
" toAddress VARCHAR(68),\n" +
" fusionCommand VARCHAR(128),\n" +
" commandExtra VARCHAR(128),\n" +
" commandExtra2 VARCHAR(128),\n" +
" commandExtra3 VARCHAR(128),\n" +
" data text,\n" +
" transaction json,\n" +
" receipt json,\n" +
" PRIMARY KEY (hash),\n" +
" INDEX `height` (`height`),\n" +
" INDEX `heightDesc` (`height` DESC),\n" +
" INDEX `recCreated` (`recCreated`),\n" +
" INDEX `toFromAddress` (`toAddress`,`fromAddress`),\n" +
" INDEX `fromAddress` (`fromAddress`),\n" +
" INDEX `timestamp` (`timeStamp`),\n" +
" INDEX `commandExtra` (`commandExtra`),\n" +
" INDEX `commandExtra2` (`commandExtra2`),\n" +
" INDEX `commandExtra3` (`commandExtra3`),\n" +
" INDEX `toAddress` (`toAddress`),\n" +
" INDEX `fusionCommand` (`fusionCommand`,`commandExtra`)\n" +
") ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n" +
"COMMIT;"
},
{
txt: "create triggle for transaction",
sql:
// "DELIMITER ;;\n" +
"CREATE TRIGGER transactionTriggerAfterInsert AFTER INSERT ON transactions\n" +
"FOR EACH ROW\n" +
"BEGIN\n" +
"UPDATE info set transactionCount=transactionCount+1 where _id = 'INFO_ID';\n" +
"END\n"
// "DELIMITER ;\n"
},
{
txt: "Build Info Table",
sql:
"Begin;\n" +
"CREATE TABLE IF NOT EXISTS info (\n" +
" _id varchar(68) NOT NULL UNIQUE,\n" +
" lastheightProcessed BIGINT NOT NULL,\n" +
" transactionCount BIGINT NOT NULL,\n" +
" version BIGINT NOT NULL,\n" +
" recCreated DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
" recEdited DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
" PRIMARY KEY (lastheightProcessed)\n" +
") ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;" +
"INSERT IGNORE INTO info( _id, lastHeightProcessed, recCreated, recEdited)\n" +
"VALUES " +
" ( 'INFO_ID', -1, NOW(), NOW() ) \n" +
"ON DUPLICATE KEY UPDATE recEdited = NOW();\n" +
"Commit;\n"
},
{
txt: "Build Current Balance Table",
sql:
"Begin;\n" +
"CREATE TABLE IF NOT EXISTS currentBalance (\n" +
" _id varchar(68) NOT NULL UNIQUE,\n" +
" recCreated DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
" recEdited DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
" ticketsWon BIGINT(20) DEFAULT 0,\n" +
" rewardEarn DOUBLE DEFAULT 0.0,\n" +
" fsnBalance VARCHAR(40) DEFAULT 0,\n" +
" numberOfTransactions DOUBLE DEFAULT 0.0,\n" +
" san VARCHAR(32),\n" +
" assetsHeld DOUBLE DEFAULT 0.0,\n" +
" balanceInfo json,\n" +
" PRIMARY KEY (_id),\n" +
" INDEX `san` (`san`),\n" +
" INDEX `assetsHeld` (`assetsHeld`),\n" +
" INDEX `fsnBalance` (`fsnBalance`),\n" +
" INDEX `ticketsWon` (`ticketsWon`)\n" +
") ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;" +
"Commit;\n"
},
{
txt: "Build Fusion Price Table",
sql:
"Begin;\n" +
"CREATE TABLE IF NOT EXISTS priceWatch (\n" +
" _id varchar(68) NOT NULL UNIQUE,\n" +
" recCreated DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
" recEdited DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
" price DOUBLE,\n" +
" market_cap DOUBLE,\n" +
" circulating_supply DOUBLE,\n" +
" percentChange1H DOUBLE,\n" +
" percentChange24H DOUBLE,\n" +
" last_updated DATETIME,\n" +
" total_supply BIGINT(20),\n" +
" PRIMARY KEY (_id),\n" +
" index `last_updated` (`last_updated`)\n" +
") ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;" +
"Commit;\n"
}
];
var buildIndex = 0;
function createTables(resolve, reject) {
console.log("Start - " + buildTheSystem[buildIndex].txt);
_masterConnection
.query(buildTheSystem[buildIndex].sql)
.then((results, fields) => {
buildIndex += 1;
if (buildIndex === buildTheSystem.length) {
console.log("All done building DB tables");
resolve(true);
} else {
setTimeout(() => {
createTables(resolve, reject);
}, 10);
}
})
.catch(err => {
if (err.code === "ER_TRG_ALREADY_EXISTS") {
console.log("trigger already exists no problem");
buildIndex += 1;
if (buildIndex === buildTheSystem.length) {
console.log("All done building DB tables");
resolve(true);
} else {
setTimeout(() => {
createTables(resolve, reject);
}, 10);
}
return;
}
console.log("ERROR: " + buildTheSystem[buildIndex].txt, err);
reject(err);
});
}
let starttUp = true;
function keepSQLAlive() {
_isDBConnected = false;
_pool = mysql.createPool(
Object.assign({ multipleStatements: true }, dbConnect)
);
_pool
.getConnection()
.then(conn => {
_masterConnection = conn;
return new Promise((resolve, reject) => {
createTables(resolve, reject);
}).then(ret => {
_masterConnection
.query("select * from info where _id = '" + INFO_ID + "';")
.then(rows => {
if (starttUp) {
lastBlock = rows[0].lastheightProcessed; // redo last height to ensure no logic errors
starttUp = false;
}
// console.log(rows)
_isDBConnected = true;
console.log("Databsase connected!");
return { success: true };
});
});
})
.catch(err => {
starttUp = true;
console.error(
"connect to database failed, trying again in five seconds",
err,
JSON.stringify(err)
);
setTimeout(() => {
keepSQLAlive();
}, 60000);
})
.finally(() => {
console.log("Connection setup finally called");
if (_masterConnection) {
_masterConnection.release();
_masterConnection = null;
}
});
}
let lastConnectTimer;
function keepWeb3Alive() {
//debugger
lastConnectTimer = null;
console.log("STARTING WEB3 connection");
provider = new Web3.providers.WebsocketProvider(process.env.CONNECT_STRING, {
timeout: 60000,
clientConfig: {
maxReceivedFrameSize: 100000000,
maxReceivedMessageSize: 100000000
}
});
provider.on("connect", function() {
//debugger
web3._isConnected = true;
web3.fsn.enableBigIntJSONParse();
scheduleNewScan(10);
});
provider.on("error", function(err) {
//debugger
//debugger
if (provider && !provider.___disconnected) {
provider.___disconnected = true;
provider.disconnect();
provider = null;
web3._isConnected = false;
console.log("web3 connection error ", err);
console.log("will try to reconnect");
lastConnectTimer = lastConnectTimer = setTimeout(() => {
keepWeb3Alive();
}, 1000);
}
});
provider.on("end", function(err) {
//debugger
if (!lastConnectTimer) {
if (provider && !provider.___disconnected) {
provider.___disconnected = true;
try {
provider.disconnect();
} catch (e) {}
provider = null;
web3._isConnected = false;
console.log("web3 connection error ", err);
console.log("will try to reconnect");
lastConnectTimer = setTimeout(() => {
keepWeb3Alive();
}, 10000);
}
}
});
web3 = new Web3(provider);
web3._isConnected = false;
web3 = web3FusionExtend.extend(web3);
}
// startup our web3 connection
//
keepWeb3Alive();
keepSQLAlive();
let lastBlock = 1;
function queryAddTagsForInsert(q, p) {
for (let i = 0; i < p.length; i++) {
if (i > 0) {
q += ",?";
} else {
q += "?";
}
}
q += ")";
return q;
}
async function updateLastBlockProcessed() {
let conn;
try {
let now = new Date();
conn = await _pool.getConnection();
await conn.query(
"begin;" +
"update info set lastheightProcessed=" +
lastBlock +
", recEdited = ?, version = ? where _id = '" +
INFO_ID +
"';" +
"commit;",
[now, version]
);
return { success: true };
} catch (err) {
console.log("UPDATET block error ", err);
throw err;
} finally {
if (conn) {
conn.release();
}
}
}
// setup for database writing
//
async function logBlock(block, tkinfo) {
let conn;
try {
conn = await _pool.getConnection();
let query = "Insert into blocks Values(";
let now = new Date();
// " hash VARCHAR(68) NOT NULL UNIQUE,\n" +
// " height BIGINT NOT NULL UNIQUE,\n" +
// " recCreated DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
// " recEdited DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
// " timeStamp BIGINT UNSIGNED,\n" +
// " numberOfTransactions int,\n" +
// " block json,\n" +
let params = [
block.hash.toLowerCase(),
block.number,
now,
now,
block.timestamp,
block.miner,
block.transactions.length,
tkinfo.selected,
JSON.stringify(block),
JSON.stringify(tkinfo)
];
query = queryAddTagsForInsert(query, params);
let okPacket = await conn.query(query, params);
return okPacket.affectedRows === 1;
} catch (err) {
if (err.code === "ER_DUP_ENTRY") {
// block was already written
// normal when we restart scan
return true;
}
console.log("Block log error ", err);
throw err;
} finally {
if (conn) {
conn.release();
}
}
}
async function logTransactions(block) {
if (block.transactions.length === 0) {
return new Promise((resolve, reject) => {
resolve(true);
});
}
return new Promise((resolve, reject) => {
console.log(block.transactions.length + " transactions ");
logTransaction(null,block, block.transactions, 0, resolve, reject);
});
}
let gtl = { blockNumber: 0 };
async function getTransactionLog(transaction) {
// if a fusion command go and get the log for this transaction
if (transaction.blockNumber != gtl.blockNumber) {
// reset gtl
gtl = { blockNumber: transaction.blockNumber };
}
if (gtl[transaction.hash]) {
// we have this transaction already
return new Promise((resolve, reject) => {
return resolve(gtl[transaction.hash]);
});
}
let add = transaction.to ? transaction.to.toLowerCase() : null;
let from = transaction.from;
if (
add === web3.fsn.consts.FSNCallAddress ||
add === web3.fsn.consts.TicketLogAddress ||
from === web3.fsn.consts.FSNCallAddress ||
from === web3.fsn.consts.TicketLogAddress
) {
// we will need to go and get the logs for this item
return web3.eth
.getPastLogs({
address: [
web3.fsn.consts.FSNCallAddress,
web3.fsn.consts.TicketLogAddress
],
fromBlock: transaction.blockNumber,
toBlock: transaction.blockNumber
})
.then(logs => {
if (logs) {
// save cache of all logs from this block for this adddress
for (let log of logs) {
gtl[log.transactionHash] = log;
}
}
return gtl[transaction.hash];
});
} else {
// return a blank transaction
return new Promise((resolve, reject) => {
return resolve(null);
});
}
}
let balancesToGet = {};
async function getBalances(conn, addrs, index, resolve, reject) {
if (addrs.length === index) {
if ( conn && !conn.__released ) {
conn.__released = true
conn.release()
conn = null
}
resolve(true);
return;
}
// debugger
let address = addrs[index];
if (
!address ||
address === web3.fsn.consts.FSNCallAddress ||
address === web3.fsn.consts.TicketLogAddress ||
address.length === 0
) {
return getBalances(conn, addrs, index + 1, resolve, reject);
}
let all;
if (balancesReturned[address] && balancesReturned[address] > lastBlock) {
// we have this balance already
console.log("ALREADY HAVE BALANCE " + address);
getBalances(conn, addrs, index + 1, resolve, reject);
return;
}
console.log("GETTTING BALANCE " + address);
try {
let allInfo = await web3.fsn.allInfoByAddress( address )
let balances = allInfo.balances // await web3.fsn.getAllBalances(address);
let timeLockBalances = allInfo.timeLockBalances // await web3.fsn.getAllTimeLockBalances(address);
let tickets = allInfo.tickets // await web3.fsn.allTicketsByAddress(address);
let swaps = -1; // new function will need to be supported to get this info as allSwaps is depreciated await web3.fsn.allSwapsByAddress(address);
let notation = allInfo.notation // await web3.fsn.getNotation(address);
all = JSON.stringify({
balances,
timeLockBalances,
tickets,
swaps,
notation
});
if ( !conn ) {
conn = await _pool.getConnection();
}
let rows = await conn.query(
`select ((select count(*) from transactions where toAddress="${address}")+
(select count(*) from transactions where fromAddress="${address}")) as 'count(*)';`
);
let count = rows[0]["count(*)"];
let fsnBalance =
balances[
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
];
if (!fsnBalance) {
fsnBalance = "0";
}
if (fsnBalance.length < 36) {
fsnBalance = "0".repeat(36 - fsnBalance.length) + fsnBalance;
}
let assetsHeld = Object.keys(Object.assign(balances, timeLockBalances))
.length;
let sql =
`INSERT INTO currentBalance( _id, recCreated, recEdited, numberOfTransactions, assetsHeld, fsnBalance, san , balanceInfo )\n` +
`VALUES( "${address}", NOW(), NOW(), ${count}, ${assetsHeld}, '${fsnBalance}', '${notation}', '${all}' )\n` +
`ON DUPLICATE KEY UPDATE recEdited = NOW(), assetsHeld = ${assetsHeld}, fsnBalance = '${fsnBalance}', numberOfTransactions = ${count}, san = '${notation}', balanceInfo = '${all}' ;\n`;
rows = await conn.query(sql);
balancesReturned[address] = glb_highestBlockOnChain;
getBalances(conn, addrs, index + 1, resolve, reject);
} catch (err) {
if ( conn && !conn.__released ) {
conn.__released = true
conn.release()
conn = null
}
console.log(" getAllBalances error ", err);
reject(err);
} finally {
}
}
let lastAssetBlock = -1
let assets = { }
async function cachedGetAsset( block, getAssetBalance) {
// await web3.fsn.getAsset(getAssetBalance);
if ( block !== lastAssetBlock ) {
lastAssetBlock = block
assets = {}
}
if ( assets[getAssetBalance] !== undefined ) {
return assets[getAssetBalance]
}
let asset = await web3.fsn.getAsset(getAssetBalance);
assets[getAssetBalance] = asset
return asset
}
async function logTransaction( conn , block, transactions, index, resolve, reject) {
console.log(" Transaction " + index + " being proceessed");
if (index === 0) {
balancesToGet = {};
}
if (transactions.length === index) {
let keys = Object.keys(balancesToGet);
if (keys.length) {
return getBalances(conn, keys, 0, resolve, reject);
} else {
if ( conn && !conn.__released ) {
conn.__released = true
conn.release()
conn = null
}
resolve(true);
}
return;
}
if (!web3._isConnected) {
reject(new Error("web3 not connected"));
return;
}
try {
let data = await web3.fsn.getTransactionAndReceipt(transactions[index]);
let transaction = data.tx // await web3.eth.getTransaction(transactions[index]);
let receipt = data.receiptFound ? data.receipt : null // await web3.eth.getTransactionReceipt(transactions[index]);
if (!receipt) {
reject(new Error("transaction not complete, no receipt"));
return;
}
let log = receipt.logs.length > 0 ? receipt.logs[0] : undefined
if ( !conn ) {
conn = await _pool.getConnection();
}
// merge receipt and transaction
//debugger
transaction.topics =
log && log.topics && log.topics.length > 0 ? log.topics : null;
// debugger;
let query = "Insert into transactions Values(";
let now = new Date();
let fusionCommand;
let commandExtra;
let commandExtra2;
let commandExtra3;
let getAssetBalance;
let logData = null;
let jsonLogData;
let saveData = null;
if (receipt.logs.length) {
try {
saveData = web3.fsn.hex2a(receipt.logs[0].data);
jsonLogData = JSON.parse(saveData);
logData = JSON.stringify(jsonLogData);
} catch (e) {
logData = null;
saveData = null;
}
}
if (!logData && receipt.logs) {
try {
logData = JSON.stringify({ data: receipt.logs[0].data });
} catch (e) {
logData = null;
}
}
transaction.to = transaction.to ? transaction.to.toLowerCase() : "";
transaction.from = transaction.from.toLowerCase();
balancesToGet[transaction.from] = true;
if (transaction.topics) {
let topic = parseInt(transaction.topics[0].substr(2));
if (
transaction.to === web3.fsn.consts.FSNCallAddress ||
transaction.from === web3.fsn.consts.FSNCallAddress
) {
fusionCommand = web3.fsn.consts.FSNCallAddress_Topic_To_Function[topic];
} else if (
transaction.to === web3.fsn.consts.TicketLogAddress ||
transaction.from === web3.fsn.consts.TicketLogAddress
) {
fusionCommand =
web3.fsn.consts.TicketLogAddress_Topic_To_Function[topic];
balancesToGet[transaction.to] = true;
}
} else {
balancesToGet[transaction.to] = true;
}
if (jsonLogData) {
if (jsonLogData.To) {
let addTo = jsonLogData.To.toLowerCase();
balancesToGet[addTo] = true;
// we are reserving command extra 3 for To field
commandExtra3 = addTo;
}
switch (fusionCommand) {
case "AssetValueChangeFunc":
case "AssetValueChangeExtFunc":
commandExtra = jsonLogData.AssetID;
getAssetBalance = jsonLogData.AssetID;
break;
case "GenAssetFunc":
commandExtra = jsonLogData.AssetID;
commandExtra2 =
jsonLogData.Name + " (" + jsonLogData.Symbol.toUpperCase() + ")";
getAssetBalance = jsonLogData.AssetID;
break;
case "SendAssetFunc":
commandExtra = jsonLogData.AssetID;
break;
case "TimeLockFunc":
commandExtra = jsonLogData.AssetID;
fusionCommand = jsonLogData.LockType;
break;
case "BuyTicketFunc":
commandExtra = jsonLogData.Ticket;
break;
case "MakeSwapFuncExt":
case "MakeSwapFunc":
case "MakeSwapFuncExtOld":
case "MakeMultiSwapFunc":
commandExtra = jsonLogData.SwapID;
break;
case "TakeSwapFunc":
case "TakeSwapFuncExt":
case "TakeMultiSwapFunc":
commandExtra = jsonLogData.SwapID;
break;
case "RecallSwapFunc":
case "RecallMultiSwapFunc":
commandExtra = jsonLogData.SwapID;
break;
}
}
// " hash VARCHAR(68) NOT NULL UNIQUE,\n" +
// " height BIGINT NOT NULL,\n" +
// " recCreated DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
// " recEdited DATETIME DEFAULT CURRENT_TIMESTAMP,\n" +
// " fromAddress VARCHAR(68),\n" +
// " toAddress VARCHAR(68),\n" +
// " fusionCommand VARCHAR(68),\n" +
// " data json,\n"+
// " transaction json,\n" +
let params = [
transaction.hash.toLowerCase(),
transaction.blockNumber,
block.timestamp,
now,
now,
transaction.from,
transaction.to,
fusionCommand,
commandExtra,
commandExtra2,
commandExtra3,
saveData,
JSON.stringify(transaction),
JSON.stringify(receipt)
];
query = queryAddTagsForInsert(query, params);
await conn.query(query, params);
index += 1;
if (getAssetBalance) {
let asset = cachedGetAsset( block, getAssetBalance) // await web3.fsn.getAsset(getAssetBalance);
let totalSupply = asset.Total;
await conn.query(
"UPDATE transactions set commandExtra3 = ? where hash = ?;",
[totalSupply, transaction.hash.toLowerCase()]
);
logTransaction(conn,block, transactions, index, resolve, reject);
} else {
logTransaction(conn,block, transactions, index, resolve, reject);
}
} catch (err) {
if (err.code === "ER_DUP_ENTRY") {
// block was already written
// normal when we restart scan
logTransaction(conn,block, transactions, index + 1, resolve, reject);
return true;
}
if (conn && !conn.__released) {
conn.__released = true
conn.release();
conn = null;
}
console.log("transaction log error ", err);
reject(err);
} finally {
}
}
function calcReward(height) {
let i;
// initial reward 2.5
let reward = 2.5;
// every 4915200 blocks divide reward by 2
let segment = Math.floor(height / 4915200);
for (i = 0; i < segment; i++) {
reward = reward / 2;
}
return reward;
}
async function logTicketPurchased(blockNumber, tikinfo) {
let conn;
try {
conn = await _pool.getConnection();
let { selected } = tikinfo;
let tkQuery =
"select fromAddress from transactions where fusionCommand='BuyTicketFunc' and commandExtra ='" +
selected +
"';";
// console.log(tkQuery);
let rows = await conn.query(tkQuery);
try {
let address = rows[0].fromAddress;
await conn.query(
"UPDATE currentBalance set ticketsWon = ticketsWon + 1, rewardEarn = rewardEarn + ? where _id = ?;",
[calcReward(blockNumber), address]
);
} catch (e) {
//debugger
}
conn.release();
conn = null;
} finally {
if (conn) {
conn.release();
}
}
}
function callBlockScanAgain() {
if (!timerSet) {
setTimeout(() => {
timerSet = null;
resumeBlockScan();
}, 2000);
}
}
function resumeBlockScan() {
if (!web3._isConnected) {
console.log("web3 connection down returning");
callBlockScanAgain();
return;
}
if (!_isDBConnected) {
console.log("Database is not connected yet ");
callBlockScanAgain();
return;
}
updateOnlinePrice();
if (lastBlock === -1) {
lastBlock = 0;
}
if (inHere) {
console.log("...Already Processing Block");
counter++;
if (counter === 10) {
counter = 0;
inHere = false;
}
if (!timerSet) {
timeerSet = setTimeout(() => {
timerSet = null;
resumeBlockScan();
}, 50000);
}
return;
}
counter = 0;
inHere = true;
let query = `SELECT
CONCAT(z.expected, IF(z.got-1>z.expected, CONCAT(' thru ',z.got-1), '')) AS missing
FROM (
SELECT
@rownum:=@rownum+1 AS expected,
IF(@rownum=height, 0, @rownum:=height) AS got
FROM
(SELECT @rownum:=0) AS a
JOIN blocks
ORDER BY height
) AS z
WHERE z.got!=0;`;
if (process.env.FILLIN === "true") {
let releaseConn = false;
return _pool.getConnection().then(conn => {
return conn
.query(query)
.then(rows => {
conn.release();
releaseConn = true;
if (rows && rows.length > 0) {
let missing = rows[0]["missing"].split(" ")[0];
lastBlock = parseInt(missing);
console.log("filling in missing block ", lastBlock);
doBlockScan();
} else {
console.log("no missing blocks, trying again in 30 seconds");
inHere = false;
if (!timerSet) {
timerSet = setTimeout(() => {
timerSet = null;
resumeBlockScan();
}, 30000);
}
}
})
.catch(err => {
// throw err;
console.log("database error ", err);
inHere = false;
if (!timerSet) {
timerSet = setTimeout(() => {
timerSet = null;
resumeBlockScan();
}, 10);
}
})
.finally(() => {
if (!releaseConn) {
conn.release();
}
});
});
} else {
doBlockScan();
}
}
function scheduleNewScan(timeToSet) {
inHere = false;
if (!timerSet) {
timerSet = setTimeout(() => {
timerSet = null;
resumeBlockScan();
}, timeToSet || 14000);
}
}
async function doBlockScan() {
try {
let currentBlock = await web3.eth.getBlockNumber();
glb_highestBlockOnChain = currentBlock;
if (process.env.FILLIN !== "true" && currentBlock + 1 < lastBlock) {
// wait for two blocks for data to fill in
console.log("Really Waiting for new block..." + new Date());
scheduleNewScan();
return true;
}
let block = await web3.eth.getBlock(lastBlock);
if (!block) {
// wait for block to update
console.log("Waiting for new block..." + new Date());
// lets update the database to show we alive
scheduleNewScan();
return;
}
console.log("Start Block -> ", lastBlock, block.hash);
let c= web3.utils.numberToHex(lastBlock)
dbg("get snapshot ")
let jt = await web3.fsn.getSnapshot(c);
dbg("snapshot done")
await logBlock(block, jt);
dbg("log block done")
await logTransactions(block);
await logTicketPurchased(lastBlock, jt);
console.log("Did Block -> ", lastBlock, block.hash);
if (process.env.FILLIN === "true") {
scheduleNewScan(10);
return true;
}
await updateLastBlockProcessed();
lastBlock += 1;
scheduleNewScan(10);
} catch (err) {
console.log("uncaught error, try again ", err);
scheduleNewScan();
}
}
var lasttime = 0;
var inpriceget = false;
var onlineCounter = 0;
function updateOnlinePrice() {
if (!_isDBConnected) {
return // this will be called on next block
}
if (!process.env.CMC_KEY) {
return;
}
if (inpriceget) {
return;
}
if (lasttime !== 0) {
let newtime = new Date().getTime();
if (lasttime + (600 * 1000) < newtime) {
// we check every 10 minutes to keep api correct
lasttime = newtime;
} else {
return;
}
} else {
lasttime = new Date().getTime();
}
console.log("get quote");