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 all commits
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_BUCKET: 'S3_BUCKET',
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_BUCKET: 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_BUCKET!,
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,10 @@
export interface ISolution {
workerAddress: string;
solution: string;
invalid?: boolean;
}

export interface ISolutionsFile {
exchangeAddress: string;
solutions: ISolution[];
}
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 @@ -2,10 +2,21 @@ import { ConfigService } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import { JobController } from './job.controller';
import { JobService } from './job.service';
import { JobDetailsDto, SolveJobDto } from './job.dto';
import { InvalidJobDto, 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';
import { StorageService } from '../storage/storage.service';

describe('JobController', () => {
let jobController: JobController;
Expand All @@ -22,6 +33,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 All @@ -38,6 +66,7 @@ describe('JobController', () => {
}),
},
},
StorageService,
{
provide: HttpService,
useValue: {
Expand Down Expand Up @@ -104,13 +133,11 @@ describe('JobController', () => {
workerAddress,
solution,
};
const expectedResult = true;

jest.spyOn(jobService, 'solveJob').mockResolvedValue(expectedResult);
jest.spyOn(jobService, 'solveJob').mockResolvedValue();

const result = await jobController.solveJob(solveJobDto);
await jobController.solveJob(solveJobDto);

expect(result).toBe(expectedResult);
expect(jobService.solveJob).toHaveBeenCalledWith(
solveJobDto.chainId,
solveJobDto.escrowAddress,
Expand All @@ -119,4 +146,22 @@ describe('JobController', () => {
);
});
});

describe('invalidJobSolution-solution', () => {
it('should mark a job solution as invalid', async () => {
const solveJobDto: InvalidJobDto = {
chainId,
escrowAddress,
workerAddress,
};

jest.spyOn(jobService, 'processInvalidJobSolution').mockResolvedValue();

await jobController.invalidJobSolution(solveJobDto);

expect(jobService.processInvalidJobSolution).toHaveBeenCalledWith(
solveJobDto,
);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { JobService } from './job.service';
import { JobDetailsDto, SolveJobDto } from './job.dto';
import { InvalidJobDto, JobDetailsDto, SolveJobDto } from './job.dto';

@ApiTags('Job')
@Controller('job')
Expand Down Expand Up @@ -33,4 +33,9 @@ export class JobController {
body.solution,
);
}

@Patch('invalid-solution')
invalidJobSolution(@Body() body: InvalidJobDto): Promise<any> {
return this.jobService.processInvalidJobSolution(body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ export class SolveJobDto {
public solution: string;
}

export class InvalidJobDto {
@ApiProperty()
@IsString()
@IsValidEthereumAddress()
public escrowAddress: string;

@ApiProperty({
enum: ChainId,
})
@IsEnum(ChainId)
public chainId: ChainId;

@ApiProperty()
@IsString()
@IsValidEthereumAddress()
workerAddress: string;
}

export class EscrowFailedWebhookDto {
public chain_id: ChainId;
public escrow_address: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ 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';
import { StorageModule } from '../storage/storage.module';

@Module({
imports: [ConfigModule, HttpModule, Web3Module],
imports: [
ConfigModule.forFeature(s3Config),
ConfigModule,
HttpModule,
Web3Module,
StorageModule,
],
controllers: [JobController],
providers: [JobService],
})
Expand Down
Loading