Blockchain, Ethereum - Getting started

Photo by Annie Spratt on Unsplash

Blockchain, Ethereum - Getting started

Sedhu's photo
Sedhu
·Feb 27, 2022·

20 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

This is the first article in my series about blockchains.

Blockchain and its Basics

Blockchain, is a combination of a decentralized and distributed database containing a registry of transactions that are distributed among peers or fellow participants in the network. The registry includes a long list of transactions and is continually updated with new transactions as they take place. Starting from the very first transaction, a bunch of transactions are grouped into a block as per a predefined block size (1 MB in case of Bitcoin). Once the block size is achieved by one block, the next set of transactions forms another block which is then linked to the block previously formed. Over time, a series of blocks is formed where each block is connected to another block that was created just before it. Thus, we call this chain of blocks the blockchain.

In the existing world, newspapers can be a loose example of where every piece of news is a transaction, Every day its new block and printing press is a block producer and every reader is a node.

To create a basic blockchain, following existing technologies and techniques were used.

  1. Digital Signatures
  2. Hashing
  3. Merkle Trees
  4. Hash Cash
  5. TCP/IP
  6. P2P Network

Cryptocurrency is a reward for node operators to run a blockchain node to cover their operating costs. Without this public chain can't exist.

Digital Signatures

A digital signature acts as a verifier of the authenticity of the sender.

There are two types of digital signatures:

Symmetric digital signatures:

  • One single key is used to encrypt the messages.
  • The sender encrypts the message with that key and sends it to the receiver.
  • Once the receiver receives it, they need the same key to decrypt or unlock that message.
  • So, the sender also shares the key he/she encrypted the message with so that the receiver can use it to decrypt the message.

Asymmetric Digital Signatures:

  • A pair of public and private keys are used.
  • A message encrypted with a public key can only be decrypted with the corresponding
  • Private key of the public-private key pair and vice versa.
  • The public key of each participant is shared across the network and the private key is held secret by the owner/sender.

Additionally, in the case of normal web traffic, combination of both Asymmetric and Symmetric has been used to share the information between the browser and the server. During the initial step, Asymmetric Digital Signatures are used to form a secure connection and then A Symmetric Key will be shared between both parties. This will help to reduce the overhead in subsequent traffic.

Hashing

It is an encryption technique which is used to encrypt the data to ensure data security, and it's done using hash functions. The hash function is a mathematical function that can take in any length of input and convert it to an output of fixed length. The output hash serves like a fingerprint for that data.

Hash uniquely represents that data. Even a small change in the data generates an entirely random and different hash.

Hashing & its uses

This technique is used to pack transactions into block. So, if a bad actor tries to tamper with the existing transaction, it will result in hash output mismatch.

Merkle Tree

Hash_Tree.png

Merkel Tree is a way of organizing transactions in the block. The output of a Merkle tree is a Merkle root. The Merkle tree utilizes hash functions to arrive at the Merkle root by recursive hashing.

Even the smallest of changes in any of the data points will change its hash and hence, the root hash will also be changed. Thus, root hash is the fingerprint representing all the data points or block.

The Merkle trees help in verifying transaction data on a block by acting as a unique identifier for the transactions.

Hash Cash

Hash cash is a proof of work the sender of an email must do before sending an email. The sender must calculate the hash of the email data and make sure that the calculated hash satisfies a predefined condition. This limits the scope of spam emails.

The technique of hash cash is used in the process of creation of blocks in the blockchain. So that miner nodes are spending their resources (Computational power, memory, electricity etc.) to form a block.

TCP/IP

The TCP/IP is a communication protocol which sends data in the form of packets over a server using the IP addresses of the sender and receiver.

Peer to Peer Network

In peer-to-peer (P2P) networking, a group of computers are linked together with equal permissions and responsibilities for processing data. Unlike traditional client-server networking, no devices in a P2P network are designated solely to serve or to receive data.

p2p-Copy.webp

In a peer-to-peer network, each device can act as a server and as a client at different points in time. In a blockchain peer-to-peer network, the peers use TCP/IP protocols to connect to other peers and transfer the data.

Basic Architecture

All the above-mentioned techniques to create the ideal business network, i.e., blockchain. The architecture of the blockchain network is as follows:

  • There is a peer-to-peer network created between all the participants of the network.
  • Each network participant has a digital signature for identifying the participants Hash Cash TCP/IP and Peer to Peer Network Basic Architecture of Blockchain
  • Any transaction happening between two network participants gets flooded in the network with the help of gossip protocol to all the participants for validation.
  • Each participant has a transaction pool which is a memory space allocated to store the verified transactions.
  • Each transaction is secured using the hashing algorithm.
  • The transaction pool is at a node level.
  • There are designated nodes which create the blocks out of the transactions happening in the network at a frequency known as mining rate.
  • Once the block is created it gets flooded in the network and each network participant verifies the block of data and once there is a single source of truth for the block, the block gets added to the blockchain.

Consensus

Since public blockchain is an open source network, there should be a way for participants to mutually agree on the transactions and blocks in blockchain. So there is a need for a consensus layer.

Mathematical algorithms are used to verify transactions and ensure trust between transacting parties. Transacting parties have to trust the output achieved using these mathematical algorithms.

These algorithms together are known as the consensus mechanism in the blockchain. The consensus can for a transaction or for an entire block

There are many types of consensus algorithms; proof of work, proof of stake, practical BFT etc. Every blockchain network has formed its own consensus protocol based on their network needs.

Data Immutability

Blockchain offers one more important feature that differentiates it from other networks: Data Immutability.

In practical terms, it refers to the extreme difficulty that one will face in trying to alter or make changes to the existing data.

Blockchain offers data immutability as follows:

  • Blockchain forms a chain of blocks which are connected to each other via a link, also known as the previous hash pointer or simply the previous block hash.
  • The hash of a block is calculated for all the contents in the block header using a hashing algorithm.
  • Merkel root is a part of the block header and any change in any constituent transaction results in the change in the Merkel root.
  • Change in the Merkel root will result in changing the hash of the entire block.
  • If the hash of one block is changed, the block next to it will not have a link to this block as the previous hash will not match the new block.
  • Hence, the link between the blocks will be broken, and these blocks will become invalidated.

Immutability is an important feature of the blockchain which helps protect data from being manipulated and also helps in identifying malicious nodes in the network.

Block header hash = SHA256 (previous block hash + Merkle root + timestamp + difficulty target + nonce) .

immutablity.png

*Challenges in protecting a blockchain network

51% attack: In case more than 51% of the nodes in the network are malicious the network could become unstable.*

BFT Network

For a network to be byzantine fault tolerant the number of malicious nodes in the network should be less than 1/3rd of the total nodes in the network. Whenever a node receives two conflicting messages, it goes for a majority vote and accepts the message which comes from majority number of nodes. To ensure that the correct message is accepted by the nodes in the network the total number of malicious nodes needs to less than 33.33% or 1/3rd of the network

(3F+1) - Where is f is no of faulty nodes that can be present in the network

Types of Blockchain Networks

  1. Public - Bitcoin, Ethereum
    • Permissionless: A public permissionless blockchain is free for anyone to join or leave. This type of network provides anonymity, immutability and transparency but compromises on efficiency.Public
      • Permissioned: A public permissioned blockchain is an intermediate between private and public networks. It values efficiency and immutability over transparency and anonymity, where every participating member is aware of the identities of the other members in the network.
  2. Private / Enterprise - Hyperledger Fabric, Hyperledger Besu
    • A private blockchain is one which is operated and managed by a single entity. These type of blockchains are generally applicable in the case of a conglomerate where the parent company runs the network for the underlying group of companies. In such a situation, they value efficiency over anonymity, transparency and immutability.
  3. Consortium
    • A consortium blockchain is largely similar to a private blockchain but differs when you consider who controls or manages the network. Instead of concentrating all power in one entity, authority is distributed across two or more participants.
      1. Hybrid (Public + Private)
    • It is used by some private solution providers where they roll up the transactions on their private chain and submit them to a public blockchain for integrity and data immutability.

Bitcoin

It's a first generation Blockchain with a vision of open money.

Ethereum

Initially, the blockchain was used only for the transfer of values between multiple parties. Later, the blockchain community saw an opportunity to make advancements and that gave birth to smart contracts.

Smart Contracts

A smart contract is the digital version of an agreement contract that runs on the Blockchain nodes and is executed when a set of trigger criteria are met. If designed right, a smart contract will surely be successful as it is driven by a computer system without human intervention.

The Smart contract has evolved through the following phases:

  • State Machine: The very first version of the blockchain was a state machine. It was just like an accounting ledger, where it held only the state of the node.
  • Smart Contracts: All the nodes within a computer network have a code associated with these nodes, which can run on its own based on predefined logic known as smart contracts.
  • Oracles: Oracles are external agents which connect the smart contract to the external world. Any external data that is required for the smart contract will be fetched by agents called oracles.

Introduction

Ethereum is a decentralised, open-source, distributed computing platform that enables the creation of smart contracts and decentralised applications, also known as Dapps.

It is essentially a transaction-based state machine. State machine refers to something that would read from a series of inputs and then transition to a new state based on those inputs.

The Ethereum blockchain starts from the ‘genesis state’, which is the first state. And after every transaction, the state of the entire blockchain changes.

ethereum.org/en/whitepaper

image.png

Basics

Ether

  • Ether is the main currency on the Ethereum blockchain.
  • It is a form of payment made by the client of the platform to the machine that executes its requested operation.
  • Every time a block is validated, 5 Ethers are created and awarded to the successful node.
  • 1 Ether = 1018 wei (Smallest Unit)
  • 1 Gwei = 109 wei

Gas

  • Gas is a unit to measure the fee required for a computation.
  • It refers to the fee required to successfully perform a transaction or execute a contract on the Ethereum blockchain
  • The exact price of gas is determined by the network’s miners, who can decline from processing a transaction if the gas price does not meet their threshold.

Gas Price

  • Gas price is the amount of Ether paid per unit of Gas spent by a miner for running a transaction

Gas Limit

Gas limit is the maximum amount of Gas required to run a transaction.

Transaction limit is the maximum amount that the sender would need to spend in order to perform a transaction successfully.

Transaction Limit = Gas Limit X Gas Price

Gas price and Gas limit are set by the sender of a transaction.

The system would deduct this amount from the sender based on the amount of Gas consumed in running the transaction.

Accounts

In Ethereum, the state is made up of objects called "accounts", with each account having a 20-byte address and state transitions being direct transfers of value and information between accounts. An Ethereum account contains four fields:

  • The nonce, a counter used to make sure each transaction can only be processed once
  • The account's current ether balance
  • The account's contract code, if present
  • The account's storage (empty by default)

Accounts in Ethereum are stored in a global ‘shared state’ in the form of key–value pairs.

The list of all these key–value pairs representing accounts defines the state of Ethereum at that point.

Ethereum has two types of accounts:

  1. Externally owned accounts (EOAs)
  2. Contract accounts (CAs).
Externally Owned Accounts (EOAs):

These are combinations of public addresses and private keys, and there is no code associated with them.

  • Send and receive Ether to/from another account,
  • Send transactions to smart contracts.
Contract Accounts (CAs):

These accounts do not have a corresponding private key. These accounts are generated when you deploy your contract on blockchain. You will see them referred to as just contracts

  • Send and receive Ether just like EOAs.
  • Unlike EOAs, they have code associated with them.
  • Transactions have to be triggered by an EOA or another contract.

In a key–value pair, the key is a 20-byte string, which is usually the public address of the account.

Nonce: For EOAs, nonce is the number of transactions that are sent from an account’s address. For CAs, nonce is the number of contracts created by the account.

Balance: It is calculated as Number of wei/Ether owned by an account.

Storage Root (Storage Hash): This is a hash of the root node of a Merkle tree that encodes the hash of the storage content of an account.

Code Hash: This is the hash of the EVM (Ethereum Virtual Machine) code of this account

State Root

Whenever a new block is created, a state root is stored in the header of that block. State root is the Merkle root of all the accounts at that moment. Simply put, all the key–value pairs that represent an account together, form a Merkle tree is known as state root.

This state root is captured by a block at the time of its creation. Any change in the data would lead to the calculation of the Merkle tree all over again to match the state root in the block.

This is highly impossible, and hence the immutability is maintained in the network. Along with state root, the transaction root and the receipt root are also used to capture the state of the network in every block. By calculating and storing the roots mentioned above, Ethereum captures the network state every time a new block is created (state root, transaction root and receipt root).

Transactions

Interaction between accounts are called transactions. Its a signed data package that stores a message that is to be sent from an EOA to another account on the blockchain.

The following points summaries the transactions in Ethereum:

  • An EOA can send messages to other EOAs or to other CAs by creating and signing a transaction using its private key.
  • A message between two EOAs is simply a value transfer.
  • But a message from an EOA to a CA activates the CA’s code, allowing it to perform various actions (e.g., transfer tokens, write to internal storage, mint new tokens, perform some calculation, create new contracts, etc.).
  • Unlike EOAs, CAs cannot initiate new transactions on their own.
  • Instead, CAs can only fire transactions in response to other transactions that they have received (from an EOA or from another CA).
  • An action that occurs on the Ethereum blockchain is always set in motion by transactions that are fired from EOAs.

Note: Transactions can be triggered from an EOA only. Components of a Transaction Nonce: A count of the number of transactions sent by the sender

Gas Price: The amount of Wei that the sender is willing to pay per unit of Gas required to execute the transaction

Gas Limit: The maximum amount of gas that the sender is willing to pay to execute a transaction

to: The address of the recipient. In a contract-creating transaction, an empty value is used

value: The amount of Wei to be transferred from the sender to the recipient. In a contract-creating transaction, this value serves as the starting balance within the newly created contract account

v, r, s: Used to generate the signature that identifies the sender of the transaction

init: An EVM code fragment that is used to initialise the new contract account

data: The input data (i.e., parameters) of the message call. Each type of transaction has all the components above Types of Transaction

Message Calls: These are internal transactions between an EOA and a CA or between one CA and another. When one CA sends a message to another CA, the associated code that exists on the recipient CA is executed.

Value Transfer: These are transfer of value from one EOA to another.

Contract Creation Calls: They are initiated by EOAs, and the recipient’s address is kept empty. Such transactions create a new CA and, hence, are used to create and install new Ethereum contracts

Block

Blocks are batches of transactions with a hash of a previous block in the chain

A block consists of

  • Header
  • List of Transactions
  • Headers Hash of Ommer Block
  • Mining details

image.png

Consensus in Ethereum

Ethereum is currently operating on a Proof-of-Work (PoW) consensus protocol, but in the future, it will be shifting to a Proof-of-Stake (PoS) protocol.

The current Ethereum blockchain uses a consensus algorithm called Ethash, which is built specifically for the Ethereum blockchain.

The Ethash PoW algorithm introduces the property of ‘memory hardness’ to the Ethereum blockchain.

Ethereum made its mining process highly I/O intensive. This is done with the help of a DAG (directed acyclic graph) – a very large file that is passed along with a block as an input to the hashing algorithm, Ethash.

The DAG requires sufficient memory size, and so, the entire hashing process is a CPU-based rather than a GPU-based process.

Therefore, any system with memory large enough to hold a DAG can now stand a chance to mine the block successfully. And this is why Ethereum has a 15-second block creation rate, as compared with 10 minutes for Bitcoin.

Forks Forks can be classified as accidental or intentional.

Soft Fork

An accidental fork is created when two or more miners find a block at nearly the same time.

The fork is resolved when subsequent block(s) are added and one of the chains becomes longer than the alternative chain(s).

The network abandons the blocks that are not in the longest chain (they are called orphaned blocks).

Ommer Block Ommer/uncle blocks are created in Ethereum blockchains when two blocks are mined and submitted to the ledger at roughly the same time. Only one can enter the ledger as a block, while the other does not. They are similar to Bitcoin orphans but have an integrated use, unlike their Bitcoin counterparts. The orphan block is called the Ommer block

Hard Fork

A hard fork is a permanent split of a blockchain, and it is backward-incompatible.

It divides a blockchain into two separate chains, both of which are dominant.

A hard fork is generally a change in protocol that renders the older version invalid.

Ex: Ethereum and Ethereum Classic

Transaction Flow

Logs and Receipts

Ethereum maintains logs to track various transactions and messages.

A contract can explicitly generate a log by defining events.

A log entry contains:

1.The account address,

  1. Events
  2. Data.

A log stored in the header comes from the log information present in the transaction receipt.

Ethereum generates a receipt for every transaction.

These receipts include:

  1. Block number,
  2. Block hash,
  3. Transaction hash,
  4. Gas used,
  5. Cumulative gas used in the block
  6. Logs of current transaction

State Transition

Ethereum is a State Transition Machine, which is a process that takes the system from an existing state (N1) to a new state (N2) after a transaction is given as an input to the network.

Ethereum keeps moving between these states as new transactions keep occurring on the network.

image.png

Whenever a transaction is triggered by an EOA, that transaction has to go through certain checks and a specific process as follows

  1. valid signature
  2. valid transaction / sender account nonce
  3. gas limit
  4. sender account balance

Once all parameters are valid, the transaction gets executed as follows

  • Upfront cost of execution is deducted from the sender’s balance
  • Nonce of the sender’s account is increased by 1 to account for the current transaction
  • When the transaction starts executing. Throughout the execution, Ethereum keeps track of the ‘substate’, which is a way to record information accrued during the transaction that would be needed immediately after the transaction is complete
  • A self-destruct set, which is a set of accounts (if any) that would be discarded after the transaction completes
  • Log series, which are archived and indexable checkpoints of the virtual machine’s code execution
  • Refund balance, which is the amount to be refunded to the sender’s account after the transaction
  • The various computations required by the transaction are processed.
  • Once all the steps required by the transaction have been processed, and assuming there is no invalid state, the state is finalised by determining the amount of unused gas to be refunded to the sender

Tokens

Ethereum tokens are simply digital assets with standards that are being built on top of the Ethereum blockchain. They benefit from Ethereum’s existing infrastructure instead of developers having to build an entirely new blockchain.

Ethereum is a platform that can be used to create any arbitrary smart contract including smart contracts that represent digital assets called Ethereum tokens.

These token standards are called ERC standards.

ERC stands for Ethereum Request for Comments. These are application-level comments or standards defined for the token. Anyone can create an ERC. However, the ERC’s author must include a clear explanation of its standard.

Applications can use this common ERC token and make it easier to interact with each other. The conditions of the standard can include anything; for example, the way it will interact with a smart contract.

Some of the common ERC standards are ERC 20, ERC 721 and ERC 777 etc. Fungible and Non-Fungible Tokens Fungible tokens

  • These are tokens with identical properties.
  • One token can be interchanged with another token of the same value.
  • Tokens are divisible into smaller amounts as long as they add up to the same
  • Some of the examples of fungible token standards are ERC 20, ERC 777 and ERC 223.

ERC - 20

  • ERC 20 is an asset created on Ethereum and is written in Solidity.
  • It is hosted on the Ethereum blockchain.
  • ERC 20 tokens are stored and sent using Ethereum addresses and transactions.
  • This standard has six functions – totalSupply, balanceOf, transfer, transferFrom, approve and allowance.
  • These functions can be used to transfer tokens to an address or a smart contract.

ERC 777

  • It is similar to ERC 20 with extra functionalities like mint and burn.

Non-Fungible Tokens

  • These are tokens with unique properties. Even if two tokens are of the same type, they are unique
  • No two tokens can be interchanged as they all have unique specifications.
  • As these tokens can represent an identity, they are not divisible into smaller units.

ERC - 721

  • Every non-fungible token (like ERC 721) has a metadata associated with it which defines the token.
  • This metadata is unique for each token. For example, suppose you have a land registry document set. Each document represents one token.
  • Creating a unique set of tokens is also known as tokenizing assets on the blockchain network where each document can represent an asset with certain properties

Challenges in public ethereum

  • Scalability/throughput: The number of transactions per second is very low
  • Cost: The mining process is expensive and transaction gas fees is also very high
  • Size: The size of the network is increasing, and nodes have to be highly equipped in order to store a blockchain of this size

Merkle Patricia Trie

Ethereum stores data in both Merkle trees and a radix tree called Patricia trie.

  • Ethereum combines the properties of a Merkle tree and a Patricia trie to create a modified Merkle Patricia trie.
  • A Merkle tree can be used only to check whether a value is present in the tree or not.
  • A Patricia trie (a radix tree) is a data structure that stores values in key–value pairs. This is optimum in finding common prefixes of the searched value.
  • These combined properties of both the trees yields a structure that is more optimum for storing and checking values than Merkle tree individual. Ethereum uses a database to store its trie structure. Some examples of such databases could include LevelDb and RocksDB. The purpose of storing this data is to go forward and backward, and identify the exact data that was tampered with.

In the next article, I will write about Applications of Ethereum.

Did you find this article valuable?

Support Sedhu by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this