From 7ddcfd2d9660cbf8e85c135992f900d04eb31d8c Mon Sep 17 00:00:00 2001 From: madlabman <10616301+madlabman@users.noreply.github.com> Date: Thu, 17 Oct 2024 17:47:07 +0300 Subject: [PATCH] fix: check CSFeeDistributor invariants when it makes sense --- .../CSFeeDistributor/CSFeeDistributor.srv.ts | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/csm-alerts/src/services/CSFeeDistributor/CSFeeDistributor.srv.ts b/csm-alerts/src/services/CSFeeDistributor/CSFeeDistributor.srv.ts index 6b2d657e..fdfd97ff 100644 --- a/csm-alerts/src/services/CSFeeDistributor/CSFeeDistributor.srv.ts +++ b/csm-alerts/src/services/CSFeeDistributor/CSFeeDistributor.srv.ts @@ -98,25 +98,30 @@ export class CSFeeDistributorSrv implements Service { ) } - async handleBlock(blockEvent: BlockEvent, provider: Provider): Promise { - return [ - ...this.handleDistributionOverdue(blockEvent), - ...(await this.checkInvariants(blockEvent, provider)), - ] + async handleBlock(blockEvent: BlockEvent): Promise { + return [...this.handleDistributionOverdue(blockEvent)] } - public async handleTransaction(txEvent: TransactionEvent): Promise { + public async handleTransaction( + txEvent: TransactionEvent, + provider: ethers.Provider, + ): Promise { const distributionDataUpdatedEvents = filterLog( txEvent.logs, ICSFeeDistributor.getEvent('DistributionDataUpdated').format('full'), DEPLOYED_ADDRESSES.CS_FEE_DISTRIBUTOR, ) + const out: Finding[] = [] + if (distributionDataUpdatedEvents.length > 0) { this.state.lastDistributionUpdatedAt = txEvent.block.timestamp + out.push(...(await this.checkInvariants(txEvent, provider))) } - return this.handleTransferSharesInvalidReceiver(txEvent) + out.push(...this.handleTransferSharesInvalidReceiver(txEvent)) + + return out } private handleDistributionOverdue(blockEvent: BlockEvent): Finding[] { @@ -188,31 +193,31 @@ export class CSFeeDistributorSrv implements Service { return out } - private async checkInvariants(blockEvent: BlockEvent, provider: ethers.Provider) { + private async checkInvariants(txEvent: TransactionEvent, provider: ethers.Provider) { const distributor = CSFeeDistributor__factory.connect( DEPLOYED_ADDRESSES.CS_FEE_DISTRIBUTOR, provider, ) const steth = Lido__factory.connect(DEPLOYED_ADDRESSES.LIDO_STETH, provider) - const blockTag = blockEvent.blockHash + const blockTag = txEvent.block.hash const out: Finding[] = [] const treeRoot = await distributor.treeRoot({ blockTag }) const treeCid = await distributor.treeCid({ blockTag }) if (treeRoot !== ethers.ZeroHash && treeCid === '') { - out.push(invariantAlert(blockEvent, 'Tree exists, but no CID')) + out.push(invariantAlert(txEvent, 'Tree exists, but no CID')) } if (treeRoot === ethers.ZeroHash && treeCid !== '') { - out.push(invariantAlert(blockEvent, 'Tree CID exists, but no root')) + out.push(invariantAlert(txEvent, 'Tree CID exists, but no root')) } if ( (await distributor.totalClaimableShares({ blockTag })) > (await steth.sharesOf(distributor, { blockTag })) ) { - out.push(invariantAlert(blockEvent, "distributed more than the contract's balance")) + out.push(invariantAlert(txEvent, "distributed more than the contract's balance")) } return out