Docs » Introduction to Smart Contracts
Q. What is the difference between a transaction and a call?
A call is a local invocation of a contract function that does not broadcast or publish anything on the blockchain.
It is a read-only operation and will not consume any Ether. It simulates what would happen in a transaction, but discards all the state changes when it is done.
It is synchronous and the return value of the contract function is returned immediately.
A transaction is broadcasted to the network, processed by miners, and if valid, is published on the blockchain.
It is a write-operation that will affect other accounts, update the state of the blockchain, and consume Ether (unless a miner accepts it with a gas price of zero).
It is asynchronous, because it is possible that no miners will include the transaction in a block (for example, the gas price for the transaction may be too low).
Since it is asynchronous, the immediate return value of a transaction is always the transaction’s hash. To get the “return value” of a transaction to a function, Events need to be used
Since a sendTransaction costs Ether, it is a good practice to “test the waters” by issuing a call first, before the sendTransaction. This is a free way to debug and estimate if there will be any problems with the sendTransaction, for example if an Out of Gas exception will be encountered.
This “dry-run” usually works well, but in some cases be aware that call is an estimate, for example a contract function that returns the previous blockhash, will return different results based on when the call was performed, and when the transaction is actually mined.
Finally, note that even though a call does not consume any Ether, sometimes it may be necessary to specify the actual gas amount for the call: the default gas for call in clients such as Geth, may still be insufficient and can still lead to Out of Gas.
- 닉 자보(Nick Szabo)가 1994년 최초 제안한 개념
- 디지털 명령어로 계약을 작성하면
조건에 따라 계약 내용을 자동으로 실행할 수 있다 주장