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

[Job Launcher] Get reputation networks using sdk #2458

Merged
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 @@ -16,9 +16,6 @@ export class Web3ConfigService {
get reputationOracleAddress(): string {
return this.configService.get<string>('REPUTATION_ORACLE_ADDRESS', '');
}
get reputationOracles(): string {
return this.configService.get<string>('REPUTATION_ORACLES', '');
}
get fortuneExchangeOracleAddress(): string {
return this.configService.get<string>(
'FORTUNE_EXCHANGE_ORACLE_ADDRESS',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
MOCK_ADDRESS,
MOCK_GAS_PRICE_MULTIPLIER,
MOCK_PRIVATE_KEY,
MOCK_REPUTATION_ORACLES,
} from './../../../test/constants';
import { NetworkConfigService } from '../../common/config/network-config.service';
import { Web3ConfigService } from '../../common/config/web3-config.service';
Expand Down Expand Up @@ -43,8 +42,6 @@ describe('Web3Service', () => {
return MOCK_GAS_PRICE_MULTIPLIER;
case 'RPC_URL_POLYGON_AMOY':
return 'http://0.0.0.0:8545';
case 'REPUTATION_ORACLES':
return MOCK_REPUTATION_ORACLES;
default:
return defaultValue;
}
Expand Down Expand Up @@ -304,17 +301,20 @@ describe('Web3Service', () => {
});

describe('getReputationOraclesByJobType', () => {
beforeEach(async () => {
mockConfigService.get = jest
.fn()
.mockReturnValue(MOCK_REPUTATION_ORACLES);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should return matching oracle addresses based on job type', async () => {
const mockLeader = {
address: '0x0000000000000000000000000000000000000000',
reputationNetworks: [
'0x0000000000000000000000000000000000000001',
'0x0000000000000000000000000000000000000002',
'0x0000000000000000000000000000000000000003',
],
};

const mockLeader1 = {
address: '0x0000000000000000000000000000000000000001',
jobTypes: ['Points'],
Expand All @@ -329,6 +329,7 @@ describe('Web3Service', () => {
};

(OperatorUtils.getLeader as jest.Mock)
.mockResolvedValueOnce(mockLeader)
.mockResolvedValueOnce(mockLeader1)
.mockResolvedValueOnce(mockLeader2)
.mockResolvedValueOnce(mockLeader3);
Expand All @@ -342,10 +343,52 @@ describe('Web3Service', () => {
'0x0000000000000000000000000000000000000001',
'0x0000000000000000000000000000000000000003',
]);
expect(OperatorUtils.getLeader).toHaveBeenCalledTimes(3);
expect(OperatorUtils.getLeader).toHaveBeenCalledTimes(4);
});

it('should return an empty array if reputation networks not found for chain', async () => {
const mockLeader = {
address: '0x0000000000000000000000000000000000000000',
};

const mockLeader1 = {
address: '0x0000000000000000000000000000000000000001',
jobTypes: ['NewJobType1'],
};
const mockLeader2 = {
address: '0x0000000000000000000000000000000000000002',
jobTypes: ['NewJobType2'],
};
const mockLeader3 = {
address: '0x0000000000000000000000000000000000000003',
jobTypes: ['NewJobType3'],
};

(OperatorUtils.getLeader as jest.Mock)
.mockResolvedValueOnce(mockLeader)
.mockResolvedValueOnce(mockLeader1)
.mockResolvedValueOnce(mockLeader2)
.mockResolvedValueOnce(mockLeader3);

const result = await web3Service.getReputationOraclesByJobType(
ChainId.POLYGON_AMOY,
'Points',
);

expect(result).toEqual([]);
expect(OperatorUtils.getLeader).toHaveBeenCalledTimes(1);
});

it('should return an empty array if no oracles match the job type', async () => {
const mockLeader = {
address: '0x0000000000000000000000000000000000000000',
reputationNetworks: [
'0x0000000000000000000000000000000000000001',
'0x0000000000000000000000000000000000000002',
'0x0000000000000000000000000000000000000003',
],
};

const mockLeader1 = {
address: '0x0000000000000000000000000000000000000001',
jobTypes: ['NewJobType1'],
Expand All @@ -360,6 +403,7 @@ describe('Web3Service', () => {
};

(OperatorUtils.getLeader as jest.Mock)
.mockResolvedValueOnce(mockLeader)
.mockResolvedValueOnce(mockLeader1)
.mockResolvedValueOnce(mockLeader2)
.mockResolvedValueOnce(mockLeader3);
Expand All @@ -370,7 +414,7 @@ describe('Web3Service', () => {
);

expect(result).toEqual([]);
expect(OperatorUtils.getLeader).toHaveBeenCalledTimes(3);
expect(OperatorUtils.getLeader).toHaveBeenCalledTimes(1);
});

it('should handle errors from getLeader and return an empty array', async () => {
Expand All @@ -384,7 +428,7 @@ describe('Web3Service', () => {
);

expect(result).toEqual([]);
expect(OperatorUtils.getLeader).toHaveBeenCalledTimes(3);
expect(OperatorUtils.getLeader).toHaveBeenCalledTimes(1);
});

it('should return an empty array if no reputation oracles are configured', async () => {
Expand All @@ -396,7 +440,7 @@ describe('Web3Service', () => {
);

expect(result).toEqual([]);
expect(OperatorUtils.getLeader).not.toHaveBeenCalled();
expect(OperatorUtils.getLeader).toHaveBeenCalledTimes(1);
});
});
});
26 changes: 17 additions & 9 deletions packages/apps/job-launcher/server/src/modules/web3/web3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,27 @@ export class Web3Service {
chainId: ChainId,
jobType: string,
): Promise<string[]> {
const oracleAddresses = this.web3ConfigService.reputationOracles
.split(',')
.map((address) => address.trim())
.filter((address) => address);
const leader = await OperatorUtils.getLeader(
chainId,
this.getOperatorAddress(),
);

if (!leader || !leader.reputationNetworks) {
this.logger.error(
`Leader or reputation networks not found for chain ${chainId}.`,
);
return [];
}

const matchingOracles = await Promise.all(
oracleAddresses.map(async (address) => {
leader.reputationNetworks.map(async (address) => {
try {
const leader = await OperatorUtils.getLeader(chainId, address);
const networkLeader = await OperatorUtils.getLeader(chainId, address);
console.log(1111, networkLeader);

return leader?.jobTypes &&
this.matchesJobType(leader.jobTypes, jobType)
? leader.address
return networkLeader?.jobTypes &&
this.matchesJobType(networkLeader.jobTypes, jobType)
? networkLeader.address
: null;
} catch (error) {
this.logger.error(
Expand Down
2 changes: 0 additions & 2 deletions packages/apps/job-launcher/server/test/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export const MOCK_BUCKET_FILES = [
export const MOCK_PRIVATE_KEY =
'd334daf65a631f40549cc7de126d5a0016f32a2d00c49f94563f9737f7135e55';
export const MOCK_GAS_PRICE_MULTIPLIER = 1;
export const MOCK_REPUTATION_ORACLES =
'0x0000000000000000000000000000000000000001,0x0000000000000000000000000000000000000002,0x0000000000000000000000000000000000000003';
export const MOCK_WEB3_RPC_URL = 'http://localhost:8545';
export const MOCK_WEB3_NODE_HOST = 'localhost';
export const MOCK_BUCKET_NAME = 'bucket-name';
Expand Down