-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
110 lines (96 loc) · 2.74 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
const express = require('express');
const dotenv = require('dotenv');
dotenv.config();
const client = require('./connection/bitcoin-core').client;
const app = express();
const query = require('./db/queries');
const root = require('./routes/root');
const PORT = process.env.PORT || 8080;
let height = 0;
/** @function
* @name findOpReturn
* @param {Object[]} vout
* @returns {String|Boolean} returns OP_RETURN Hex or false
*
* @description Search the vout array for OP_RETURN and return hex value or false.
*/
function findOpReturn(vout){
if(vout.length > 0){
for(let i = 0; i < vout.length; i++){
if(vout[i].scriptPubKey && vout[i].scriptPubKey.asm.includes('OP_RETURN')){
return vout[i].scriptPubKey.asm.split(" ")[1];
}
}
}
return false;
}
/** @function
* @name checkBlockChainInfo
* @returns {Void}
*
* @description Checks the blockchain information and update the height value. Self start in 10 minutes incase of error or no new update. Start dataProcessing in case of updates.
*/
async function checkBlockChainInfo(){
try{
let blockChainInformation = await client.getBlockchainInformation();
if(height < blockChainInformation.pruneheight){
height = blockChainInformation.pruneheight;
}
if(height >= blockChainInformation.blocks){
setTimeout(checkBlockChainInfo, 600000);
}else{
dataProcessing();
}
}catch(error){
console.log("error",error);
setTimeout(checkBlockChainInfo, 600000);
}
}
/** @function
* @name dataProcessing
* @param {String} hash
* @returns {Void}
*
* @description Get block transactions list from hash. Use findOpReturn function to find the OP_RETURN HEX and addEntry function to make an entry to the database.
*/
async function dataProcessing(hash){
try{
let detail_hash = null;
if(hash){
detail_hash = hash;
}else{
let block_stats = await client.command([{method:'getblockstats', parameters:[height]}]);
detail_hash = block_stats[0].blockhash;
}
let block_details = await client.getBlockByHash(detail_hash);
for(let i = 0; i < block_details.tx.length; i++){
let OP_RETURN = findOpReturn(block_details.tx[i].vout);
if(OP_RETURN){
query.addEntry(OP_RETURN,block_details.tx[i].hash, block_details.hash);
}
}
height = block_details.height;
if(block_details.nextblockhash){
dataProcessing(block_details.nextblockhash);
}else{
checkBlockChainInfo();
}
}catch(error){console.log(error)
checkBlockChainInfo();
}
}
const start = async () => {
try{
let table = await query.createTableIfNotExist();
let index = await query.createIndexForOPReturn();
app.use('/',root);
checkBlockChainInfo();
app.listen(PORT,()=>{
console.log(`server started on port ${PORT}`);
})
}catch(error){
console.log(error);
process.exit();
}
}
start();