1
votes

I am currently learning solidity for writing smart contracts. I understand smart contracts are stored on the Ethereum block chain and broadcast and exectued by all the nodes on the network. google: A smart contract refers to a piece of computer program that gets executed by a quorum of blockchain nodes independently in order to record the latest program state.

This is where I am having a bit of problem understanding the execution cycle. Let's say we are doing something that happens only once, exchanging an ERC20 token for ETH for example. Take Node A and B. Node A exectues the transfer and does the transfer, say for example. When Node B executes the code, there will be a check on the balance in the smart contract or something, but does it even get to this point. Is the actual smart contract really executed more than once or is it done once and the other executions are somehow different?

confused since we are writing a contract which does an action one time but gets executed multiple times , thanks for links to other readings.

2

2 Answers

1
votes

Node A exectues the transfer and does the transfer, say for example. When Node B executes the code, there will be a check on the balance in the smart contract or something, but does it even get to this point. Is the actual smart contract really executed more than once or is it done once and the other executions are somehow different?

Currently all Ethereum nodes in the world execute all transactions in a blockchain. The node that is a mining node produces a block that contains the state transition from state 1 -> state 2. This is so called "transaction is included in a block". Then all other nodes that download the block will check that all transactions in this node were correctly executed.

0
votes

To get a better grasp of how it all comes together, I recommend checking out http://ethviewer.live/. When you write your smart contract, you need to deploy it to the Ethereum blockchain first. This deploy costs gas, and is transmitted to the chain as a transaction just as it would for a typical token transfer. You can observer contract creations / contract-calls being pooled into the transaction queue before they are pushed into blocks.

If Node A is only deploying the contract once (to be executed say, thousands of times by other people), Node A ONLY pays the gas cost of deploying that contract to the blockchain. Whoever wishes to interact with Node A's contract at a future time (say, Node B), would have to pay the associated gas cost in order to execute those contract calls.