preparatory work
1, three accounts, eth.account[0] for the default account, the mining rewards will be entered into this account
> eth.getbalance (eth.accounts[0])
736031150000000000000
> Eth.getbalance (eth.accounts[1))
500050000000000000
> Eth.getbalance (eth.accounts[2])
500050000000000000
Gas required by the general Exchange
> Eth.estimategas ({from:eth.accounts[1], to:eth.accounts[2], value:50000000000000})
21001
> Eth.gasprice
20000000000
As shown above, the transaction that shows this account[1] => account[2] requires 21001 gas (Estimategas is used to calculate the gas required for the transaction), and the current Gasprice is 20000000000 (Gasprice gets the value), Here to verify
Unlock the account 1, send the deal, and open the ore-digging package
> eth.sendtransaction ({from:eth.accounts[1], to:eth.accounts[2], value:50000000000000})
I0318 00:24:21.360815 internal/ethapi/api.go:1143] Tx ( 0X33B58084A35E99245B9C931204A0D161B9D00F9FAE5FFB307AFF29F200E5CD30) to: 0x49fbd70ca9f90972806c375a111d08950d203f96
" 0x33b58084a35e99245b9c931204a0d161b9d00f9fae5ffb307aff29f200e5cd30 "
After the transaction is packaged
> eth.getbalance (eth.accounts[1])
499580000000000000
> Eth.getbalance (eth.accounts[2))
500100000000000000
by cost = gas * Gasprice, (account 1 reduced assets-account 2 increased assets)/Gasprice = consumption of gas, that is, the following formula should be established
(500050000000000000-499580000000000000)-(500100000000000000-500050000000000000) = 21001 * 20000000000 = 420020000000 000
However, careful students should find that this formula does not succeed, 21001 this number how to see how awkward, if minus 1 this formula succeeded ... Go on
View the details of this transaction
> Eth.gettransactionreceipt ("0x33b58084a35e99245b9c931204a0d161b9d00f9fae5ffb307aff29f200e5cd30") {blockHash: "0x8e411163367bc42a70ecc230d05dd2038afe0dccfab29c8a718a57bdbea0b2fa", blocknumber:134, Contractaddress:null, Cumu lativegasused:21000, from: "0x27c649b7c4f66cfaedb99d6b38527db4deda6f41", gasused:21000, logs: [], Logsbloom: "0x 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000 ", Root:" 0x2008f134f3328e48d4d05919666a5924767b00b286cf1ff27b7956654d5b6482 ", To:" 0x49fbd70ca9f90972806c375a111d08950d203f96 ", Transactionhash:" 0x33b58084a35e99245b9c931204a0d161b9d00f9fae5ffb307aff29f200e5cd30 ", transactionindex:0}
gasused:21000 This is right, then why Eth.estimategas () The result of the calculation to 1 more. There is a reason for this, if the calculated value is equal to the gasused, then the transaction may be a failure, but if gasused is less than the calculated value, then the transaction can be judged to be successful. The total transaction fee will be 0.00042 ether. the gas required to invoke the contract method
The gas required for a regular transfer exchange is fixed at 21000, but the gas required to invoke the contract method is not necessarily, and the more resources (computational, memory, etc.) are used to summarize, the more gas is required. Prepare a simple contract first.
pragma solidity ^0.4.8;
Contract Test {
uint public num;
function Setnum (uint newnum) {
num = newnum;
}
}
Deploy to a private chain, this process is no longer demonstrated, the final contract instance testinstance. Look at the asset information for Eth.accounts[1] and eth.accounts[2, and then calculate
> eth.getbalance (eth.accounts[1])
499580000000000000
> Eth.getbalance (eth.accounts[2))
500100000000000000
Calculate the calling contract method Setnum () required gas
> TestInstance.setNum.estimateGas (4, {from:eth.accounts[1]})
41645
Start calling
> TestInstance.setNum.sendTransaction (4, {from:eth.accounts[1]})
I0318 07:21:31.344279 internal/ethapi/ API.GO:1143] Tx (0x3fad05f17f7904e08dcb9257ad28f85f29bd54c4729784fa39a9df88e3fcffab) to: 0x03a4fb357f8c38694ab536d09003076033442f9e
" 0x3fad05f17f7904e08dcb9257ad28f85f29bd54c4729784fa39a9df88e3fcffab "
Open the mine and let the deal be packaged, and then check to see if the gasused matches the numbers calculated above.
> Eth.gettransactionreceipt (' 0x3fad05f17f7904e08dcb9257ad28f85f29bd54c4729784fa39a9df88e3fcffab ') {blockHash: "0x494f5f6fc0c156f105ffe3e4e1aa886c60f916a5998d44a03916b3f2cc733b8a", blocknumber:139, Contractaddress:null, Cumu lativegasused:41644, from: "0x27c649b7c4f66cfaedb99d6b38527db4deda6f41", gasused:41644, logs: [], Logsbloom: "0x 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000 ", Root:" 0x857063e074cc3195ee2f3962438f3f6c31a759cfae461448e8726a5fa069d1ae ", To:" 0x03a4fb357f8c38694ab536d09003076033442f9E ", Transactionhash:" 0x3fad05f17f7904e08dcb9257ad28f85f29bd54c4729784fa39a9df88e3fcffab ", transactionindex:0}
You can see that the gasused:41644 is 1 less than the calculated figure, which has already been explained, and is not discussed here.