How to Build Ethereum from Scratch — Ethereum Tutorial

David Katz
6 min readAug 31, 2019

--

What if there was a recipe to build Ethereum?

What if you wanted to build your own Ethereum-like platform? Or perhaps a very similar one with modifications.

Well, look no further. This article is a recipe — a recipe to build Ethereum.

Before going into the recipe, it’s fruitful to start with a definition of Ethereum. This can actually boil down to two words. Ethereum is a:

decentralized computer.

A decentralized computer is essentially like a normal computer. A normal computer executes code, uses memory, has internal storage, etc. And a decentralized computer does these things too; but, in a massively distributed way.

With a normal computer, one machine is executing code.

But in a decentralized computer, you have multiple machines executing code. For a program to run on a decentralized computer, all machines in the network must execute that program’s code.

The benefit is that the decentralized computer is trustless. You don’t have to put your faith in a bank, and their one centralized computer system. Who knows if their programs that manage your finance numbers have flaws. But in a decentralized computer network, every machine validates the cleanliness of a program, executes it, and needs to agree upon the results. Thus, your trust is not in one entity;. Rather, it’s in the mutual enforcement of valid code by every single machine in the decentralized network.

With the definition of Ethereum established, here’s the recipe to build Ethereum — a recipe to build a decentralized computer.

1) Define a smart contract language.

A decentralized computer’s main purpose is to execute code. That code needs to have a language. The language itself will consist of a set of instructions including ADD, STORE, STOP, and more. With the language, a coder should have the ability to build programs that consist of everything from loops to conditionality.

A program defined in the decentralized computer language is called a smart contract. More on that later.

2) Build a blockchain.

A blockchain is a public ledger. It consists of a series of blocks. Each block is a storage unit of information.

In a decentralized computer, the blocks store transactions. The purpose of a transaction will be explained more deeply soon.

For now, the important part to focus on is that the blockchain enables decentralization. Every node in the network of the decentralized computer has access to the blockchain. They receive its full history, and any blocks that get added as time goes on.

Because every node has access to the blockchain, they also have access to all transactions in history, and all transactions in the future.

3) Create the network

A decentralized computer consists of multiple machines. Therefore, there has to be some way for those machines to communicate with each other. It needs a network.

The network implementation can be publish/subscribe, peer-to-peer server communication, or something else. As long as there’s a way for each node to share important information like an updated blockchain.

4) Support accounts and transactions

Here’s where Ethereum as a cryptocurrency comes in.

Every node in the network has an individual account. Each account consists of few fields. These include an address, a balance (in ether tokens, the Ethereum unit of currency), account storage, and more.

Then the system needs to support transactions. Transactions in ethereum exchange currency between two accounts. But that’s actually not their full purpose.

Truly, transactions in Ethereum describe changes in state. As in, they outline how the decentralized computer should transition from one state to another.

For example, say account Foo is initializing a transaction to send account Bar 10 ether. Well, the transaction would describe this change in state: decrease the balance of account Foo by 10. And increase the balance of account Bar by 10.

Another example of a transaction is the creation of an account. A transaction like this would describe a change in state like so: record a new account with some randomly generated address, zero balance, and empty storage.

This leads to how smart contracts work. Smart contracts are also accounts. They have an address, balance, and individual storage too. But what makes them different is that they also have code — built with the smart contract language.

Therefore, the interaction of smart contracts happens all through transactions. To register a smart contract in the decentralized computer, whip up a transaction that creates the smart contract account. To execute the smart contract, use its address within a transaction. After all, the smart contract has an address like any other account.

5) Craft the state manager

Each node needs to be equipped with a state management data structure (i.e. state manager). The state manager records the results of transactions.

Recall that every block consists of transactions. Well, when a node receives that new block, they will run each of its transactions. The results of those transactions get stored in the state manager.

The state manager has logic that understands the difference between transaction types. Those types being the “create account” transaction, or “standard” one that outlines a currency exchange. Every account is maintained in the state manager, along with its proper balance, storage, etc.

This is also why Ethereum can be described as a decentralized state machine. Every node is executing transactions that transition it from its current state to the next one.

This unified execution of every single transaction, in every single block, by every single machine in the network manifests the state of the entire decentralized computer’s world. Or, as it’s referred to within Ethereum documentation: world state.

With a smart contract language, blockchain, network, accounts and transactions, and a state manager, you have the all of the ingredients for a decentralized computer.

These are the essential elements. But in practice, a lot more comes into play.

For example, the system needs validations to ensure that each block and transaction is formatted properly. Or it can optimize the state manager by using tries as the underlying data structure. Going on, there is also the concept of gas to charge ether for every instruction executed within a smart contract.

These details can be further explored once you start building your own project based on the recipe. As you research Ethereum’s implementation further, you’ll find even more advanced concepts like patricia tries, the ethash mining algorithm, the logs bloom, etc. Honestly, the complexity can be overwhelming.

So it’s easier to start by focusing on the basic recipe. Add the extra ingredients that you find once you’ve completed the five elements above. And before you know it, you’ll have your own Ethereum-like platform.

If you’re interested in building a project based on this recipe, I’ve published a course which helps you do exactly that. The course is divided into sections based on each of the elements outlined above. And by the end, you’ll have your own decentralized computer system that can execute smart contracts, track account balances for a cryptocurrency, and more.

Check out the link below (applied with a special Medium discount):

Hope to see you in the course!

Author, David Katz, is a software engineer in San Francisco. He is also a course creator, with 18 courses published so far. Subjects include JavaScript, React, building a blockchain from scratch, and more. He is passionate about building high quality learning experiences that are both informative and fun.

--

--

David Katz

David Katz is a software engineer and course creator, with 16 courses published so far. Check them out at https://davidtkatz.com/