Nonce and Gas Mechanism in the Blockchain System and High Performance Blockchain’s Optimization

HPB Global
HPB-Foundation
Published in
11 min readJan 15, 2021

When using a crypto wallet to make transfers, users are confused with blockchain concepts, such as Nonce, Gas, and Gas Price. Taking Ethereum and High-Performance Blockchain (HPB) as examples, this article will explain the role of Nonce and Gas from the perspective of the underlying layer of the blockchain system and its transaction process (as shown in Figure 1), and analyze the reasons why these parameter configuration errors may cause transfers at an abnormally high cost. Finally, this article introduces the optimization and innovations of the HPB blockchain system regarding these parameters.

Figure 1. Transaction Process on the HPB MainNet

1. Nonce in the Blockchain System

The nonce is an essential concept in the blockchain system. In Bitcoin, Nonce is mainly used to adjust the difficulty of the PoW-based mining algorithm. In Turing complete blockchain systems such as Ethereum and HPB, the nonce is used to adjust the mining algorithm but also used to control the transaction of external accounts. In particular, when an account sends a transaction request, the nonce prevents double-spend attacks.

Every external account (account binding with a private key) has a Nonce value. When a transaction request is initiated by one account from one node, the nonce value will start from 0. When one transaction request is processed, 1 Nonce in value will be accumulated. Only when the previous Nonce is processed can the next transaction Nonce be processed and added to the total Nonce value.

Note that the transactions sent from the same address at the same node hold the same nonce.

1.1 Principles of Nonce

Here are some rules for using Nonce:

● When the nonce of the user’s transaction is too small (less than the Nonce value used by the previous transaction, as shown in the figure below), the transaction will be rejected directly.

● When the Nonce of the transaction provided by the user is too big (much bigger than the Nonce value used in the previous transaction, as it is shown in the figure below), the user’s transaction will be placed in the queued list of txpool. In other words, the transaction will not enter the transaction pool until all the missing Nonce values are submitted to the transaction pool.

● When the Nonce of the user’s transaction is too big (much bigger than the Nonce value used in the previous transaction, as shown in the figure below), the user’s transaction will be placed in the queued list of txpool. In other words, the transaction will not enter the transaction pool until all the missing Nonce values are submitted to the transaction pool.

● If the geth client is stoped and its transactions are still pending in the queue, the queued list will be deleted.

● When a new transaction has the same Nonce value as a pending transaction, and its gas price is too low, then the new transaction shall not overwrite the pending transaction. Suppose the gas price of the new transaction is 110% higher than that of the pending transaction. In that case, the pending transaction will be overwritten.

● The transaction queue only stores up to 64 transactions issued from the same account. In other words, if you want to make transfers in batches, do not send more than 64 transactions from the same node.

● If you have sent a transaction request, but because the gwei is relatively low or the network is busy, the transaction has not been mined by the miners. You can now “overwrite” the previous transaction by setting a higher gas fee with the same Nonce.

1.2 Common Issues when using Nonce

Based on the rules above, let’s discuss the issues that may arise in actual applications.

● If two transactions with the same Nonce are submitted at the same time, the system may report an error. Suppose a user sent a transaction with a Nonce of 45, then another transaction is submitted with the same Nonce (while the gas price does not reach 110% of the previous one), now a reminder of “Nonce too low” will pop up.

>web3.eth.sendTransaction({from: “0xb8d22d1a950cd52897b8687f6bc13898f6f0f6cc”, to: “ 0x290402ee6f59bc247a8dd478b8f0f96d906abefe”, value: “1000000000000000000”, Nonce: “45”})

Error: Nonce too low

at web3.js:3143:20

at web3.js:6347:15

at web3.js:5081:36

at <anonymous>:1:1

● Get the Nonce of the pending transaction, and manually add 1 to the Nonce value. When you re-submitted the transaction, the system reminds you that “Nonce too low”.

For this case, as the pending transaction might already be mined, Nonce value obtained of the current address in the transaction pool by using the Client’s GetNonce method can be too low. (Only the latest Nonce of the mined transaction is valid, but not the latest Nonce of the transaction in the txpool) . As a result, even if the user performs an adding operation on the Nonce, the actual Nonce in the system is still larger than that in the Client, which will cause the “Nonce too low” issue.

● Before one transaction is completed, Nonce is stored and counted in a single node of the blockchain. In other words, Nonce is not stored on the blockchain but is dynamically changing based on the calculation of the confirmed transactions for one account. Therefore, when the same account uses multiple wallet addresses, it may cause confusion in the use of Nonce. Therefore, the best way is to operate the same account with only one wallet node. Otherwise, there will be abnormal transactions.

2. Gas Mechanism in Blockchain System

In the blockchain systems, gas, gas price, and gas limit are a systemic mechanism that balances security, availability, and economic models.

2.1 Gas in Transactions

Gas means “fuel” in the blockchain system. An EVM (Ethereum Virtual Machine) code environment is implemented on Ethereum. When a transaction is performed on the chain, every node in the network will record this transaction with their node’s computation. Each command that can be executed on the chain has a gas consumption cost. For example: PUSH needs to consume 3 gas, and one transfer generally consumes 21000 gas. The storage consumption on Ethereum is actually expensive. In order to encourage the nodes, every transaction executed on-chain needs a certain payment, and this payment is calculated in the unit of gas.

2.2 Gas price in Transactions

Gas Price is the Ether you are willing to pay for each unit of Gas, represented by Gwei. Therefore, the higher the Gas Price is, the more Eth is paid for each computing step in the transaction. You may be relatively new to Gwei. Gwei is actually 10 ^ -9 Eth. In other words, 1 Gwei = 0.000000001 Eth. Therefore, when you set Gas price = 20 Gwei, you are willing to pay 0.00000002 Eth for every computation.

Tx Fee = Actual Gas Used * Gas Price

For example, if you wish Ethereum to complete the computing in 50 steps, and the Gas Price is 2 Gwei, the total transaction fee will be 50 * 2 = 100 Gwei.

It should be noted that no matter whether the command you execute is successful or not, you always need to pay the computing fee. Because nodes have verified and executed your transaction (with computing power) even the transaction failed. So you’ll have to pay the same fee as that of a successful transaction.

2.3 Gas limit in the Block

Each block has a gas limit, which is the maximum amount of gas allowed in a single block, and that can be used to determine how many transactions can be packed in a single block (it’s the maximum gas allowed in one block, not necessarily the number of transactions). We must set a gas limit for every transaction or contract call for use. If the amount of gas actually used for the transaction is less than or equal to the gas limit you set, it will be executed, but if the total gas consumption exceeds the gas limit, all the execution will be reset, while the fee will still be charged. The total gas value actually consumed during the transaction execution is called gas used, and the unused gas will be refunded to the original account. If you try to package a transaction that succeeds the gas limit of the current block, the transaction request will be rejected by the network with a “below gas limit” reminder.

2.4 Gas limit in the transaction

Each transaction will also have its own gas limit, that is, the maximum amount of gas allowed for a single transaction. From this, it can also be inferred how many transactions can be packed in a single block (which is determined by the gas limit permitted in a single block but not necessarily by the limit numbers of transactions). Increasing the gas limit of the blockchain can increase the permitted transactions in a block, while the block will also become congested. If the network processing speed of an oversized block cannot keep up with the verification speed of the block during network synchronization, it will cause network congestion and seriously affect the blockchain’s network performance. At the same time, if the gas limit of the transactions is set too low, a large number of invalid transactions will be discarded, wasting gas consumption (as the invalid transaction canceled still consumes gas). Therefore, a reasonable gas limit needs to be set by considering factors such as the miner’s computer and network performance. The current gas limit of the HPB MainNet is shown as below:

3. HPB Innovation

HPB uses hardware to accelerate the verification of transaction signatures and has added txpool in the MainNet V1.0.7.0, which reduces the transaction’s gas consumption and gas price. With a lower gas fee, one block can process more transactions, thus increasing TPS greatly. However, as the transaction fee is reduced, the risk of DDOS attack will increase, and the network congestion incurred will affect the transaction’s processing speed.

3.1 How to prevent DDOS attacks

A DDOS attack (Distributed Denial of Service) refers to attacks on one or several targets launched by multiple attackers from various locations simultaneously, or one attacker controls multiple machines located in different locations and uses these machines to attack the target at the same time. As the attacks were sent from attackers located distributedly, this type of attack is called a distributed denial-of-service attack, in which there can be multiple attackers.

3.2 Filtering txpool

First of all, whether the transaction comes from a local node or a p2p network broadcast, it should enter the transaction pool txpool first. The filtering mechanism of each node’s txpool is different, and it can be customized according to the node’s specific requirements.

● In the first phase of the filtering mechanism, the txpool of each node will set the lowest gas price, and the tx below this gas price will be directly discarded. As we know, DDOS attacks must use a large number of low gas price transactions to initiate spam transactions. Since the configuration of each node is different, these spam transactions will not spread in the network.

● In the second phase of the filtering mechanism, the gas price configuration can prevent spam from spreading in the network, but a single node can be attacked by spam transactions. To solve this issue, we can use a limit configuration to fix it. The node can set its own limit settings, like how many accounts can be accepted and how many transactions each account can process. Those that exceed the limit set will be rejected, and a new transaction with a higher gas price needs to be sent to replace the spam transaction. This is not worth the loss for the attacker. You can refer to the logic of adding transactions to the transaction pool as below:

● In the third phase of the filtering mechanism, when the node transaction pool’s cache is full, new transactions will not be accepted. A timeout mechanism will be triggered to delete transactions that are not packaged at the expiration time.

Discussion on the lowest gas price of the node in TxPool:

The lowest gas price setting of the transaction pool is one of the node’s personalized configurations, representing the lowest price transaction that the node can accept from the network or RPC. If the node is also a miner, the gas price will determine how much transaction fee they can charge for their mining service. Therefore, when there are a lot of transaction requests, miners can accept higher price transactions only to increase their income by increasing the gas price in the transaction pool accordingly. However, suppose there are relatively few transaction requests. In that case, the gas price can be lowered to process as many transactions as possible to maintain the node income.

One thing to note here is that if the user is just a node serving for synchronization, and its gas price is lower than the gas price of all the miner nodes, then some transactions in his trading pool may fail. Although the transaction meets the requirements, the miner may refuse to package it, because the gas price is too low for the transaction to be processed. Therefore, the gas price settings of the synchronization node still need to refer to that of the recently packed transactions.

3.3 Gas Mechanism of HPB MainNet

Txpool’s filtering mechanism is only implemented at the node, and the smart contract virtual machine can prevent attacks during the execution of the transaction. Users can run any program on their nodes. However, suppose the user writes an infinite loop program. In that case, the entire system may crash as the CPU, storage, and bandwidth resources of these nodes run out of use.

In order to prevent such DDOS attacks, the miner fee gas mechanism is designed. Any program running on the blockchain network will have calculation steps according to the resources that need to be consumed, and each step needs to pay a certain amount of gas. When the user runs this program, the total amount of gas that needs to be paid in advance is the gas limit in the figure. If the gas is consumed in advance during the calculation process, the program will be terminated automatically. In this way, malicious DDOS attacks can be prevented on the network. On the basis of inheriting the mechanism of Ethereum, HPB also increases the Gas Limit considering the security of the system.

References

This article is contributed by The HPB Blockchain Lab, translated and edited by Sophia and Fay. Also thanks Gordon Glass for the English proofreading!

For the original article written in Chinese, please visit: https://mp.weixin.qq.com/s/2VkiTG7_nbGoYHLvaQBObA

About HPB

High Performance Blockchain (HPB) is a revolutionary permissionless blockchain architecture that combines HPB’s customized hardware Blockchain Offload Engine (BOE), with high-performance blockchain software, enabling unrivaled scalability.

| Website | Twitter | Telegram | Reddit | Youtube | Github |

--

--