Skip to content

Commit

Permalink
Reward only first TN measurements per round. Closes #98 (#101)
Browse files Browse the repository at this point in the history
* Reward only first TN measurements per round. Closes #98

* Update lib/evaluate.js

Co-authored-by: Miroslav Bajtoš <oss@bajtos.net>

* Update lib/evaluate.js

Co-authored-by: Miroslav Bajtoš <oss@bajtos.net>

---------

Co-authored-by: Miroslav Bajtoš <oss@bajtos.net>
  • Loading branch information
juliangruber and bajtos authored Dec 13, 2023
1 parent 14790c3 commit 224b0fd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export const evaluate = async ({
const fraudAssessments = {
OK: 0,
INVALID_TASK: 0,
DUP_INET_GROUP: 0
DUP_INET_GROUP: 0,
TOO_MANY_TASKS: 0
}
for (const m of measurements) {
fraudAssessments[m.fraudAssessment] = (fraudAssessments[m.fraudAssessment] ?? 0) + 1
Expand Down Expand Up @@ -249,6 +250,19 @@ export const runFraudDetection = async (roundIndex, measurements, sparkRoundDeta
}
}

//
// 3. Reward only first TN measurements
//
const tasksPerNode = new Map()
for (const m of measurements) {
if (m.fraudAssessment && m.fraudAssessment !== 'OK') continue
const node = `${m.inet_group}::${m.participantAddress}`
tasksPerNode.set(node, (tasksPerNode.get(node) ?? 0) + 1)
if (tasksPerNode.get(node) > sparkRoundDetails.maxTasksPerNode) {
m.fraudAssessment = 'TOO_MANY_TASKS'
}
}

if (debug.enabled) {
for (const m of measurements) {
// Print round & participant address & CID together to simplify lookup when debugging
Expand Down
2 changes: 2 additions & 0 deletions lib/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface RetrievalTask {
export interface RoundDetails {
roundId: string; // BigInt serialized as String (JSON does not support BigInt)
retrievalTasks: RetrievalTask[];
maxTasksPerNode: number;
}

export type RecordTelemetryFn = (
Expand All @@ -32,6 +33,7 @@ export type FraudAssesment =
| 'OK'
| 'INVALID_TASK'
| 'DUP_INET_GROUP'
| 'TOO_MANY_TASKS'


// When adding a new enum value, remember to update the summary initializer inside `reportRetrievalStats()`
Expand Down
41 changes: 41 additions & 0 deletions test/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,45 @@ describe('fraud detection', () => {
}
})
})

it('rejects measurements above maxTasksPerNode', async () => {
const sparkRoundDetails = {
roundId: 1234,
retrievalTasks: [
{
...VALID_TASK,
cid: 'cid1'
}, {
...VALID_TASK,
cid: 'cid2'
}, {
...VALID_TASK,
cid: 'cid3'
}
],
maxTasksPerNode: 1
}
const measurements = [
{
...VALID_MEASUREMENT,
cid: 'cid1',
inet_group: 'group1'
},
{
...VALID_MEASUREMENT,
cid: 'cid2',
inet_group: 'group1'
},
{
...VALID_MEASUREMENT,
cid: 'cid3'
}
]

await runFraudDetection(1, measurements, sparkRoundDetails)
assert.deepStrictEqual(
measurements.map(m => m.fraudAssessment),
['OK', 'TOO_MANY_TASKS', 'OK']
)
})
})

0 comments on commit 224b0fd

Please sign in to comment.