...by Daniel Szego
quote
"Simplicity is the ultimate sophistication."
Leonardo da Vinci

Sunday, August 12, 2018

How to implement a Blockchain from scratch - smart contract simplified


In a simple account/ balance/ state based blokchain system implementing smart contracts is pretty straightforward. Accounts represent for the first run not necessarily just balance but a kind of a general data as well that can be modified by the smart contracts. In order to create smart contracts, you should define the language or smart contract programming environment itself and the effect that a smart contract can result in the state. Certainly one way of doing it is to define a virtual machine which guarantees that the smart contract is executed exactly the same way on every peer. However we might as well consider an exiting virtual machine as well, like the java virtual machine and limit somehow the effects of the program. As an example a simple smart contract could: 
- read some of the state information of the blockchain manifested by accounts data and balances. This state information is the previous block on which we want to mine our contract. 
- having some computation on top.
- changing the data value of a certain account. 
- storing the smart contract code somehow as data or string, like with the help of serialization
- creating a special transaction containing the smart contract as data with the sign of the account that you want to modify, indicated indirectly the owner of the account allows the data to be changed. 

At mining process:
- The signature of the smart contract transaction has to be checked. 
- It has to be made sure that only the effected accounts are modified.
- The code has to be run and the new data value must be calculated. 
- It has to be made sure that the smart contract does not cause infinite loop, one way of doing it is to avoid general loops, or to terminate the contract after a certain number of iteration resulting the transaction as invalid. Certainly another way can be built in as well, like with the help of a cryptoeconomical mechanism longer smart contract runtime can disincentivized, just like as with Ethereum.
-  The new value or values have to be applied.
- The block must contain the valid transaction and the new valid state, which is the new value of the computed accounts. 

At validation process, practically the same steps have to be repeated, without the last one, which is putting the transaction and state to a new block and doing proof of work calculation or voting of a byzantian consensus mechanism:
- The signature of the smart contract transaction has to be checked. 
- It has to be made sure that only the effected accounts are modified.
- The code has to be run and the new data value must be calculated.
- The calculation must have finite time. 
- It has to be checked if the new values of the state of the given are the calculated values based on the values of the previous block.
  
The wallet functionality has to be extended:
- to have the possibility to write or integrate smart contracts.
- to transform the programs into data, like with the help of serialization.
- to create transactions based on the smart contract.
- to sign them.
- to broadcast them on the network.