Skip to content

Commit

Permalink
fix: staking router migration behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
eddort committed Mar 29, 2023
1 parent d590e38 commit 0ae8732
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
42 changes: 36 additions & 6 deletions src/contracts/repository/repository.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class RepositoryService {
string,
LidoAbi | LocatorAbi | SecurityAbi | StakingRouterAbi
> = {};
private cachedDSMPrefixes: Record<string, string> = {};
private permanentContractsCache: Record<string, DepositAbi> = {};

/**
Expand All @@ -41,16 +42,15 @@ export class RepositoryService {
/**
* Init cache for each contract or wait if it makes some error
*/
public async initOrWaitCachedContracts(blockTag?: BlockTag) {
public async initOrWaitCachedContracts() {
const block = await this.providerService.getBlock();
try {
const _blockTag = blockTag || {
blockHash: (await this.providerService.getBlock()).hash,
};
await this.initCachedContracts(_blockTag);
await this.initCachedContracts({ blockHash: block.hash });
return block;
} catch (error) {
this.logger.error('Init contracts error. Retry', error);
await sleep(10_000);
await this.initOrWaitCachedContracts();
return await this.initOrWaitCachedContracts();
}
}

Expand Down Expand Up @@ -148,6 +148,15 @@ export class RepositoryService {
DSM_ABI,
SecurityAbi__factory.connect(address, provider),
);

// prune dsm prefixes
this.cachedDSMPrefixes = {};

// re-init dsm prefixes
await Promise.all([
this.getAttestMessagePrefix(),
this.getPauseMessagePrefix(),
]);
}

/**
Expand Down Expand Up @@ -182,6 +191,27 @@ export class RepositoryService {
);
}

/**
* Returns a prefix from the contract with which the deposit message should be signed
*/
public async getAttestMessagePrefix(): Promise<string> {
if (this.cachedDSMPrefixes.attest) return this.cachedDSMPrefixes.attest;
const contract = await this.getCachedDSMContract();
this.cachedDSMPrefixes.attest = await contract.ATTEST_MESSAGE_PREFIX();
return this.cachedDSMPrefixes.attest;
}

/**
* Returns a prefix from the contract with which the pause message should be signed
*/
public async getPauseMessagePrefix(): Promise<string> {
if (this.cachedDSMPrefixes.pause) return this.cachedDSMPrefixes.pause;
const contract = await this.getCachedDSMContract();
this.cachedDSMPrefixes.pause = await contract.PAUSE_MESSAGE_PREFIX();

return this.cachedDSMPrefixes.pause;
}

/**
* Returns Deposit contract address
*/
Expand Down
32 changes: 2 additions & 30 deletions src/contracts/security/security.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ export class SecurityService {
private walletService: WalletService,
) {}

private cachedAttestMessagePrefix: string | null = null;
private cachedPauseMessagePrefix: string | null = null;

public async initialize(blockTag: BlockTag): Promise<void> {
const guardianIndex = await this.getGuardianIndex(blockTag);
const address = this.walletService.address;
Expand All @@ -47,31 +44,6 @@ export class SecurityService {

return contractWithSigner;
}
/**
* Returns a prefix from the contract with which the deposit message should be signed
*/
public async getAttestMessagePrefix(): Promise<string> {
if (!this.cachedAttestMessagePrefix) {
const contract = await this.repositoryService.getCachedDSMContract();
const messagePrefix = await contract.ATTEST_MESSAGE_PREFIX();
this.cachedAttestMessagePrefix = messagePrefix;
}

return this.cachedAttestMessagePrefix;
}

/**
* Returns a prefix from the contract with which the pause message should be signed
*/
public async getPauseMessagePrefix(): Promise<string> {
if (!this.cachedPauseMessagePrefix) {
const contract = await this.repositoryService.getCachedDSMContract();
const messagePrefix = await contract.PAUSE_MESSAGE_PREFIX();
this.cachedPauseMessagePrefix = messagePrefix;
}

return this.cachedPauseMessagePrefix;
}

/**
* Returns the maximum number of deposits per transaction from the contract
Expand Down Expand Up @@ -124,7 +96,7 @@ export class SecurityService {
blockHash: string,
stakingModuleId: number,
): Promise<Signature> {
const prefix = await this.getAttestMessagePrefix();
const prefix = await this.repositoryService.getAttestMessagePrefix();

return await this.walletService.signDepositData({
prefix,
Expand All @@ -143,7 +115,7 @@ export class SecurityService {
blockNumber: number,
stakingModuleId: number,
): Promise<Signature> {
const prefix = await this.getPauseMessagePrefix();
const prefix = await this.repositoryService.getPauseMessagePrefix();

return await this.walletService.signPauseData({
prefix,
Expand Down
4 changes: 1 addition & 3 deletions src/guardian/guardian.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ export class GuardianService implements OnModuleInit {
// Does not wait for completion, to avoid blocking the app initialization
(async () => {
try {
const block = await this.providerService.getBlock();
const block = await this.repositoryService.initOrWaitCachedContracts();
const blockHash = block.hash;

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

await Promise.all([
this.depositService.initialize(block.number),
this.securityService.initialize({ blockHash }),
Expand Down

0 comments on commit 0ae8732

Please sign in to comment.