从 geth 事务跟踪中提取发出的事件(日志)(debug_traceCall)

Extracting emitted events (logs) from geth transaction trace (debug_traceCall)

使用 debug_traceCall 时,我得到了执行期间所有操作码和状态更改的低级 EVM 跟踪。这个太详细了。当我使用默认 callTracer 时,我可以获得更好的调用树。但是,无论哪种方式,我似乎都无法从跟踪中提取发出的事件。我可以在跟踪中看到它们(LOG* 操作码)但是没有简单的方法将它们实际解析为“可读”的东西(连同值和原始地址)必须有一种获取日志的方法 - 任何想法?

例如。这是 Etherscan 显示的内容 https://etherscan.io/tx-decoder?tx=0x3e3ad35fda1fddd9e154b3860b50371a1acd2fdb4f27f897e234846522bde732(请参阅已发出的事件部分)

所以我自己想到了这一点 - 我为 geth 创建了一个自定义 JavaScript 跟踪器,它在第三个参数中传递给 geth 到 debug_traceCall(参见 API 提供的参考 link):

{
    data: [],
    fault: function (log) {
    },
    step: function (log) {
        var topicCount = (log.op.toString().match(/LOG(\d)/) || [])[1];
        if (topicCount) {
            var res = {
                address: log.contract.getAddress(),
                data: log.memory.slice(parseInt(log.stack.peek(0)), parseInt(log.stack.peek(0)) + parseInt(log.stack.peek(1))),
            };
            for (var i = 0; i < topicCount; i++)
                res['topic' + i.toString()] = log.stack.peek(i + 2);
            this.data.push(res);
        }
    },
    result: function () {
        return this.data;
    }
}

此跟踪程序由 geth 为跟踪中的每个操作执行。它的主要作用:

  • 检查这是否是 LOG0LOG1LOG2LOG3LOG4 EVM 操作码之一
  • 从当前合约中提取合约地址
  • 提取默认 topic0 和后续主题(如果有)
  • 从内存中提取额外的事件数据(注意:stack[0]是偏移量,stack[1]是数据大小)

将跟踪器传递给 geth 如下所示:

res = await ethersProvider.send('debug_traceCall', [{
    from: tx.from,
    to: tx.to,
    gas: BigNumber.from(tx.gas)._hex.replace('0x0', '0x'),
    gasPrice: BigNumber.from(tx.gasPrice)._hex.replace('0x0', '0x'),
    value: BigNumber.from(tx.value)._hex.replace('0x0', '0x'),
    data: tx.input
}, "latest", {
    tracer: "{\n" +
        "    data: [],\n" +
        "    fault: function (log) {\n" +
        "    },\n" +
        "    step: function (log) {\n" +
        "        var topicCount = (log.op.toString().match(/LOG(\d)/) || [])[1];\n" +
        "        if (topicCount) {\n" +
        "            var res = {\n" +
        "                address: log.contract.getAddress(),\n" +
        "                data: log.memory.slice(parseInt(log.stack.peek(0)), parseInt(log.stack.peek(0)) + parseInt(log.stack.peek(1))),\n" +
        "            };\n" +
        "            for (var i = 0; i < topicCount; i++)\n" +
        "                res['topic' + i.toString()] = log.stack.peek(i + 2);\n" +
        "            this.data.push(res);\n" +
        "        }\n" +
        "    },\n" +
        "    result: function () {\n" +
        "        return this.data;\n" +
        "    }\n" +
        "}",
    enableMemory: true,
    enableReturnData: true,
    disableStorage: true
}])