|
| 1 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import { RunNodeState, runNode } from "../../lib/node"; |
| 3 | +import * as eth from "../../lib/ethViem"; |
| 4 | +import { beforeEachWithCleanup } from "../../lib/lifecycle"; |
| 5 | +import callee from "../../lib/abis/evmTracing/callee"; |
| 6 | +import caller from "../../lib/abis/evmTracing/caller"; |
| 7 | +import { encodeFunctionData } from "viem"; |
| 8 | +import { customRpcRequest } from "../../lib/rpcUtils"; |
| 9 | + |
| 10 | +describe("test debug trace block by number or hash logic", () => { |
| 11 | + let node: RunNodeState; |
| 12 | + let publicClient: eth.PublicClientWebSocket; |
| 13 | + let devClients: eth.DevClientsWebSocket; |
| 14 | + beforeEachWithCleanup(async (cleanup) => { |
| 15 | + node = runNode( |
| 16 | + { |
| 17 | + args: ["--tracing-mode=debug", "--dev", "--tmp"], |
| 18 | + }, |
| 19 | + cleanup.push, |
| 20 | + ); |
| 21 | + |
| 22 | + await node.waitForBoot; |
| 23 | + |
| 24 | + publicClient = eth.publicClientFromNodeWebSocket(node, cleanup.push); |
| 25 | + devClients = eth.devClientsFromNodeWebSocket(node, cleanup.push); |
| 26 | + }, 60 * 1000); |
| 27 | + |
| 28 | + let calleeAddress: `0x${string}`; |
| 29 | + let callerAddress: `0x${string}`; |
| 30 | + |
| 31 | + beforeEach(async () => { |
| 32 | + const [alice, _] = devClients; |
| 33 | + |
| 34 | + const deployCalleeContractTxHash = await alice.deployContract({ |
| 35 | + abi: callee.abi, |
| 36 | + bytecode: callee.bytecode, |
| 37 | + }); |
| 38 | + const deployCalleeContractTxReceipt = |
| 39 | + await publicClient.waitForTransactionReceipt({ |
| 40 | + hash: deployCalleeContractTxHash, |
| 41 | + timeout: 18_000, |
| 42 | + }); |
| 43 | + calleeAddress = deployCalleeContractTxReceipt.contractAddress!; |
| 44 | + |
| 45 | + const deployCallerContractTxHash = await alice.deployContract({ |
| 46 | + abi: caller.abi, |
| 47 | + bytecode: caller.bytecode, |
| 48 | + }); |
| 49 | + const deployCallerContractTxReceipt = |
| 50 | + await publicClient.waitForTransactionReceipt({ |
| 51 | + hash: deployCallerContractTxHash, |
| 52 | + timeout: 18_000, |
| 53 | + }); |
| 54 | + callerAddress = deployCallerContractTxReceipt.contractAddress!; |
| 55 | + }); |
| 56 | + |
| 57 | + it("should trace block by number and hash", async () => { |
| 58 | + const [alice, _] = devClients; |
| 59 | + |
| 60 | + const txHash = await alice.sendTransaction({ |
| 61 | + to: callerAddress, |
| 62 | + data: encodeFunctionData({ |
| 63 | + abi: caller.abi, |
| 64 | + functionName: "someAction", |
| 65 | + args: [calleeAddress, 7n], |
| 66 | + }), |
| 67 | + }); |
| 68 | + const txReceipt = await publicClient.waitForTransactionReceipt({ |
| 69 | + hash: txHash, |
| 70 | + }); |
| 71 | + const blockNumberHex = txReceipt.blockNumber.toString(16); |
| 72 | + const blockHash = txReceipt.blockHash; |
| 73 | + |
| 74 | + const responseByNumber = await customRpcRequest( |
| 75 | + node.meta.rpcUrlHttp, |
| 76 | + "debug_traceBlockByNumber", |
| 77 | + [blockNumberHex, { tracer: "callTracer" }], |
| 78 | + ); |
| 79 | + |
| 80 | + expect(responseByNumber.length).to.equal(1); |
| 81 | + expect(txHash).to.equal(responseByNumber[0].txHash); |
| 82 | + |
| 83 | + const responseByHash = await customRpcRequest( |
| 84 | + node.meta.rpcUrlHttp, |
| 85 | + "debug_traceBlockByHash", |
| 86 | + [blockHash, { tracer: "callTracer" }], |
| 87 | + ); |
| 88 | + |
| 89 | + expect(responseByHash.length).to.equal(1); |
| 90 | + expect(txHash).to.equal(responseByHash[0].txHash); |
| 91 | + }); |
| 92 | +}); |
0 commit comments