This article is a part of the free crypto course and assumes that you have completed the resources that come prior to this one.
In particular, we assume you are familiar with terms like blockchain, nodes, hash, public and private keys, digital signatures, smart contracts, and mining.
It is also best if you are familiar with state machines, but it is not necessary.
What is Ethereum?
Without getting into the details right away, you can think of Ethereum as a global computer (a very simple one – no parallel processing, no decimals, etc.) that stores all its data (transaction history) on a blockchain.
A transaction on the Ethereum network can pertain to:
- Transferring Ether from one account to another
- Deploying a smart contract
- Executing (the technical term is calling) a smart contract.
Anyone can download the entire Ethereum blockchain and serially run all the transactions from the beginning to the end and arrive at the exact same state of the network (i.e. all the nodes can independently agree on which accounts have how much Ether, what smart contracts are stored in the database, and what the values for their stored variables are.)
(If this stuff isn’t making that much sense to you right now, don’t worry – you’ll understand it well by the end of the article.)
Ether is the currency of the network and is used to bid on something called gas (don’t think too much about this, it will be covered in the next piece).
The Accounts Model and The Ethereum State Machine
Note: Ethereum does not use the UTXO model that bitcoin does. It uses the much simpler “accounts and balances” model, much like what your bank uses. Do not relate Bitcoin’s UTXO with the Accounts model that Ethereum uses. They are very different.
Note: Don’t worry too much if you don’t know what a state machine is. Think of it as a way to represent the current data balances in the system (called “state” – the images in this article are visualizations of this state).
Think of Ethereum as a computer that maintains two lists.
The first list contains the Ether balances of all the accounts, and the second list contains the data of the programs (called smart contracts).
Each smart contract also has an address and a balance which appears in the first list (accounts can belong to a human user called externally owned accounts or EOA, and those that belong to a smart contract are called contract accounts).
Here is a simplified image that represents an Ethereum state:
The first list tells that there are 6 accounts so far and what their ether balances are.
Looking at the second list, we can see that accounts 3 and 5 are contract accounts (owned and operated by the code in the contract). This means that accounts 1, 2, 4, and 6 are user accounts (Externally owned accounts) that are operated using transactions generated by the user with their private keys.
(Contract accounts do not have a private key. They receive and transfer ether based on their code.)
This image represents a “state”. The state changes with every successful transaction (which could be an ether transfer from one account to another, creation of a contraction, or simply calling an existing contract).
For example, let’s say that in the next transaction, account 6 transfer 10 ether to account 1. This is what the updated state will look like:
As you can see, the balance in account 1 has increased by 10 and the balance in account 6 has decreased by 10.
Note: These images have many simplifications. In reality, the account number involves the public key for Externally Owned Accounts and the hash of the code for Contract accounts (they are not serially assigned numbers). Also, the account balance is represented in wei, which is a quintillionth of an ether.
1 ether = 1,000,000,000,000,000,000 wei (1018)
(Similar to how one bitcoin is 100,000,000 satoshis)
Ethereum Transaction Structure
An Ethereum transaction has this structure:
- From: The address of the account making the transaction goes here. (Of course, in case of an externally owned account, the entire transaction is digitally signed by the owner’s private key, so only the owner of the account can send valid transactions from that account.)
- To: This field accepts an account address. If you’re sending ether, this is the account the ether will go to. If you want to call a contract, you put the contract’s address here. If you want to create a new contract, fill zeros in this field and all the nodes will understand that this is a contract creation transaction.
- Value: The amount of Ether to be transferred.
- Data/Input: When creating a new smart contract, the code of the contract goes here. When calling a smart contract, this is where you put any input data the contract needs. (For those who know how to code: if the contract has multiple functions, you also mention which function you are calling).
- Gas Price: The maximum gas price you’re willing to pay (will be covered in the next piece).
- Gas Limit: The maximum amount of gas you’re willing to spend on this transaction (will be covered in the next piece).
(There are some other fields called v, r, s that relate to elliptical signatures, and a field called nonce, but these are out of the scope of this article and we won’t be getting into them.)
State Updates (Running The Computer)
Let’s step through some example transactions to understand how Ethereum works. You start by creating a wallet on your computer.
Transaction 1: You acquire some ether
This can be from a transfer from another account or from mining a block and earning ether as your block reward.
Either ways, the blockchain now says that your account address (represented using your public key) now has X ether balance (think of list 1 in the state images above).
If you bought the ether from an exchange, the transaction looks like this (ignore the gas price and gas limit fields for now):
From: The account address of the exchange
To: Your account address
Value: Number of ether (in wei)
(If something was filled in the data/input field, it would just be ignored as the address in the To field does not belong to a smart contract.)
Transaction 2: You create a smart contract
You want to get into a savings habit, so you decide to save some ether from your income this month.
You know that if the money is accessible to you, you will end up spending it, so you create a smart contract that will lock in your ether for 1 year (you will send your ether to the contract, and you will only be able to tell the contract to send it back after 1 year).
For those of you who can code, here is an implementation of this smart contract written in solidity. It has two variables, one to store the address of the creator of the contract (this is where ether will be returned to after 1 year), and the other to store the contract creation date and time (the block timestamp). The values of these variables will show up along with the code of the contract in the state.
This is what the transaction will look like:
From: Your account address
To: Zeroes (to signify contract creation)
Data/Input: The code of the contract
After this transaction, the state of the network will reflect a new account in the list of addresses (the first list in the image structure we used above) with 0 ether as its balance (because no ether has been sent to it yet), and that account will be listed in the list of smart contracts with its data and the values of its variables (the second list in the image structure we used above).
Transaction 3: You send Ether to the smart contract
Similar to transaction 1, you send some ether to the smart contract. The transaction would look like this:
From: Your account address
To: The address of the smart contract
Value: Number of ether (in wei) you want to send
Sending ether to a smart contract is no different from sending ether to an EOA.
The state will update and the balance in your account will reduce and the balance in the contract’s account will increase.
Transaction 4: Withdrawing the Ether from the smart contract
After 1 year, you can send a transaction calling (executing a function of) the smart contract. The smart contract will then run its code (in this case, it will check whether a year has passed, and if so, transfer its entire balance to your account).
This is what the transaction you send will look like:
From: Your account address
To: The address of the smart contract
Data/Input: The function of the smart contract you are calling [in this case withdraw()]
(For those of you who can’t code, just think of the data/input field here to be a reference to the part of the contract that checks if the year has passed and then sends ether back.)
The contract has code that allows it to construct transactions and send ether back to you, which it will do as it is coded.
After this transaction is included in the blockchain, the state will then update to reflect the new balances. The balances in your account will increase, and the balances in the contract’s account will go to zero.
The contract code/data will still exist in the blockchain even though you are done with using it. It lives in the blockchain forever (since it’s all just transaction history as far as the blockchain is concerned).
If you try to withdraw the ether before the 1 year period, your transaction will fail as the smart contract is coded to only send the ether to your account after 1 year.
This doesn’t mean the transaction won’t be recorded on the blockchain. It will be recorded on the blockchain, every node on the network will process your transaction, it will fail, and they will all mark it as failed – no state update will take place since the transaction failed.
An important thing to note is that you have to do a transaction calling the contract to make the contract execute. Contracts do not run automatically and there is no way to schedule them to run at a future date.
In this example, if you do not call the contract (even though the 1 year has passed) the ether will just stay in the contract account (until you call the contract). The contract will not automatically run itself and transfer the ether to you. Contracts only run when an EOA calls it.
The Ethereum blockchain is just a long series of transactions.
Anyone can download the entire blockchain, create a node, start with an empty state, run all the transactions sequentially and update the state after every transaction.
After they have processed all the transactions in the blockchain so far, every single node will arrive at the exact same state of the network – the same addresses, the same balances, and the same smart contracts with the same data.
The miners mine new transactions to the blockchain. All the nodes verify whether the new block is valid (every transaction on the block is valid) and process every transaction on the block and update the state along the way.
This is how the “global computer” runs on thousands of nodes worldwide, and they all are in consensus about the state of the network.
What you learnt
In this article, we covered:
- What is Ethereum
- The Accounts model that Ethereum uses (Externally owned accounts and contract accounts)
- How an Ethereum transaction is structured (include some example transactions pertaining to ether transfer, creation of a smart contract, and calling for the execution of a smart contract)
- What the Ethereum state machine looks like and how it updates with every transaction
- How all the nodes of the Ethereum network stay in consensus
Further reading (required): What is Ethereum Gas and what the cryptocurrncy ether is for?
Hope this helped. If you have questions, leave them in the comments below.
P.S. This article is a part of the free crypto course. If you found this article useful, go check the course out.