Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,11 @@ export class JobService {
recordingOracleSolutions.filter((solution) => !solution.error).length >=
submissionsRequired
) {
let reputationOracleWebhook: string | null = null;
try {
const reputationOracleAddress =
await escrowClient.getReputationOracleAddress(webhook.escrowAddress);
reputationOracleWebhook = (await KVStoreUtils.get(
webhook.chainId,
reputationOracleAddress,
KVStoreKeys.webhookUrl,
)) as string;
} catch {
//Ignore the error
}
const reputationOracleWebhook = await KVStoreUtils.get(
webhook.chainId,
await escrowClient.getReputationOracleAddress(webhook.escrowAddress),
KVStoreKeys.webhookUrl,
);

if (reputationOracleWebhook) {
await sendWebhook(
Expand All @@ -239,16 +232,11 @@ export class JobService {
}

if (errorSolutions.length) {
let exchangeOracleURL: string | null = null;
try {
exchangeOracleURL = (await KVStoreUtils.get(
webhook.chainId,
await escrowClient.getExchangeOracleAddress(webhook.escrowAddress),
KVStoreKeys.webhookUrl,
)) as string;
} catch {
//Ignore the error
}
const exchangeOracleURL = await KVStoreUtils.get(
webhook.chainId,
await escrowClient.getExchangeOracleAddress(webhook.escrowAddress),
KVStoreKeys.webhookUrl,
);

if (exchangeOracleURL) {
const eventData: AssignmentRejection[] = errorSolutions.map(
Expand Down Expand Up @@ -311,18 +299,11 @@ export class JobService {
{ timeoutMs: this.web3ConfigService.txTimeoutMs },
);

let reputationOracleWebhook: string | null = null;
try {
const reputationOracleAddress =
await escrowClient.getReputationOracleAddress(webhook.escrowAddress);
reputationOracleWebhook = (await KVStoreUtils.get(
webhook.chainId,
reputationOracleAddress,
KVStoreKeys.webhookUrl,
)) as string;
} catch {
//Ignore the error
}
const reputationOracleWebhook = await KVStoreUtils.get(
webhook.chainId,
await escrowClient.getReputationOracleAddress(webhook.escrowAddress),
KVStoreKeys.webhookUrl,
);

if (reputationOracleWebhook) {
await sendWebhook(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
ChainId,
InvalidKeyError,
KVStoreKeys,
KVStoreUtils,
} from '@human-protocol/sdk';
import { ChainId, KVStoreKeys, KVStoreUtils } from '@human-protocol/sdk';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { HttpException, Inject, Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';
Expand Down Expand Up @@ -36,9 +31,7 @@ export class KvStoreGateway {

oracleUrl = await KVStoreUtils.get(chainId, address, KVStoreKeys.url);
} catch (error) {
if (error instanceof InvalidKeyError) {
oracleUrl = '';
} else if (error.toString().includes('Error: Invalid address')) {
if (error.toString().includes('Error: Invalid address')) {
throw new HttpException(
`Unable to retrieve URL from address: ${address}`,
400,
Expand All @@ -53,7 +46,7 @@ export class KvStoreGateway {
}
}

if (!oracleUrl || oracleUrl === '') {
if (!oracleUrl) {
throw new HttpException('Oracle does not have URL set in KV store', 422);
}

Expand Down Expand Up @@ -92,7 +85,7 @@ export class KvStoreGateway {
}
}

if (!jobTypes || jobTypes === '') {
if (!jobTypes) {
return;
} else {
await this.cacheManager.set(
Expand Down
16 changes: 5 additions & 11 deletions packages/apps/job-launcher/server/src/modules/job/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,17 +780,11 @@ export class JobService {
oracleAddress: string,
chainId: ChainId,
): Promise<bigint> {
let feeValue: string | undefined;

try {
feeValue = await KVStoreUtils.get(
chainId,
oracleAddress,
KVStoreKeys.fee,
);
} catch {
// Ignore error
}
const feeValue = await KVStoreUtils.get(
chainId,
oracleAddress,
KVStoreKeys.fee,
);

return BigInt(feeValue ? feeValue : 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,23 @@ describe.only('QualificationService', () => {
expect(result).toEqual(qualifications);
});

it('should throw a ServerError when KVStoreUtils.get fails', async () => {
(KVStoreUtils.get as any).mockRejectedValue(new Error('KV store error'));
it('should throw a ServerError when reputation oracle url not set', async () => {
(KVStoreUtils.get as any).mockResolvedValueOnce('');

await expect(
qualificationService.getQualifications(ChainId.LOCALHOST),
).rejects.toThrow(new ServerError(ErrorWeb3.ReputationOracleUrlNotSet));
});

it('should throw a ServerError when KVStoreUtils.get fails', async () => {
const syntheticError = new Error('KV store error');
(KVStoreUtils.get as any).mockRejectedValue(syntheticError);

await expect(
qualificationService.getQualifications(ChainId.LOCALHOST),
).rejects.toThrow(new ServerError(syntheticError.message));
});

it('should throw a ServerError when HTTP request fails', async () => {
(KVStoreUtils.get as any).mockResolvedValue(MOCK_REPUTATION_ORACLE_URL);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,15 @@ export class QualificationService {
public async getQualifications(
chainId: ChainId,
): Promise<QualificationDto[]> {
let reputationOracleUrl = '';
this.web3Service.validateChainId(chainId);

try {
reputationOracleUrl = await KVStoreUtils.get(
chainId,
this.web3ConfigService.reputationOracleAddress,
KVStoreKeys.url,
);
} catch {
// Ignore error
}
const reputationOracleUrl = await KVStoreUtils.get(
chainId,
this.web3ConfigService.reputationOracleAddress,
KVStoreKeys.url,
);

if (!reputationOracleUrl || reputationOracleUrl === '') {
if (!reputationOracleUrl) {
throw new ServerError(ErrorWeb3.ReputationOracleUrlNotSet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ export class InvalidOperatorSignupDataError extends BaseError {
}

export class InvalidOperatorRoleError extends InvalidOperatorSignupDataError {
constructor(role: string) {
constructor(role?: string) {
super(`Invalid role: ${role}`);
}
}

export class InvalidOperatorFeeError extends InvalidOperatorSignupDataError {
constructor(fee: string) {
constructor(fee?: string) {
super(`Invalid fee: ${fee}`);
}
}

export class InvalidOperatorUrlError extends InvalidOperatorSignupDataError {
constructor(url: string) {
constructor(url?: string) {
super(`Invalid url: ${url}`);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe('AuthService', () => {
return faker.internet.url();
}

throw new Error('Invalid key');
return '';
},
);

Expand Down Expand Up @@ -278,7 +278,7 @@ describe('AuthService', () => {
return mockedRole;
}

throw new Error('Invalid key');
return '';
},
);

Expand All @@ -301,9 +301,7 @@ describe('AuthService', () => {

mockUserRepository.findOneByAddress.mockResolvedValueOnce(null);
const mockedRole = faker.string.alpha();
mockKVStoreUtils.get.mockImplementation(async () => {
throw new Error('Invalid key');
});
mockKVStoreUtils.get.mockResolvedValueOnce('');

await expect(
service.web3Signup(signature, ethWallet.address),
Expand Down Expand Up @@ -332,7 +330,7 @@ describe('AuthService', () => {
return '';
}

throw new Error('Invalid key');
return '';
},
);

Expand Down Expand Up @@ -360,7 +358,7 @@ describe('AuthService', () => {
return Role.ExchangeOracle;
}

throw new Error('Invalid key');
return '';
},
);

Expand Down Expand Up @@ -400,7 +398,7 @@ describe('AuthService', () => {
return invalidUrl;
}

throw new Error('Invalid key');
return '';
},
);

Expand Down Expand Up @@ -432,7 +430,7 @@ describe('AuthService', () => {
return String(faker.number.int({ min: 1, max: 50 }));
}

throw new Error('Invalid key');
return '';
},
);

Expand Down Expand Up @@ -821,7 +819,7 @@ describe('AuthService', () => {
return mockedOperatorStatus;
}

throw new Error('Invalid key');
return '';
},
);

Expand Down Expand Up @@ -855,9 +853,7 @@ describe('AuthService', () => {
accessToken: faker.string.alpha(),
refreshToken: faker.string.uuid(),
});
mockKVStoreUtils.get.mockImplementation(async () => {
throw new Error('Invalid key');
});
mockKVStoreUtils.get.mockResolvedValueOnce('');

await service.web3Auth(operator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,8 @@ export class AuthService {
}

const chainId = this.web3ConfigService.reputationNetworkChainId;
let role = '';
try {
role = await KVStoreUtils.get(chainId, address, KVStoreKeys.role);
} catch {
// noop
}

const role = await KVStoreUtils.get(chainId, address, KVStoreKeys.role);
// We need to exclude ReputationOracle role
const isValidRole = [
Role.JobLauncher,
Expand All @@ -131,22 +126,12 @@ export class AuthService {
throw new InvalidOperatorRoleError(role);
}

let fee = '';
try {
fee = await KVStoreUtils.get(chainId, address, KVStoreKeys.fee);
} catch {
// noop
}
const fee = await KVStoreUtils.get(chainId, address, KVStoreKeys.fee);
if (!fee) {
throw new InvalidOperatorFeeError(fee);
}

let url = '';
try {
url = await KVStoreUtils.get(chainId, address, KVStoreKeys.url);
} catch {
// noop
}
const url = await KVStoreUtils.get(chainId, address, KVStoreKeys.url);
if (!url || !httpUtils.isValidHttpUrl(url)) {
throw new InvalidOperatorUrlError(url);
}
Expand Down Expand Up @@ -317,16 +302,12 @@ export class AuthService {
* and subgraph does not have the actual value yet,
* the status can be outdated
*/
let operatorStatus = OperatorStatus.INACTIVE;
try {
operatorStatus = (await KVStoreUtils.get(
const operatorStatus =
(await KVStoreUtils.get(
this.web3ConfigService.reputationNetworkChainId,
this.web3ConfigService.operatorAddress,
userEntity.evmAddress,
)) as OperatorStatus;
} catch {
// noop
}
)) || OperatorStatus.INACTIVE;

const jwtPayload = {
status: userEntity.status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,11 @@ export class UserService {
const signer = this.web3Service.getSigner(chainId);
const kvstore = await KVStoreClient.build(signer);

let status: string | undefined;
try {
status = await KVStoreUtils.get(
chainId,
signer.address,
operatorUser.evmAddress,
);
} catch {
// noop
}
const status = await KVStoreUtils.get(
chainId,
signer.address,
operatorUser.evmAddress,
);

if (status === OperatorStatus.ACTIVE) {
throw new UserError(
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/python/human-protocol-sdk/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ format:
pipenv run black .

unit-test:
make build-contracts
./scripts/run-unit-test.sh

run-test:
make build-contracts

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script is used in CI, but locally it makes sense to have just unit-test to not rebuild contracts every time

make unit-test

build-package:
Expand Down
Loading
Loading