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

Test: TasksService, ImporterService, ValidatorService #10

Merged
merged 4 commits into from
Mar 20, 2024
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
11 changes: 3 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ on:
- main
tags:
- 'v*'
pull_request:
branches: [ main ]
types: [ opened ]
pull_request: {}

jobs:
main:
Expand All @@ -31,21 +29,18 @@ jobs:
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 100

- uses: actions/cache@v2
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}

- name: npm install
run: npm ci

- name: npm lint
run: npm run lint

Comment on lines -46 to -48

Choose a reason for hiding this comment

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

would be nice to keep this somewhere

Copy link
Member Author

@tmrdlt tmrdlt Mar 20, 2024

Choose a reason for hiding this comment

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

As mentioned above, I removed it because linting now also happens in the Code Check CI (At least npm run lint:check. npm run lint also fixes potential errors, but I don't see why this is needed in the tests).

Choose a reason for hiding this comment

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

was on the wrong branch 🤦

- name: unit test
run: npm run test

Expand Down
157 changes: 157 additions & 0 deletions src/tasks/pair-liquidity-info-history-importer.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { PairLiquidityInfoHistoryImporterService } from './pair-liquidity-info-history-importer.service';
import { Test, TestingModule } from '@nestjs/testing';
import { MdwClientService } from '../clients/mdw-client.service';
import { PairService } from '../database/pair.service';
import { PairLiquidityInfoHistoryService } from '../database/pair-liquidity-info-history.service';
import { PairLiquidityInfoHistoryErrorService } from '../database/pair-liquidity-info-history-error.service';
import { ContractAddress } from '../lib/utils';
import { Contract } from '../clients/mdw-client.model';

const mockMdwClientService = {
getContract: jest.fn(),
getMicroBlock: jest.fn(),
getContractLogsUntilCondition: jest.fn(),
getContractBalancesAtMicroBlockHashV1: jest.fn(),
getAccountBalanceForContractAtMicroBlockHash: jest.fn(),
};

const mockPairService = {
getAll: jest.fn(),
};

const mockPairLiquidityInfoHistoryService = {
getLastlySyncedBlockByPairId: jest.fn(),
upsert: jest.fn(),
};

const mockPairLiquidityInfoHistoryErrorService = {
getErrorByPairIdAndMicroBlockHashWithinHours: jest.fn(),
};

describe('PairLiquidityInfoHistoryImporterService', () => {
let service: PairLiquidityInfoHistoryImporterService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
PairLiquidityInfoHistoryImporterService,
{ provide: MdwClientService, useValue: mockMdwClientService },
{ provide: PairService, useValue: mockPairService },
{
provide: PairLiquidityInfoHistoryService,
useValue: mockPairLiquidityInfoHistoryService,
},
{
provide: PairLiquidityInfoHistoryErrorService,
useValue: mockPairLiquidityInfoHistoryErrorService,
},
],
}).compile();
service = module.get<PairLiquidityInfoHistoryImporterService>(
PairLiquidityInfoHistoryImporterService,
);
});

describe('import', () => {
it('should import liquidity correctly', async () => {
// Mock data
const pair1 = {
id: 1,
address: 'ct_pair' as ContractAddress,
token0: { address: 'ct_token0' },
token1: { address: 'ct_token1' },
};
const pair1Contract: Contract = {
aexn_type: '',
block_hash: 'mh_hash0',
contract: pair1.address,
source_tx_hash: 'th_',
source_tx_type: '',
create_tx: {},
};
const initialMicroBlock = {
hash: pair1Contract.block_hash,
height: '10000',
time: '1000000000000',
};
const pairContractLog1 = {
block_time: '1000000000001',
block_hash: 'mh_hash1',
height: '10001',
};
const pairContractLog2 = {
block_time: '1000000000002',
block_hash: 'mh_hash2',
height: '10002',
};

// Mock functions
mockPairService.getAll.mockResolvedValue([pair1]);
mockPairLiquidityInfoHistoryErrorService.getErrorByPairIdAndMicroBlockHashWithinHours.mockResolvedValue(
undefined,
);
mockPairLiquidityInfoHistoryService.getLastlySyncedBlockByPairId.mockReturnValue(
undefined,
);
mockMdwClientService.getContract.mockResolvedValue(pair1Contract);
mockMdwClientService.getMicroBlock.mockResolvedValue(initialMicroBlock);
mockPairLiquidityInfoHistoryService.upsert.mockResolvedValue(null);
mockMdwClientService.getContractLogsUntilCondition.mockResolvedValue([
pairContractLog1,
pairContractLog2,
]);
mockMdwClientService.getContractBalancesAtMicroBlockHashV1.mockResolvedValue(
{ amounts: { ak_a: '1', ak_b: '1' } },
);
mockMdwClientService.getAccountBalanceForContractAtMicroBlockHash.mockResolvedValue(
{ amount: '1' },
);
jest.spyOn(service.logger, 'log');

// Start import
await service.import();

// Assertions
// Insert initial liquidity
expect(mockPairLiquidityInfoHistoryService.upsert).toHaveBeenCalledWith({
pairId: pair1.id,
totalSupply: '0',
reserve0: '0',
reserve1: '0',
height: parseInt(initialMicroBlock.height),
microBlockHash: initialMicroBlock.hash,
microBlockTime: BigInt(initialMicroBlock.time),
});
expect(
mockPairLiquidityInfoHistoryErrorService.getErrorByPairIdAndMicroBlockHashWithinHours,
).toHaveBeenCalledTimes(3);
expect(service.logger.log).toHaveBeenCalledWith(
`Started syncing pair ${pair1.id} ${pair1.address}. Need to sync 2 micro block(s). This can take some time.`,
);
expect(mockPairLiquidityInfoHistoryService.upsert).toHaveBeenCalledWith({
pairId: pair1.id,
totalSupply: '2',
reserve0: '1',
reserve1: '1',
height: parseInt(pairContractLog1.height),
microBlockHash: pairContractLog1.block_hash,
microBlockTime: BigInt(pairContractLog1.block_time),
});
expect(mockPairLiquidityInfoHistoryService.upsert).toHaveBeenCalledWith({
pairId: pair1.id,
totalSupply: '2',
reserve0: '1',
reserve1: '1',
height: parseInt(pairContractLog2.height),
microBlockHash: pairContractLog2.block_hash,
microBlockTime: BigInt(pairContractLog2.block_time),
});
expect(service.logger.log).toHaveBeenCalledWith(
`Completed sync for pair ${pair1.id} ${pair1.address}. Synced 2 micro block(s).`,
);
expect(service.logger.log).toHaveBeenCalledWith(
'Finished liquidity info history sync for all pairs.',
);
});
});
});
26 changes: 3 additions & 23 deletions src/tasks/pair-liquidity-info-history-importer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import {
} from '../lib/utils';
import { PairLiquidityInfoHistoryErrorService } from '../database/pair-liquidity-info-history-error.service';
import { getClient } from '../lib/contracts';
import { Cron, CronExpression } from '@nestjs/schedule';
import { ContractLog } from '../clients/mdw-client.model';
import { TasksService } from './tasks.service';

type MicroBlock = {
hash: MicroBlockHash;
Expand All @@ -23,34 +21,16 @@ type MicroBlock = {
@Injectable()
export class PairLiquidityInfoHistoryImporterService {
constructor(
private tasksService: TasksService,
private mdwClientService: MdwClientService,
private pairService: PairService,
private pairLiquidityInfoHistoryService: PairLiquidityInfoHistoryService,
private pairLiquidityInfoHistoryErrorService: PairLiquidityInfoHistoryErrorService,
) {}

private readonly logger = new Logger(
PairLiquidityInfoHistoryImporterService.name,
);
readonly logger = new Logger(PairLiquidityInfoHistoryImporterService.name);

readonly WITHIN_HOURS_TO_SKIP_IF_ERROR = 6;

@Cron(CronExpression.EVERY_5_MINUTES)
async runTask() {
try {
if (!this.tasksService.isRunning) {
this.tasksService.setIsRunning(true);
await this.syncPairLiquidityInfoHistory();
this.tasksService.setIsRunning(false);
}
} catch (error) {
this.logger.error(`Sync failed. ${error}`);
this.tasksService.setIsRunning(false);
}
}

private async syncPairLiquidityInfoHistory() {
async import() {
this.logger.log(`Started syncing pair liquidity info history.`);

// Fetch all pairs from DB
Expand Down Expand Up @@ -211,7 +191,7 @@ export class PairLiquidityInfoHistoryImporterService {
}
}

this.logger.log(`Finished liquidity info history sync for all pairs.`);
this.logger.log('Finished liquidity info history sync for all pairs.');
}

private async insertInitialLiquidity(pairWithTokens: PairWithTokens) {
Expand Down
110 changes: 110 additions & 0 deletions src/tasks/pair-liquidity-info-history-validator.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { PairLiquidityInfoHistoryValidatorService } from './pair-liquidity-info-history-validator.service';
import { Test, TestingModule } from '@nestjs/testing';
import { MdwClientService } from '../clients/mdw-client.service';
import { PairLiquidityInfoHistoryService } from '../database/pair-liquidity-info-history.service';

const mockMdwClientService = {
getKeyBlockMicroBlocks: jest.fn(),
};

const mockPairLiquidityInfoHistoryService = {
getWithinHeightSorted: jest.fn(),
deleteFromMicroBlockTime: jest.fn(),
};

describe('PairLiquidityInfoHistoryValidatorService', () => {
let service: PairLiquidityInfoHistoryValidatorService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
PairLiquidityInfoHistoryValidatorService,
{ provide: MdwClientService, useValue: mockMdwClientService },
{
provide: PairLiquidityInfoHistoryService,
useValue: mockPairLiquidityInfoHistoryService,
},
],
}).compile();
service = module.get<PairLiquidityInfoHistoryValidatorService>(
PairLiquidityInfoHistoryValidatorService,
);
});

describe('validate', () => {
it('should validate history correctly', async () => {
// Mock data
const historyEntry1 = {
height: 10001,
microBlockTime: 1000000000001n,
microBlockHash: 'mh_hash1',
};
const historyEntry2 = {
height: 10001,
microBlockTime: 1000000000002n,
microBlockHash: 'mh_hash2',
};
const historyEntry3 = {
height: 10003,
microBlockTime: 1000000000003n,
microBlockHash: 'mh_hash3',
};
const historyEntry4 = {
height: 10004,
microBlockTime: 1000000000004n,
microBlockHash: 'mh_hash4',
};
const historyEntry5 = {
height: 10005,
microBlockTime: 1000000000005n,
microBlockHash: 'mh_hash5',
};
// Mock functions
mockPairLiquidityInfoHistoryService.getWithinHeightSorted.mockResolvedValue(
[historyEntry1, historyEntry2, historyEntry3, historyEntry4],
);
mockMdwClientService.getKeyBlockMicroBlocks.mockImplementation(
(height: number) => {
if (height === historyEntry1.height) {
return [
{ hash: historyEntry1.microBlockHash },
{ hash: historyEntry2.microBlockHash },
{ hash: 'mh_xyz' },
];
} else if (height === historyEntry3.height) {
return [{ hash: historyEntry3.microBlockHash }, { hash: 'mh_xyz' }];
} else {
return [];
}
},
);
mockPairLiquidityInfoHistoryService.deleteFromMicroBlockTime.mockResolvedValue(
{ count: 2 },
);
jest.spyOn(service.logger, 'log');

// Start validation
await service.validate();

// Assertions
expect(mockMdwClientService.getKeyBlockMicroBlocks).toHaveBeenCalledWith(
historyEntry1.height,
);
expect(mockMdwClientService.getKeyBlockMicroBlocks).toHaveBeenCalledWith(
historyEntry3.height,
);
expect(mockMdwClientService.getKeyBlockMicroBlocks).toHaveBeenCalledWith(
historyEntry4.height,
);
expect(
mockMdwClientService.getKeyBlockMicroBlocks,
).not.toHaveBeenCalledWith(historyEntry5.height);
expect(
mockPairLiquidityInfoHistoryService.deleteFromMicroBlockTime,
).toHaveBeenCalledWith(historyEntry4.microBlockTime);
expect(service.logger.log).toHaveBeenCalledWith(
`Found an inconsistency in pair liquidity info history. Deleted 2 entries.`,
);
});
});
});
Loading