From 651a94dc3b5e5a4062f1594398c6313738e84e51 Mon Sep 17 00:00:00 2001 From: Sergey White Date: Wed, 29 May 2024 12:02:45 +0300 Subject: [PATCH] fix: l2-bridge-base added test for getWithdrawalEvents --- l2-bridge-base/src/agent.spec.ts | 22 +++-- l2-bridge-base/src/agent.ts | 13 +-- l2-bridge-base/src/app.ts | 29 +----- .../src/clients/base_client.spec.ts | 93 ++++++++++++++----- l2-bridge-base/src/clients/base_client.ts | 2 +- .../src/clients/eth_provider.spec.ts | 2 +- .../src/services/monitor_withdrawals.ts | 4 +- 7 files changed, 95 insertions(+), 70 deletions(-) diff --git a/l2-bridge-base/src/agent.spec.ts b/l2-bridge-base/src/agent.spec.ts index 2386c8cc..e6e7a21c 100644 --- a/l2-bridge-base/src/agent.spec.ts +++ b/l2-bridge-base/src/agent.spec.ts @@ -1,21 +1,29 @@ import { App } from './app' import * as E from 'fp-ts/Either' import { Finding } from 'forta-agent' +import { ethers } from 'ethers' import BigNumber from 'bignumber.js' describe('agent-base functional test', () => { - test('should process app on 13_022_644 and 13_022_720 (25 l2 blocks)', async () => { - const app = await App.getInstance() + test('should process app on latest 25 l2 blocks', async () => { + const l1rpcURL = 'https://eth.drpc.org' + const app = App.getInstance() - const l1Block = 19_632_237 - const l2StartBlock = 13_022_720 - const l2EndBlock = 13_022_745 - const l2BlocksDto = await app.baseClient.fetchL2Blocks(l2StartBlock, l2EndBlock) + const ehtProvider = new ethers.providers.JsonRpcProvider(l1rpcURL) + const l1Block = await ehtProvider.getBlockNumber() + + const l2EndBlock = await app.baseClient.getLatestL2Block() + if (E.isLeft(l2EndBlock)) { + throw l2EndBlock + } + + const l2StartBlock = l2EndBlock.right.number - 25 + const l2BlocksDto = await app.baseClient.fetchL2Blocks(l2StartBlock, l2EndBlock.right.number) for (const proxyWatcher of app.proxyWatchers) { const err = await proxyWatcher.initialize(l2StartBlock) if (err !== null) { - throw null + throw err } } diff --git a/l2-bridge-base/src/agent.ts b/l2-bridge-base/src/agent.ts index 37dbcdc3..92bf32ba 100644 --- a/l2-bridge-base/src/agent.ts +++ b/l2-bridge-base/src/agent.ts @@ -17,14 +17,7 @@ export function initialize(): Initialize { } return async function (): Promise { - const app = await App.getInstance() - - const token = await App.getJwt() - if (E.isLeft(token)) { - app.logger.error(token.left) - - process.exit(1) - } + const app = App.getInstance() const latestL2Block = await app.baseClient.getLatestL2Block() if (E.isLeft(latestL2Block)) { @@ -84,7 +77,7 @@ export const handleBlock = (): HandleBlock => { } isHandleBLockRunning = true - const app = await App.getInstance() + const app = App.getInstance() const findings: Finding[] = [] const findingsAsync = await app.findingsRW.read() @@ -148,7 +141,7 @@ export const handleBlock = (): HandleBlock => { export const healthCheck = (): HealthCheck => { return async function (): Promise { - const app = await App.getInstance() + const app = App.getInstance() if (!app.healthChecker.isHealth()) { return ['There is too much network errors'] diff --git a/l2-bridge-base/src/app.ts b/l2-bridge-base/src/app.ts index f103a273..c4f67c47 100644 --- a/l2-bridge-base/src/app.ts +++ b/l2-bridge-base/src/app.ts @@ -1,4 +1,5 @@ -import { ethers, fetchJwt, getJsonRpcUrl } from 'forta-agent' +import { getJsonRpcUrl } from 'forta-agent' +import { ethers } from 'ethers' import { BaseClient } from './clients/base_client' import { EventWatcher } from './services/event_watcher' import { getL2BridgeEvents } from './utils/events/bridge_events' @@ -14,8 +15,6 @@ import { FindingsRW } from './utils/mutex' import * as Winston from 'winston' import { Logger } from 'winston' import { BorderTime, HealthChecker, MaxNumberErrorsPerBorderTime } from './services/health-checker/health-checker.srv' -import { verifyJwt } from 'forta-agent/dist/sdk/jwt' -import * as E from 'fp-ts/Either' import { BridgeBalanceSrv } from './services/bridge_balance' import { ETHProvider } from './clients/eth_provider_client' @@ -39,29 +38,7 @@ export class App { private constructor() {} - public static async getJwt(): Promise> { - let token: string - try { - token = await fetchJwt({}) - } catch (e) { - return E.left(new Error(`Could not fetch jwt. Cause ${e}`)) - } - - if (process.env.NODE_ENV === 'production') { - try { - const isTokenOk = await verifyJwt(token) - if (!isTokenOk) { - return E.left(new Error(`Token verification failed`)) - } - } catch (e) { - return E.left(new Error(`Token verification failed`)) - } - } - - return E.right(token) - } - - public static async getInstance(): Promise { + public static getInstance(): Container { if (!App.instance) { const logger: Winston.Logger = Winston.createLogger({ format: Winston.format.simple(), diff --git a/l2-bridge-base/src/clients/base_client.spec.ts b/l2-bridge-base/src/clients/base_client.spec.ts index 2d744982..07630d8a 100644 --- a/l2-bridge-base/src/clients/base_client.spec.ts +++ b/l2-bridge-base/src/clients/base_client.spec.ts @@ -1,34 +1,81 @@ -import { App } from '../app' import * as E from 'fp-ts/Either' -import { ETH_DECIMALS } from '../utils/constants' +import { Address, ETH_DECIMALS } from '../utils/constants' import BigNumber from 'bignumber.js' +import { AVG_BLOCK_TIME_2SECONDS, HOURS_12 } from '../services/monitor_withdrawals' +import { App } from '../app' +import * as Winston from 'winston' +import { ethers } from 'forta-agent' +import { ERC20Short__factory, L2Bridge__factory } from '../generated' +import { BaseClient } from './base_client' + +const timeout = 120_000 describe('base provider tests', () => { - test('should fetch block logs', async () => { - const app = await App.getInstance() + const app = App.getInstance() + + test( + 'should fetch block logs', + async () => { + const latestBlock = await app.baseClient.getLatestL2Block() + if (E.isLeft(latestBlock)) { + throw latestBlock + } + + const blocksDto = await app.baseClient.getL2Logs(latestBlock.right.number, latestBlock.right.number) + if (E.isLeft(blocksDto)) { + throw blocksDto + } + + expect(blocksDto.right.length).toBeGreaterThan(1) + }, + timeout, + ) + + test( + 'getWithdrawalEvents fetches 21_601 blocks for getting withdrawal events', + async () => { + const currentBlock = 15_091_860 + + const pastBlock = currentBlock - Math.ceil(HOURS_12 / AVG_BLOCK_TIME_2SECONDS) + + expect(21_601).toEqual(currentBlock - pastBlock + 1) + + const withdrawalEvents = await app.baseClient.getWithdrawalEvents(pastBlock, currentBlock - 1) + if (E.isLeft(withdrawalEvents)) { + throw withdrawalEvents + } + + expect(withdrawalEvents.right.length).toEqual(0) + }, + timeout, + ) - const latestBlock = await app.baseClient.getLatestL2Block() - if (E.isLeft(latestBlock)) { - throw latestBlock - } + test( + 'getWstEthTotalSupply is 16388.426826708573275643 wsETH', + async () => { + const logger: Winston.Logger = Winston.createLogger({ + format: Winston.format.simple(), + transports: [new Winston.transports.Console()], + }) - const blocksDto = await app.baseClient.getL2Logs(latestBlock.right.number, latestBlock.right.number) - if (E.isLeft(blocksDto)) { - throw blocksDto - } + const baseNetworkID = 8453 + const defaultL2RPCURL = 'https://base.drpc.org' - expect(blocksDto.right.length).toBeGreaterThan(1) - }, 120_000) + const baseProvider = new ethers.providers.JsonRpcProvider(defaultL2RPCURL, baseNetworkID) + const adr: Address = Address - test('getWstEthTotalSupply is 1696.070092078019991932 wsETH', async () => { - const app = await App.getInstance() + const l2Bridge = L2Bridge__factory.connect(adr.BASE_L2ERC20_TOKEN_BRIDGE_ADDRESS, baseProvider) + const bridgedWSthEthRunner = ERC20Short__factory.connect(adr.BASE_WSTETH_ADDRESS, baseProvider) + const baseClient = new BaseClient(baseProvider, l2Bridge, logger, bridgedWSthEthRunner) - const baseBlockNumber = 13_022_744 - const balance = await app.baseClient.getWstEthTotalSupply(baseBlockNumber) - if (E.isLeft(balance)) { - throw balance.left - } + const baseBlockNumber = 15_091_860 + const balance = await baseClient.getWstEthTotalSupply(baseBlockNumber) + if (E.isLeft(balance)) { + throw balance.left + } - expect(balance.right.dividedBy(ETH_DECIMALS)).toEqual(new BigNumber('11430.956916416032084584')) - }, 120_000) + expect(balance.right.dividedBy(ETH_DECIMALS)).toEqual(new BigNumber('16388.426826708573275643')) + }, + timeout, + ) }) diff --git a/l2-bridge-base/src/clients/base_client.ts b/l2-bridge-base/src/clients/base_client.ts index 630e509b..5df7959c 100644 --- a/l2-bridge-base/src/clients/base_client.ts +++ b/l2-bridge-base/src/clients/base_client.ts @@ -185,7 +185,7 @@ export class BaseClient implements IBaseClient, IMonitorWithdrawalsClient, IL2Br batchRequests.push(i) } - const chunkSize = 125 + const chunkSize = 10_000 const out: WithdrawalInitiatedEvent[] = [] const batchPromises: Promise[] = [] diff --git a/l2-bridge-base/src/clients/eth_provider.spec.ts b/l2-bridge-base/src/clients/eth_provider.spec.ts index 7b3df5c6..4ef7f42a 100644 --- a/l2-bridge-base/src/clients/eth_provider.spec.ts +++ b/l2-bridge-base/src/clients/eth_provider.spec.ts @@ -5,7 +5,7 @@ import BigNumber from 'bignumber.js' describe('eth provider tests', () => { test('getBalanceByBlockHash is 10786.9163900726000737487 wsETH', async () => { - const app = await App.getInstance() + const app = App.getInstance() const adr = Address const blockNumber = 19_619_102 diff --git a/l2-bridge-base/src/services/monitor_withdrawals.ts b/l2-bridge-base/src/services/monitor_withdrawals.ts index ffbcd53a..57415abe 100644 --- a/l2-bridge-base/src/services/monitor_withdrawals.ts +++ b/l2-bridge-base/src/services/monitor_withdrawals.ts @@ -9,8 +9,8 @@ import { dateTimeFormat, elapsedTime } from '../utils/time' import { getUniqueKey } from '../utils/finding.helpers' // 12 hours -const HOURS_12 = 60 * 60 * 12 -const AVG_BLOCK_TIME_2SECONDS: number = 2 //s +export const HOURS_12 = 60 * 60 * 12 +export const AVG_BLOCK_TIME_2SECONDS: number = 2 //s const ETH_DECIMALS = new BigNumber(10).pow(18) // 10k wstETH const MAX_WITHDRAWALS_10K_WstEth = 10_000