> For the complete documentation index, see [llms.txt](https://docs.rome.builders/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rome.builders/zh/canton-zk-evm/how-it-works.md).

# 工作原理

跟随一个 Ostia 区块从 reth 经证明到其 Canton 提交。

每次 builder 运行都会生成、证明并提交一个区块。Canton 决定该区块是否成为链的下一个头部。一个 EVM 区块及其记录的 Canton 记录项会在同一笔交易中提交。

本页介绍 commit `a84e84e439a5cc669c0ae61facf7c8c995d9d564`. 请阅读完整的 [设计](https://github.com/rome-protocol/canton-zk-evm/blob/a84e84e439a5cc669c0ae61facf7c8c995d9d564/docs/DESIGN.md).

```mermaid
flowchart TD
    W["EVM 交易"] --> R["reth 按需构建"]
    R --> B["构建器读取网关记录项并找到对应的 Canton 合约"]
    B -->|"某个记录项无法结算：将其交易排除并重新构建"| R
    B -->|"所有记录项都可以结算"| P["ZisK 证明该区块"]
    P --> A["推进：将区块、证明和记录项放在一笔 Canton 交易中"]
    A --> S["外部调用：sidecar 检查证明和网关状态"]
    S --> D["Daml 检查头部并结算这些记录项"]
    D --> C["Canton 将区块及其记录项一并提交"]
    C --> F["reth 将该区块标记为最终确定"]
```

## 基于 Canton 的头部构建

钱包将 EVM 交易发送到 reth。所提供的启动器会关闭对等节点发现。构建器请求时，reth 会生成一个区块。

构建器首先读取当前生效的 `ZkChain` Canton 上的合约。它将 reth 的 head、safe block 和 finalized block 移到该已提交的 head。它按价格选择待处理交易，保持每个发送方的 nonce 顺序，并限制在 gas 上限内。随后它要求 reth 使用该明确的交易列表进行构建。

需要 Canton 操作的 EVM 交易会调用网关。网关记录一条记录项，并将其纳入该区块的滚动哈希。构建器按日志顺序读取网关事件，并找出每个记录项所需的 Canton 合约。

如果某项分配已被撤销、接收方尚未接受提款，或者缺少其他必需合约，构建器就会排除该交易以及该发送方之后的交易。它会将 reth 回退到 Canton 的头部并重新构建。这些检查在证明之前进行。该 [构建器 README](https://github.com/rome-protocol/canton-zk-evm/blob/a84e84e439a5cc669c0ae61facf7c8c995d9d564/builder/README.md) 列出每个记录项的检查。

## 证明该区块

构建器保存区块和 reth 的执行见证。输入工具将区块、见证和 genesis 组合为证明器输入。ZisK 运行 guest 程序，由其依据链的固定规则验证该区块。

证明器返回一个 1,344 字节的封装证明，它承诺于区块哈希。构建器提交 `推进` 连同区块头、交易、证明、记录项以及网关在该区块状态根处的账户和存储证明一起提交。

## 在一笔交易中检查并结算

每个确认参与方运行一个 sidecar。Canton 的外部调用会将区块发送到该参与方的 sidecar 以进行 `验证`。它检查证明、固定的程序密钥和 ZisK 版本，以及区块头哈希和交易列表根。

Daml 将答案与以下内容进行核对 `ZkChain`：父区块必须是当前 head，编号必须是下一个编号，并且 gas 限额和已用 gas 必须符合上限。Daml 比较 sidecar 的答案；它不会自行验证零知识证明。

随后，Daml 会为其所命名的 Canton 合约中的每个记录项写入一行。某次提款的 id、burner 和金额来自构建器；已证明的哈希将它们与区块记录的内容绑定。sidecar 的 `交易腿` 函数会检查网关的账户和存储证明，按顺序对这些行进行哈希，并将结果与已证明状态中网关的值进行比较。没有记录项的区块也必须用空列表通过同样的检查。

Daml 按顺序结算这些记录项并创建 `BlockRecord` 以及下一个 `ZkChain`。Canton 会将这些更改一并提交。如果检查或结算失败，则不会提交任何内容。消耗链上的唯一状态合约使 Canton 成为区块顺序的权威。请参阅完整的 [Daml 接口](https://github.com/rome-protocol/canton-zk-evm/blob/a84e84e439a5cc669c0ae61facf7c8c995d9d564/daml/README.md) 和 [sidecar 检查](https://github.com/rome-protocol/canton-zk-evm/blob/a84e84e439a5cc669c0ae61facf7c8c995d9d564/sidecar/README.md).

## 当 Canton 拒绝时

在 Canton 提交该区块后，构建器会在 reth 中将其标记为最终确定。如果 Canton 拒绝它，构建器会将 reth 恢复到 Canton 上一次提交的区块，并把被拒绝区块的交易送回交易池。下一次运行会再次检查它们的记录项。已被撤回分配的付款会被排除在外。

如果构建器丢失了提交回复，它会再次读取 Canton 的 head，以确定该区块是否已提交。如果这次读取也失败，它会报告结果未知，并保持 reth 当前位置不变。下一次运行将从 Canton 的 head 开始。这些恢复规则见 [构建器 README](https://github.com/rome-protocol/canton-zk-evm/blob/a84e84e439a5cc669c0ae61facf7c8c995d9d564/builder/README.md#what-one-run-does).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rome.builders/zh/canton-zk-evm/how-it-works.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
