This repository was archived by the owner on Jan 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathevaluate.js
More file actions
158 lines (140 loc) · 4.79 KB
/
evaluate.js
File metadata and controls
158 lines (140 loc) · 4.79 KB
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
import createDebug from 'debug'
import { updatePublicStats } from './public-stats.js'
import { buildRetrievalStats, recordCommitteeSizes } from './retrieval-stats.js'
import { groupMeasurementsToCommittees } from './committee.js'
import pRetry from 'p-retry'
/** @import {Measurement} from './preprocess.js' */
const debug = createDebug('spark:evaluate')
export const REQUIRED_COMMITTEE_SIZE = 1
/**
* @param {object} args
* @param {import('./round.js').RoundData} args.round
* @param {bigint} args.roundIndex
* @param {number} [args.requiredCommitteeSize]
* @param {any} args.ieContract
* @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 {(round: import('./round.js').RoundData, committees: Iterable<import('./committee.js').Committee>) => Promise<void>} args.prepareProviderRetrievalResultStats
*/
export const evaluate = async ({
round,
roundIndex,
requiredCommitteeSize,
ieContract,
fetchRoundDetails,
recordTelemetry,
createPgClient,
logger,
prepareProviderRetrievalResultStats
}) => {
requiredCommitteeSize ??= REQUIRED_COMMITTEE_SIZE
// Get measurements
/** @type {Measurement[]} */
const measurements = round.measurements || []
// Detect fraud
const sparkRoundDetails = await fetchRoundDetails(await ieContract.getAddress(), roundIndex, recordTelemetry)
round.details = sparkRoundDetails
debug('ROUND DETAILS for round=%s', roundIndex, sparkRoundDetails)
for (const m of measurements) {
// Mark all measurements as accepted by default.
m.taskingEvaluation = 'OK'
}
const evaluationCommittees = groupMeasurementsToCommittees(measurements)
for (const c of evaluationCommittees.values()) {
c.evaluate({ requiredCommitteeSize })
}
const committees = Array.from(evaluationCommittees.values())
logger.log(
'EVALUATE ROUND %s: Accepted all %s measurements.\n',
roundIndex,
measurements.length
)
// Telemetry and stats (no rewards)
recordTelemetry('evaluate', point => {
point.intField('round_index', roundIndex)
point.intField('total_measurements', measurements.length)
point.intField('total_nodes', countUniqueNodes(measurements))
})
const ignoredErrors = []
// Both retrieval_stats_honest and retrieval_stats_all report the same stats,
// but we keep retrieval_stats_honest for backwards compatibility.
try {
recordTelemetry('retrieval_stats_honest', (point) => {
point.intField('round_index', roundIndex)
buildRetrievalStats(measurements, point)
})
} catch (err) {
console.error('Cannot record retrieval stats (honest).', err)
ignoredErrors.push(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)
}
try {
recordTelemetry('committees', (point) => {
point.intField('round_index', roundIndex)
point.intField('committees_all', committees.length)
point.intField('committees_too_small',
committees
.filter(c => c.decision?.retrievalResult === 'COMMITTEE_TOO_SMALL')
.length
)
recordCommitteeSizes(committees, point)
})
} catch (err) {
console.error('Cannot record committees.', err)
ignoredErrors.push(err)
}
if (createPgClient) {
try {
await updatePublicStats({
createPgClient,
committees,
allMeasurements: measurements,
findDealClients: (minerId, cid) => sparkRoundDetails.retrievalTasks
.find(t => t.cid === cid && t.minerId === minerId)?.clients,
findDealAllocators: (minerId, cid) => sparkRoundDetails.retrievalTasks
.find(t => t.cid === cid && t.minerId === minerId)?.allocators
})
} catch (err) {
console.error('Cannot update public stats.', err)
ignoredErrors.push(err)
}
}
try {
await pRetry(
() => prepareProviderRetrievalResultStats(round, committees),
{
async onFailedAttempt (ctx) {
console.warn(ctx)
console.warn(
'Preparing provider retrieval result stats failed. Retrying...'
)
}
}
)
} catch (err) {
console.error('Cannot prepare provider retrieval result stats.', err)
ignoredErrors.push(err)
}
return { ignoredErrors }
}
/**
* @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
}