Skip to content

Commit

Permalink
fix: spec
Browse files Browse the repository at this point in the history
  • Loading branch information
eddort committed Mar 13, 2023
1 parent 14092e7 commit 958938b
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 25 deletions.
38 changes: 38 additions & 0 deletions src/contracts/deposit/deposit.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,49 @@ import { LoggerModule } from 'common/logger';
import { ConfigModule } from 'common/config';
import { APP_VERSION } from 'app.constants';
import { BlsService } from 'bls';
import { LocatorService } from 'contracts/repository/locator/locator.service';

const mockSleep = sleep as jest.MockedFunction<typeof sleep>;

const mockLocator = (locator: LocatorService) => {
const lidoAddr = jest
.spyOn(locator, 'getLidoAddress')
.mockImplementation(async () => '0x' + '1'.repeat(40));

const DSMAddr = jest
.spyOn(locator, 'getDSMAddress')
.mockImplementation(async () => '0x' + '2'.repeat(40));
const SRAddr = jest
.spyOn(locator, 'getStakingRouterAddress')
.mockImplementation(async () => '0x' + '3'.repeat(40));
const locatorAddr = jest
.spyOn(locator, 'getLocatorAddress')
.mockImplementation(async () => '0x' + '4'.repeat(40));

return { lidoAddr, locatorAddr, SRAddr, DSMAddr };
};

const mockRepository = async (repositoryService: RepositoryService) => {
const address1 = '0x' + '5'.repeat(40);

const depositAddr = jest
.spyOn(repositoryService, 'getDepositAddress')
.mockImplementation(async () => address1);

await repositoryService.initCachedContracts('latest');
jest.spyOn(repositoryService, 'getCachedLidoContract');

return { depositAddr };
};

describe('DepositService', () => {
let providerService: ProviderService;
let cacheService: CacheService<DepositEventGroup>;
let depositService: DepositService;
let loggerService: LoggerService;
let repositoryService: RepositoryService;
let blsService: BlsService;
let locatorService: LocatorService;

const depositAddress = '0x' + '1'.repeat(40);

Expand All @@ -55,10 +88,15 @@ describe('DepositService', () => {
blsService = moduleRef.get(BlsService);
loggerService = moduleRef.get(WINSTON_MODULE_NEST_PROVIDER);

locatorService = moduleRef.get(LocatorService);

jest.spyOn(loggerService, 'log').mockImplementation(() => undefined);
jest.spyOn(loggerService, 'warn').mockImplementation(() => undefined);
jest.spyOn(loggerService, 'debug').mockImplementation(() => undefined);

mockLocator(locatorService);
await mockRepository(repositoryService);

jest
.spyOn(repositoryService, 'getDepositAddress')
.mockImplementation(async () => depositAddress);
Expand Down
47 changes: 46 additions & 1 deletion src/contracts/lido/lido.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,51 @@ import { ConfigModule } from 'common/config';
import { LoggerModule } from 'common/logger';
import { MockProviderModule, ProviderService } from 'provider';
import { LidoAbi__factory } from 'generated';
import { RepositoryModule } from 'contracts/repository';
import { RepositoryModule, RepositoryService } from 'contracts/repository';
import { Interface } from '@ethersproject/abi';
import { LidoService } from './lido.service';
import { LidoModule } from './lido.module';
import { LocatorService } from 'contracts/repository/locator/locator.service';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';

const mockLocator = (locator: LocatorService) => {
const lidoAddr = jest
.spyOn(locator, 'getLidoAddress')
.mockImplementation(async () => '0x' + '1'.repeat(40));

const DSMAddr = jest
.spyOn(locator, 'getDSMAddress')
.mockImplementation(async () => '0x' + '2'.repeat(40));
const SRAddr = jest
.spyOn(locator, 'getStakingRouterAddress')
.mockImplementation(async () => '0x' + '3'.repeat(40));
const locatorAddr = jest
.spyOn(locator, 'getLocatorAddress')
.mockImplementation(async () => '0x' + '4'.repeat(40));

return { lidoAddr, locatorAddr, SRAddr, DSMAddr };
};

const mockRepository = async (repositoryService: RepositoryService) => {
const address1 = '0x' + '5'.repeat(40);

const depositAddr = jest
.spyOn(repositoryService, 'getDepositAddress')
.mockImplementation(async () => address1);

await repositoryService.initCachedContracts('latest');
jest.spyOn(repositoryService, 'getCachedLidoContract');

return { depositAddr };
};

describe('SecurityService', () => {
let lidoService: LidoService;
let providerService: ProviderService;

let repositoryService: RepositoryService;
let locatorService: LocatorService;

beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
Expand All @@ -25,6 +61,15 @@ describe('SecurityService', () => {

lidoService = moduleRef.get(LidoService);
providerService = moduleRef.get(ProviderService);

repositoryService = moduleRef.get(RepositoryService);
locatorService = moduleRef.get(LocatorService);
jest
.spyOn(moduleRef.get(WINSTON_MODULE_NEST_PROVIDER), 'log')
.mockImplementation(() => undefined);

mockLocator(locatorService);
await mockRepository(repositoryService);
});

describe('getWithdrawalCredentials', () => {
Expand Down
10 changes: 5 additions & 5 deletions src/contracts/repository/repository.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
const mockLocator = (locator: LocatorService) => {
const lidoAddr = jest
.spyOn(locator, 'getLidoAddress')
.mockImplementationOnce(async () => '0x' + '1'.repeat(40));
.mockImplementation(async () => '0x' + '1'.repeat(40));

const DSMAddr = jest
.spyOn(locator, 'getDSMAddress')
.mockImplementationOnce(async () => '0x' + '2'.repeat(40));
.mockImplementation(async () => '0x' + '2'.repeat(40));
const SRAddr = jest
.spyOn(locator, 'getStakingRouterAddress')
.mockImplementationOnce(async () => '0x' + '3'.repeat(40));
.mockImplementation(async () => '0x' + '3'.repeat(40));
const locatorAddr = jest
.spyOn(locator, 'getLocatorAddress')
.mockImplementationOnce(async () => '0x' + '4'.repeat(40));
.mockImplementation(async () => '0x' + '4'.repeat(40));

return { lidoAddr, locatorAddr, SRAddr, DSMAddr };
};
Expand All @@ -32,7 +32,7 @@ const mockRepository = async (repositoryService: RepositoryService) => {

const depositAddr = jest
.spyOn(repositoryService, 'getDepositAddress')
.mockImplementationOnce(async () => address1);
.mockImplementation(async () => address1);

await repositoryService.initCachedContracts('latest');
jest.spyOn(repositoryService, 'getCachedLidoContract');
Expand Down
10 changes: 5 additions & 5 deletions src/contracts/security/security.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ jest.mock('../../transport/stomp/stomp.client');
const mockLocator = (locator: LocatorService) => {
const lidoAddr = jest
.spyOn(locator, 'getLidoAddress')
.mockImplementationOnce(async () => '0x' + '1'.repeat(40));
.mockImplementation(async () => '0x' + '1'.repeat(40));

const DSMAddr = jest
.spyOn(locator, 'getDSMAddress')
.mockImplementationOnce(async () => '0x' + '2'.repeat(40));
.mockImplementation(async () => '0x' + '2'.repeat(40));
const SRAddr = jest
.spyOn(locator, 'getStakingRouterAddress')
.mockImplementationOnce(async () => '0x' + '3'.repeat(40));
.mockImplementation(async () => '0x' + '3'.repeat(40));
const locatorAddr = jest
.spyOn(locator, 'getLocatorAddress')
.mockImplementationOnce(async () => '0x' + '4'.repeat(40));
.mockImplementation(async () => '0x' + '4'.repeat(40));

return { lidoAddr, locatorAddr, SRAddr, DSMAddr };
};
Expand All @@ -41,7 +41,7 @@ const mockRepository = async (repositoryService: RepositoryService) => {

const depositAddr = jest
.spyOn(repositoryService, 'getDepositAddress')
.mockImplementationOnce(async () => address1);
.mockImplementation(async () => address1);

await repositoryService.initCachedContracts({ blockHash: '111' });
jest.spyOn(repositoryService, 'getCachedLidoContract');
Expand Down
16 changes: 8 additions & 8 deletions src/guardian/guardian.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { StakingModuleGuardModule } from './staking-module-guard';
import { BlockGuardModule, BlockGuardService } from './block-guard';
import { ScheduleModule } from 'common/schedule';
import { LocatorService } from 'contracts/repository/locator/locator.service';
import { LocatorModule } from 'contracts/repository/locator/locator.module';

jest.mock('../transport/stomp/stomp.client');

Expand Down Expand Up @@ -51,16 +50,16 @@ const stakingModuleResponse = {
const mockLocator = (locator: LocatorService) => {
const lidoAddr = jest
.spyOn(locator, 'getLidoAddress')
.mockImplementationOnce(async () => '0x' + '1'.repeat(40));
.mockImplementation(async () => '0x' + '1'.repeat(40));
const DSMAddr = jest
.spyOn(locator, 'getDSMAddress')
.mockImplementationOnce(async () => '0x' + '2'.repeat(40));
.mockImplementation(async () => '0x' + '2'.repeat(40));
const SRAddr = jest
.spyOn(locator, 'getStakingRouterAddress')
.mockImplementationOnce(async () => '0x' + '3'.repeat(40));
.mockImplementation(async () => '0x' + '3'.repeat(40));
const locatorAddr = jest
.spyOn(locator, 'getLocatorAddress')
.mockImplementationOnce(async () => '0x' + '4'.repeat(40));
.mockImplementation(async () => '0x' + '4'.repeat(40));

return { lidoAddr, locatorAddr, SRAddr, DSMAddr };
};
Expand All @@ -69,7 +68,7 @@ const mockRepository = async (repositoryService: RepositoryService) => {
const address1 = '0x' + '5'.repeat(40);
const depositAddr = jest
.spyOn(repositoryService, 'getDepositAddress')
.mockImplementationOnce(async () => address1);
.mockImplementation(async () => address1);

await repositoryService.initCachedContracts('latest');
jest.spyOn(repositoryService, 'getCachedLidoContract');
Expand Down Expand Up @@ -110,8 +109,8 @@ describe('GuardianService', () => {
GuardianMetricsModule,
],
})
// .overrideProvider(RepositoryService)
// .useClass(RepositoryService)
// .overrideProvider(LocatorService)
// .useClass(LocatorService)
// .useValue(mockRepository(new RepositoryService(moduleRef.get(WINSTON_MODULE_NEST_PROVIDER), )))
.compile();

Expand All @@ -131,6 +130,7 @@ describe('GuardianService', () => {

mockLocator(locatorService);
await mockRepository(repositoryService);
// console.log(repositoryService)
});

it('should exit if the previous call is not completed', async () => {
Expand Down
8 changes: 2 additions & 6 deletions src/guardian/guardian.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,8 @@ export class GuardianService implements OnModuleInit {
elBlockSnapshot: { blockHash, blockNumber },
data: stakingModules,
} = await this.stakingRouterService.getStakingModules();
console.log(blockHash);
try {
await this.repositoryService.initCachedContracts({ blockHash });
} catch (error) {
console.log(error);
}

await this.repositoryService.initCachedContracts({ blockHash });

if (
!this.blockGuardService.isNeedToProcessNewState({
Expand Down

0 comments on commit 958938b

Please sign in to comment.