-
Notifications
You must be signed in to change notification settings - Fork 10
/
service.ts
274 lines (221 loc) · 7.16 KB
/
service.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import bls from '@chainsafe/bls'
import { decrypt } from '@chainsafe/bls-keystore'
import { ssz } from '@lodestar/types'
import { fromHex, toHexString } from '@lodestar/utils'
import { DOMAIN_VOLUNTARY_EXIT } from '@lodestar/params'
import { computeDomain, computeSigningRoot } from '@lodestar/state-transition'
import { encryptedMessageDTO, exitOrEthDoExitDTO } from './dto.js'
import type { LoggerService } from 'lido-nanolib'
import type { LocalFileReaderService } from '../local-file-reader/service.js'
import type { ConsensusApiService } from '../consensus-api/service.js'
import type { ConfigService } from '../config/service.js'
import type { MetricsService } from '../prom/service.js'
import type { S3StoreService } from '../s3-store/service.js'
import type { GsStoreService } from '../gs-store/service.js'
type ExitMessage = {
message: {
epoch: string
validator_index: string
}
signature: string
}
type EthDoExitMessage = {
exit: ExitMessage
fork_version: string
}
export type MessagesProcessorService = ReturnType<typeof makeMessagesProcessor>
export const makeMessagesProcessor = ({
logger,
config,
localFileReader,
consensusApi,
metrics,
s3Service,
gsService,
}: {
logger: LoggerService
config: ConfigService
localFileReader: LocalFileReaderService
consensusApi: ConsensusApiService
metrics: MetricsService
s3Service: S3StoreService
gsService: GsStoreService
}) => {
const load = async () => {
if (!config.MESSAGES_LOCATION) {
logger.debug('Skipping loading messages in webhook mode')
return []
}
logger.info(`Loading messages from ${config.MESSAGES_LOCATION}`)
const folder = await readFolder(config.MESSAGES_LOCATION)
const messages: ExitMessage[] = []
logger.info('Parsing loaded messages')
for (const [ix, file] of folder.entries()) {
logger.info(`${ix + 1}/${folder.length}`)
let json: Record<string, unknown>
try {
json = JSON.parse(file)
} catch (error) {
logger.warn(`Unparseable JSON in file ${file}`, error)
metrics.exitMessages.inc({
valid: 'false',
})
continue
}
if ('crypto' in json) {
try {
json = await decryptMessage(json)
} catch (e) {
logger.warn(`Unable to decrypt encrypted file: ${file}`)
metrics.exitMessages.inc({
valid: 'false',
})
continue
}
}
let validated: ExitMessage | EthDoExitMessage
try {
validated = exitOrEthDoExitDTO(json)
} catch (e) {
logger.error(`${file} failed validation:`, e)
metrics.exitMessages.inc({
valid: 'false',
})
continue
}
const message = 'exit' in validated ? validated.exit : validated
messages.push(message)
}
logger.info(`Loaded ${messages.length} messages`)
return messages
}
const decryptMessage = async (input: Record<string, unknown>) => {
if (!config.MESSAGES_PASSWORD) {
throw new Error('Password was not supplied')
}
const checked = encryptedMessageDTO(input)
const content = await decrypt(checked, config.MESSAGES_PASSWORD)
const stringed = new TextDecoder().decode(content)
let json: Record<string, unknown>
try {
json = JSON.parse(stringed)
} catch {
throw new Error('Unparseable JSON after decryption')
}
return json
}
const verify = async (messages: ExitMessage[]): Promise<ExitMessage[]> => {
if (!config.MESSAGES_LOCATION) {
logger.debug('Skipping messages validation in webhook mode')
return []
}
logger.info('Validating messages')
const genesis = await consensusApi.genesis()
const state = await consensusApi.state()
const validMessages: ExitMessage[] = []
for (const [ix, m] of messages.entries()) {
logger.info(`${ix + 1}/${messages.length}`)
const { message, signature: rawSignature } = m
const { validator_index: validatorIndex, epoch } = message
let validatorInfo: { pubKey: string; isExiting: boolean }
try {
validatorInfo = await consensusApi.validatorInfo(validatorIndex)
} catch (e) {
logger.error(
`Failed to get validator info for index ${validatorIndex}`,
e
)
metrics.exitMessages.inc({
valid: 'false',
})
continue
}
if (validatorInfo.isExiting) {
logger.debug(`${validatorInfo.pubKey} exiting(ed), skipping validation`)
metrics.exitMessages.inc({
valid: 'false',
})
continue
}
const pubKey = fromHex(validatorInfo.pubKey)
const signature = fromHex(rawSignature)
const GENESIS_VALIDATORS_ROOT = fromHex(genesis.genesis_validators_root)
const CURRENT_FORK = fromHex(state.current_version)
const PREVIOUS_FORK = fromHex(state.previous_version)
const verifyFork = (fork: Uint8Array) => {
const domain = computeDomain(
DOMAIN_VOLUNTARY_EXIT,
fork,
GENESIS_VALIDATORS_ROOT
)
const parsedExit = {
epoch: parseInt(epoch, 10),
validatorIndex: parseInt(validatorIndex, 10),
}
const signingRoot = computeSigningRoot(
ssz.phase0.VoluntaryExit,
parsedExit,
domain
)
const isValid = bls.verify(pubKey, signingRoot, signature)
logger.debug(
`Singature ${
isValid ? 'valid' : 'invalid'
} for validator ${validatorIndex} for fork ${toHexString(fork)}`
)
return isValid
}
let isValid = false
isValid = verifyFork(CURRENT_FORK)
if (!isValid) isValid = verifyFork(PREVIOUS_FORK)
if (!isValid) {
logger.error(`Invalid signature for validator ${validatorIndex}`)
metrics.exitMessages.inc({
valid: 'false',
})
continue
}
validMessages.push(m)
metrics.exitMessages.inc({
valid: 'true',
})
}
logger.info('Finished validation', { validAmount: validMessages.length })
return validMessages
}
const exit = async (
messages: ExitMessage[],
event: { validatorPubkey: string; validatorIndex: string }
) => {
const message = messages.find(
(msg) => msg.message.validator_index === event.validatorIndex
)
if (!message) {
logger.error(
'Validator needs to be exited but required message was not found / accessible!'
)
metrics.exitActions.inc({ result: 'error' })
return
}
try {
await consensusApi.exitRequest(message)
logger.info(
'Voluntary exit message sent successfully to Consensus Layer',
event
)
metrics.exitActions.inc({ result: 'success' })
} catch (e) {
logger.error(
'Failed to send out exit message',
e instanceof Error ? e.message : e
)
metrics.exitActions.inc({ result: 'error' })
}
}
const readFolder = async (uri: string): Promise<string[]> => {
if (uri.startsWith('s3://')) return s3Service.read(uri)
if (uri.startsWith('gs://')) return gsService.read(uri)
return localFileReader.readFilesFromFolder(uri)
}
return { load, verify, exit }
}