-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbit.js
108 lines (108 loc) · 3.62 KB
/
bit.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
// Input: ZMQ
const zmq = require("zeromq")
const mingo = require("mingo")
const bcode = require("fountainhead-bcode")
const jq = require("fountainhead-bigjq")
const BigNumber = require('bignumber.js')
const defaults = { host: "127.0.0.1", port: 28339 }
const init = function(config) {
let sock = zmq.socket("sub")
let host = (config.host ? config.host : defaults.host)
let port = (config.port ? config.port : defaults.port)
let connections = config.connections
sock.connect("tcp://" + host + ":" + port)
sock.subscribe("mempool")
sock.subscribe("mempool-slp-genesis")
sock.subscribe("mempool-slp-send")
sock.subscribe("mempool-slp-mint")
sock.subscribe("block")
sock.subscribe("block-slp-genesis")
sock.subscribe("block-slp-send")
sock.subscribe("block-slp-mint")
sock.on("message", async function(topic, message) {
let type = topic.toString()
let o = message.toString()
if (config.verbose) {
console.log(message);
}
// TODO change this to be more efficient when schema finalized
function convert_numberdecimal_to_string(o) {
for (const i in o) {
if (o[i] !== null && typeof(o[i]) === "object") {
if (o[i].hasOwnProperty('$numberDecimal')) {
o[i] = new BigNumber(o[i]['$numberDecimal'].toString()).toFixed()
}
convert_numberdecimal_to_string(o[i])
}
}
}
switch (type) {
case "mempool": {
let tx = JSON.parse(o)
console.log(tx)
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("u") >= 0) {
let filter = new mingo.Query(encoded.q.find)
if (filter.test(tx)) {
let decoded = bcode.decode(tx)
convert_numberdecimal_to_string(decoded)
let result
try {
if (encoded.r && encoded.r.f) {
result = await jq.run(encoded.r.f, [decoded])
} else {
result = [decoded]
}
} catch (e) {
console.log("Error", e)
}
connection.res.sseSend({ type: type, data: result })
}
}
})
break
}
case "block": {
let block = JSON.parse(o)
console.log(block)
Object.keys(connections.pool).forEach(async function(key) {
let connection = connections.pool[key]
const encoded = bcode.encode(connection.query)
console.log(encoded)
const types = encoded.q.db
if (!types || types.indexOf("c") >= 0) {
let filter = new mingo.Query(encoded.q.find)
let filtered = block.txns.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)
convert_numberdecimal_to_string(decoded)
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: type, index: block.i, data: transformed
})
}
})
break
}
}
})
}
module.exports = { init: init }