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

Fixes after code review (2024-05-07) #75

Merged
merged 31 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9562495
File `global-common.interface.ts` has been renamed to `global-common.…
MWBlocky May 7, 2024
e3c3880
Renamed and moved EnvironmentVariableMissingError class.
MWBlocky May 7, 2024
37bfb3c
Renamed and moved Pageable classes to utils directory.
MWBlocky May 7, 2024
bb234c6
Remove unused import in exchange-oracle.gateway.spec.ts
MWBlocky May 7, 2024
e9e3f44
Remove console.log from fetchAssignedJobs method.
MWBlocky May 7, 2024
4fd1d68
Update dependencies in package.json.
MWBlocky May 7, 2024
e59e945
Update terminology in README.md
MWBlocky May 7, 2024
709539d
Remove unused Headers import from job-assignment.controller.ts
MWBlocky May 7, 2024
320959a
Removed OnModuleInit import from kv-store-gateway.service.ts
MWBlocky May 7, 2024
a1ba473
Corrected the data types for REDIS_PORT and REDIS_HOST in .env.exampl…
MWBlocky May 7, 2024
18ce38a
Remove redundant volume configuration in Docker-compose
MWBlocky May 7, 2024
8dce76f
Add RPC_URL environment variable to docker-compose
MWBlocky May 7, 2024
e92e428
Replace hardcoded URL_KEY in KvStoreGateway with imported constant.
MWBlocky May 7, 2024
3dfe042
fix: Update KVStoreClient import and test in KvStoreGateway
MWBlocky May 7, 2024
cd1a23a
Add Reputation Oracle address to .env and refactor Oracle Discovery.
MWBlocky May 7, 2024
56ffd41
Add token authentication for Oracle statistics.
MWBlocky May 7, 2024
dc826c0
Remove manual environment variable check.
MWBlocky May 8, 2024
5220e10
Refactor address to use escrowAddress in job assignment service.
MWBlocky May 8, 2024
8cbdb5d
Ensure environment variables validation takes place before module loa…
MWBlocky May 9, 2024
1ed59b1
Revert "Add token authentication for Oracle statistics."
MWBlocky May 9, 2024
fa99045
Update terminology from 'Interfaces' to 'Model' in README
MWBlocky May 9, 2024
dd675f8
Refactor PageableClasses to PageableDto and the file renamed from pag…
MWBlocky May 9, 2024
c851c98
Update KV store gateway test to use actual SDK.
MWBlocky May 9, 2024
a2c1f15
Added REPUTATION_ORACLE_ADDRESS in app configuration and docker compo…
MWBlocky May 9, 2024
1406153
Integrate and use EscrowUtils for job assignment processing.
MWBlocky May 9, 2024
8d73ca3
Added chainId parameter to `getExchangeOracleAddressByEscrowAddress` …
MWBlocky May 9, 2024
2732020
Remove redundant code KV-store gateway test
MWBlocky May 9, 2024
1274ff3
Update EscrowUtilsGateway tests.
MWBlocky May 9, 2024
a2134ad
Add mock ConfigService to oracle-discovery service test.
MWBlocky May 9, 2024
283abf9
Revert "Add mock ConfigService to oracle-discovery service test."
MWBlocky May 9, 2024
8ec5fe1
Update oracle-discovery.service.spec.ts tests.
MWBlocky May 9, 2024
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
5 changes: 3 additions & 2 deletions packages/apps/human-app/server/.env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
HOST= # string, example: localhost
PORT= # number, example: 5010
REPUTATION_ORACLE_URL= # string
REDIS_PORT= # string, example: localhost
REDIS_HOST= # number, example: 6379
REPUTATION_ORACLE_ADDRESS= # string
REDIS_HOST= # string, example: localhost
REDIS_PORT= # number, example: 6379
CACHE_TTL_ORACLE_DISCOVERY= # number, example: 43200
CACHE_TTL_ORACLE_STATS= # number, example: 900
CACHE_TTL_USER_STATS= # number, example: 86400
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/human-app/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ and transformation of data. NestJS provides many built-in pipes, so check the do
- **Filters**: Classes implementing the `ExceptionFilter` interface, used to handle exceptions.
- **Interceptors**: Used to bind extra logic before or after method execution or to extend the behavior of the method.

### Model
MWBlocky marked this conversation as resolved.
Show resolved Hide resolved
### Interfaces

Interfaces are used to define the shape and responsibilities of the data:

Expand Down
5 changes: 2 additions & 3 deletions packages/apps/human-app/server/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ services:
REDIS_PORT: ${REDIS_PORT}
MWBlocky marked this conversation as resolved.
Show resolved Hide resolved
REDIS_HOST: redis
CACHE_TTL_ORACLE_DISCOVERY: ${CACHE_TTL_ORACLE_DISCOVERY}
RPC_URL: ${RPC_URL}
depends_on:
- redis
redis:
Expand All @@ -27,6 +28,4 @@ services:
ports:
- '${REDIS_PORT}:6379'
volumes:
- redis_data:/data
volumes:
redis_data:
- redis_data:/data
5 changes: 2 additions & 3 deletions packages/apps/human-app/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"@nestjs/platform-express": "^10.2.6",
"@nestjs/swagger": "^7.1.13",
"cache-manager": "^5.4.0",
"cache-manager-redis-store": "^3.0.1",
"ioredis": "^5.3.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"ethers": "^6.11.0",
Expand All @@ -40,7 +42,6 @@
"rxjs": "^7.2.0"
},
"devDependencies": {
"@nestjs/cache-manager": "^2.2.1",
"@nestjs/cli": "^9.4.3",
"@nestjs/schematics": "^9.2.0",
"@nestjs/testing": "^9.4.3",
Expand All @@ -50,11 +51,9 @@
"@types/supertest": "^2.0.15",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"cache-manager-redis-store": "^3.0.1",
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"ioredis": "^5.3.2",
"jest": "29.5.0",
"nock": "^13.5.1",
"prettier": "^3.1.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ConfigService } from '@nestjs/config';
import { Injectable } from '@nestjs/common';
import { EnvironmentVariableMissingError } from '../interfaces/custom-exceptions.interface';
const DEFAULT_CACHE_TTL_ORACLE_STATS = 12 * 60 * 60;
const DEFAULT_CACHE_TTL_USER_STATS = 15 * 60;
const DEFAULT_CACHE_TTL_ORACLE_DISCOVERY = 24 * 60 * 60;
Expand All @@ -10,21 +9,22 @@ const DEFAULT_CORS_ALLOWED_HEADERS = 'Content-Type, Accept';
export class EnvironmentConfigService {
constructor(private configService: ConfigService) {}
get host(): string {
return this.conditionallyReturnMandatoryEnvVariable<string>('HOST');
return this.configService.getOrThrow<string>('HOST');
}
get port(): number {
return this.conditionallyReturnMandatoryEnvVariable<number>('PORT');
return this.configService.getOrThrow<number>('PORT');
}
get reputationOracleUrl(): string {
return this.conditionallyReturnMandatoryEnvVariable<string>(
'REPUTATION_ORACLE_URL',
);
return this.configService.getOrThrow<string>('REPUTATION_ORACLE_URL');
}
get reputationOracleAddress(): string {
return this.configService.getOrThrow<string>('REPUTATION_ORACLE_ADDRESS');
}
get cachePort(): number {
return this.conditionallyReturnMandatoryEnvVariable<number>('REDIS_PORT');
return this.configService.getOrThrow<number>('REDIS_PORT');
}
get cacheHost(): string {
return this.conditionallyReturnMandatoryEnvVariable<string>('REDIS_HOST');
return this.configService.getOrThrow<string>('REDIS_HOST');
}
get cacheTtlOracleStats(): number {
return this.configService.get<number>(
Expand All @@ -47,7 +47,7 @@ export class EnvironmentConfigService {
);
}
get rpcUrl(): string {
return this.conditionallyReturnMandatoryEnvVariable<string>('RPC_URL');
return this.configService.getOrThrow<string>('RPC_URL');
}
get isCorsEnabled(): boolean {
return this.configService.get<boolean>('CORS_ENABLED', false);
Expand All @@ -61,35 +61,7 @@ export class EnvironmentConfigService {
get corsAllowedHeaders(): string {
return this.configService.get<string>(
'CORS_ALLOWED_HEADERS',
DEFAULT_CORS_ALLOWED_HEADERS
DEFAULT_CORS_ALLOWED_HEADERS,
);
}
conditionallyReturnMandatoryEnvVariable<T>(envName: string): T {
const value = this.configService.get<T>(envName);
if (!value) {
throw new EnvironmentVariableMissingError(envName);
}
return value;
}
checkMandatoryConfig(): void {
MWBlocky marked this conversation as resolved.
Show resolved Hide resolved
const mandatoryVariables = [
'HOST',
'PORT',
'REPUTATION_ORACLE_URL',
'REDIS_PORT',
'REDIS_HOST',
'RPC_URL',
];
const missingVariables: string[] = [];

mandatoryVariables.forEach((variable) => {
if (!this.configService.get(variable)) {
missingVariables.push(variable);
}
});

if (missingVariables.length > 0) {
throw new EnvironmentVariableMissingError(missingVariables.join(', '));
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsEnum, IsNumber, IsOptional, Max, Min } from 'class-validator';
import { SortOrder } from '../enums/global-common.interface';
import { SortOrder } from '../enums/global-common';
import { AutoMap } from '@automapper/classes';

export abstract class PageableDto {
MWBlocky marked this conversation as resolved.
Show resolved Hide resolved
export abstract class PageableClasses {
@AutoMap()
@ApiPropertyOptional({
minimum: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ export class ExchangeOracleGateway {
const options: AxiosRequestConfig = {
method: HttpMethod.GET,
url: `${details.exchangeOracleUrl}/stats`,
headers: {
Authorization: details.token,
},
};
return this.callExternalHttpUtilRequest<OracleStatisticsResponse>(options);
}
async fetchAssignedJobs(
details: JobsFetchParamsDetails,
): Promise<JobsFetchResponse> {
console.log("Details: ", details);
const jobFetchParamsData = this.mapper.map(
details.data,
JobsFetchParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HttpService } from '@nestjs/axios';
import { ExchangeOracleGateway } from '../exchange-oracle.gateway';
import {
oracleStatsDetailsFixture,
statisticsExchangeOracleAddress, statisticsExchangeOracleUrl,
statisticsExchangeOracleUrl,
userStatsDetailsFixture,
} from '../../../modules/statistics/spec/statistics.fixtures';
import { AutomapperModule } from '@automapper/nestjs';
Expand Down Expand Up @@ -92,7 +92,9 @@ describe('ExchangeOracleApiGateway', () => {
describe('fetchOracleStatistics', () => {
it('should successfully call the requested url for oracle statistics', async () => {
const details = oracleStatsDetailsFixture;
nock(statisticsExchangeOracleUrl).get('/stats').reply(200);
nock(statisticsExchangeOracleUrl).get('/stats')
.matchHeader('Authorization', `Bearer ${details.token}`)
MWBlocky marked this conversation as resolved.
Show resolved Hide resolved
.reply(200);
await gateway.fetchOracleStatistics(details);
expect(httpService.request).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { EnvironmentConfigService } from '../../common/config/environment-config.service';
import { ethers } from 'ethers';
import { KVStoreClient } from '@human-protocol/sdk';
import { KVStoreClient, KVStoreKeys } from '@human-protocol/sdk';

@Injectable()
export class KvStoreGateway {
private URL_KEY = 'url';
private URL_KEY = KVStoreKeys.url;
private kvStoreClient: KVStoreClient;
constructor(private environmentConfig: EnvironmentConfigService) {}
async onModuleInit(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import { KVStoreClient } from '@human-protocol/sdk';
import { KVStoreClient, KVStoreKeys } from '@human-protocol/sdk';
import { KvStoreGateway } from '../kv-store-gateway.service';
import { EnvironmentConfigService } from '../../../common/config/environment-config.service';
import { ethers } from 'ethers';
Expand All @@ -12,6 +12,9 @@ jest.mock('@human-protocol/sdk', () => ({
}),
),
},
KVStoreKeys: {
url: 'url',
MWBlocky marked this conversation as resolved.
Show resolved Hide resolved
},
}));

describe('KvStoreGateway', () => {
Expand Down Expand Up @@ -75,6 +78,11 @@ describe('KvStoreGateway', () => {
'url',
);

expect(service['kvStoreClient'].get).toHaveBeenCalledWith(
testAddress,
KVStoreKeys.url,
);

expect(result).toBe(expectedUrl);
});
});
Expand Down
1 change: 0 additions & 1 deletion packages/apps/human-app/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ async function bootstrap() {

const configService: ConfigService = app.get(ConfigService);
const envConfigService = new EnvironmentConfigService(configService);
envConfigService.checkMandatoryConfig();
if (envConfigService.isCorsEnabled) {
app.enableCors({
origin: envConfigService.corsEnabledOrigin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
Body,
Controller,
Get,
Headers,
Post,
Query,
UsePipes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export class JobAssignmentService {
command: JobAssignmentCommand,
): Promise<JobAssignmentResponse> {
const exchangeOracleUrl =
await this.kvStoreGateway.getExchangeOracleUrlByAddress(command.address);
await this.kvStoreGateway.getExchangeOracleUrlByAddress(
command.data.escrowAddress,
);
const details = this.mapper.map(
command,
JobAssignmentCommand,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@ import { AutoMap } from '@automapper/classes';
import { IsEnum, IsNumber, IsOptional, IsString } from 'class-validator';
import {
PageableData,
PageableDto,
PageableClasses,
PageableParams,
} from '../../../common/interfaces/pageable.interface';
} from '../../../common/utils/pageable-classes';
import {
AssignmentSortField,
AssignmentStatus,
} from '../../../common/enums/global-common.interface';
} from '../../../common/enums/global-common';
import { Type } from 'class-transformer';

export class JobAssignmentDto {
@AutoMap()
@IsString()
@ApiProperty()
address: string;
@AutoMap()
@IsString()
@ApiProperty()
Expand All @@ -39,8 +35,6 @@ export class JobAssignmentCommand {
data: JobAssignmentParams;
@AutoMap()
token: string;
@AutoMap()
address: string;
}

export class JobAssignmentDetails {
Expand Down Expand Up @@ -71,7 +65,7 @@ export class JobAssignmentResponse {
expires_at: string;
}

export class JobsFetchParamsDto extends PageableDto {
export class JobsFetchParamsDto extends PageableClasses {
@AutoMap()
@ApiProperty()
address: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
AssignmentSortField,
AssignmentStatus,
SortOrder,
} from '../../../common/enums/global-common.interface';
} from '../../../common/enums/global-common';
const EXCHANGE_ORACLE_URL = 'https://www.example.com/api';
const EXCHANGE_ORACLE_ADDRESS = '0x34df642';
const ESCROW_ADDRESS = 'test_address';
Expand All @@ -40,7 +40,6 @@ const TOKEN = 'test_user_token';
export const jobAssignmentToken = TOKEN;
export const jobAssignmentOracleUrl = EXCHANGE_ORACLE_URL;
export const jobAssignmentDtoFixture: JobAssignmentDto = {
address: EXCHANGE_ORACLE_ADDRESS,
escrow_address: ESCROW_ADDRESS,
chain_id: CHAIN_ID,
};
Expand All @@ -52,7 +51,6 @@ const jobAssignmentParams: JobAssignmentParams = {
export const jobAssignmentCommandFixture: JobAssignmentCommand = {
data: jobAssignmentParams,
token: TOKEN,
address: EXCHANGE_ORACLE_ADDRESS,
};
export const jobAssignmentDetailsFixture: JobAssignmentDetails = {
data: jobAssignmentParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('JobAssignmentService', () => {

expect(
kvStoreGatewayMock.getExchangeOracleUrlByAddress,
).toHaveBeenCalledWith(command.address);
).toHaveBeenCalledWith(command.data.escrowAddress);
expect(
exchangeOracleGatewayMock.postNewJobAssignment,
).toHaveBeenCalledWith(details);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
JobDiscoveryFieldName,
JobDiscoverySortField,
JobStatus,
} from '../../../common/enums/global-common.interface';
} from '../../../common/enums/global-common';
import {
PageableData,
PageableDto,
PageableClasses,
PageableParams,
} from '../../../common/interfaces/pageable.interface';
} from '../../../common/utils/pageable-classes';

export class JobsDiscoveryParamsDto extends PageableDto {
export class JobsDiscoveryParamsDto extends PageableClasses {
@AutoMap()
@IsString()
@ApiProperty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
JobDiscoveryFieldName,
JobDiscoverySortField, JobStatus,
SortOrder,
} from '../../../common/enums/global-common.interface';
} from '../../../common/enums/global-common';
const EXCHANGE_ORACLE_URL = 'https://www.test_url.org';
const ESCROW_ADDRESS = 'test_address';
const CHAIN_ID = 1;
Expand Down
Loading
Loading