Skip to content

Commit

Permalink
fix: add sr modules check during start
Browse files Browse the repository at this point in the history
  • Loading branch information
Amuhar committed Aug 8, 2023
1 parent 385f786 commit 844b38a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/jobs/keys-update/keys-update.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { ExecutionProviderService } from 'common/execution-provider';
import { SRModuleStorageService } from 'storage/sr-module.storage';
import { IsolationLevel } from '@mikro-orm/core';
import { PrometheusService } from 'common/prometheus';
import { SrModuleEntity } from 'storage/sr-module.entity';
import { StakingModule } from 'staking-router-modules/interfaces/staking-module.interface';

class KeyOutdatedError extends Error {
lastBlock: number;
Expand Down Expand Up @@ -114,18 +116,26 @@ export class KeysUpdateService {
const prevElMeta = await this.elMetaStorage.get();

if (prevElMeta && prevElMeta?.blockNumber > currElMeta.number) {
this.logger.warn('Previous data is newer than current data');
this.logger.warn('Previous data is newer than current data', prevElMeta);
console.log('curr', currElMeta);
return;
}

// TODO: еcли была реорганизация, может ли currElMeta.number быть меньше и нам надо обновиться ?

const storageModules = await this.srModulesStorage.findAll();
// get staking router modules from SR contract
const modules = await this.stakingRouterFetchService.getStakingModules({ blockHash: currElMeta.hash });

// TODO: what will happen if module исчез из списка
// TODO: is it correct that i use here modules from blockchain instead of storage

if (!this.modulesWereNotDeleted(modules, storageModules)) {
const error = new Error('Modules list is wrong');
this.logger.error(error);
process.exit(1);
}

await this.entityManager.transactional(
async () => {
// Update el meta in db
Expand Down Expand Up @@ -206,4 +216,12 @@ export class KeysUpdateService {
{ isolationLevel: IsolationLevel.REPEATABLE_READ },
);
}

private modulesWereNotDeleted(contractModules: StakingModule[], storageModules: SrModuleEntity[]): boolean {
// we want to check here that all modules from storageModules exist in list contractModules
// will check contractAddress
const addresses = contractModules.map((module) => module.stakingModuleAddress);

return storageModules.every((module) => addresses.includes(module.stakingModuleAddress));
}
}
2 changes: 2 additions & 0 deletions src/migrations/Migration20230724204141.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export class Migration20230724204141 extends Migration {
// maybe better instead of this migration truncate values in db but than update will take more time for holders
// I prefer second way

// we also can skip for new chains, as their db is empty

throw new Error('CHAIN_ID is wrong, it should be 1 or 5');
}

Expand Down

0 comments on commit 844b38a

Please sign in to comment.