How does Ethereum estimate gas? There is a gas used by TXN in the transaction info of Etherscan, and the result is consistent with the result given by Remix and the result given by gasUsed of getTransactionReceipt in GETH. Gas cost can be estimated directly using GETH or Remix simulation.

I haven’t been able to figure this out, so LET’s do an experiment.

At the bottom of the Remix browser there is an executable log page that can detail and debug, which is very convenient.

Where there is gas cost, there are two places, transaction cost and execution cost. What is the difference between the two? You can refer to their source code.

Briefly: Transaction Cost refers to the cost of sending a transaction to the Ethereum Blockchain, based on the size of the data size, which is used to deploy the contract. Execution Cost refers to the cost required for the virtual machine (VM) to execute the construction child and some initialization work when the contract is deployed.

Here’s a simple contract experiment:

contract Test {

bytes32 public tmp;

function test(

bytes32 input,

uint num

)

constant returns (bytes32){

bytes32 result = input;

for(uint i = 0; i < num; i++) { result = sha3(result); }}function set(bytes32 input, uint num) {

tmp = test(input, num); }}Copy the code

If you call constant function directly, it will not consume gas because it is calculated by its own node that does not change to the blockchain. However, if a normal contract (non-constant function call) calls a constant function, it will consume gas because the miner calculates the constant function.

In the simple contract above, I asked test to do sha3 on the first bytes32 argument, and the second uint argument to do several loops. I called set and test with arguments 10 and 1000, respectively.

setX63d7db5ce060b288ecf5390594d5969bc1a206ceeb24df31cffcc8876df5e44b (" 0 ", 10) transaction cost: 30628 execution cost: 6988Copy the code
setX63d7db5ce060b288ecf5390594d5969bc1a206ceeb24df31cffcc8876df5e44b (" 0 ", 1000) transaction cost:196022 execution cost:172318Copy the code
testX63d7db5ce060b288ecf5390594d5969bc1a206ceeb24df31cffcc8876df5e44b (" 0 ", 10) transaction cost:25663 (cost only applies when called by a contract) execution cost:2023 (cost only applies when called by a contract)Copy the code
testX63d7db5ce060b288ecf5390594d5969bc1a206ceeb24df31cffcc8876df5e44b (" 0 ", 1000) transaction cost:191057(cost only applies when called by a contract) execution cost:167353(cost only applies when called by a contract)Copy the code

Ps: If transaction cost is subtracted from execution cost, 1, 3 will get 23,640, 2, 4 will get 23,704

And that’s pretty much how it works. When the discovery parameter is set to 1000, transaction cost also increases. (The calculation of the initial guess plus PS: Execution cost has been included in the transaction cost, and the fee to be paid to Miner at the end of the transaction cost has been calculated, because each subtraction result is similar.)

In addition, geth’s estimateGas is not accurate because some uncertain operator operations will be different. For example, in a contract, if the mantisda of blockhash is odd, it will execute the contract that will consume a large amount of gas. Otherwise, he will execute the Hello World contract, so his gas cost will be half and half in a large case.

Always set a reasonable gas limit to protect yourself from malicious attacks.

In addition, it is recommended to refer to the traceTransaction directive to see the gas cost of each Opcode. To verify how well miners process transactions, a simple experiment was performed on Ropsten Testnet. First play with a little ether on Ropsten Faucet, then send the trade on Metamask, since Ropsten is the environment that simulates POW, I believe it will be the right number.

Important words again conclusion: There is a gas used by TXN in the transaction info of Etherscan, and the result is consistent with the result given by Remix and the result given by gasUsed of getTransactionReceipt in GETH. In the future, gas cost can be directly estimated by GETH or Remix simulation.

Reference: Ethereum DApp development combat entry

You can add wechat to pull ethereum technology group chat.