Nonce Management
In Conflux, each account has a nonce value, representing the number of transactions executed by that account. This value can be obtained using the RPC method cfx_getNextNonce
. The nonce field in a transaction is used to specify the execution order, with lower nonce values indicating earlier execution. Typically, you can directly use this value as the nonce for the next transaction.
However, in scenarios with high network transaction volume (congestion) or when quick transaction submission is required, obtaining the nonce value becomes more complex. This article will provide a detailed explanation of the nonce update mechanism and how to manage transaction nonces in special circumstances.
Nonce Mechanism
Here are some details about the nonce mechanism:
- The execution of transactions on the blockchain is in the order of account nonce from small to large.
- Nonce 的初始值是 0,每执行一次交易,nonce 就增加 1。
- Nonce 不能重复使用。
- Nonce 不能跳过:假设一个账户的当前 nonce 是 n。 If the nonce of the transaction is m such that m > n, then the transaction will not be executed until all transactions with nonce < m have been executed.
- After the transaction is sent via the
cfx_sendRawTransaction
method, it will not be executed immediately. 你必须等待矿工先打包它。 一旦打包,它将延迟 5 个 epoch 执行。 交易执行后,账户的 nonce 将增加一。
Issues Caused by Improper Nonce Usage
Setting the nonce incorrectly when sending transactions can result in transaction failures or getting stuck in the transaction pool, preventing it from being packaged and executed. Below are some common error messages and their corresponding solutions.
Discarded Due to a Too Stale Nonce
If the nonce of a newly sent transaction is less than the current nonce of the account, the transaction will be rejected, and an error message like the following will be returned:
"\"Transaction 0x0101010110 is discarded due to a too stale nonce\""
This error indicates that the nonce value used is outdated or has been reused, and it needs to be updated to the latest nonce value.
Tx With Same Nonce Already Inserted
If a transaction is sent to the transaction pool but has not been executed yet, sending another transaction with the same nonce will result in an error message like:
"Tx with the same nonce already inserted. To replace it, you need to specify a gas price > {}""
In this case, you should wait for the transaction in the pool to be executed. If you want to replace the transaction in the pool, you need to set a higher gas price and resend it.
Sometimes, the error message may also be:
"\"tx already exist\""
The handling is the same as above.
discarded due to in too distant future
If the nonce value for a transaction is too large, exceeding the user's current nonce by more than 2000, an error message will be returned:
"\"Transaction 0x0101010101010101 is discarded due to in too distant future\""
Solution: Use the correct nonce value when sending the transaction.
In addition to nonce misconfiguration causing transaction failures, there are other scenarios as well. For more details, refer to Sending Transaction Errors.
Unable to Retrieve Transaction Receipt After Sending
There is a situation where, after sending a transaction, the receipt cannot be obtained for an extended period. This is typically due to the transaction using non-consecutive nonces. In such cases, the transaction gets stuck in the transaction pool, awaiting the execution of prior transactions.
For example, if the account's current nonce is 1 and you send a transaction with nonce 5, it will be stuck in the transaction pool, waiting for transactions with nonces 1, 2, 3, and 4 to be sent and executed.
To ensure the execution of this transaction, you need to send transactions with nonces 1, 2, 3, and 4 to the transaction pool. Once these transactions are packaged, the transaction with nonce 5 will automatically be included and executed.
For more information on pending transactions, refer to Transaction Pending.