> 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/rome-xue-yuan/04-how-rome-runs-your-code.md).

# 4. Rome 如何运行你的代码

Rome 交易如何转化为 Solana 交易，为什么大多数调用会原子执行，以及当某个调用过大时会发生什么变化。

你的 Solidity 无需修改即可运行。改变的是运行位置：在 Solana 程序内，并受 Solana 限制约束。本课将为你提供所需的全景图，帮助你预测一次调用的成本以及它何时会失败。

## 你将学到

* 一次交易从你的钱包到 Solana 验证者，再返回到 EVM 收据所经历的路径。
* 原子执行与迭代执行的区别，以及为什么你需要原子执行。
* 哪些 Solana 限制会影响你的 Solidity：计算、大小、账户数量、锁定。
* 如何衡量一次调用的真实成本。

## 开始之前

* 完成 [3. 你在 Hadrian 上的第一个合约](https://docs.rome.builders/zh/rome-xue-yuan/03-first-contract-on-hadrian.md)。你需要一个已部署的合约，以及一笔你已发送给它的交易。
* 请把那笔交易哈希放在手边。你将在本课结束时在浏览器中打开它。

## 一笔交易的路径

你签署一笔普通的 EVM 交易，并将其发送到 Rome 代理，使用 `eth_sendRawTransaction`。代理此时不会立即转发。它会先在一个模拟器中链下执行该调用，模拟器执行与链上程序相同的 EVM 逻辑。该模拟会告诉它三件事：这笔调用需要多少 gas、调用会触及哪些 Solana 账户，以及该调用是否能装入一笔 Solana 交易中。

随后，代理将你的调用封装为 Solana 指令并发送出去。Rome EVM 程序是一个普通的 Solana 程序，它会解释你的字节码。状态变更会落入 Solana 账户中。索引器读取结果，并生成你的工具所期望的 EVM 区块和收据。

每个 EVM 地址都映射到一个由 Rome 程序拥有的 Solana 账户。你的合约余额、nonce、代码和存储，全部都是 Solana 账户。这就是为什么代理在发送前必须知道账户列表：Solana 要求所有账户都必须事先列明。

在 [执行模型](https://docs.rome.builders/zh/he-xin-gai-nian/execution-model.md).

## 原子优先

大多数调用都会在一笔 Solana 交易中运行。Rome 将此称为原子执行（VmAt）。它是全有或全无：如果任何一步失败，整个调用都会回滚。转账、兑换以及大多数 DeFi 调用都能适配。也有一些不能：在 Hadrian 上，一次 Aave 借款需要超过一笔 Solana 交易。

一次原子调用的预算约为 140 万计算单元。以下限制会影响每一次调用：

| 限制   | 这对你意味着什么                                                              |
| ---- | --------------------------------------------------------------------- |
| 计算   | EVM 执行与你所调用的每个程序共用约 140 万 CU                                          |
| 账户   | 64 个地址，包括 Rome 自身的账户以及你的所有 CPI 所触及的内容                                 |
| 大小   | 每笔 Solana 交易 4 KB；更大的 EVM 载荷，例如大型部署，会通过一个 holder 账户分阶段处理，SDK 会为你处理这一切 |
| 合约大小 | 24 KB，与 Ethereum 相同                                                   |

包含这些数值的完整列表在 [约束](https://docs.rome.builders/zh/he-xin-gai-nian/constraints.md).

## 当调用过大时，改为迭代执行

如果模拟器发现某次调用无法装入，Rome 会以迭代方式运行（VmIt）。该调用会被切分为多个腿（legs）。每一腿都是一笔独立的 Solana 交易，并且只执行其计算预算允许范围内的操作码。各腿之间，VM 状态会保存在 holder 账户中，下一腿再接着执行。

迭代执行可以工作，但有其代价：

* 一个会话在 160 个腿之后结束。超过这个数量后，它会释放锁并且不提交任何内容。
* 会话会锁定其余额和存储账户几秒钟，并在每一腿续期。对已锁定账户的原子写入会被拒绝，返回 `AccountLocked`，客户端随后会重新发送。
* 在旧路径上进行 Solana CPI 的调用（第 5 课中的 `CpiProgram` 和 `HelperProgram` 调用）根本不能迭代运行。它们必须满足原子执行要求。
* 在 Solana 车道上，由钱包自行签署每一腿的情况，会让用户每一腿都收到一次提示。

设计原则因此很明确：将每个用户操作塑造成一个原子事务即可。如果一个流程更大，就把它拆成两个用户操作，而不要依赖迭代模式。第 5 课在你调用 Solana 程序时会再次回到这一点。

## 你支付什么

Rome 以 Solana lamports 计算一次调用的成本：签名费、固定通行费，以及该调用创建的任何账户的租金。一个 gas 单位代表一 lamport 的成本。gas 价格会将其换算为链上的 gas 代币，在 Hadrian 和 Rubicon 上该代币是 USDC。

租金是最容易让人意外的部分。第一次转账到一个从未使用过的地址会创建账户，因此成本远高于重复转账。首次触达时 gas 会很高，之后则恢复正常。具体计算示例见 [Solana 与 EVM 如何在 Rome 上交汇](https://docs.rome.builders/zh/he-xin-gai-nian/how-solana-and-evm-meet.md#gas-and-rent).

## 衡量，不要猜测

EVM `gasUsed` 并不能真实反映 Solana 计算。对于 Rome 来说，1 个 gas 单位就是 1 lamport 的成本，因此 `eth_estimateGas` 只能帮你设定 gas 上限，却不能告诉你计算单元是多少。请从 Solana 一侧读取计算值。通过链接进入该笔 Solana 交易在“Settled in”一行中的链接，Solana 浏览器会显示它消耗的计算单元。在代码中，可通过 `rome_solanaTxForEvmTx`获取 EVM 哈希对应的 Solana 签名，再使用 Solana 的 `getTransaction` 和 `maxSupportedTransactionVersion: 1`，并读取 `computeUnitsConsumed`。一次迭代调用每一腿对应一笔交易。 [计算预算](https://docs.rome.builders/zh/he-xin-gai-nian/compute-budget.md) 列出了每个操作的典型成本，以及能够降低成本的良好习惯：为程序 ID 使用常量、缓存 PDA 推导结果、减少账户数量。

有两个行为与 Ethereum 不同，值得现在就了解：

* Rome 没有按操作码计量的 gas 表。你传给 `CALL` 的 gas 不会被强制执行，也不存在 2300 gas 津贴。请显式防范重入。
* `block.number` 对应的是 Solana slot，而 `block.timestamp` 对应的是 Solana 时钟。

## 自我检查

在 Hadrian 浏览器中打开你第 3 课的交易。查找 [Hadrian](https://docs.rome.builders/zh/wang-luo/hadrian.md) 中的网络链接，以及 [Via explorer](https://docs.rome.builders/zh/rome-shang-de-ying-yong/via.md).

中的浏览器说明。

1. 在“Settled in”这一行中，找到结算你调用的 Solana 交易。
2. 在 Solana 浏览器上查看它消耗了多少计算单元。

将该数值与 140 万预算进行比较。一个简单调用只会消耗其中很小一部分。

然后回答：

<details>

<summary>1. 为什么代理在发送调用前要先模拟它？</summary>

Solana 要求所有账户都必须事先列明，而且代理必须知道该调用是否能装入一笔交易。模拟会找出被触及的账户、估算 gas，并决定使用原子模式还是迭代模式。

</details>

<details>

<summary>2. 一个调用会产生旧路径的 CPI（即第 5 课中的 `CpiProgram` 和 `HelperProgram` 调用），并且需要超过 140 万 CU。会发生什么？</summary>

它无法运行。旧路径 CPI 在迭代交易中会被拒绝，因此该调用必须能装入一笔原子交易。请把工作拆分成更小的用户操作。

</details>

<details>

<summary>3. 为什么第一次转账到新地址会很贵？</summary>

Rome 会为其创建 Solana 账户，而这些账户的租金会作为 gas 计入发送方。

</details>

## 下一步

[5. 从 Solidity 调用 Solana](https://docs.rome.builders/zh/rome-xue-yuan/05-call-solana-from-solidity.md)


---

# 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/rome-xue-yuan/04-how-rome-runs-your-code.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.
