|
| 1 | +import { join } from "node:path"; |
| 2 | +import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3"; |
| 3 | +import type { |
| 4 | + EIP1193Parameters, |
| 5 | + HttpTransportConfig, |
| 6 | + PublicRpcSchema, |
| 7 | +} from "viem"; |
| 8 | +import type { Config } from "./config/index.js"; |
| 9 | +import { DI } from "./di.js"; |
| 10 | +import { type ILogger, Logger } from "./log/index.js"; |
| 11 | + |
| 12 | +/** |
| 13 | + * This is temporary solution to diagnose bug where compressor occasionally returns many accounts with HF = 0 |
| 14 | + */ |
| 15 | +@DI.Injectable(DI.MulticallSpy) |
| 16 | +export default class MulticallSpy { |
| 17 | + @Logger("MulticallSpy") |
| 18 | + log!: ILogger; |
| 19 | + |
| 20 | + @DI.Inject(DI.Config) |
| 21 | + config!: Config; |
| 22 | + |
| 23 | + #client = new S3Client({}); |
| 24 | + #detectedCalls: EIP1193Parameters<PublicRpcSchema>[] = []; |
| 25 | + #detectedBlock = 0n; |
| 26 | + |
| 27 | + public multicallSpy: Required<HttpTransportConfig>["onFetchRequest"] = |
| 28 | + async request => { |
| 29 | + if (!this.config.debugGetCreditAccounts) { |
| 30 | + return; |
| 31 | + } |
| 32 | + const data = (await request.json()) as EIP1193Parameters<PublicRpcSchema>; |
| 33 | + const blockNumber = isGetCreditAccountsMulticall(data); |
| 34 | + if (blockNumber) { |
| 35 | + this.#storeCall(blockNumber, data); |
| 36 | + this.log.debug( |
| 37 | + `stored getCreditAccounts multicall at block ${blockNumber}, total calls: ${this.#detectedCalls.length}`, |
| 38 | + ); |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + public async dumpCalls(): Promise<void> { |
| 43 | + if (!this.config.debugGetCreditAccounts) { |
| 44 | + return; |
| 45 | + } |
| 46 | + if (!this.config.outS3Bucket) { |
| 47 | + this.log.error("outS3Bucket is not set"); |
| 48 | + return; |
| 49 | + } |
| 50 | + const key = join( |
| 51 | + this.config.outS3Prefix, |
| 52 | + `getCreditAccounts_${this.#detectedBlock}.json`, |
| 53 | + ); |
| 54 | + const s3Url = `s3://${this.config.outS3Bucket}/${key}`; |
| 55 | + try { |
| 56 | + this.log.debug(`uploading to ${s3Url}`); |
| 57 | + await this.#client.send( |
| 58 | + new PutObjectCommand({ |
| 59 | + Bucket: this.config.outS3Bucket, |
| 60 | + Key: key, |
| 61 | + ContentType: "application/json", |
| 62 | + Body: JSON.stringify(this.#detectedCalls), |
| 63 | + }), |
| 64 | + ); |
| 65 | + } catch (e) { |
| 66 | + this.log.error(e, `failed to upload to ${s3Url}`); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + #storeCall( |
| 71 | + blockNumber: bigint, |
| 72 | + data: EIP1193Parameters<PublicRpcSchema>, |
| 73 | + ): void { |
| 74 | + if (blockNumber !== this.#detectedBlock) { |
| 75 | + this.#detectedBlock = blockNumber; |
| 76 | + this.#detectedCalls = []; |
| 77 | + } |
| 78 | + this.#detectedCalls.push(data); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Detects multicalls to CreditAccounts.getCreditAccounts |
| 84 | + * @param data |
| 85 | + * @returns block number if it's a getCreditAccounts multicall, undefined otherwise |
| 86 | + */ |
| 87 | +function isGetCreditAccountsMulticall( |
| 88 | + data: EIP1193Parameters<PublicRpcSchema>, |
| 89 | +): bigint | undefined { |
| 90 | + try { |
| 91 | + if ( |
| 92 | + data.method === "eth_call" && |
| 93 | + // detect eth_call to multicall3: 0xca11bde05977b3631167028862be2a173976ca11 |
| 94 | + data.params[0].to === "0xca11bde05977b3631167028862be2a173976ca11" && |
| 95 | + typeof data.params[1] === "string" && |
| 96 | + data.params[1]?.startsWith("0x") && // non-latest block |
| 97 | + // that contain CA compressor: "0x4115708Fc8fe6bB392De2e0C21c2C81dA2222394" |
| 98 | + data.params[0].data?.includes( |
| 99 | + "4115708fc8fe6bb392de2e0c21c2c81da2222394", |
| 100 | + ) && |
| 101 | + // includes getCreditAccounts signature |
| 102 | + // cast 4byte 0xf43bdb34 |
| 103 | + // getCreditAccounts((address[],address[],address[],address),(address,bool,uint256,uint256,bool),uint256) |
| 104 | + // cast 4byte 0x8b59b911 |
| 105 | + // getCreditAccounts((address[],address[],address[],address),(address,bool,uint256,uint256,bool),uint256,uint256) |
| 106 | + (data.params[0].data?.includes("f43bdb34") || |
| 107 | + data.params[0].data?.includes("8b59b911")) |
| 108 | + ) { |
| 109 | + return BigInt(data.params[1]); |
| 110 | + } |
| 111 | + } catch {} |
| 112 | +} |
0 commit comments