A Byte of Blockchain - Week 40 Exploring Ethereum... Ether & GAS

·

5 min read

A Byte of Blockchain - Week 40
Exploring Ethereum... Ether & GAS

Recap

Last week, we revisited the concepts of virtualization for EVM (Ethereum Virtual Machine), state machines & Turing completeness. We then dived into difference between Ethereum & Bitcoin blockchain. We will go back & forth between concepts to solidify our understanding.

Ethereum Blockchain

Ethereum, like any other public blockchain consists of :

  1. A peer-to-peer (P2P) network for

    a. Propagating transactions

    b. Verifying transactions & blocks

  2. Consensus protocols for agreeing on transactions & block verification.

  3. Incentive Mechanisms (Proof of work) to secure the network

  4. Cryptographic paradigms where messages are signed & verified through hashes & secured signatures.

  5. Blocks of transactions which are linked to each other & secured through cryptographic hashes.

  6. Cryptocurrency

The below diagram summarizes the above :

Ethereum8.jpg

P2P Network

The P2P network for Ethereum is called the Ethereum Main Network or Mainnet. This is more like a "live" environment or "production" environment where real-world transactions get processed.

For testing & development purposes, there is another network called Testnet. This is used only for testing before the changes are deployed into live environment (Mainnet).

Consensus Protocol

We discussed consensus protocols in Weeks 9 & 10. These are protocols which define the parameters around which transactions are verified & authorized. Ethereum currently uses the Nakamoto consensus which is used in Bitcoin. This means that the longest chain which went through maximum proof of work is considered as the valid chain & reflects the current state of the Blockchain.

Proof of Work Algorithm

The Proof of work algorithm currently used by Ethereum is called Ethash. This will eventually be dropped to adopt Proof of Stake (PoS) (which we will get into in the coming weeks). This transition from Proof of Work to Proof of Stake is what is known as THE MERGE which is expected to happen in Q3 / Q4 of 2022.

Cryptocurrency

Why does a blockchain need cryptocurrency? Let us take a step back & ask -

  1. What resources are we using when we interact or transact in a Blockchain? &

  2. What is the objective of cryptocurrency in a Blockchain?

The Bitcoin blockchain uses bitcoin (cryptocurrency) as incentive to reward miners for maintaining the network. The primary objective of Bitcoin blockchain is to be a platform for transferring Bitcoin i.e., as a payment mechanism for bitcoin.

The core purpose of Ethereum is not payments but as a general programmable world computer. Ethereum also has its own cryptocurrency called Ether. So, why do we need Ether? Let us again take a step back to single computers.

pexels-craig-dennis-205421.jpg

Computers have limited resources in terms of

a. Processing power &

b. Memory &

c. Storage Space

Blockchains are nothing but a network of computers connected through a client software. Thus, there is limitation of resources in terms of processing power, memory & storage space. To ensure only genuine transactions are processed & efficient utilization of scarce resources, we have to pay the cost of all the above resources through Ether.

Thus Ether is used as a Utility Currency to pay for using the processing power, memory & storage space of Ethereum. That means, for any transaction to be processed in Ethereum, we have to pay ether.

The miner nodes in Bitcoin blockchain maintain the block creation & security of the network through proof of work. They are incentivized for the resources used (processing power) through newly minted bitcoin (via coinbase transactions).

Ethereum is Turing complete which means it can run code & programs which utilizes it's computing resources. We can run any application in Ethereum unlike Bitcoin where the scripting language does not support complex logic & loops. The code running in Ethereum is mostly implemented through smart contracts (more on smart contracts later!!).

The contracts will have different objectives like running an escrow account, check conditions to make payments etc. These smart contracts will run instructions on a periodical basis which are incorporated into its code. These instructions consume resources & hence are subject to charge.

We referred to Denial of Service attacks & and how an infinite loop can cause such attacks. In a single computer, we can switch it off and on to reset everything. However, Ethereum is not one single computer and in case of a DoS attack, it cannot be switched on and off. The consequences of such an action would be enormous. So, it is better to discourage such actions by substantially increasing the cost of running a smart contract with infinite loops.

How do we know how much gas will be used for a transaction?

Let us consider a real life example - We all use gas to power our vehicles. Imagine a car rental where the gas is prefilled and the car provided to you. At the end of the day, when you return the vehicle back to the rental agency, you will be charged only for the gas actually used & not for the entire amount of gas filled in when you took the car.

gasoline-g4f6368ec0_1280.jpg

The same principle applies here as below :

  1. Each instruction in a smart contract or program running in Ethereum has a predetermined cost in terms of gas.

  2. When a transaction is initiated, it should include the maximum gas cost that could occur for that transaction.

  3. If the actual gas consumed exceeds this maximum, the transaction is terminated.

  4. If the actual gas cost is less than this maximum based on actual execution, the unused gas is refunded back to the initiator of the transaction.

So, if a transaction is initiated in Ethereum, we have to pay gas for that transaction. It is how resources usage is kept track of and limits the resources a program can use preventing DoS attacks.

Gas.jpg

We will keep coming back to gas in the coming weeks.