• Market Cap
    $393.227B -0.73%
  • POW Market Cap
    $302.920B -1.11%
  • POS Market Cap
    $19.553B 1.02%
  • Masternodes Market Cap
    $1.203B 0.33%

Features and Principles of the Solidity Programming Language

By Inkarias - 2019-11-09

Solidity is the programming language of the most popular and most widely used decentralized application platform: Ethereum. Like Bitcoin's scripting language, which allows conditional financial transactions to be programmed with its opcode set, Solidity can encode smart contracts, a set of instructions that allow the network to update the status of the blockchain according to the conditions and variables defined by their creator. However, Solidity goes further than the language used on Bitcoin and allows for more complex transactions because it presents a major difference.

With Solidity, developers can write applications that implement the self-application business logic outlined in smart contracts, leaving a non-repudiable and authoritative record of transactions. Writing smart contracts in a language that is specific to them such as Solidity - is relatively simple for anyone with some skills in object programming.

Solidity Origins

The Solidity project was initially proposed at Ethereum in August 2014 by a certain Gavin Wood, co-founder and ex-CTO of Ethereum. Once the project was validated, the language was then developed by the Solidity team of the Ethereum project, led by Christian Reitwiessner and including developers Alex Beregszaszi and Yoichi Hirai. Solidity is one of four languages designed to target the Ethereum virtual machine (EVM for Ethereum Virtual Machine), the other three being Snake, LLL and Mutan (already deprecated). These languages have the common goal of writing smart contracts on, mainly but not only, the Ethereum blockchain. Solidity is currently the main language on Ethereum as well as on other private blockchains competing with Ethereum, such as Monax. The Swift Global Interbank Network has deployed a proof of concept running on an implementation of Tendermint by Monax Industries Eris using Solidity.

Solidity: a Turing-complete language

By its nature, the Bitcoin's scripting language is not "Turing-complete", and this is precisely what prompted Vitalik Buterin to create a blockchain based on a different system.

What does the term "Turing-complete" mean?

As we previously introduced in our smart contract article, it refers to the mathematician and cryptologist Alan Turing, a precursor of computer science, who described a model to define the concepts of algorithm, programming and decidability: the famous Turing machine . It's primarily a concept and not a physical object, even though Alan Turing gave his entire life to this ideal calculator concept, creating the "bomb", a kind of computer before the hour, that was imagined to be able to decipher the messages of the Germans during the Second World War.

A language is said to be "Turing-complete" if it can make it possible to simulate a Turing machine, with the same expressive power and the same computability. In particular, a Turing-complete language must make it possible to execute recursive or "computable" functions, necessary to set up conditional loops (if, then, else) and more generally so that the language is deterministic (the same functions applied with the same arguments will always give the same result).

This Turing machine has several points in common with Ethereum:

  • The ribbon (infinite in the abstract model) has been replaced by lines of codes, the basic instructions of smart contracts;
  • The read / write head by the validating nodes and the material of the miners, which update the status of the contracts within the blockchain;
  • The state register by the Ethereum blockchain;
  • Finally, the action table by the compiler that translates the instructions of the contracts into binary code so that they are executed simply.

How Solidity works

Solidity is an object-oriented programming language that has similarities to JavaScript or C++. It is a high-level language, which will be compiled in low-level language (bytecode) to be interpreted by the Ethereum runtime environment.

This is the Ethereum Virtual Machine or EVM: this runtime environment is isolated from the network and allows to interpret the code Solidity once compiled, to ensure the update of the contract status (and blockchain), via all validating nodes of the network. It is the miners who execute these instructions and update the Ethereum blockchain and are financially rewarded for it. Each instruction of a Solidity contract will therefore have a cost of execution (in gas). In order to avoid infinite loops within a program, there is always a limit to the gas that can be consumed.

Like classes in object-oriented programming languages, Solidity contracts are defined by state variables, functions, modifiers, events, and types. It is possible to call other contracts within a contract (message calls), inject external data into a contract, and there are also special contracts, libraries and interfaces.

The EVM has three separate areas for processing data:

  • The storage area: associated with an Ethereum account, it is persistent and intervenes between the call of a function and a transaction. It is therefore the most expensive in gas.
  • The memory zone: associated with each contract and updated during each call (call message), its size varies proportionally to the amount of data managed by the smart contract. It also has a cost in gas.
  • The stack of the virtual machine: it contains the instructions of the contract, which will be executed in a descending way - the operations are done by taking the element located at the top of the pile, by sending the result in the pile and by passing to the next element.

The instruction panel of the EVM is minimal for reasons of compatibility and security, but it is obviously possible to do all the usual arithmetic and logical operations, conditional jumps, etc... It is also possible to access data at the block level, such as its version number or timestamp.

The components of a smart contract

Concretely, we can visualize a contract as a set of lines of code (its functions), and a set of data (which define the state of the contract on the Ethereum blockchain) stored on an Ethereum account (generally that of its owner or creator). On Ethereum, we are talking more about account and not address, because there are two types of accounts: external accounts, controlled by pairs of public / private keys (typical addresses controlled by humans), and dedicated accounts contracts (controlled by the associated code).

More precisely, a contract is defined by different elements:

  • State variables: they can be of different types and their value is permanently stored in the contract.
  • The functions: at the heart of the contract code, they will be executed according to the arguments and the parameters injected and will return the desired variables. They can be called internally or externally, and their degree of visibility can be defined.
  • Modifiers: these tools make it possible to modify the functions of a contract in a declarative way, which is practical to avoid repetitions in the code and to gain readability.
  • Events: they make it possible to interact with the logging system of the actions performed by the contract (the "logs" generated by the EVM).
  • Types: are used to define what a variable represents. With Solidity, one can create structs ("custom" types that can group several variables) and make mapping (index variables according to a key system -> value). There are notable differences with other object-oriented languages, especially in the context of mapping.

The concept of inheritance is also important to understand Solidity: a contract can inherit the properties of one or more other contracts. With inheritance, if we use external smart contracts, the code of these will be compiled within the contract created and then deployed on the blockchain.

The interfaces are like records specifying the structure of the functions of a contract (name, inputs / outputs) without describing the code. They are useful for interacting with multiple external contracts and creating complex contracts, such as using libraries. Libraries are similar to contracts that are deployed indefinitely on the blockchain, whose code is reused by multiple contracts using it. Libraries are therefore isolated code, which will be executed specifically for the smart contract integrating them.

There are many Solidity integrations and development interfaces. Remix is the most used IDE (Integrated Drive Electronics), Atom is a well-liked community text editor, Metamask the popular thin / wallet client. Many free tools and plugins are available to develop on Ethereum. A good way to understand Ethereum's smart contracts and get an overview of Solidity is to read its official documentation and take a look at Etherscan at a few simple contracts.

There are several testnets on Ethereum: errors are allowed and gas costs nothing. However, once deployed on the blockchain Ethereum, a contract will work as long as it will have enough gas to feed it! Errors can be very costly, which was the case in the famous TheDAO case or the Parity smart contract case.

Smart Contract Example

pragma solidity >=0.4.0 <0.7.0;

contract SimpleStorage {

uint storedData;

function set(uint x) public {

storedData = x;

}

function get() public view returns (uint) {

return storedData;
}
}

This simple contract allows the storage of a number within the latter and has two functions. The first will make the change of the stored data, replacing it with the new data set in parameter. The second simply returns the stored data when called. When at the very first line, it simply indicates the method of compiling the contract.

In this example, any compiler with a version greater than 0.4.0 and less than 0.7.0 is usable. As I told you, if you have already programmed, especially on object languages, these few lines should not be very complex to read. Solidity langage allows to develop some basic instructions like on the example above or even more complex systems as we may have seen in some famous Ethereum Dapps.

How to start programming in Solidity

Solidity is currently the main language for coding and deploying smart contracts. Many blockchain platforms use it, like Qtum, Ubiq, or even Rootstock. If you're interested in the technical side of cryptocurrencies and decentralized applications, understanding and using Solidity is a must!

If you are looking to go deeper with Solidity, some resources are directly available on the internet and have been written by experts in this field. The various guides offered on these external platforms will allow you to have a complete understanding of the language in a simple and fun way: