prerequisite to this article is my bitcoin article
The first conceptual leap is to stop thinking of the blockchain as just a database. Think of it as a single, shared, and incredibly trustworthy global computer.
what does a computer need to be a computer?
Variable Value and Code Storage
Compiler to run the code
in bitcoin where we were storing transactions, we can similarly store global variables and code
and miners can run the compiler on their local machine and instead of transactions store the change in global variables in the block
The programs that run on this world computer are called Smart Contracts.
there is a special programming language in which you can write smart contracts on ethereum called solidity and a special compiler called The Ethereum Virtual Machine (EVM)
let’s go through a very simple smart contract in which anyone can pay money but only the owner can take out money on reaching 100 ethereum.

If you look carefully in the above code, the deposit function is payable, that means that msg.value has to be eth.
There is one issue, what if somebody runs a infinity while loop on a smart contract? it would jam the network as the validator ( miners) node would keep running this code and get stuck wasting a lot of resources.
Ethereum's solution is both elegant and economic. It doesn't try to predict if a program will finish; instead, it makes the user pay for every single step of the computation.
The central idea is that every single operation a smart contract performs has a cost. Nothing is free. Just like a car needs fuel to drive, a transaction needs "fuel" to execute on the EVM.
This fuel is called Gas.
How You Pay for Execution
So, if Gas is just a unit of work, how do you actually pay for it? You pay in Ethereum's native currency, Ether (ETH).
Gas Limit
Purpose: This is a safety mechanism to protect you. If you interact with a buggy or malicious contract that goes into an infinite loop, the execution will run until it consumes your entire Gas Limit and then stop. It prevents the transaction from draining your entire wallet.
If your transaction completes successfully using less Gas than your limit, the unused Gas is refunded to you.
If your transaction runs out of Gas before it finishes, it is reverted. All changes are undone, but you do not get your Gas fee back. You paid the miners for the work they did up to that point.
Gas Price (The Price per Liter)
At any given moment, there is a market price for Gas. This price is determined by the supply and demand for block space on the network.
Supply: The number of transactions that can fit in a single block is finite.
Demand: The number of users trying to get their transactions included in the next block.
When the network is busy (high demand), the price of Gas goes up. When it's quiet, the price goes down. The Gas Price is typically measured in a tiny denomination of ETH called Gwei (1 Gwei = 0.000000001 ETH).
Putting It All Together: The Total Transaction Fee
The maximum fee you might pay for a transaction is calculated as: Total Fee = Gas Limit * Gas Price
Example:
You set a Gas Limit of 21,000 (the standard for a simple ETH transfer).
The current Gas Price is 20 Gwei.
Maximum Fee = 21,000 * 20 Gwei = 420,000 Gwei = 0.00042 ETH.
Miners (or Validators in Proof-of-Stake) are economically rational. When they build a new block, they will prioritize transactions from users who are willing to pay a higher Gas Price, because that means more profit for them.
A Transaction's Journey
Let's see how Gas works in the context of our PiggyBank contract from the last chapter.
You want to deposit 1 ETH into the PiggyBank. You use your wallet to call the deposit() function.
Your wallet prepares the transaction:
To: PiggyBank_Contract_Address
Value: 1 ETH (the amount you want to deposit)
Data: A small piece of data identifying the deposit() function.
Gas Limit: Your wallet estimates the Gas needed. A simple deposit might need 45,000 Gas. It sets this as the limit.
Gas Price: Your wallet checks the network and sees the current market price is, say, 15 Gwei. It sets this price.
You sign and broadcast the transaction.
A Validator receives your transaction. They see you are willing to pay 15 Gwei per unit of Gas. They include your transaction in the candidate block they are building.
Execution inside the EVM:
The EVM starts running the deposit() function.
It performs the first operation: this.current_balance = this.current_balance + msg.value.
It deducts the Gas cost for this operation (e.g., 25,000 Gas) from your "tank" of 45,000 Gas.
The function finishes successfully. The total Gas used was, say, 30,000.
Final Calculation:
Gas Used: 30,000
Gas Price: 15 Gwei
Actual Fee Paid: 30,000 * 15 Gwei = 450,000 Gwei = 0.00045 ETH. This amount is paid to the validator.
Your Refund: Gas Limit (45,000) - Gas Used (30,000) = 15,000 Gas worth of ETH is refunded to you.
Total Debited from your account: 1 ETH (the deposit) + 0.00045 ETH (the transaction fee).
one thing worth knowing is what exactly is stored inside the block of ethereum. in blockchain we know its transactions.
// Ethereum transaction structure
STRUCT Transaction:
gasPrice
gasLimit
toAddress
value // Amount of Ether to send (in Wei)
data : TxDATA | ContractCreationData
signature
STRUCT TxData:
functionSelector // First 4 bytes: Keccak256 hash of function signature
functionArguments // Encoded arguments for the function call (ABI encoded)
// Example: When deploying a contract
STRUCT ContractCreationData:
contractBytecode // Compiled EVM bytecode of the contract
constructorArgs // ABI encoded constructor arguments
so while in blockchain you specify that “send 10 btc to alice”, in ethereum you say “run this X function of this Y contract and here take the gas fee and function arguments from me”
so now you must be thinking that okay if in blocks the world state (value of variables is not stores and who owns how much eth) is not stored than where is it stored.
world state is stored in the hard drive of miners in a database (rocksdb, leveldb etc)
whenever a miner has to run your function or smart contract, it pulls the state from the hard disk, see the initial values of world state variables and then run your smart contract functions and changes the value of the state varibales and stores the whole changed state again in the hard drive.
similarly the other noder who have to validate the block, runs the same process and store the changed state in their hard disk.
but how do you verify if all the miners have the same state stored in the database?
Ethereum has an elegant solution for this, the miner who first runs your smart contract, after calculating the state it passes it through SHA-256 and calculates a state root, which is stored in the block headers and transmitted the block on the etherium network.
now when the other miners run the evm and change the state, the just confirm that the state root matches. they do not need to match the whole 500gb state with each other.
Network Requests Inside Smart Contracts
in any real world program you often make a lot of external api requests to get data for example weather api, openai api, dollar price api etc
so in smart contracts also we need to make these external requests, but there is a fundamental issue with making external requests on ethereum, see all the functions on ethereum are pure functions, which means for the same input you will allways give you the same output.
now imagine a senerio where the block proposer (the miner who first runs your smart contracts and transmit the proposed block to ethereum network) runs your smart contract, which has a funtion call to external api asking for dollar ruppee price and he gets 80 and this 80 becomes input to another function in the same contract, so he get the final state as X.
now when the validators run the the same contract, by then the dollar price has changed so even though following everything correctly the output will be differnet and the block will be rejected
The solution to this are Oracles and chain links.
firstly I want you to remember that EVM allows one smart contract to read the data from any other smart contract if allowed by the other smart contract
so the solution is to store the dollar price, in a special smart chainlink contract which will update its price when special off-node oracle contract run its update function.
off-node contracts mean the contracts which are not running on the ethereum blockchain. similar to mobile wallets in bitcoin which are not running on blockchain but can run functions of smart contracts.
but how does the chainlink contract know that the ones changing data are oracles only? by publlic and private keys.
let’s look at some sudo-code
The Chainlink Aggregator Contract

The Off-Chain Chainlink Network
(This is NOT a smart contract. This is a regular program running on the servers of the oracle node operators.)

you might be thinking okay we have our chainlink smart contract which has the dollar value stored but that might also be changed by the oracle nodes right?
No, changing the value inside the chainlink smart contract requires running a transaction which has to be included in the processed block of ethereum.
let’s take an example
suppose the chain is at block #499 and at this time a smart contract contacts the chainlink contract function to get dollar price and just after that the oracles also run the update function of chainlink contract.
now all the transactions will be run by the block proposer miner and the updated price of the dollar will have no impact to the smart contract because miner will see its value from its previous stored world state.
so for that particular block no matter which miner runs the code or if the price of dollar changes in chainlink contract, all the miners will get a constant price.
we have covered almost everything of how ethereum really works, just one thing is left that is Proof-of-Stake.
Proof-of-Stake is a replacement of Proof-of-Work algorithm which consumes a lot of energy. in proof-of-stake every miner is required to submit a colletrel of eth before they can start mining, if any miner found behaving maliciously, his collateral is stashed and not returned.
one more thing is unlike bitcoin ethereum is not limited, its production is controlled algorithmically by supply and demand.
that’s it for ethereum article , stay tunned for dApps and L2 Networks Article by suscribing