forked from 21centurymotorcompany/bitsocketd
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbit.js
More file actions
195 lines (181 loc) · 6.19 KB
/
bit.js
File metadata and controls
195 lines (181 loc) · 6.19 KB
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
// Input: ZMQ
const zmq = require("zeromq")
const mingo = require("mingo")
const bcode = require("bcode")
const jq = require("bigjq")
const Agenda = require('agenda')
const defaults = {
zmq: { host: "127.0.0.1", port: 28339 },
mongo: { host: "127.0.0.1", port: 27020 }
}
/*
config := {
zmq: {
host: [host],
port: [port]
},
mongo: {
host: [host],
port: [port]
}
}
*/
var mysql = require('mysql');
function recordMempoolNotification(mysql_conn, result, str_status) {
result.out.forEach(function(out) {
if (out.e.a) {
let addSql = 'INSERT INTO notify_history(Id,txid,sub_address,create_time,status) VALUES(0,?,?,?,?)';
// console.log('result=',result)
let addSqlParams = [result['tx']['h'], out.e.a, Math.floor(new Date() / 1000), str_status];
mysql_conn.query(addSql,addSqlParams,function (err, result) {
if(err){
console.log('[INSERT ERROR] - ', err.message);
return;
}
console.log('-------INSERT------');
console.log('INSERT ID:', result);
console.log('-----------------------------------\n\n');
});
}
})
}
const init = async function(config) {
let sock = zmq.socket("sub")
let concurrency = (config.concurrency && config.concurrency.mempool ? config.concurrency.mempool : 1)
let zmqhost = ((config.zmq && config.zmq.host) ? config.zmq.host : defaults.zmq.host)
let zmqport = ((config.zmq && config.zmq.port) ? config.zmq.port : defaults.zmq.port)
let mongohost = ((config.mongo && config.mongo.host) ? config.mongo.host : defaults.mongo.host)
let mongoport = ((config.mongo && config.mongo.port) ? config.mongo.port : defaults.mongo.port)
let connections = config.connections
const agenda = new Agenda({
db: {address: mongohost + ":" + mongoport + "/agenda" },
defaultLockLimit: 1000,
maxConcurrency: 1,
})
var mysql_conn = mysql.createConnection({
host: config.mysql.host,
user: config.mysql.user,
password: config.mysql.password,
database: config.mysql.database,
});
agenda.define('SSE', (job, done) => {
let data = job.attrs.data
console.log("JOB = ", data)
let o = data.o
let tx = JSON.parse(o)
let promises = Object.keys(connections.pool).map(function(key) {
let connection = connections.pool[key]
return new Promise(function(resolve, reject) {
console.log("3. Encoding")
const encoded = bcode.encode(connection.query)
const types = encoded.q.db
if (!types || types.indexOf("u") >= 0) {
let filter = new mingo.Query(encoded.q.find)
if (filter.test(tx)) {
console.log("4. Decoding")
let decoded = bcode.decode(tx)
try {
if (encoded.r && encoded.r.f) {
console.log("5. Running")
jq.run(encoded.r.f, [decoded]).then(function(result) {
console.log("6. Finished Running")
try {
connection.res.sseSend({ type: "mempool", data: result })
recordMempoolNotification(mysql_conn, result, 'success')
} catch (e) {
/* handle error */
recordMempoolNotification(mysql_conn, result, 'fail')
}
resolve()
})
.catch(function(err) {
console.log("#############################")
console.log("## Error")
console.log(err)
console.log("## Processor:", encoded.r.f)
console.log("## Data:", [decoded])
console.log("#############################")
resolve()
})
} else {
console.log("5. No Running")
result = [decoded]
console.log("6. Finished No Running")
try {
connection.res.sseSend({ type: "mempool", data: result })
recordMempoolNotification(mysql_conn, result, 'success')
} catch (e) {
/* handle error */
recordMempoolNotification(mysql_conn, result, 'fail')
}
resolve()
}
} catch (e) {
console.log("Error", e)
resolve()
}
} else {
resolve()
}
} else {
resolve()
}
})
})
Promise.all(promises).then(function() {
console.log("## Finished", o)
done()
})
})
await agenda.start()
console.log("Job queue started")
sock.connect("tcp://" + zmqhost + ":" + zmqport)
sock.subscribe("mempool")
sock.subscribe("block")
sock.on("message", function(topic, message) {
let type = topic.toString()
let o = message.toString()
switch (type) {
case "mempool": {
console.log("1. Mempool")
agenda.now("SSE", { o: o })
break
}
case "block": {
let block = JSON.parse(o)
Object.keys(connections.pool).forEach(async function(key) {
let connection = connections.pool[key]
const encoded = bcode.encode(connection.query)
const types = encoded.q.db
if (!types || types.indexOf("c") >= 0) {
let filter = new mingo.Query(encoded.q.find)
let filtered = block.txs.filter(function(tx) {
return filter.test(tx)
})
let transformed = []
for(let i=0; i<filtered.length; i++) {
let tx = filtered[i]
let decoded = bcode.decode(tx)
let result
try {
if (encoded.r && encoded.r.f) {
result = await jq.run(encoded.r.f, [decoded])
} else {
result = decoded
}
transformed.push(result)
} catch (e) {
console.log("Error", e)
}
}
connection.res.sseSend({
type: "block", index: block.i, data: transformed
})
}
})
break
}
}
})
}
module.exports = { init: init }