-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-para.ts
146 lines (137 loc) · 3.58 KB
/
check-para.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
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
import { createClient } from "@polkadot-api/substrate-client"
import { getWsProvider, WsEvent } from "polkadot-api/ws-provider/web"
import { getObservableClient } from "@polkadot-api/observable-client"
import {
catchError,
concatMap,
debounceTime,
endWith,
filter,
from,
ignoreElements,
map,
mergeMap,
Observable,
of,
race,
ReplaySubject,
startWith,
take,
timer,
} from "rxjs"
import { withPolkadotSdkCompat } from "polkadot-api/polkadot-sdk-compat"
import { withLogsRecorder } from "polkadot-api/logs-provider"
import { kusama, paseo, polkadot, type ChainConfig } from "./chains"
import { appendFileSync } from "node:fs"
const enum StateType {
OK,
OLD_SPEC,
RPC_ERROR,
TIMEOUT,
ERROR,
}
const checkEndpoint = (input: string) => {
const logs: Array<string> = []
let errors = new ReplaySubject<void>()
const daProvider = getWsProvider(input, (e) => {
if (e.type === WsEvent.CLOSE || e.type === WsEvent.ERROR) {
logs.push(
e.type === WsEvent.CLOSE ? "connection closed" : "connection error",
)
errors.next()
} else if (e.type === WsEvent.CONNECTED) {
logs.push("connected")
}
})
const client = getObservableClient(
createClient(
withPolkadotSdkCompat(
withLogsRecorder((log) => {
logs.push(log)
}, daProvider),
),
),
)
const chainHead = client.chainHead$()
const inner = race(
chainHead.runtime$.pipe(
filter(Boolean),
take(1),
map(() => StateType.OK),
),
chainHead.finalized$.pipe(
debounceTime(100),
take(3),
ignoreElements(),
endWith(StateType.OLD_SPEC),
),
errors.pipe(take(3), ignoreElements(), endWith(StateType.RPC_ERROR)),
timer(40_000).pipe(map(() => StateType.TIMEOUT)),
).pipe(
catchError(() => of(StateType.ERROR)),
map((type) => {
return { type, logs }
}),
)
const result: typeof inner = new Observable((observer) => {
const subscription = inner.subscribe(observer)
return () => {
subscription.unsubscribe()
errors.unsubscribe()
try {
chainHead.unfollow()
} catch (_) {}
client.destroy()
}
})
return result
}
const checkParachain = (input: ChainConfig) => {
return from(Object.entries(input.providers)).pipe(
mergeMap(([name, uri]) =>
checkEndpoint(uri).pipe(
map((result) => ({ chain: input, name, result })),
),
),
)
}
const icons: Record<StateType, string> = {
[StateType.ERROR]: "☠️",
[StateType.TIMEOUT]: "⏰",
[StateType.RPC_ERROR]: "⛔",
[StateType.OLD_SPEC]: "👴",
[StateType.OK]: "✅",
}
Object.entries({ polkadot, kusama, paseo }).forEach(([relayChain, chain]) => {
const file = `./results/${relayChain}.txt`
from(
chain.filter(
(x) => !x.isUnreachable && Object.keys(x.providers).length > 0,
),
)
.pipe(concatMap((x) => checkParachain(x).pipe(startWith(x.info))))
.subscribe({
next(v) {
if (typeof v === "string") {
appendFileSync(file, `\n\n-------${v.toUpperCase()}-------\n`, "utf8")
} else {
const icon = icons[v.result.type]
if (v.result.type !== StateType.OK) {
Bun.write(
`./results/wire-logs/${relayChain}/${v.chain.info}_${v.name}.logs`,
v.result.logs.join("\n"),
)
}
appendFileSync(
file,
`${icon} ${v.name} (${v.chain.providers[v.name]})\n`,
"utf8",
)
}
},
error: console.error,
complete() {
console.log("all done!", relayChain)
},
})
})