Skip to content

Commit 5745f16

Browse files
authored
Fix recording oracle algorithm to clasify workers solutions (#2582)
Update assignment status for rejected solutions in exchange oracle
1 parent e2b2a35 commit 5745f16

File tree

4 files changed

+52
-27
lines changed

4 files changed

+52
-27
lines changed

packages/apps/fortune/exchange-oracle/server/src/modules/assignment/assignment.repository.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ export class AssignmentRepository extends BaseRepository<AssignmentEntity> {
3939
});
4040
}
4141

42+
public async findOneByEscrowAndWorker(
43+
escrowAddress: string,
44+
chainId: ChainId,
45+
workerAddress: string,
46+
): Promise<AssignmentEntity | null> {
47+
return this.findOne({
48+
where: {
49+
job: { escrowAddress, chainId },
50+
workerAddress,
51+
},
52+
});
53+
}
54+
4255
public async findOneById(
4356
assignmentId: number,
4457
): Promise<AssignmentEntity | null> {

packages/apps/fortune/exchange-oracle/server/src/modules/job/job.service.spec.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import { HttpService } from '@nestjs/axios';
99
import { ConfigService } from '@nestjs/config';
1010
import { Test } from '@nestjs/testing';
1111
import { of } from 'rxjs';
12-
import {
13-
MOCK_MANIFEST_URL,
14-
mockConfig,
15-
} from '../../../test/constants';
12+
import { MOCK_MANIFEST_URL, mockConfig } from '../../../test/constants';
1613
import {
1714
AssignmentStatus,
1815
JobFieldName,
@@ -577,7 +574,7 @@ describe('JobService', () => {
577574
});
578575
});
579576

580-
describe('processInvalidJob', () => {
577+
describe('processInvalidJobSolution', () => {
581578
it('should mark a job solution as invalid', async () => {
582579
const workerAddress = '0x1234567890123456789012345678901234567891';
583580
const solution = 'test';
@@ -591,6 +588,12 @@ describe('JobService', () => {
591588
.fn()
592589
.mockResolvedValue(existingJobSolutions);
593590
storageService.uploadJobSolutions = jest.fn();
591+
assignmentRepository.findOneByEscrowAndWorker = jest
592+
.fn()
593+
.mockResolvedValue({
594+
id: 1,
595+
status: AssignmentStatus.VALIDATION,
596+
});
594597

595598
await jobService.processInvalidJobSolution({
596599
chainId,
@@ -610,6 +613,10 @@ describe('JobService', () => {
610613
},
611614
],
612615
);
616+
expect(assignmentRepository.updateOne).toHaveBeenCalledWith({
617+
id: 1,
618+
status: AssignmentStatus.REJECTED,
619+
});
613620
});
614621

615622
it('should throw an error if solution was not previously in S3', async () => {

packages/apps/fortune/exchange-oracle/server/src/modules/job/job.service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ export class JobService {
250250

251251
if (foundSolution) {
252252
foundSolution.error = true;
253+
const assignment =
254+
await this.assignmentRepository.findOneByEscrowAndWorker(
255+
invalidJobSolution.escrowAddress,
256+
invalidJobSolution.chainId,
257+
foundSolution.workerAddress,
258+
);
259+
if (assignment) {
260+
assignment.status = AssignmentStatus.REJECTED;
261+
this.assignmentRepository.updateOne(assignment);
262+
}
253263
} else {
254264
throw new BadRequestException(
255265
`Solution not found in Escrow: ${invalidJobSolution.escrowAddress}`,

packages/apps/fortune/recording-oracle/src/modules/job/job.service.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,40 +55,36 @@ export class JobService {
5555

5656
filteredExchangeSolution.forEach((exchangeSolution) => {
5757
if (errorSolutions.includes(exchangeSolution)) return;
58+
const duplicatedInUnique = uniqueSolutions.filter(
59+
(solution) =>
60+
solution.workerAddress === exchangeSolution.workerAddress ||
61+
solution.solution === exchangeSolution.solution,
62+
);
63+
if (
64+
duplicatedInUnique.length > 0 &&
65+
!errorSolutions.includes(exchangeSolution)
66+
) {
67+
errorSolutions.push({
68+
...exchangeSolution,
69+
error: SolutionError.Duplicated,
70+
});
71+
return;
72+
}
5873

5974
const duplicatedInRecording = recordingSolutions.filter(
6075
(solution) =>
61-
solution.workerAddress === exchangeSolution.workerAddress ||
76+
solution.workerAddress === exchangeSolution.workerAddress &&
6277
solution.solution === exchangeSolution.solution,
6378
);
6479

6580
if (duplicatedInRecording.length === 0) {
66-
const duplicatedInExchange = filteredExchangeSolution.filter(
67-
(solution) =>
68-
solution.workerAddress === exchangeSolution.workerAddress ||
69-
solution.solution === exchangeSolution.solution,
70-
);
71-
if (duplicatedInExchange.length > 1) {
72-
duplicatedInExchange.forEach((duplicated) => {
73-
if (
74-
(duplicated.solution !== exchangeSolution.solution ||
75-
duplicated.workerAddress !== exchangeSolution.workerAddress) &&
76-
!errorSolutions.includes(duplicated)
77-
) {
78-
errorSolutions.push({
79-
...duplicated,
80-
error: SolutionError.Duplicated,
81-
});
82-
}
83-
});
84-
}
8581
if (checkCurseWords(exchangeSolution.solution))
8682
errorSolutions.push({
8783
...exchangeSolution,
8884
error: SolutionError.CurseWord,
8985
});
9086
else uniqueSolutions.push(exchangeSolution);
91-
}
87+
} else uniqueSolutions.push(exchangeSolution);
9288
});
9389
return { errorSolutions, uniqueSolutions };
9490
}
@@ -161,7 +157,6 @@ export class JobService {
161157
);
162158

163159
const recordingOracleSolutions: ISolution[] = [
164-
...existingJobSolutions,
165160
...uniqueSolutions,
166161
...errorSolutions,
167162
];

0 commit comments

Comments
 (0)