-
Notifications
You must be signed in to change notification settings - Fork 1
/
liveness-scan.ts
47 lines (38 loc) · 1.83 KB
/
liveness-scan.ts
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
/// @title Automatically scan for liveness issues
import dotenv from 'dotenv';
import EventSource from 'eventsource';
import axios from 'axios';
import { utils } from 'ethers';
dotenv.config();
const K2_REPORTER_SERVER_URL = `http://localhost:${process.env.PORT}`;
const BEACON_URL = process.env.BEACON_URL;
const FINALITY_EVENTS = '/eth/v1/events?topics=finalized_checkpoint';
const FULL_CONSENSUS_LAYER_URL = `${BEACON_URL}${FINALITY_EVENTS}`;
async function checkLiveness(slot: number, debtor: string = '') {
// Debtor is an optional parameter in the event the middleware is appointed for more than one SBP
let debtorQuery = ''
if (debtor !== '') {
try {
// Check the debtor address is valid before generating the query
const debtorAddress = utils.getAddress(debtor)
debtorQuery = `&debtor=${debtorAddress}`
} catch (e: any) { /* Do nothing - there will just be no debtor query */ }
}
const { data } = await axios.get(`${K2_REPORTER_SERVER_URL}/liveness?slot=${slot}${debtorQuery}`)
console.log('Liveness result', data)
}
async function main() {
console.log('Starting up...')
checkLiveness(5) // For demo purposes, slot 5 is a demo slot where liveness issues will be found
// Use websockets to listen to the occurrence of new epochs as a trigger for checking liveness events
let es = new EventSource(FULL_CONSENSUS_LAYER_URL, { headers: { Accept: "text/event-stream" } })
es.addEventListener("finalized_checkpoint", async function(e) {
const dataParsed = JSON.parse(e.data)
const currentEpoch = Number(dataParsed.epoch)
const finalizedEpoch = currentEpoch - 2
const finalizedSlot = finalizedEpoch * 32
console.log(`Event listener triggered for slot ${finalizedSlot}`)
checkLiveness(finalizedSlot)
})
}
main();