'Must all nodes on the blockchain execute every smart contract function cal?
I understand why it's important that all nodes on the Ethereum mainnet must execute any smart contract function call which changes the internal state of the contract or the chain. (For example, transfers from one account to another ec.)
What I'm wondering is, if its true that every node must execute every function called on any smart contract, even if the function doesn't result in a state change.
For example, if an ERC721 smart contract has a function "getName()" which just returns the name of the artwork the NFT represents which is stored in the NFt. Let's say joe connects to the network, and wants executes getName() on a contract. Does that mean that all 9,000 nodes end up spinning cycles executing getName(), even though Joe only needs it to be executed once? Does the gas cost of running "getName()" compensate each of the nodes for the overhead of running "getName()"? If that is true (that every node gets paid) will gas get even more expensive as more nodes join the pool?
Is one of the reasons gas prices are high is because of the inefficiency of every node having to execute every function called on a smart contract, even those that have no effect on state?
If so it would seem to be a very (and perhaps unnecessarily) expensive proposition to execute a computationally intensive but "pure" (no side effects) function on Ethereum, right?
Thanks. apologies for the possibly naive question!
Solution 1:[1]
There's a difference between a transaction (can make state changes - but doesn't need to), and a call (read-only, cannot make state changes).
I'll start with the call simply because it's easier.
When a node performs a call, it executes the contract function that most likely reads from storage, stores to memory, and returns from memory.
Example:
string name;
function getName() external view returns (string memory) {
// reads from storage, stores to memory, returns from memory
return name;
}
But it doesn't broadcast any info (such as "I've just performed a read operation") to other nodes. I.e. other nodes don't perform the same operation.
A transaction is executed once on each node.
string name;
function setName(string memory _name) external {
// reads from memory, stores to storage
name = _name;
}
A miner takes a transaction from the mempool (list of transactions waiting to be mined), executes it on their machine, performs the state changes, and add the transaction to the block (including the expected state changes).
All (validator) nodes then download the new block, execute the transactions, and perform the same state changes on their end. If they find a mismatch betweeen their own state changes and the state changes declared by the miner, they broadcast this into the network, declaring a possible invalid block.
To answer your questions:
Does that mean that all 9,000 nodes end up spinning cycles executing getName(), even though Joe only needs it to be executed once?
If Joe requests a call, getName()
is executed only on one node (where Joe is connected to). If he requests a transaction, it's executed on all nodes.
Does the gas cost of running "getName()" compensate each of the nodes for the overhead of running "getName()"?
It only compesates the (winning) miner, who takes the transactions fees. Larger tx fees (the total amount of ETH, not the gas price) compensate for bigger use of resources (mostly CPU time) that the machine needs to use, compared to multiple "easier" transactions that they could execute using the same resources.
If that is true (that every node gets paid) will gas get even more expensive as more nodes join the pool?
Not every node gets paid, see above.
Is one of the reasons gas prices are high is because of the inefficiency of every node having to execute every function called on a smart contract, even those that have no effect on state?
It's probably mostly for economic reasons - supply and demand. The more people want to execute their transaction as fast as possible and are willing to pay higher gas price, the higher they are going to push the gas price. Possibly they just use the price recommended by their wallet app, which can be higher than the average.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Petr Hejda |