-
Notifications
You must be signed in to change notification settings - Fork 11
/
eosdac-processor.js
executable file
·296 lines (238 loc) · 9.67 KB
/
eosdac-processor.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
#!/usr/bin/env node
process.title = 'eosdac-processor';
const cluster = require('cluster');
const {TextDecoder, TextEncoder} = require('text-encoding');
const {Api, JsonRpc, Serialize} = require('@jafri/eosjs2');
const {DeltaHandler} = require('./handlers');
const fetch = require('node-fetch');
const MongoClient = require('mongodb').MongoClient;
const MongoLong = require('mongodb').Long;
const Amq = require('./connections/amq');
const connectMongo = require('./connections/mongo');
const Int64 = require('int64-buffer').Int64BE;
const crypto = require('crypto');
const {loadConfig} = require('./functions');
const {arrayToHex} = require('@jafri/eosjs2/dist/eosjs-serialize');
const watchers = require('./watchers');
const DacDirectory = require('./dac-directory');
const {IPC} = require('node-ipc');
class JobProcessor {
constructor() {
this.config = loadConfig();
const rpc = new JsonRpc(this.config.eos.endpoint, {fetch});
this.api = new Api({
rpc,
signatureProvider: null,
chainId: this.config.chainId,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder(),
});
this.logger = require('./connections/logger')('eosdac-processor', this.config.logger);
}
async connectDb() {
if (this.config.mongo) {
this.db = await connectMongo(this.config);
}
}
async connectAmq() {
this.amq = new Amq(this.config);
return await this.amq.init();
}
async processedActionJob(job, doc) {
this.logger.info(`Processed action job, notifying watchers`);
this.amq.ack(job);
watchers.forEach((watcher) => {
watcher.action({doc, dac_directory:this.dac_directory, db:this.db});
});
// broadcast to master, to send via ipc
try {
process.send(doc);
}
catch (e){}
}
async processActionJob(job) {
const sb = new Serialize.SerialBuffer({
textEncoder: new TextEncoder,
textDecoder: new TextDecoder,
array: new Uint8Array(job.content)
});
const block_num = new Int64(sb.getUint8Array(8)).toString();
const block_timestamp_arr = sb.getUint8Array(4);
let buffer = Buffer.from(block_timestamp_arr);
var block_timestamp_int = buffer.readUInt32BE(0);
const block_timestamp = new Date(block_timestamp_int * 1000);
const trx_id_arr = sb.getUint8Array(32);
const trx_id = arrayToHex(trx_id_arr);
const recv_sequence = new Int64(sb.getUint8Array(8)).toString();
const global_sequence = new Int64(sb.getUint8Array(8)).toString();
const account = sb.getName();
const name = sb.getName();
const data = sb.getBytes();
this.logger.info(`Process action ${block_num} ${account} ${name} ${recv_sequence} ${global_sequence}`);
const action = {account, name, data};
this.logger.info(`Deserializing action ${account}:${name}`);
let act;
try {
act = await this.api.deserializeActions([action]);
} catch (e) {
this.logger.error(`Error deserializing action data ${account}:${name} - ${e.message}`, {e});
this.amq.ack(job);
return;
}
// const authorization = act[0].authorization;
// delete act[0].authorization;
const doc = {
block_num: MongoLong.fromString(block_num),
block_timestamp,
trx_id,
action: act[0],
recv_sequence: MongoLong.fromString(recv_sequence),
global_sequence: MongoLong.fromString(global_sequence)
};
const self = this;
let dac_id = '';
if (act[0].data.dac_id){
dac_id = act[0].data.dac_id;
}
const col = this.db.collection('actions');
col.insertOne(doc).then(() => {
const action_log_meta = {action:doc.action, block_num};
if (dac_id){
action_log_meta.dac_id = dac_id;
}
this.logger.info('Action save completed', action_log_meta);
self.processedActionJob(job, doc);
}).catch((e) => {
if (e.code === 11000) { // Duplicate index
self.processedActionJob(job, doc);
} else {
this.logger.error('DB save failed :(', {e});
this.amq.then((amq) => {
amq.reject(job);
})
}
});
}
async processTransactionRow(job){
const sb = new Serialize.SerialBuffer({
textEncoder: new TextEncoder,
textDecoder: new TextDecoder,
array: new Uint8Array(job.content)
});
}
async processedContractRow(job, doc) {
this.logger.info(`Processed contract row job, notifying watchers`);
this.amq.ack(job);
watchers.forEach((watcher) => {
watcher.delta({doc, dac_directory:this.dac_directory, db:this.db});
});
// broadcast to master, to send via ipc
try {
process.send(doc);
}
catch (e){}
}
async processContractRow(job) {
const sb = new Serialize.SerialBuffer({
textEncoder: new TextEncoder,
textDecoder: new TextDecoder,
array: new Uint8Array(job.content)
});
const block_num = new Int64(sb.getUint8Array(8)).toString();
const present = sb.get();
const block_timestamp_arr = sb.getUint8Array(4);
// const block_timestamp_int = sb.getUint32();
let buffer = Buffer.from(block_timestamp_arr);
var block_timestamp_int = buffer.readUInt32BE(0);
const block_timestamp = new Date(block_timestamp_int * 1000);
sb.get(); // version
const code = sb.getName();
const scope = sb.getName();
const table = sb.getName();
const primary_key = new Int64(sb.getUint8Array(8)).toString();
const payer = sb.getName();
const data_raw = sb.getBytes();
try {
const table_type = await this.delta_handler.getTableType(code, table);
const data_sb = new Serialize.SerialBuffer({
textEncoder: new TextEncoder,
textDecoder: new TextDecoder,
array: data_raw
});
const data = table_type.deserialize(data_sb);
if (code !== 'eosio') {
// this.logger.info(`row version ${row_version}`);
// this.logger.info(`code ${code}`);
// this.logger.info(`scope ${scope}`);
// this.logger.info(`table ${table}`);
// this.logger.info(`primary_key ${primary_key}`);
// this.logger.info(`payer ${payer}`);
// // this.logger.info(`data`)
// this.logger.info(data);
this.logger.info(`Storing ${code}:${table}:${block_timestamp_int}`);
const data_hash = crypto.createHash('sha1').update(data_raw).digest('hex');
const doc = {
block_num: MongoLong.fromString(block_num),
block_timestamp,
code,
scope,
table,
primary_key: MongoLong.fromString(primary_key),
payer,
data,
data_hash,
present
};
const col = this.db.collection('contract_rows');
col.insertOne(doc).then(() => {
this.logger.info('Contract row save completed', {dac_id:scope, code, scope, table, block_num});
this.processedContractRow(job, doc);
// this.amq.ack(job);
}).catch((e) => {
if (e.code === 11000) {
// duplicate index
// this.amq.ack(job);
this.processedContractRow(job, doc);
} else {
this.logger.error('Contract rowDB save failed :(', {e});
this.amq.reject(job);
}
});
}
} catch (e) {
this.logger.error(`Error deserializing ${code}:${table} : ${e.message}`, {e});
this.amq.ack(job);
}
}
worker_message(doc){
this.ipc.server.broadcast('action', doc);
}
async start() {
await this.connectAmq();
await this.connectDb();
this.delta_handler = new DeltaHandler({config: this.config, queue: this.amq});
this.dac_directory = new DacDirectory({config: this.config, db:this.db});
await this.dac_directory.reload();
if (cluster.isMaster) {
this.logger.info(`Starting processor with ${this.config.clusterSize} threads...`);
// start ipc server that clients can subscribe to for api cache updates
this.ipc = new IPC();
this.ipc.config.appspace = this.config.ipc.appspace;
this.ipc.config.id = 'processor';
this.ipc.serve(() => {
this.logger.info(`Started IPC`);
});
this.ipc.server.start();
for (let i = 0; i < this.config.clusterSize; i++) {
const worker = cluster.fork();
worker.on('message', this.worker_message.bind(this));
}
} else {
this.amq.listen('contract_row', this.processContractRow.bind(this));
// amq.listen('generated_transaction', self.processTransactionRow.bind(self));
this.amq.listen('action', this.processActionJob.bind(this));
}
}
}
const processor = new JobProcessor();
processor.start();