Solidity, a Smart Contract Language
This blog is in reference with the Blockchain At Berkeley course lab 01.
Ethereum Virtual Machine

Ethereum is an open programmable blockchain platform that lets anyone build and use decentralized applications that runs on blockchain technology. It is an open-source project built by many people around the world. No one controls or owns Ethereum. It is easy to create new applications(smart contracts) on the Ethereum platform, with the release of Homestead. Ethereum allows user to create their own operations of any complexity hence it serves as a platform for many decentralized applications. At the heart of ethereum is its execution environment known as Ethereum Virtual Machine which can execute code of any algorithmic complexity.
It is a VM that runs on everybody’s system on the network. Once received a transaction on the node the next step is to runs the transaction on the local copy of the VM. If all the actors in node runs the same transaction then all must arrive in the same state. If I broadcast a calculation 1+1=2 then I am not going to send the result 2 to my peers instead I shall send my transaction 1+1 to them. Since the EVM runs the same way on everybody’s machine, they runs the transaction on the EVM and all should arrive at answer 2.
Ether (ETH) is the fuel for ethereum network. Whatever computation we do on the network , we must pay for it. That payment is calculated in gas and gas is paid in ETH. It is like an internal pricing for running a transaction or contract in Ethereum.
The EVM reads byte code which are composed of granular opcodes like STOP, ADD etc. The ethereum byte code language is complete with jumps and loops which allows malicious actors to take advantage of certain security flaws that may arise. With more freedom the turning complete languages provide come more vulnerabilities and more ways to take advantage of ill thought-out language features whereas turning incomplete languages are more limited in what they can do.
Solidity Overview

Solidity is the language used to program on ethereum. It has similarities to JavaScript and C. Like objects in OOP, each contract contains state variables, functions, and common data types. Contract-specific features include modifier (guard) clauses, event notifiers for listeners, and custom global variables.
Refer the code below. It explains the basic functionalities of a bank. It allows deposits, withdrawal and balance checks.
So lets decipher the code!!
Declared the file compiler version. Contract are like class in JavaScript, then the name of the contract is defined. Then comes the mapping which maps address(here address is a data type) to unsigned integers (uint) which is declared as private and are called balance . If declared private other contracts can’t directly query balances but can be viewed by other parties on blockchain. Then we declare a state variable owner, which is public with datatype as address.
Smart contracts are running on the network, so for an external listener instead of checking for a state change after something happens, one can listen for the LogDepositMade event. If this event is fired deposit has been made. It is actually a method with no body. Its function is to take some parameters and to print those to the network so one can listen for them.
So the first function, SimpleBank is a constructor(the only method with the same name as that of the contract. It will only run once during deployment of the contract. We can never call it later or stop it from running if we define it. It defines the local variable owner as msg.sender which has the address of the sender of the transaction. A msg is a built in accessible variable that provides a lot of information like, details about the current transaction that’s being executed. So in short, since it is the first deployment it sets the sender to owner.
Further, we have a method deposit(), which takes no parameters. It is made public and returns users balance. This function maps balances and msg.sender[msg.sender is the senders address and msg.value is the value that he is depositing] and further, it looks for msg.sender in balances. If balances is not found it is set as 0 and it will add msg.value to it. Then the change in the state is broadcasted by firing the event LogDepositMade. and returns the updated balance of the sender.
There is a withdraw() function which is public that takes an unsigned integer withdrawAmount as a parameter and returns an unsigned integer remainingBalance. There is a condition check using if to check whether the person who has a balance in the contract has enough funds to retrieve. Then through mapping accesses the balance of the sender and decrement it by the withdrawAmount. In the lecture video of Blockchain at Berkeley, there is one more statement. It is actually a method call which takes the withdrawAmount as the parameter. .send returns true or false depending whether the transaction is succeed or not. If returned true it will return the updated balance of the sender, else it will first increment the balance of the sender with the withdrawAmount followed by the return statement.
The balance() function is a constant function(function that does not change the state of the contract) and returns an unsigned integer which is the current balance of the sender.
(), this function does not have a name and is referred as a fall back function. It is used to revert any side effects of the code. Like if someone calls a function that does not exist on the contract it just throw. In case, while transferring funds if somebody specify a method that does not exist, the money will be stuck in the contract without any owner. So it put the money in a back door as the owner and can pull out the money.
And its done!!