0Pricing
Web3 & DApp Development Fundamentals · 课时

Nonce 与排序

交易排序

Nonce 与排序 是 CoddyKit 上的免费 Web3 & DApp Development Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web3 & DApp Development Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Order Matters

Imagine sending two transactions: first deposit, then withdraw. If they execute out of order, the withdrawal could fail.

The nonce guarantees your transactions run in the exact order you intend.

What Is an Account Nonce?

An account nonce is a counter tracking how many transactions an account has sent. It starts at 0 and increases by 1 with each confirmed transaction.

Note: this is different from the mining nonce in Proof of Work.

Account history:
  tx with nonce 0  (1st tx)
  tx with nonce 1  (2nd tx)
  tx with nonce 2  (3rd tx)
  next expected nonce: 3

Enforcing Sequence

The network only executes a transaction whose nonce matches the account's next expected value.

A transaction with nonce 5 will not run until nonces 0 through 4 have been processed.

Preventing Replay Attacks

Because each nonce is used once, an attacker cannot replay (resubmit) an old signed transaction.

The network already processed that nonce, so the duplicate is rejected automatically.

Stuck Transactions

If a transaction with a given nonce gets stuck (too low a fee), all later nonces are blocked behind it.

Nothing can proceed until that gap is filled, because nonces must be sequential.

pending:
  nonce 4  (stuck, low fee)
  nonce 5  (waiting...)
  nonce 6  (waiting...)
-> nonce 4 must clear first

Replacing a Transaction

To unstick things, you can replace a pending transaction by resubmitting it with the same nonce and a higher fee.

This is often called speed-up or cancel — validators take the higher-paying version.

Gaps in Nonces

A nonce gap happens when you submit nonce 7 while nonce 6 is missing.

The transaction sits unprocessed until the gap is filled, since the network requires a continuous sequence.

Ordering Within a Block

The nonce orders transactions from one account, but ordering between different accounts within a block is decided by the validator.

Validators typically order by fee, which is why ordering can be exploited (see MEV).

MEV and Ordering

Because validators choose transaction order, they can profit by reordering, inserting, or censoring transactions. This is called Maximal Extractable Value (MEV).

It shows that ordering is economically valuable, not just technical bookkeeping.

Checking the Next Nonce

Wallets query the network for the next nonce using eth_getTransactionCount.

This returns the count of confirmed transactions, which equals the next nonce to use.

count = eth_getTransactionCount(
    myAddress, "pending")
// count is the next nonce to use

Putting It Together

The account nonce sequences your transactions, prevents replays, and must be continuous.

Validators decide cross-account ordering, which introduces fee-based prioritization and MEV.

Quick Check

Test your nonce knowledge.

Recap: Nonces and Ordering

You learned that:

  • The nonce counts an account's transactions and enforces order
  • It prevents replay attacks
  • A stuck low nonce blocks later ones until replaced
  • Validators decide cross-account ordering, enabling MEV

Next course: EVM fundamentals.

常见问题解答

「Nonce 与排序」课时是免费的吗?

是的 — 「Nonce 与排序」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web3 & DApp Development Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Web3 & DApp Development Fundamentals 课程共包含 4 节课。

「Nonce 与排序」这节课中我会学到什么?

交易排序 你通过在浏览器中直接运行的动手代码来练习 Web3 & DApp Development Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web3 & DApp Development Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web3 & DApp Development Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「Nonce 与排序」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Web3 & DApp Development Fundamentals 课中编写并运行代码吗?

能。每节 Web3 & DApp Development Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 什么是 Gas
  2. 交易生命周期
  3. Gas 限制与费用
  4. Nonce 与排序
← 返回 Web3 & DApp Development Fundamentals