-
Notifications
You must be signed in to change notification settings - Fork 2
/
websocketWorker.js
238 lines (209 loc) · 8 KB
/
websocketWorker.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
// websocketWorker.js
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const { parentPort } = require('worker_threads');
const WebSocket = require('ws');
const axios = require('axios');
const { Tx } = require('cosmjs-types/cosmos/tx/v1beta1/tx');
import { effectedPackets, uneffectedPackets, frontRunCounter } from "./metrics.js";
import decodeMessage from './ibc_helper.js';
import db from './db.js';
parentPort.on('message', (chain) => {
startBlockListener(chain);
});
// Function to start listening for new blocks
function startBlockListener(chain) {
let wsUrl = ""
if (chain.rpcUrl.includes('https://')) {
wsUrl = 'wss://' + chain.rpcUrl.split('https://')[1] + '/websocket';
} else if (chain.rpcUrl.includes('http://')) {
wsUrl = 'ws://' + chain.rpcUrl.split('http://')[1] + '/websocket';
} else {
console.error('RPC must be provided with prefixed http:// or https://. RPC: ', chain.rpcUrl);
process.exit(1);
}
const ws = new WebSocket(wsUrl);
ws.on('open', () => {
console.log('WebSocket connection established.');
ws.send(JSON.stringify({ "jsonrpc": "2.0", "id": 1, "method": "subscribe", "params": { "query": "tm.event='NewBlock'" } }));
});
ws.on('message', async (data) => {
const parsedData = JSON.parse(data);
if (parsedData.result && parsedData.result.data && parsedData.result.data.value && parsedData.result.data.value.block) {
const blockHeight = parsedData.result.data.value.block.header.height;
const chainId = parsedData.result.data.value.block.header.chain_id;
console.log(`${chainId}: ${blockHeight}`);
await handleNewBlock(chain, blockHeight);
}
});
ws.on('close', () => {
console.log('WebSocket connection closed. Retrying...');
setTimeout(() => startBlockListener(chain), 2000);
});
ws.on('error', (error) => {
console.error('WebSocket error:', error.message);
});
}
// Save new packet and check if it already has been handled
async function savePacket(msg) {
const packetParams = [
msg.value.packet.sourceChannel,
msg.value.packet.sourcePort,
msg.value.packet.destinationChannel,
msg.value.packet.destinationPort,
msg.value.packet.sequence.low,
msg.typeUrl,
];
const query = `
SELECT packets.*, signers.signer as signer
FROM packets
LEFT JOIN signers ON packets.signer_id = signers.id
WHERE source_channel = ?
AND source_port = ?
AND destination_channel = ?
AND destination_port = ?
AND sequence = ?
AND msg_type_url = ?
`;
let existingPacket = await new Promise((resolve, reject) => {
db.get(query, packetParams, (err, row) => {
if (err) reject(err);
else resolve(row);
});
});
if (!existingPacket || !existingPacket.signer_id) {
msg.effected = true;
msg.effectedSigner = msg.value.signer;
effectedPackets.labels(
msg.chainId,
msg.value.packet.sourceChannel,
msg.value.packet.sourcePort,
msg.value.packet.destinationChannel,
msg.value.packet.destinationPort,
msg.effectedSigner,
msg.memo
).inc();
} else {
msg.effected = false;
msg.effectedSigner = existingPacket.signer;
uneffectedPackets.labels(
msg.chainId,
msg.value.packet.sourceChannel,
msg.value.packet.sourcePort,
msg.value.packet.destinationChannel,
msg.value.packet.destinationPort,
msg.value.signer,
msg.memo
).inc();
frontRunCounter.labels(
msg.chainId,
msg.value.packet.sourceChannel,
msg.value.packet.sourcePort,
msg.value.packet.destinationChannel,
msg.value.packet.destinationPort,
msg.value.signer,
msg.memo,
msg.effectedSigner
).inc();
}
return new Promise((resolve, reject) => {
db.serialize(() => {
// Save the signer if not exists
db.run('INSERT OR IGNORE INTO signers (signer) VALUES (?)', msg.value.signer, function (err) {
if (err) {
reject('Error saving signer: ' + err.message);
return;
}
// Get the signer
db.get('SELECT id FROM signers WHERE signer = ?', msg.value.signer, (err, row) => {
if (err) {
reject('Error fetching signer ID: ' + err.message);
return;
}
const signerId = row.id;
// Save the packet
db.run(
'INSERT INTO packets (chain_id, signer_id, memo, sequence, source_channel, source_port, destination_channel, destination_port, msg_type_url, created_at, effected) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, datetime("now"), ?)',
[
msg.chainId,
signerId,
msg.memo,
msg.value.packet.sequence.low,
msg.value.packet.sourceChannel,
msg.value.packet.sourcePort,
msg.value.packet.destinationChannel,
msg.value.packet.destinationPort,
msg.typeUrl,
msg.effected,
],
function (err) {
if (err) {
reject('Error saving packet: ' + err.message);
} else {
resolve(msg);
}
}
);
});
});
});
});
}
// Function to handle new blocks
async function handleNewBlock(chain, height) {
// // Prune packets older than 1 hour
// const pruneQuery = `
// DELETE FROM packets WHERE datetime(created_at) < datetime('now', '-1 hour')
// `;
// await db.run(pruneQuery);
try {
const res = await axios.get(`${chain.rpcUrl}/block?height=${height}`);
const block = res.data.result.block;
const txs = block.data.txs;
if (txs) {
for (const tx of txs) {
const buff = Buffer.from(tx, 'base64');
const transaction = Tx.decode(buff);
const msgs = transaction.body.messages;
for (let msg of msgs) {
if (msg.typeUrl.startsWith('/ibc')) {
decodeMessage(msg);
msg.chainId = block.header.chain_id
msg.memo = transaction.body.memo;
// Log decoded message
if (msg.result.includes('Undecoded')) {
console.warn(msg);
} else {
if (msg.relevant) {
try {
msg = await savePacket(msg);
const sourcePort = msg.value.packet.sourcePort;
const sourceChannel = msg.value.packet.sourceChannel;
const destinationPort = msg.value.packet.destinationPort;
const destinationChannel = msg.value.packet.destinationChannel;
const sequence = msg.value.packet.sequence.low;
const signer = msg.value.signer;
const msgTypeUrl = msg.typeUrl;
const effected = msg.effected;
const effectedSigner = msg.effectedSigner;
let isValsetUpdate = false;
console.log(`${msg.chainId} | ${sourcePort}/${sourceChannel}: ${msgTypeUrl} (${sequence}) | ${signer} | ${effected}${effected ? '' : ' | ' + msg.effectedSigner}`);
if (sourcePort == 'provider' && destinationPort == 'consumer') {
isValsetUpdate = true;
// let valsetUpdate = ValidatorSetChangePacketData.decode(msg.value.data);
// console.log(msg)
}
} catch (error) {
console.error(error);
}
}
}
}
}
}
}
}
catch (error) {
console.error(`Error at block ${height}:`, error.message);
}
}