-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevaluate.js
458 lines (405 loc) · 14.1 KB
/
evaluate.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import * as Sentry from '@sentry/node'
import createDebug from 'debug'
import assert from 'node:assert'
import { getRandomnessForSparkRound } from './drand-client.js'
import { updatePublicStats } from './public-stats.js'
import { buildRetrievalStats, getTaskId, recordCommitteeSizes } from './retrieval-stats.js'
import { getTasksAllowedForStations } from './tasker.js'
import { groupMeasurementsToCommittees } from './committee.js'
import pRetry from 'p-retry'
/** @import {Measurement} from './preprocess.js' */
const debug = createDebug('spark:evaluate')
export const MAX_SCORE = 1_000_000_000_000_000n
export const MAX_SET_SCORES_PARTICIPANTS = 700
export const REQUIRED_COMMITTEE_SIZE = 30
export class SetScoresBucket {
constructor () {
this.participants = []
this.scores = []
}
add (participant, score) {
this.participants.push(participant)
this.scores.push(score)
}
get size () {
return this.participants.length
}
}
export const createSetScoresBuckets = participants => {
const buckets = [new SetScoresBucket()]
for (const [participant, score] of Object.entries(participants)) {
let currentBucket = buckets[buckets.length - 1]
if (currentBucket.size === MAX_SET_SCORES_PARTICIPANTS) {
currentBucket = new SetScoresBucket()
buckets.push(currentBucket)
}
currentBucket.add(participant, score)
}
return buckets
}
/**
* @param {object} args
* @param {import('./round.js').RoundData} args.round
* @param {bigint} args.roundIndex
* @param {number} [args.requiredCommitteeSize]
* @param {any} args.ieContractWithSigner
* @param {import('./spark-api.js').fetchRoundDetails} args.fetchRoundDetails,
* @param {import('./typings.js').RecordTelemetryFn} args.recordTelemetry
* @param {import('./typings.js').CreatePgClient} [args.createPgClient]
* @param {Pick<Console, 'log' | 'error'>} args.logger
* @param {import('cancel-stuck-transactions').StuckTransactionsCanceller} args.stuckTransactionsCanceller
*/
export const evaluate = async ({
round,
roundIndex,
requiredCommitteeSize,
ieContractWithSigner,
fetchRoundDetails,
recordTelemetry,
createPgClient,
logger,
stuckTransactionsCanceller
}) => {
requiredCommitteeSize ??= REQUIRED_COMMITTEE_SIZE
// Get measurements
/** @type {Measurement[]} */
const measurements = round.measurements || []
// Detect fraud
const sparkRoundDetails = await fetchRoundDetails(await ieContractWithSigner.getAddress(), roundIndex, recordTelemetry)
// Omit the roundDetails object from the format string to get nicer formatting
debug('ROUND DETAILS for round=%s', roundIndex, sparkRoundDetails)
const started = Date.now()
const { committees } = await runFraudDetection({
roundIndex,
measurements,
sparkRoundDetails,
requiredCommitteeSize,
logger
})
const honestMeasurements = measurements.filter(m => m.fraudAssessment === 'OK')
// Calculate reward shares
const participants = {}
let sum = 0n
for (const measurement of honestMeasurements) {
if (!participants[measurement.participantAddress]) {
participants[measurement.participantAddress] = 0n
}
participants[measurement.participantAddress] += 1n
}
for (const [participantAddress, participantTotal] of Object.entries(participants)) {
const score = participantTotal *
MAX_SCORE /
BigInt(honestMeasurements.length)
participants[participantAddress] = score
sum += score
}
if (sum < MAX_SCORE) {
const delta = MAX_SCORE - sum
const score = (participants['0x000000000000000000000000000000000000dEaD'] ?? 0n) + delta
participants['0x000000000000000000000000000000000000dEaD'] = score
logger.log('EVALUATE ROUND %s: added %s as rounding to MAX_SCORE', roundIndex, delta)
}
// Calculate aggregates per fraud detection outcome
// This is used for logging and telemetry
/** @type {Partial<Record<import('./typings.js').FraudAssesment, number>>} */
const fraudAssessments = {
OK: 0,
TASK_NOT_IN_ROUND: 0,
DUP_INET_GROUP: 0,
TOO_MANY_TASKS: 0
}
for (const m of measurements) {
fraudAssessments[m.fraudAssessment] = (fraudAssessments[m.fraudAssessment] ?? 0) + 1
}
logger.log(
'EVALUATE ROUND %s: Evaluated %s measurements, found %s honest entries.\n%o',
roundIndex,
measurements.length,
honestMeasurements.length,
fraudAssessments
)
const fraudDetectionDuration = Date.now() - started
// Submit scores to IE
const totalScore = Object.values(participants).reduce((sum, val) => sum + val, 0n)
logger.log(
'EVALUATE ROUND %s: Invoking IE.setScores(); number of participants: %s, total score: %s',
roundIndex,
Object.keys(participants).length,
totalScore === 1000000000000000n ? '100%' : totalScore
)
const start = Date.now()
const buckets = createSetScoresBuckets(participants)
for (const [bucketIndex, bucket] of Object.entries(buckets)) {
try {
const tx = await ieContractWithSigner.setScores(
roundIndex,
bucket.participants,
bucket.scores
)
logger.log(
'EVALUATE ROUND %s: IE.setScores() TX hash: %s CALL: %s/%s',
roundIndex,
tx.hash,
Number(bucketIndex) + 1,
buckets.length
)
await stuckTransactionsCanceller.addPending(tx)
;(async () => {
await tx.wait()
stuckTransactionsCanceller.removeConfirmed(tx)
})().catch(console.error)
} catch (err) {
logger.error('CANNOT SUBMIT SCORES FOR ROUND %s (CALL %s/%s): %s',
roundIndex,
Number(bucketIndex) + 1,
buckets.length,
err
)
Sentry.captureException(err, {
extra: {
roundIndex,
bucketIndex: Number(bucketIndex) + 1,
bucketCount: buckets.length
}
})
}
}
const setScoresDuration = Date.now() - start
recordTelemetry('evaluate', point => {
point.intField('round_index', roundIndex)
point.intField('total_participants', Object.keys(participants).length)
point.intField('total_measurements', measurements.length)
point.intField('total_nodes', countUniqueNodes(measurements))
point.intField('honest_measurements', honestMeasurements.length)
point.intField('set_scores_duration_ms', setScoresDuration)
point.intField('fraud_detection_duration_ms', fraudDetectionDuration)
for (const [type, count] of Object.entries(fraudAssessments)) {
point.intField(`measurements_${type}`, count)
}
})
const ignoredErrors = []
try {
recordTelemetry('retrieval_stats_honest', (point) => {
point.intField('round_index', roundIndex)
buildRetrievalStats(honestMeasurements, point)
})
} catch (err) {
console.error('Cannot record retrieval stats (honest).', err)
ignoredErrors.push(err)
Sentry.captureException(err)
}
try {
recordTelemetry('retrieval_stats_all', (point) => {
point.intField('round_index', roundIndex)
buildRetrievalStats(measurements, point)
})
} catch (err) {
console.error('Cannot record retrieval stats (all).', err)
ignoredErrors.push(err)
Sentry.captureException(err)
}
try {
recordTelemetry('committees', (point) => {
point.intField('round_index', roundIndex)
point.intField('committees_all', committees.length)
point.intField('committees_too_small',
committees
.filter(c => c.evaluation?.retrievalResult === 'COMMITTEE_TOO_SMALL')
.length
)
recordCommitteeSizes(committees, point)
})
} catch (err) {
console.error('Cannot record committees.', err)
ignoredErrors.push(err)
Sentry.captureException(err)
}
if (createPgClient) {
try {
await updatePublicStats({
createPgClient,
committees,
honestMeasurements,
allMeasurements: measurements,
findDealClients: (minerId, cid) => sparkRoundDetails.retrievalTasks
.find(t => t.cid === cid && t.minerId === minerId)?.clients
})
} catch (err) {
console.error('Cannot update public stats.', err)
ignoredErrors.push(err)
Sentry.captureException(err)
}
}
return { ignoredErrors }
}
/**
* @param {object} args
* @param {bigint} args.roundIndex
* @param {Measurement[]} args.measurements
* @param {import('./typings.js').RoundDetails} args.sparkRoundDetails
* @param {number} args.requiredCommitteeSize
* @param {Pick<Console, 'log' | 'error'>} args.logger
*/
export const runFraudDetection = async ({
roundIndex,
measurements,
sparkRoundDetails,
requiredCommitteeSize,
logger
}) => {
const randomness = await pRetry(
() => getRandomnessForSparkRound(Number(sparkRoundDetails.startEpoch)),
{
async onFailedAttempt (error) {
console.warn(error)
console.warn('Drand request failed. Retrying...')
}
}
)
const taskBuildingStarted = Date.now()
const tasksAllowedForStations = await getTasksAllowedForStations(
sparkRoundDetails,
randomness,
measurements
)
logger.log(
'EVALUATE ROUND %s: built per-node task lists in %sms [Tasks=%s;TN=%s;Nodes=%s]',
roundIndex,
Date.now() - taskBuildingStarted,
sparkRoundDetails.retrievalTasks.length,
sparkRoundDetails.maxTasksPerNode,
tasksAllowedForStations.size
)
//
// 0. Filter out measurements missing required fields.
//
for (const m of measurements) {
if (!m.indexerResult) {
m.fraudAssessment = 'IPNI_NOT_QUERIED'
}
}
//
// 1. Filter out measurements not belonging to any valid task in this round
// or missing some of the required fields like `inet_group`
//
for (const m of measurements) {
if (m.fraudAssessment) continue
// sanity checks to get nicer errors if we forget to set required fields in unit tests
assert(typeof m.inet_group === 'string', 'missing inet_group')
assert(typeof m.finished_at === 'number', 'missing finished_at')
const isValidTask = sparkRoundDetails.retrievalTasks.some(
t => t.cid === m.cid && t.minerId === m.minerId
)
if (!isValidTask) {
m.fraudAssessment = 'TASK_NOT_IN_ROUND'
continue
}
const isValidTaskForNode = tasksAllowedForStations.get(m.stationId).some(
t => t.cid === m.cid && t.minerId === m.minerId
)
if (!isValidTaskForNode) {
m.fraudAssessment = 'TASK_WRONG_NODE'
}
}
//
// 2. Accept only maxTasksPerNode measurements from each inet group
//
/** @type {Map<string, Measurement[]>} */
const inetGroups = new Map()
for (const m of measurements) {
if (m.fraudAssessment) continue
const key = m.inet_group
let group = inetGroups.get(key)
if (!group) {
group = []
inetGroups.set(key, group)
}
group.push(m)
}
const getHash = async (/** @type {Measurement} */ m) => {
const bytes = await globalThis.crypto.subtle.digest('SHA-256', Buffer.from(new Date(m.finished_at).toISOString()))
return Buffer.from(bytes).toString('hex')
}
for (const [key, groupMeasurements] of inetGroups.entries()) {
debug('Evaluating measurements from inet group %s', key)
// Pick one measurement per task to reward and mark all others as not eligible for rewards.
// We also want to reward at most `maxTasksPerNode` measurements in each inet group.
//
// The difficult part: how to choose a measurement randomly but also fairly, so
// that each measurement has the same chance of being picked for the reward?
// We also want the selection algorithm to be deterministic.
//
// Note that we cannot rely on participant addresses because it's super easy
// for node operators to use a different address for each measurement.
//
// Let's start with a simple algorithm we can later tweak:
// 1. Hash the `finished_at` timestamp recorded by the server
// 2. Pick the measurement with the lowest hash value
// This relies on the fact that the hash function has a random distribution.
// We are also making the assumption that each measurement has a distinct `finished_at` field.
//
const measurementsWithHash = await Promise.all(
groupMeasurements.map(async (m) => ({ m, h: await getHash(m) }))
)
// Sort measurements using the computed hash
measurementsWithHash.sort((a, b) => a.h.localeCompare(b.h, 'en'))
const tasksSeen = new Set()
for (const { m, h } of measurementsWithHash) {
const taskId = getTaskId(m)
if (tasksSeen.has(taskId)) {
debug(' pa: %s h: %s task: %s - task was already rewarded', m.participantAddress, h, taskId)
m.fraudAssessment = 'DUP_INET_GROUP'
continue
}
if (tasksSeen.size >= sparkRoundDetails.maxTasksPerNode) {
debug(' pa: %s h: %s task: %s - already rewarded max tasks', m.participantAddress, h, taskId)
m.fraudAssessment = 'TOO_MANY_TASKS'
continue
}
tasksSeen.add(taskId)
m.fraudAssessment = 'OK'
debug(' pa: %s h: %s task: %s - REWARD', m.participantAddress, h, taskId)
}
}
//
// 3. Group measurements to per-task committees and find majority result
//
// PERFORMANCE: Avoid duplicating the array of measurements because there are
// hundreds of thousands of them. All the function groupMeasurementsToCommittees
// needs is to iterate over the accepted measurements once.
const iterateAcceptedMeasurements = function * () {
for (const m of measurements) {
if (m.fraudAssessment !== 'OK') continue
yield m
}
}
const committees = groupMeasurementsToCommittees(iterateAcceptedMeasurements())
for (const c of committees.values()) {
c.evaluate({ requiredCommitteeSize })
}
if (debug.enabled) {
for (const m of measurements) {
// Print round & participant address & CID together to simplify lookup when debugging
// Omit the `m` object from the format string to get nicer formatting
debug(
'FRAUD ASSESSMENT for round=%s client=%s cid=%s minerId=%s',
roundIndex,
m.participantAddress,
m.cid,
m.minerId,
m)
}
}
return { committees: Array.from(committees.values()) }
}
/**
* @param {Measurement[]} measurements
*/
const countUniqueNodes = (measurements) => {
const nodes = new Set()
for (const m of measurements) {
const key = `${m.inet_group}::${m.participantAddress}`
nodes.add(key)
}
return nodes.size
}