Skip to content

Commit 73da86e

Browse files
committed
fix: tests, obsolete files, some of code correspondence to other l2 code
1 parent bfd4f7f commit 73da86e

File tree

8 files changed

+58
-121
lines changed

8 files changed

+58
-121
lines changed

l2-bridge-ethereum/src/constants.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,8 @@ const SCROLL_L1_MESSENGER_EVENTS = [
517517
severity: FindingSeverity.High,
518518
type: FindingType.Info,
519519
},
520-
// There is also `event UpdateFeeVault(address _oldFeeVault, address _newFeeVault)`
521-
// for skipping it as not significant enough
520+
// There is also `event UpdateFeeVault(address _oldFeeVault, address _newFeeVault)`,
521+
// but it is skipped as not significant enough
522522
];
523523

524524
const SCROLL_L1_GATEWAY_ROUTER_PROXY_EVENTS = [
@@ -597,7 +597,8 @@ const SCROLL_L1_GATEWAY_ROUTER_EVENTS = [
597597
`\non Gateway Router ${SCROLL_L1_GATEWAY_ROUTER}`,
598598
severity: FindingSeverity.Critical,
599599
type: FindingType.Info,
600-
// Occurred in block 18318378 but than there were no Lido custom gateway contracts
600+
// Occurred in block 18318378 but than there were no Lido custom gateway contracts,
601+
// thus it cannot be used in tests
601602
},
602603
];
603604

l2-bridge-scroll/src/abi/L2LidoGateway.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,4 +858,4 @@
858858
"stateMutability": "payable",
859859
"type": "function"
860860
}
861-
]
861+
]

l2-bridge-scroll/src/app.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { FortaGuardClient } from './clients/forta_guard_client'
21
import { ethers, Finding } from 'forta-agent'
32
import { ScrollClient } from './clients/scroll_client'
43
import { EventWatcher } from './services/event_watcher'
@@ -19,7 +18,6 @@ import { BridgeBalanceSrv } from './services/bridge_balance'
1918
import { getJsonRpcUrl } from 'forta-agent/dist/sdk/utils'
2019
import { BorderTime, HealthChecker, MaxNumberErrorsPerBorderTime } from './services/health-checker/health-checker.srv'
2120

22-
2321
export type Container = {
2422
scrollClient: ScrollClient
2523
proxyWatcher: ProxyWatcher
@@ -47,9 +45,9 @@ export class App {
4745
})
4846

4947
const adr = Constants
50-
const scrollRpcURL = FortaGuardClient.getSecret()
48+
const scrollRpcURL = adr.SCROLL_NETWORK_RPC
5149

52-
const nodeClient = new ethers.providers.JsonRpcProvider(scrollRpcURL, adr.L2_NETWORK_ID)
50+
const nodeClient = new ethers.providers.JsonRpcProvider(scrollRpcURL, adr.SCROLL_NETWORK_ID)
5351

5452
const l2Bridge = L2LidoGateway__factory.connect(adr.L2_ERC20_TOKEN_GATEWAY.address, nodeClient)
5553
const bridgedWSthEthRunner = ERC20Short__factory.connect(adr.SCROLL_WSTETH_BRIDGED.address, nodeClient)

l2-bridge-scroll/src/clients/forta_guard_client.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

l2-bridge-scroll/src/clients/scroll_client.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ describe('ScrollProvider', () => {
1111
async () => {
1212
const app = await App.getInstance()
1313

14-
const startL2BlockNumber = 31_653_550
15-
const endL2BlockNumber = 31_653_600
14+
const startL2BlockNumber = 5_927_000
15+
const endL2BlockNumber = 5_927_050
1616
const l2Blocks = await app.scrollClient.fetchL2Blocks(startL2BlockNumber, endL2BlockNumber)
1717

1818
expect(l2Blocks.length).toEqual(endL2BlockNumber - startL2BlockNumber + 1)
@@ -21,17 +21,17 @@ describe('ScrollProvider', () => {
2121
)
2222

2323
test(
24-
'getWstEthTotalSupply is 1177.342778779487684996 wstEth',
24+
'getWstEthTotalSupply is 7620.760541243359204164 wstEth',
2525
async () => {
2626
const app = await App.getInstance()
2727

28-
const baseBlockNumber = 31_653_550
28+
const baseBlockNumber = 5_927_366
2929
const balance = await app.scrollClient.getWstEthTotalSupply(baseBlockNumber)
3030
if (E.isLeft(balance)) {
3131
throw balance.left
3232
}
3333

34-
expect(balance.right.dividedBy(ETH_DECIMALS)).toEqual(new BigNumber('1177.342778779487684996'))
34+
expect(balance.right.dividedBy(ETH_DECIMALS)).toEqual(new BigNumber('7620.760541243359204164'))
3535
},
3636
TEST_TIMEOUT,
3737
)

l2-bridge-scroll/src/services/monitor_withdrawals.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ import { IMonitorWithdrawalsClient } from '../clients/scroll_client'
88
import { NetworkError } from '../utils/error'
99
import { elapsedTime } from '../utils/time'
1010
import { getUniqueKey } from '../utils/finding.helpers'
11-
import { Constants } from '../utils/constants'
11+
import { Constants, ETH_DECIMALS } from '../utils/constants'
12+
import { formatAddress } from 'forta-agent/dist/cli/utils'
1213

13-
const ETH_DECIMALS = new BigNumber(10).pow(18)
1414
// 10k wstETH
1515
const MAX_WITHDRAWALS_SUM = 10_000
1616

1717
export type MonitorWithdrawalsInitResp = {
1818
currentWithdrawals: string
1919
}
20-
export const TWO_DAYS = 60 * 60 * 24 * 2
21-
20+
export const HOURS_48 = 60 * 60 * 24 * 2
2221

2322
export class MonitorWithdrawals {
2423
private readonly name: string = 'WithdrawalsMonitor'
@@ -30,7 +29,7 @@ export class MonitorWithdrawals {
3029
private readonly l2Erc20TokenGatewayAddress: string
3130
private readonly withdrawalsClient: IMonitorWithdrawalsClient
3231

33-
private withdrawalsCache: WithdrawalRecord[] = []
32+
private withdrawalsStore: WithdrawalRecord[] = []
3433
private lastReportedTooManyWithdrawalsTimestamp = 0
3534

3635
constructor(withdrawalsClient: IMonitorWithdrawalsClient, l2Erc20TokenGatewayAddress: string, logger: Logger) {
@@ -44,7 +43,7 @@ export class MonitorWithdrawals {
4443
}
4544

4645
public async initialize(currentBlock: number): Promise<E.Either<NetworkError, MonitorWithdrawalsInitResp>> {
47-
const pastBlock = currentBlock - Math.ceil(TWO_DAYS / Constants.SCROLL_APPROX_BLOCK_TIME_3_SECONDS)
46+
const pastBlock = currentBlock - Math.ceil(HOURS_48 / Constants.SCROLL_APPROX_BLOCK_TIME_3_SECONDS)
4847

4948
const withdrawalEvents = await this.withdrawalsClient.getWithdrawalEvents(pastBlock, currentBlock - 1)
5049
if (E.isLeft(withdrawalEvents)) {
@@ -59,7 +58,7 @@ export class MonitorWithdrawals {
5958
const withdrawalsSum = new BigNumber(0)
6059
for (const wc of withdrawalRecords.right) {
6160
withdrawalsSum.plus(wc.amount)
62-
this.withdrawalsCache.push(wc)
61+
this.withdrawalsStore.push(wc)
6362
}
6463

6564
this.logger.info(`${MonitorWithdrawals.name} started on block ${currentBlock}`)
@@ -68,37 +67,40 @@ export class MonitorWithdrawals {
6867
})
6968
}
7069

71-
public handleBlocks(logs: Log[], blocksDto: BlockDto[]): Finding[] {
70+
public handleBlocks(l2Logs: Log[], l2BlocksDto: BlockDto[]): Finding[] {
7271
const start = new Date().getTime()
7372

7473
// adds records into withdrawalsCache
75-
const withdrawalRecords = this.getWithdrawalRecords(logs, blocksDto)
76-
this.withdrawalsCache.push(...withdrawalRecords)
74+
const withdrawalRecords = this.getWithdrawalRecords(l2Logs, l2BlocksDto)
75+
if (withdrawalRecords.length !== 0) {
76+
this.logger.info(`Withdrawals count = ${withdrawalRecords.length}`)
77+
}
78+
this.withdrawalsStore.push(...withdrawalRecords)
7779

7880
const out: Finding[] = []
7981

80-
for (const block of blocksDto) {
82+
for (const l2Block of l2BlocksDto) {
8183
// remove withdrawals records older than MAX_WITHDRAWALS_WINDOW
8284
const withdrawalsCache: WithdrawalRecord[] = []
83-
for (const wc of this.withdrawalsCache) {
84-
if (wc.time > block.timestamp - TWO_DAYS) {
85+
for (const wc of this.withdrawalsStore) {
86+
if (wc.time > l2Block.timestamp - HOURS_48) {
8587
withdrawalsCache.push(wc)
8688
}
8789
}
8890

89-
this.withdrawalsCache = withdrawalsCache
91+
this.withdrawalsStore = withdrawalsCache
9092

9193
const withdrawalsSum = new BigNumber(0)
92-
for (const wc of this.withdrawalsCache) {
94+
for (const wc of this.withdrawalsStore) {
9395
withdrawalsSum.plus(wc.amount)
9496
}
9597

9698
// block number condition is meant to "sync" agents alerts
97-
if (withdrawalsSum.div(ETH_DECIMALS).isGreaterThanOrEqualTo(MAX_WITHDRAWALS_SUM) && block.number % 10 === 0) {
99+
if (withdrawalsSum.div(ETH_DECIMALS).isGreaterThanOrEqualTo(MAX_WITHDRAWALS_SUM) && l2Block.number % 10 === 0) {
98100
const period =
99-
block.timestamp - this.lastReportedTooManyWithdrawalsTimestamp < TWO_DAYS
100-
? block.timestamp - this.lastReportedTooManyWithdrawalsTimestamp
101-
: TWO_DAYS
101+
l2Block.timestamp - this.lastReportedTooManyWithdrawalsTimestamp < HOURS_48
102+
? l2Block.timestamp - this.lastReportedTooManyWithdrawalsTimestamp
103+
: HOURS_48
102104

103105
const uniqueKey = `C167F276-D519-4906-90CB-C4455E9ABBD4`
104106

@@ -110,45 +112,45 @@ export class MonitorWithdrawals {
110112
alertId: 'HUGE-WITHDRAWALS-FROM-L2',
111113
severity: FindingSeverity.Medium,
112114
type: FindingType.Suspicious,
113-
uniqueKey: getUniqueKey(uniqueKey, block.number),
115+
uniqueKey: getUniqueKey(uniqueKey, l2Block.number),
114116
})
115117

116118
out.push(finding)
117119

118-
this.lastReportedTooManyWithdrawalsTimestamp = block.timestamp
120+
this.lastReportedTooManyWithdrawalsTimestamp = l2Block.timestamp
119121

120122
const tmp: WithdrawalRecord[] = []
121-
for (const wc of this.withdrawalsCache) {
122-
if (wc.time > block.timestamp - this.lastReportedTooManyWithdrawalsTimestamp) {
123+
for (const wc of this.withdrawalsStore) {
124+
if (wc.time > l2Block.timestamp - this.lastReportedTooManyWithdrawalsTimestamp) {
123125
tmp.push(wc)
124126
}
125127
}
126128

127-
this.withdrawalsCache = tmp
129+
this.withdrawalsStore = tmp
128130
}
129131
}
130132

131133
this.logger.info(elapsedTime(MonitorWithdrawals.name + '.' + this.handleBlocks.name, start))
132134
return out
133135
}
134136

135-
private getWithdrawalRecords(logs: Log[], blocksDto: BlockDto[]): WithdrawalRecord[] {
137+
private getWithdrawalRecords(l2Logs: Log[], l2BlocksDto: BlockDto[]): WithdrawalRecord[] {
136138
const blockNumberToBlock = new Map<number, BlockDto>()
137139
const logIndexToLogs = new Map<number, Log>()
138-
const addresses: string[] = []
140+
const addresses = new Set<string>()
139141

140-
for (const log of logs) {
141-
logIndexToLogs.set(log.logIndex, log)
142-
addresses.push(log.address)
142+
for (const l2Log of l2Logs) {
143+
logIndexToLogs.set(l2Log.logIndex, l2Log)
144+
addresses.add(l2Log.address.toLowerCase())
143145
}
144146

145-
for (const blockDto of blocksDto) {
146-
blockNumberToBlock.set(blockDto.number, blockDto)
147+
for (const l2BlockDto of l2BlocksDto) {
148+
blockNumberToBlock.set(l2BlockDto.number, l2BlockDto)
147149
}
148150

149151
const out: WithdrawalRecord[] = []
150-
if (this.l2Erc20TokenGatewayAddress in addresses) {
151-
const events = filterLog(logs, this.withdrawalInitiatedEvent, this.l2Erc20TokenGatewayAddress)
152+
if (formatAddress(this.l2Erc20TokenGatewayAddress) in addresses) {
153+
const events = filterLog(l2Logs, this.withdrawalInitiatedEvent, formatAddress(this.l2Erc20TokenGatewayAddress))
152154

153155
for (const event of events) {
154156
// eslint-disable-next-line @typescript-eslint/ban-ts-comment

l2-bridge-scroll/src/services/proxy_watcher.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Finding, FindingSeverity, FindingType } from 'forta-agent'
22
import { IProxyContractClient } from '../clients/proxy_contract_client'
33
import * as E from 'fp-ts/Either'
44
import { retry } from 'ts-retry'
5-
import { DataRW } from '../utils/mutex'
65
import { getUniqueKey, networkAlert } from '../utils/finding.helpers'
76
import { elapsedTime } from '../utils/time'
87
import { Logger } from 'winston'
@@ -63,29 +62,21 @@ export class ProxyWatcher {
6362

6463
async handleBlocks(blockNumbers: number[]): Promise<Finding[]> {
6564
const start = new Date().getTime()
65+
const findings: Finding[] = []
6666

6767
const BLOCK_INTERVAL = 25
68-
const batchPromises: Promise<void>[] = []
69-
const out = new DataRW<Finding>([])
70-
71-
for (const blockNumber of blockNumbers) {
72-
if (blockNumber % BLOCK_INTERVAL === 0) {
73-
const promiseProxyImpl = this.handleProxyImplementationChanges(blockNumber).then((findings: Finding[]) => {
74-
out.write(findings)
75-
})
76-
77-
const promiseAdminChanges = this.handleProxyAdminChanges(blockNumber).then((findings: Finding[]) => {
78-
out.write(findings)
79-
})
80-
81-
batchPromises.push(promiseProxyImpl, promiseAdminChanges)
68+
for (const l2BlockNumber of blockNumbers) {
69+
if (l2BlockNumber % BLOCK_INTERVAL === 0) {
70+
const [implFindings, adminFindings] = await Promise.all([
71+
this.handleProxyImplementationChanges(l2BlockNumber),
72+
this.handleProxyAdminChanges(l2BlockNumber),
73+
])
74+
findings.push(...implFindings, ...adminFindings)
8275
}
8376
}
8477

85-
await Promise.all(batchPromises)
8678
this.logger.info(elapsedTime(ProxyWatcher.name + '.' + this.handleBlocks.name, start))
87-
88-
return await out.read()
79+
return findings
8980
}
9081

9182
private async handleProxyImplementationChanges(blockNumber: number): Promise<Finding[]> {

l2-bridge-scroll/src/utils/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export type RoleHashToName = Map<string, string>
99

1010
export const ETH_DECIMALS = new BigNumber(10).pow(18)
1111
export const Constants = {
12-
L2_NETWORK_RPC: 'https://rpc.scroll.io',
13-
L2_NETWORK_ID: 534352,
12+
SCROLL_NETWORK_RPC: 'https://rpc.scroll.io',
13+
SCROLL_NETWORK_ID: 534352,
1414
SCROLL_APPROX_BLOCK_TIME_3_SECONDS: 3,
1515
L2_PROXY_ADMIN_CONTRACT_ADDRESS: '0x8e34d07eb348716a1f0a48a507a9de8a3a6dce45',
1616
GOV_BRIDGE_ADDRESS: '0x0c67d8d067e349669dfeab132a7c03a90594ee09',

0 commit comments

Comments
 (0)