|
| 1 | +// Constants related to used gas in loops can be different on various EVM versions. |
| 2 | + |
| 3 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 4 | +import { RunNodeState, runNode } from "../../lib/node"; |
| 5 | +import * as eth from "../../lib/ethViem"; |
| 6 | +import { beforeEachWithCleanup } from "../../lib/lifecycle"; |
| 7 | +import looper from "../../lib/abis/debugTrace/looper"; |
| 8 | +import { customRpcRequest } from "../../lib/rpcUtils"; |
| 9 | +import { encodeFunctionData } from "viem"; |
| 10 | + |
| 11 | +describe("test trace filter logic", () => { |
| 12 | + let node: RunNodeState; |
| 13 | + let publicClient: eth.PublicClientWebSocket; |
| 14 | + let devClients: eth.DevClientsWebSocket; |
| 15 | + beforeEachWithCleanup(async (cleanup) => { |
| 16 | + node = runNode( |
| 17 | + { |
| 18 | + args: ["--tracing-mode=trace", "--dev", "--tmp"], |
| 19 | + }, |
| 20 | + cleanup.push, |
| 21 | + ); |
| 22 | + |
| 23 | + await node.waitForBoot; |
| 24 | + |
| 25 | + publicClient = eth.publicClientFromNodeWebSocket(node, cleanup.push); |
| 26 | + devClients = eth.devClientsFromNodeWebSocket(node, cleanup.push); |
| 27 | + }, 60 * 1000); |
| 28 | + |
| 29 | + let looperAddress: `0x${string}`; |
| 30 | + |
| 31 | + beforeEach(async () => { |
| 32 | + const [alice, _] = devClients; |
| 33 | + |
| 34 | + const deployLooperContractTxHash = await alice.deployContract({ |
| 35 | + abi: looper.abi, |
| 36 | + bytecode: looper.bytecode, |
| 37 | + }); |
| 38 | + const deployLooperContractTxReceipt = |
| 39 | + await publicClient.waitForTransactionReceipt({ |
| 40 | + hash: deployLooperContractTxHash, |
| 41 | + timeout: 18_000, |
| 42 | + }); |
| 43 | + looperAddress = deployLooperContractTxReceipt.contractAddress!; |
| 44 | + }); |
| 45 | + |
| 46 | + it("should return 21653 gasUsed for 0 loop", async () => { |
| 47 | + const [alice, _] = devClients; |
| 48 | + |
| 49 | + const txHash = await alice.sendTransaction({ |
| 50 | + to: looperAddress, |
| 51 | + data: encodeFunctionData({ |
| 52 | + abi: looper.abi, |
| 53 | + functionName: "incrementalLoop", |
| 54 | + args: [0n], |
| 55 | + }), |
| 56 | + }); |
| 57 | + const txReceipt = await publicClient.waitForTransactionReceipt({ |
| 58 | + hash: txHash, |
| 59 | + }); |
| 60 | + const blockNumberHex = txReceipt.blockNumber.toString(16); |
| 61 | + |
| 62 | + const response = await customRpcRequest( |
| 63 | + node.meta.rpcUrlHttp, |
| 64 | + "trace_filter", |
| 65 | + [ |
| 66 | + { |
| 67 | + fromBlock: blockNumberHex, |
| 68 | + toBlock: blockNumberHex, |
| 69 | + }, |
| 70 | + ], |
| 71 | + ); |
| 72 | + |
| 73 | + expect(response[0].result).to.not.be.undefined; |
| 74 | + expect(response[0].result.error).to.not.exist; |
| 75 | + expect(response[0].result.gasUsed).to.equal("0x5495"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should return 106265 gasUsed for 100 loops", async () => { |
| 79 | + const [alice, _] = devClients; |
| 80 | + |
| 81 | + const txHash = await alice.sendTransaction({ |
| 82 | + to: looperAddress, |
| 83 | + data: encodeFunctionData({ |
| 84 | + abi: looper.abi, |
| 85 | + functionName: "incrementalLoop", |
| 86 | + args: [100n], |
| 87 | + }), |
| 88 | + }); |
| 89 | + const txReceipt = await publicClient.waitForTransactionReceipt({ |
| 90 | + hash: txHash, |
| 91 | + }); |
| 92 | + const blockNumberHex = txReceipt.blockNumber.toString(16); |
| 93 | + |
| 94 | + const response = await customRpcRequest( |
| 95 | + node.meta.rpcUrlHttp, |
| 96 | + "trace_filter", |
| 97 | + [ |
| 98 | + { |
| 99 | + fromBlock: blockNumberHex, |
| 100 | + toBlock: blockNumberHex, |
| 101 | + }, |
| 102 | + ], |
| 103 | + ); |
| 104 | + |
| 105 | + expect(response[0].result).to.not.be.undefined; |
| 106 | + expect(response[0].result.error).to.not.exist; |
| 107 | + expect(response[0].result.gasUsed).to.equal("0x19f19"); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should return 670577 gasUsed for 1000 loops", async () => { |
| 111 | + const [alice, _] = devClients; |
| 112 | + |
| 113 | + const txHash = await alice.sendTransaction({ |
| 114 | + to: looperAddress, |
| 115 | + data: encodeFunctionData({ |
| 116 | + abi: looper.abi, |
| 117 | + functionName: "incrementalLoop", |
| 118 | + args: [1000n], |
| 119 | + }), |
| 120 | + }); |
| 121 | + const txReceipt = await publicClient.waitForTransactionReceipt({ |
| 122 | + hash: txHash, |
| 123 | + }); |
| 124 | + const blockNumberHex = txReceipt.blockNumber.toString(16); |
| 125 | + |
| 126 | + const response = await customRpcRequest( |
| 127 | + node.meta.rpcUrlHttp, |
| 128 | + "trace_filter", |
| 129 | + [ |
| 130 | + { |
| 131 | + fromBlock: blockNumberHex, |
| 132 | + toBlock: blockNumberHex, |
| 133 | + }, |
| 134 | + ], |
| 135 | + ); |
| 136 | + |
| 137 | + expect(response[0].result).to.not.be.undefined; |
| 138 | + expect(response[0].result.error).to.not.exist; |
| 139 | + expect(response[0].result.gasUsed).to.equal("0xa3b71"); |
| 140 | + }); |
| 141 | +}); |
0 commit comments