Original Title: "Common Misunderstandings of Ethereum Programmers"
Original Source: Ethereum Enthusiasts
< /p>
Recently, I came across an article titled "Programmers' Misunderstandings About Time Zones" made me laugh out loud. This article reminds me of other misunderstandings of programmers, such as name and time , so I started looking for something about Ethereum. But to no avail, I have to do my best.
Calling estimateGas will return the amount of gas consumed by the transaction
Calling estimateGas will indeed return a gas consumption amount, but this is the The amount of gas that will be spent for packaging a transaction in the current state And the current state of the blockchain may be very different from the state when you need the transaction to be on the chain. Therefore, when your transaction is effectively included in a block, it may take a different code path and consume a completely different amount of gas.
If the executed code is the same, the amount of gas consumed by my transaction is also the same
Not true. Even if you execute the same command with the same parameters, the gas cost may be different. For example, if you are writing to a new storage location, an SSTORE (write to storage operation) will be much more expensive than a storage location that already has a non-zero value (see EIP2200). This means that if you send two ERC20 token transfers to a new address, the first transaction will cost much more than the second, even though both execute the exact same code.
If the state is exactly the same, the amount of gas consumed by my transaction is also the same
Normally yes, Unless you're unlucky enough to have a hard fork that causes some operations to be repriced. While this sounds complicated, the bottom line is that you cannot safely hardcode the gas limit for transactions in a dApp unless you are determined to release dApp updates after every hard fork.
If the code is the same, the state is the same, and there is no hard fork, I can trust  ;Is there a return value for estimateGas?
Now you can believe that the return value of estimateGas is the amount of gas consumed by your transaction, but you don’t know whether the transaction It will work as you wish. The so-called gas estimation is that the node will try your transaction with different gas values and return the lowest gas value that ensures your transaction will not fail. However, the node will only look at your transaction, not the internal calls of the transaction. This means that if the code of the contract you are calling has a try/catch block that causes the internal call to occur and cannot be undone, the gas estimate you get is good enough for the calling contract, but not for the called contract. It's not enough.
In multi-signature wallets this happens a lot: even in Transactionfailure, most multisig wallets will mark the operation as executed, which means they cannot undo the outermost transaction (so impact). Therefore, a raw gas estimate returned may be sufficient for multisig code, but not necessarily sufficient for the operation you actually want to run. This is why GNOSIS SAFE has a special GAS estimate method .
Note that this is why operations failing due to insufficient gas are hard to detect. Internal calls may run out of gas due to being allocated too little gas, while the transaction itself may still have a lot of gas available. This means that looking at a transaction's gas usage and gas limit is not a reliable way to detect gas errors.
What the hell, I just send more gas every time p>
In most cases, this method works. But remember, a contract can view the gas it received in a transaction. Therefore, contracts can be easily written to fail if too much gas is received. My suspicion though is that there's no point in doing so other than proving the point.
As long as the node accepts the transaction, the transaction will be mined
What a beautiful idea. Ethereum's network congestion can cause gas prices to fluctuate wildly, so your transaction may be evicted from the mempool (a collection of transactions waiting to be mined). If the gas price spikes, you need to resend the transaction.
I can slightly increase the gas price and resubmit the transaction
As long as you increase the gas price to the minimum required by the node you are interacting with (see txpool.pricebump ), then there is no problem, otherwise it will still be rejected.
Miners always choose the transaction with the highest gas price
< br>
Not necessarily. Miners can choose whatever they want. They may insert their own transactions for their own benefit, and even open an out-of-protocol channel< /a>, package transactions for users who meet their requirements.
However, even if they prioritize packaging based on revenue, how to fill blocks optimally is a knapsack problem (knapsack problem). Since transactions cannot be split into parts, it may be more profitable to package two 5M gas transactions in a block with a gas limit of 10M instead of one 6M gas transaction, even if the gas price of the 5M gas transaction is low Transaction at 6M gas.
If I send the same transaction with a higher gas price, miners will choose the latter transaction to Do you want to replace the previous transaction?
The replacement transaction must be sent to the miners before the old transaction is uploaded to the chain. That is, if you send a replacement transaction, you still need to monitor the hashes of all transactions you previously sent with the same nonce.
Misunderstanding about NonceI can get the nonce of my next transaction through getTransactionCount
It depends on the block parameter you are using. If you query your transaction count based on the latest block, you will ignore your unpacked transactions, and further cause you to accidentally overwrite one of your unpacked transactions.
I can get the nonce of my next transaction via getTransactionCount('pending')
While this works in most cases, you can't guarantee that all your unbundled transactions are in the mempool of the node you're querying . If you have a lot of unbundled transactions, the node you communicate with may have discarded some of them, but those transactions may still exist elsewhere!
I can effectively monitor events by continuously calling getLogs
Although this is a very useful method (yes, it is Polling!), but problems arise in case of chain reorganization. If you were to poll the new log on the latest block, you would not be notified about block reorganizations, and would not know if the events you were seeing required realignment.
I can monitor events effectively by installing filters
Until two weeks ago, This is not yet a common option as Infura doesn't support http-based filters, MetaMask uses http-based filters by default, which means 99% of your dApp's users use this filter (Note: I may have some exaggerate). In addition to new events, the filter will notify you of events removed due to block reorganization. However, this requires that the infrastructure and nodes you are interacting with remain online. If they happen to lose filter state, you run the risk of missing reorganization events.
I can pass websocket subscription to effectively monitor events
Great! In this way, in addition to believing that your node will remain online, you also need to believe that you will remain online, and the connection between you and the node is reliable. I was wondering how many times you've been dropped during a Zoom meeting this week?
Now, I must admit, I've become a bit obsessed with this topic, so much so that I gave a Lightning Talk. If you want to learn more, EIP234 nicely illustrates the rationale for these challenges, which ethereumjs-blockstream addresses.
Smart contracts are immutable
Brother, if you still have this kind of thinking, you are really out. I am in a 30 pages article This is covered in , which is really, really long.
A smart contract that does not contain any DELEGATECALL is immutable
Actually, a contract can periodically call ( CALL) into a variable address and use the result as part of a calculation, or as an instruction to change the state, thereby changing the The code that is running.
Smart contracts that do not contain any DELEGATECALL or CALL are always immutable right?
And STATICCALL. Don't forget STATICCALL!
Smart contracts that do not contain any CALL are immutable p>
You have to rule out another situation: this smart contract is deployed through CREATE2, it will dynamically load the runtime in its initial code (initcode), and it can automatically destroy. In this case, the "owner" can destroy the contract and recreate it on the same address with a different code.
Smart contracts that do not contain any CALL and are not deployed via CREATE2 are not allowed Changed
There is one more situation to exclude: this contract is deployed by a contract deployed by CREATE2. Therefore, you need to trace the entire deployment chain, find the Ethereum external account that originally created the contract, and ensure that there is nothing tricky and that there is no self-destruct operation. This article explored the issue in depth.
I will not expand, this topic is more suitable Write an entire article. Use OpenZeppelin's SafeERC20 (you can read more about it in this article) just fine. Remember that when transferring funds, the The tokens received are not necessarily equal to the tokens deducted from the sender. Let's move on to the next part.
Misconceptions About Ether The total supply of Ether will only increase p>
We all know that there are many ethers that cannot be used, some because the private key of the external account is lost, and some are accidental send to all zero address, Others are because they are stuck in the contract and cannot be processed (sorry, I couldn't help it). All in all, this part of the ether still exists, but it cannot be accessed.
However, there is a way to destroy ether. If you instruct a contract to self-destruct selfdestruct and designate itself as the recipient of funds, all ether within the contract will be destroyed. This means that as long as you are willing to burn more ether than the block reward, you can make ether deflation.
I can write a contract that rejects any ether transfer
As you probably know, if you don't declare any payable methods, Solidity will reject all ether transfers sent to your contract, preventing funds from being stuck in the contract . However, we can also Without triggering any code, send funds into a contract: either designate the contract as the recipient of the self-destruct action reward, or designate it as the recipient of the block reward. As @gorgos pointed out in the comments, it is possible to precompute the contract deployment address and send ether to that address before the contract is deployed.
That is, if you track all ether transfers sent to your contract, your total balance may be greater than the sum of all transfers you process.
Original link
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia