Trade guide

What is a nonce? (All you need to know)

In Ethereum, the word nonce means the number of transactions sent from a specific address or the number of contracts made by a particular account.

All Ethereum transactions are created based on an account. In Bitcoin, this structure is implemented based on a UTXO. Therefore, Ethereum does not need to record each transaction in order. A nonce uses here, which is a feature of the primary addresses of each transaction. This variable stores in the Ethereum blockchain, but it is calculated based on the number of transactions sent to each address.

You can also read about Futures Contract: An Essential Tool For Currency And Product Transactions

Create Nonce

A nonce creates once after each transaction creates, in which case blockchain must perform the following steps:

  1. Each transaction sent by external account (EOA);
  2. Contract Wallet

The nonce number does not change because the contract call, transaction transfer, and other calls are in an internal system.

What is the role of nonce?

Suppose you want to do two transactions in a row. Send one Ethereum once and four Ethereums once, respectively. How can miners determine which trade you want to make first? If the nonce of your first transaction is zero and your second transaction is one, this is done in the order of the transaction number. Therefore, the main application of Nonce in Ethereum can be this mode. If not sent in the Nonce transfer, the number of variables is as follows:

After serializing the transaction, for example, consider the following string:

25de0d5a1693d4e45ce0305d42774b5bf73cbd9e14230194c35545e0f01ee45ce0305d42774b5bf73cbd9e0d5a1693d4e45ce0305d427

After your transaction, ten Ethereums send to the destination. But if someone can copy and duplicate this serialized string, your tokens will be destroyed, which is called a replay attack.

If there is a nonce within the sent package, blockchain will execute each transaction only once.

How to use a nonce?

How to package and send the transaction is as follows:

There is a txpool in the Ethereum. This pool can hold all transactions. Each new transaction enters the pool first, exits the pool when the blocks are packed, and then the blocks and transactions are connected. So somewhere in the blockchain, transactions have to wait to connect to their block after extraction.

Whenever a new transaction or contract is created, the nonce value will be asked of the Ethereum blockchain and then considered as the nonce of the trade and sent to the Ethereum blockchain.

Send transaction eth_ send transaction

Creates a new message call transaction or a contract creation if the data field contains code.

Need to pass parameter nonce, the official document for the nonce:

Nonce: QUANTITY – (optional) Integer of a nonce allows you to overwrite your pending transactions that use the same nonce.

Get nonce eth_ getTransactionCount

Parameters:

params: [
   address,
   QUANTITY  // latest, pending
]

demo

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0xf40629b5F96567270794F0F29E55Ac9daDE14fFd","earliest"],"id":1}'  https://ropsten.infura.io/v3/404b78d3e9364b79921c39a8ea909b1c
{"jsonrpc":"2.0","id":1,"result":"0x5"}%

In this case, five outbound transactions consider, and the nonce of the last transaction equals 4. Because the nonce starts from zero, the equation is correct, and for the sixth transaction, the nonce must be 5.

If there is a transaction in pending mode, a transaction without nonce is available. So, in this case, you have to set the parameter to pending mode. Transactions that confirm cancellation are marked with canceled.

Once each transaction package and its Nonce number sends, blockchain will no longer cancel it.

Accelerate and cancel the transaction of Ethereum

Each nonce is an integer number so that the blockchain can replace its number with another transaction and a similar nonce.

Due to its unique nature, the nonce can be re-implemented to speed up transactions and execution.

Accelerate Ethereum

If the gas cost is low, the transaction will always remain pending. Suppose, in this case, the nonce number is equal to one.

Now a second transaction is made, and the gas rate is set higher. In this case, if the nonce remains the same as 1, the previous transaction is deleted. The new transaction replaces with the same nonce.

The second transaction is then packaged and sent ready. Miner rechecks the pool and sees a similar nonce number. In this case, if the miner detects this issue for unknown reasons, he will reject the transaction.

Cancel the deal

  1. To increase the speed of transactions.
  2. First, make the second transaction, set the values ​​to zero, and publish it.
  3. The original transaction is lost, and a new transaction is created. The miners repackage the deal, but you have to pay for the second transaction.

Nonce Exception handling

replacement-transaction-underpriced

  1. Pending: pending transactions in the account;
  2. The new transaction is the same as the transaction nonce of pending;
  3. The gas price of the new transaction is lower and cannot replace the pending transaction

Nonce rules

  1. When you set the Nonce rate too low, blockchain will reject the transaction. In this case, you must increase the nonce number to be greater than the current nonce blockchain.
  2. Transactions with very large nonces route to the transaction pool queue in the blockchain.
  3. If you submit a transaction whose nonce number is between the last acceptable nonce and the highest nonce sent, the sequence is complete, and the sequence transactions are processed.
  4. The pool queue can hold up to 64 transactions per account. So if you want to send transactions in batches, you can only send 64 transactions from one address at a time.
  5. When the geth variables are restarted, the transactions in the pool will also be lost.
  6. Blockchain will reject the transaction if the nonce number sent is correct but low account balance.

Conclusion

As we see, the nonsense variable helps a lot to prioritize transactions, especially when sending large bulk transactions to the blockchain. This function can be used to initialize each transaction so that the number is greater than the nonsense of Ethereum blockchain.

What is your reaction?

Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0

You may also like

Comments are closed.

More in:Trade guide