1

Are the above steps correct for deploying a smart contract to the blockchain:

  1. A node creates a smart contract
  2. The smart contract is converted to byte code
  3. The node creates a block out of the byte code
  4. The node adds it to his local blockchain
  5. The node broadcasts the block to other validators/miners
  6. The miners verify the block and add it to their local blockchain After some time the longest chain rule is executed and the longest chain is written to all miner nodes (consensus)

The main questions I have is, are smart contracts stored inside blocks in a blockchain and do smart contract require verification by all nodes in a blockchain network?

Your help and support is much appreciated Thanks

1

Nodes in blockchain do not understand what smart contract is. Rather smart contract is a piece of code that can be written in Solidity or Serpent(any language which supports generating bytecode understand by EVM (Ethereum Virtual Machine))

You have to write a smart contract which can have some functions maintaining its own state variables. These smart contracts will have to be compiled resulting bytecodes. Then to deploy that smart contract, this bytecode will be sent to the chain as a part of transaction. This will create an instance of the contract in the chain and an account address like other user accounts. To deploy a smart contract is a transaction and it creates storage for the contract variables modifying the state of the blockchain, consequentially. To change/modify any state value of that smart contract, you need to make transaction (this will contain some code to execute) in the blockchain. So, making transaction in ethereum blockchain means either you are transferring ether (which is modifying account balance information in the blockchain) or deploying a smart contract or invoking a function of the deployed smart contract to change the state variables of the contract.

So steps to deploy smart contract:

  1. Write a smart contract
  2. Compile the smart contract
  3. Generate byte code through compiling
  4. Make a transaction to deploy the smart contract and send the transaction to any node in the network
  5. The transaction will be mined and added in a block by any node and will be received by other nodes
  6. Get contract address from that transaction's receipt
  7. Make transactions further to that contract address to invoke methods of that deployed smart contract

In ethereum, account balance is maintained by State trie of the blockchain. So when, you are deploying a smart contract, the bytecode passed as part of the transaction will be saved in the state trie as a part of the account created for the deployed contract. In fact, inside blocks, you will find the transactions, not the contracts. When, you will again make any transaction to invoke any function of the contract or just call any method of that contract to retrieve value of the variables, the code to execute in the EVM will be found from that state trie.

All nodes only understand that it is a transaction. It can contain some code (bytecode of the contract) to execute in the evm. It just executes them and changes the state in the blockchain. It has no idea of it. Either the transaction will be executed successfully or it will fail which will be reflected in the status field of the transaction receipt. So, nodes only execute those byte codes. If any logic written in the smart contract reverts the transaction, it will just revert the state. All nodes will just verify the state of the blockchain to be consistent after executing the transaction.

If your smart contract has syntax errors, it will not generate bytecode in the compiler. If your bytecode contains wrong opcodes unknown to the EVM, the transaction will get failed.

I don't know which type of verification of smart contract you are talking about.

Hope my explanation will help you to understand.

  • thank you for the reply. In the 5th step, you mentioned that the transaction will be mined and added to a block by any node and received by any node. Say the consensus algorithm is PoW, then the node that solves the hash puzzle first creates the block and adds the smart contract (byte code) transaction to a block and adds it to his local blockchain. Then it broadcasts it to other nodes. Other nodes look at the block containing the smart contract transaction byte code and verify that PoW is correct and adds it to their local blockchain. Is this correct? – Deepal Sep 13 at 13:34
  • Yes, it is correct. But one thing to be clear, in the verification, it also itself executes all the transactions of the block and matches whether its resulting state matches with the received block along with PoW puzzle match. So, here PoW puzzle solve check and block validation, both are done in part of receiving ends according to my knowledge. – Md Rayhanul Masud Saom Sep 13 at 13:49
  • Is there a formal paper or white paper for this? If each node executes the transactions, does it mean that each node executes the smart contract functions and sees the state variables to verify the block containing the smart contract byte code transaction? – Deepal Sep 13 at 14:10
  • Sure, of course. Please go through the github link of Ethereum White Paper and more information you will get. In the blockchain and mining section, go to step 6. There has been mentioned that the txs of the received block will be applied in the local state and the final state is verified. Follow this link github.com/ethereum/wiki/wiki/White-Paper#blockchain-and-mining . – Md Rayhanul Masud Saom Sep 13 at 14:43
  • I will break my comment into two as there is a word limit. So am I correct to say that the smart contract is compiled to byte code and exists as a transaction inside a block and that block is validated by all nodes by calculating the final state and comparing it with the final state in the header, amongst other things(other validation methods)? – Deepal Sep 13 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.