The nitty-gritty of Ethereum and Solidity : the Blockchain.

Alberto Molina
Coinmonks
10 min readJun 24, 2022

--

Blockchains seem to be unknown territory for many people although the basic concepts and mechanisms underlying most of them are exactly the same and not that complicated from an overall point of view (if you want to know the details then things can become much harder).

At its most basic a blockchain is a distributed ledger, a long list of chronologically ordered transactions that involve some kind of state change, like transferring money from one account to another etc…

I will try to explain in this blog how the ethereum blockchain works, although like I said before, most of what you will read here can also be applied to Bitcoin and other blockchains.

Blockchain Blocks

Blocks are the most basic unit of a blockchain. Indeed, a blockchain is just a long list of blocks, each block is “attached” to the previous one, forming a very long chain, justifying the term “blockchain”.

The main parts of a Block are:

  • Hash of the previous block: Each block is attached to the previous one in a very simple way. The block must contain the Hash of the previous block. Hash are obtained from functions that accept any kind of input and return a string of characters of a predefined length that will always be the same for the same input and different for a different input (1-to-1 match between input and output), even the slightest change in the input will generate a completely different output hash (no correlation whatsoever).
  • Block headers: A bunch of “meta” information about the block, like the timestamp, the difficulty, etc…
  • Transactions: The chronologically ordered list of transactions that the block is adding to the blockchain. These transactions must not be in conflict with any previous transactions (those that are included in the previous blocks).

So far so good, we can consider each block like a page in a distributed ledger, and each transaction would be like a line within the page. Transactions must not conflict with each other, if someone spends 1 dollar in one transaction he/she cannot spend the same dollar in a future transaction.

Blockchain Accounts

Accounts are long strings of characters that have an unique corresponding secret key. The secret key is used to sign transactions that are submitted to the blockchain, that way accounts can never be impersonated.

Users hold the secret key(s) of their(s) account(s) and they never share them with anyone, they can actually generate as many accounts/secret keys as they wish, it is free.

The blockchain associates funds to every account. In Ethereum those funds are represented by its native cryptocurrency “Ether”. Ether can be transferred between accounts and must be use to pay for transaction fees.

Obviously, new accounts begin with an empty balance (0 ethers) and will be able to start submitting transactions the moment another account transfers funds to them.

The very first block of the blockchain, called the Genesis block, transferred the total amount of available ether at that time to some specific accounts (those belonging to the ethereum founders mostly). From that moment on, these accounts started transferring ether to other accounts, effectively “adding” accounts to the blockchain.

Blockchain Network

Description

The blockchain network is a series of nodes that hold a copy of the blockchain and are interconnected with each other.

There are mainly two types of nodes:

  • Full nodes : Hold a full copy of the blockchain.
  • Light nodes : Only hold block headers to keep a summarized state of the blockchain. If they need more detailed information, they need to contact a full node.

These nodes are basically computers or servers with a special piece of software, called the client, running on them.

The client follows the blockchain protocol and manages the interconnection with other nodes belonging to the same blockchain (running compatible clients). Clients can be implemented in multiple languages (Go, java, …) the blockchain protocol is language agnostic, but in all cases, clients must expose an API for off-chain software/users to communicate with them (in order to read the blockchain, submit new transactions etc…). The protocol used by off-chain entities to invoke the clients API is JSON-RPC.

The main problem a blockchain faces is, how can we be sure that all nodes will have the same copy of the blockchain? in other words, if nodes have their own copy of the blockchain, and there are several people submitting transactions to different nodes at different time, how do we agree on which transactions are to be included and in which order?

In order to solve this issue, blockchains use a consensus mechanism, which is a protocol that all network nodes must follow in order to agree on which blocks and transactions are valid and which is their order.

POW Consensus mechanism

The most widely used consensus mechanism is Proof Of Work (POW).

The way it works is simple, nodes have a list of valid transactions they wish to add to the network in order to receive a fee. Each node “prepares” its block (adds the block hash of the previous block, the block headers and the transactions) then tries to find a “nonce” (an integer to make it simple) that will make the block hash correspond to a “valid” hash (valid hashes are hashes with a certain number of “0” at the beginning). By changing the Nonce, the whole block hash changes, it is impossible to predict the hash from the input unless we compute it. Nodes will have no option but to start testing one nonce after the other until the block hash is valid, this takes time and ressources (in the form of electricity). This process is called Mining.

As a result, when using POW as the consensus protocol, Blocks have a “nonce” added to their headers.

The probability of mining a block is proportional to the amount of computing power a node has compared to the total computing power of the network (all nodes together).

As a matter of fact, if you hold more than 50% of the total computing power of the network, you can pretty much do whatever you want with the blockchain, you will be able to generate blocks at a higher rate than the rest of the network which can give you the possibility to carry out what is called a “50% attack”.

POS Consensus mechanism

POW is a very expensive consensus mechanism and not environmental friendly at all. Nodes with little capacity will rarely be able to mine a block, meaning that they will rarely get any fees, plus consuming electricity just for mining is a huge waste.

The ethereum community has decided to move on from the POW consensus to the Proof of Stake “POS” consensus mechanism.

In a nutshell, POS works in the following way.

Nodes that wish to create new blocks will “stake” some funds. Staking basically means locking your funds in a save vault until you wish to withdraw them. If you have funds locked in the vault you have the right to create blocks, if you withdraw your funds you lose your rights.

Nodes with staked funds are chosen by the protocol to create new blocks. The frequency at which nodes are chosen is proportional to the amount of funds they stake (compared to the total amount of staked funds provided by all the nodes in the network). If they generate a valid block they will receive the corresponding fee, if they generate an invalid block part of their staked funds will be taken from them as a penalty. Nodes are incentivised that way to follow the rules.

Since there is no more mining, blocks generated following the POS consensus mechanism do not have a “nonce” attached to their header.

The interesting thing about POS is that it will also give the possibility to any user to take part in the block generation process. Indeed, users can take part in “staking pools”, which are vaults in which anyone can lock some funds then have a node manage that vault as its own. The node will take part in the POS protocol staking all the funds collected in the staking pool, then the fees it will receive from generating new blocks will be proportionally allocated to the staking pool contributors.

There is a minimum amount of funds that nodes must stake in order to be eligible to create new blocks : 32 ETH. This minimum amount makes staking pools much more interesting and even necessary, since many nodes will not have this amount of funds.

Types of Network

Ethereum networks can be of two types

  • Public : Absolutely anyone has the right to download an ethereum client into his computer/server and become part of the network. In the same way, absolutely anyone has the right to read from the blockchain and submit transactions to it.
  • Private : Only certain people have the right to run a client into their computer/server. Read and Write accesses are also restricted by the nodes. Normally this type of network is interesting for “consortiums” of companies that wish to exchange data with each other in a private way but do not trust each other.

The public ethereum network can be further subdivided into two types of networks

  • Mainnet : There is just one public ethereum network, the “production” network, the one in which ether has actual value.
  • Testnets : There are multiple testnets which are networks identical to the public mainnet (same protocol etc..) only that ether is completely worthless. It is used for testing purposes.

Nodes can run a client that belongs to the mainnet or a testnet, they can even run multiple clients for multiple networks. The important thing is that transactions submitted to one network cannot be submitted to any other network.

Private blockchains can also have a mainnet and testnets of course, but it will depend on the consortium of companies to implement and manage them….

Block Creation

Blocks are created by nodes that hold a list of valid transactions and group them together before attaching them to the last block of the chain and submitting it to all the other nodes.

Nodes receiving a valid block from another node will also attach it to the last block of the chain.

Soft Forks

The Problem is that, because of network related latency (nodes can be in different parts of the world), several nodes can generate a valid block at the same time and send it to their neighbors.

In that case we will have two valid blockchains (one with the blue block and another one with the red block). A node receiving both blocks, will not really know which of the two it must keep, since they are both valid, it might be that some nodes are operating the “blue” blockchains and some others are operating the “red” blockchain, this is what we call a soft fork.

As a consequence, nodes receiving both blocks will keep them for a while in memory, forming sort of two blockchain branches.

These nodes will wait until they receive further blocks, attached to one of the two branches and they will chose the longest valid chain as the valid one.

It might happen that a node receives blocks attached to one branch, assumes it is the valid one, then receives further blocks attached to the other branch, making the latter longer than the former. In that case the node will “switch” and start operating the branch that is now the longest.

The consequences of this can be potentially problematic, transactions that were at some point considered as approved (part of the blue branch) might not exist in the new branch and are then “removed” from the blockchain.

Users, in theory, can NEVER be 100% sure that their transactions will be part of the blockchain for ever, even if they see them as part of a block… Nevertheless, in practicality, if a transaction is part a block that has at least 7 blocks further up the chain, we can assume that it will never be removed from the blockchain.

Blocks are generated roughly every 15 seconds, meaning that a transaction recently added to the blockchain, will take around 2 minutes to be confirmed.

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read

--

--