What Are Basics of Ethereum Programming?
Different Programming Languages
Ethereum Smart Contracts run on compiled bytecode that means there may be several high-level languages which code can be written in. Ethereum has a number of languages available in particular :
1. Solidity – It is the most popular language now (2020).
2. Vyper – This is a Language by Vitalik Buterin with an emphasis on security
3. LLL – “Low Level Lisp-like Language”
4. Mutan – Golang-like, deprecated in 2015
5. Serpend, Python-like, but seems to be no longer maintained
What exactly is a “smart contract” technically?
A smart contract is a state-machine like execution of bytecode. It runs on the Ethereum blockchain and gets executed by all nodes in the network. Normally, it is written in high-level languages which compile the code down to assembly and bytecode.
EVM Assembly and Opcodes
The Ethereum virtual machine understands several opcodes in EVM assembly, which are represented by bytecodes. The bytecodes are instructions for the EVM to read, write, jump, store etc. It may read and write from or to a stack or to storage. One good list of available opcodes is listed in this following stackexchange thread: https://ethereum.stackexchange.com/questions/119/what-opcodes-are-available-for-the-ethereum-evm40T And can also be found in the Ethereum Yellow paper https://ethereum.github.io/yellowpaper/paper.pdf
Gas and Gas Requirements
Every operation on the Ethereum blockchain is performed by following the bytecode or opcodes. Each opcode costs a certain amount of gas. The different gas requirements may be found on page 26 in the Ethereum Yellow paper (screenshots below): https://ethereum.github.io/yellowpaper/paper.pdf
The maximum amount of gas available has an upper ceiling of currently around 6 million additionally. The execution is limited by the maximum amount of gas a user can provide and a miner would accept in other words.This definitely leads to multiple problems software developers face.
1.We might have only a few users during development of the smart contract. Then in production, when things start to “scale” the contract suddenly runs into out-of-gas exceptions.
2. Loops are the biggest trap. We should never use loops if we don’t know how much gas it consumes at all times.
Gas-Costs Economics on the Blockchain
The Gas is calculated by how complex the transaction is to be executed. And the reason why we pay in “gas” rather than “Ether” is that in this way the actual $ costs may stay constant even though Ether might get much more valuable. For example: A transaction which interacts with a contract needs 4 million gas to be executed. We offer 2 GWei per gas, which is 2*10^-9 Ether * 4 * 10^6 => 2*4*10^-3 = 0.008 Ether, or at the time of writing, this would cost us around USD5. During network congestion the gas price can rise. The miners always take decision that which transactions to include in the next block and there is not unlimited amount in one block. Basically the reward the miner gets when transactions compete by gas and gas price. If we decide, for some reason, to offer 50 Gwei for the same transaction to speed it up, we’d pay: 50 * 10^-9 Ether * 4 * 10^6 = 4*50*10^-3 = 0.2 Ether, or USD140. A big difference, as you can see.
Data Storage in a Merkle Patricia Tree
Ethereum has its own, efficient, system to store information. This system has to be cryptographically secure. All key, value bindings in Ethereum are stored in a Merkle Patricia Tree that is held in a State Tree, a Storage Tree, a Transaction Tree and a Receipts Tree.
Privacy on the Ethereum Blockchain
The biggest issue on the Ethereum blockchain is the privacy issue. There are zk-SNARKs now, but one of the biggest misconceptions is that private variables in Solidity are “private”. All data, in general, is public. In a programmatic way, defining a variable “private” just means that it can’t be accessed from other smart contracts . The programmer may access all private variables information within 10 minutes, give or take. An additional layer has to be created that encrypts information to ensure privacy on the blockchain . This is still a matter of active research.