-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
130 lines (102 loc) · 3.75 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
const {get, post} = require('./fetch');
class NodeWatcher {
constructor(parsers, options){
this.scanInterval = null;
this.headInterval = null;
this.fetchingBlocks = [];
this.headBlock = 0;
this.lastHostIndex = 0;
this.paused = false;
this.endpoints = options.endpoints || [];
this.currentBlock = this.startingBlock = options.startingBlock || 1;
this.interval = options.interval || 10;
this.blockTracker = options.blockTracker;
if(this.endpoints.constructor !== Array) throw new Error("Endpoints must be an array.");
if(!this.endpoints.length) throw new Error("You must specify at least one endpoint.");
if(!this.endpoints.every(x => typeof x === 'string')) throw new Error("All endpoints must be strings.");
this.parserKeys = Object.keys(parsers);
this.parserKeys.map(key => this[key] = parsers[key]);
}
async start(){
if(this.paused) return this.paused = false;
this.setHeadBlock(await this.get('chain/get_info').catch(err => { throw new Error(err); }));
if(this.currentBlock === 0) this.currentBlock = this.headBlock;
if(this.currentBlock < 0) this.currentBlock = this.headBlock + this.currentBlock;
this.watch();
this.watchHead();
}
async pause(){
this.paused = true;
}
nextHost(){
if(this.endpoints.length === 1) return this.endpoints[0];
const nextIndex = this.lastHostIndex+1;
if(typeof this.endpoints[nextIndex] === "undefined") this.lastHostIndex = 0;
else this.lastHostIndex = nextIndex;
return this.endpoints[nextIndex];
}
get(path){
return get(`${this.nextHost()}/v1/${path}`)
}
post(path, data){
return post(`${this.nextHost()}/v1/${path}`, data)
}
setHeadBlock(res){
if(!res) return;
this.headBlock = parseInt(res.head_block_num);
}
async parseBlock(blockNum, tries = 0){
if(blockNum > this.currentBlock) return;
if(this.fetchingBlocks.includes(blockNum)) return;
if(tries >= 5) return console.error("Failed to fetch block " + blockNum);
this.fetchingBlocks.push(blockNum);
const die = () => {
this.fetchingBlocks = this.fetchingBlocks.filter(x => x !== blockNum);
return new Promise(r => setTimeout(() => r(this.parseBlock(blockNum, tries++)), 1000));
}
const blockData = await this.post(`chain/get_block`, {block_num_or_id:blockNum}).catch(() => null);
if(!blockData) return die();
if(typeof this.blockTracker === "function") this.blockTracker(blockNum, this.headBlock);
const {transactions, timestamp, block_num, id} = blockData;
if(!transactions) return die();
if(transactions.length){
transactions.map(x => {
if(!x.trx.hasOwnProperty('transaction')) return;
if(!x.trx.transaction.hasOwnProperty('actions')) return;
if(!x.trx.transaction.actions.length) return;
x.trx.transaction.actions.map(act => {
const parserKeys = ['*', `*::${act.name}`, `${act.account}::*`, `${act.account}::${act.name}`]
const parsers = parserKeys.filter(x => typeof this[x] === 'function');
parsers.map(parser => {
this[parser]({
cpu_usage:x.cpu_usage_us,
transaction_id:x.trx.id,
data:act.data,
contract:act.account,
action:act.name,
timestamp,
block_num,
});
})
})
})
}
this.fetchingBlocks = this.fetchingBlocks.filter(x => x !== blockNum);
return true;
}
async watch(){
clearTimeout(this.scanInterval);
const iterate = () => setTimeout(() => this.watch(), this.headBlock - this.currentBlock < 0 ? 500 : this.interval);
if(this.currentBlock > this.headBlock || this.paused) return iterate();
this.parseBlock(this.currentBlock);
this.currentBlock++;
iterate();
}
watchHead(){
clearInterval(this.headInterval);
this.headInterval = setInterval(async() => {
this.setHeadBlock(await this.get('chain/get_info').catch(() => null));
}, 500);
}
}
module.exports = NodeWatcher;