Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Exchange Oracle] Upload solutions file #959

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/apps/fortune/exchange-oracle/server/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# General
NODE_ENV=development
PORT=

S3_ENDPOINT=
S3_PORT=
S3_ACCESS_KEY=
S3_SECRET_KEY=
S3_REGION=

WEB3_PRIVATE_KEY=

# Reputation Level
REPUTATION_LEVEL_LOW=
REPUTATION_LEVEL_HIGH=

# Oracles
REPUTATION_ORACLE_URL=
2 changes: 1 addition & 1 deletion packages/apps/fortune/exchange-oracle/server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ lerna-debug.log*

# OS
.DS_Store
.env*
.env.development

# Tests
/coverage
Expand Down
32 changes: 32 additions & 0 deletions packages/apps/fortune/exchange-oracle/server/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '3.7'

services:
minio:
container_name: minio
image: minio/minio:RELEASE.2022-05-26T05-48-41Z
ports:
- 9001:9001
- 9000:9000
environment:
MINIO_ROOT_USER: dev
MINIO_ROOT_PASSWORD: devdevdev
entrypoint: 'sh'
command:
-c "minio server /data --console-address ':9001'"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 5s
timeout: 5s
retries: 3
minio-mc:
container_name: minio-mc
image: minio/mc
depends_on:
minio:
condition: service_healthy
entrypoint: >
/bin/sh -c "
/usr/bin/mc config host add myminio http://minio:9000 dev devdevdev;
/usr/bin/mc mb myminio/solution;
/usr/bin/mc anonymous set public myminio/solution;
"
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@ export const ConfigNames = {
PORT: 'PORT',
WEB3_PRIVATE_KEY: 'WEB3_PRIVATE_KEY',
REPUTATION_ORACLE_URL: 'REPUTATION_ORACLE_URL',
S3_ENDPOINT: 'S3_ENDPOINT',
S3_PORT: 'S3_PORT',
S3_ACCESS_KEY: 'S3_ACCESS_KEY',
S3_SECRET_KEY: 'S3_SECRET_KEY',
S3_BACKET: 'S3_BACKET',
S3_USE_SSL: 'S3_USE_SSL',
};

export const envValidator = Joi.object({
HOST: Joi.string().default('localhost'),
PORT: Joi.string().default(3002),
WEB3_PRIVATE_KEY: Joi.string().required(),
// S3
S3_ENDPOINT: Joi.string().default('127.0.0.1'),
S3_PORT: Joi.string().default(9000),
S3_ACCESS_KEY: Joi.string().required(),
S3_SECRET_KEY: Joi.string().required(),
S3_BACKET: Joi.string().default('solution'),
S3_USE_SSL: Joi.string().default(false),
});
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './env';
export * from './networks';
export * from './s3';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ConfigType, registerAs } from '@nestjs/config';

export const s3Config = registerAs('s3', () => ({
endPoint: process.env.S3_ENDPOINT!,
port: +process.env.S3_PORT!,
accessKey: process.env.S3_ACCESS_KEY!,
secretKey: process.env.S3_SECRET_KEY!,
bucket: process.env.S3_BACKET!,
useSSL: process.env.S3_USE_SSL === 'true',
}));

export const s3ConfigKey = s3Config.KEY;
export type S3ConfigType = ConfigType<typeof s3Config>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface ISolution {
exchangeAddress: string;
workerAddress: string;
solution: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as Minio from 'minio';
import { ISolution } from '../interfaces/job';
import { StorageClient } from '@human-protocol/sdk';
import { BadRequestException } from '@nestjs/common';

export async function uploadJobSolutions(
client: Minio.Client,
chainId: number,
escrowAddress: string,
workerAddress: string,
exchangeAddress: string,
solution: string,
bucket: string,
): Promise<string> {
if (!(await client.bucketExists(bucket))) {
throw new BadRequestException('Bucket not found');
}
const key = `${escrowAddress}-${chainId}.json`;
const url = `${(client as any).protocol}//${(client as any).host}:${
(client as any).port
}/${bucket}/${key}`;

let existingJobSolutions: ISolution[];
try {
existingJobSolutions = await StorageClient.downloadFileFromUrl(url);
} catch {
existingJobSolutions = [];
}

if (
existingJobSolutions.find(
(solution) => solution.workerAddress === workerAddress,
)
) {
throw new BadRequestException('User has already submitted a solution');
}

const newJobSolutions: ISolution[] = [
...existingJobSolutions,
{
exchangeAddress: exchangeAddress,
workerAddress: workerAddress,
solution: solution,
},
];

const content = JSON.stringify(newJobSolutions);

try {
await client.putObject(bucket, key, content, {
'Content-Type': 'application/json',
});

return url;
} catch (e) {
throw new BadRequestException('File not uploaded');
}
}
4 changes: 2 additions & 2 deletions packages/apps/fortune/exchange-oracle/server/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { json, urlencoded } from 'body-parser';
import { useContainer } from 'class-validator';

import { AppModule } from './app.module';
import { ConfigNames } from './common/config';
import { INestApplication } from '@nestjs/common';

async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
const app = await NestFactory.create<INestApplication>(AppModule, {
cors: true,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import { JobDetailsDto, SolveJobDto } from './job.dto';
import { Web3Service } from '../web3/web3.service';
import { HttpService } from '@nestjs/axios';
import { of } from 'rxjs';
import { ConfigModule, registerAs } from '@nestjs/config';
import {
MOCK_REPUTATION_ORACLE_WEBHOOK_URL,
MOCK_S3_ACCESS_KEY,
MOCK_S3_BUCKET,
MOCK_S3_ENDPOINT,
MOCK_S3_PORT,
MOCK_S3_SECRET_KEY,
MOCK_S3_USE_SSL,
} from '../../../test/constants';

describe('JobController', () => {
let jobController: JobController;
Expand All @@ -22,6 +32,23 @@ describe('JobController', () => {

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
ConfigModule.forFeature(
registerAs('s3', () => ({
accessKey: MOCK_S3_ACCESS_KEY,
secretKey: MOCK_S3_SECRET_KEY,
endPoint: MOCK_S3_ENDPOINT,
port: MOCK_S3_PORT,
useSSL: MOCK_S3_USE_SSL,
bucket: MOCK_S3_BUCKET,
})),
),
ConfigModule.forFeature(
registerAs('server', () => ({
reputationOracleWebhookUrl: MOCK_REPUTATION_ORACLE_WEBHOOK_URL,
})),
),
],
controllers: [JobController],
providers: [
JobService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import { JobController } from './job.controller';
import { JobService } from './job.service';
import { HttpModule } from '@nestjs/axios';
import { Web3Module } from '../web3/web3.module';
import { s3Config } from 'src/common/config';

@Module({
imports: [ConfigModule, HttpModule, Web3Module],
imports: [
ConfigModule.forFeature(s3Config),
ConfigModule,
HttpModule,
Web3Module,
],
controllers: [JobController],
providers: [JobService],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@ import { Test } from '@nestjs/testing';
import { of } from 'rxjs';
import { Web3Service } from '../web3/web3.service';
import { JobService } from './job.service';
import { EscrowClient, KVStoreClient, EscrowUtils } from '@human-protocol/sdk';
import { MOCK_PRIVATE_KEY } from '../../../test/constants';
import {
EscrowClient,
KVStoreClient,
StorageClient,
EscrowUtils,
} from '@human-protocol/sdk';
import {
MOCK_PRIVATE_KEY,
MOCK_S3_ACCESS_KEY,
MOCK_S3_BUCKET,
MOCK_S3_ENDPOINT,
MOCK_S3_PORT,
MOCK_S3_SECRET_KEY,
MOCK_S3_USE_SSL,
} from '../../../test/constants';
import { EventType } from '../../common/enums/webhook';
import { HEADER_SIGNATURE_KEY } from '../../common/constant';
import { signMessage } from '../../common/utils/signature';
import { ConfigModule, registerAs } from '@nestjs/config';

jest.mock('@human-protocol/sdk', () => ({
...jest.requireActual('@human-protocol/sdk'),
Expand All @@ -22,13 +36,21 @@ jest.mock('@human-protocol/sdk', () => ({
downloadFileFromUrl: jest.fn(),
},
}));

jest.mock('axios', () => ({
post: jest.fn(),
}));
jest.mock('minio', () => {
class Client {
putObject = jest.fn();
bucketExists = jest.fn().mockResolvedValue(true);
constructor() {
(this as any).protocol = 'http:';
(this as any).host = 'localhost';
(this as any).port = 9000;
}
}

return { Client };
});

describe('JobService', () => {
let configService: ConfigService;
let jobService: JobService;
let web3Service: Web3Service;
let httpService: HttpService;
Expand Down Expand Up @@ -60,6 +82,18 @@ describe('JobService', () => {

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
ConfigModule.forFeature(
registerAs('s3', () => ({
accessKey: MOCK_S3_ACCESS_KEY,
secretKey: MOCK_S3_SECRET_KEY,
endPoint: MOCK_S3_ENDPOINT,
port: MOCK_S3_PORT,
useSSL: MOCK_S3_USE_SSL,
bucket: MOCK_S3_BUCKET,
})),
),
],
providers: [
JobService,
{
Expand All @@ -84,7 +118,6 @@ describe('JobService', () => {
],
}).compile();

configService = moduleRef.get<ConfigService>(ConfigService);
jobService = moduleRef.get<JobService>(JobService);
web3Service = moduleRef.get<Web3Service>(Web3Service);
httpService = moduleRef.get<HttpService>(HttpService);
Expand Down Expand Up @@ -153,7 +186,11 @@ describe('JobService', () => {
});

it('should fail if reputation oracle url is empty', async () => {
configService.get = jest.fn().mockReturnValue('');
(configServiceMock as any).get.mockImplementation((key: string) => {
if (key === 'REPUTATION_ORACLE_URL') {
return '';
}
});

await expect(
jobService.getDetails(chainId, escrowAddress),
Expand Down Expand Up @@ -209,7 +246,8 @@ describe('JobService', () => {

describe('solveJob', () => {
it('should solve a job', async () => {
const solution = 'job-solution';
const solutionUrl =
'http://localhost:9000/solution/0x1234567890123456789012345678901234567890-1.json';

const recordingOracleURLMock = 'https://example.com/recordingoracle';

Expand All @@ -222,11 +260,13 @@ describe('JobService', () => {
get: jest.fn().mockResolvedValue(recordingOracleURLMock),
}));

StorageClient.downloadFileFromUrl = jest.fn().mockResolvedValue([]);

const result = await jobService.solveJob(
chainId,
escrowAddress,
workerAddress,
solution,
'solution',
);

expect(result).toBe(true);
Expand All @@ -238,7 +278,7 @@ describe('JobService', () => {
chainId,
exchangeAddress: signerMock.address,
workerAddress,
solution,
solutionUrl,
}),
);
});
Expand Down Expand Up @@ -288,6 +328,14 @@ describe('JobService', () => {
get: jest.fn().mockResolvedValue('https://example.com/recordingoracle'),
}));

StorageClient.downloadFileFromUrl = jest.fn().mockResolvedValue([
{
exchangeAddress: '0x1234567890123456789012345678901234567892',
workerAddress: '0x1234567890123456789012345678901234567891',
solution: 'test',
},
]);

jobService['storage'][escrowAddress] = [workerAddress];

await expect(
Expand Down
Loading