2 - Ethereum Tooling
What is blockchain?
Simply put, a blockchain is a linked list of records, or blocks. The first block of the chain is called the genesis block. Each block, except for the genesis block, refers to the previous block. Each block contains multiple valid transactions, the hash of the previous block and its own hash. Blocks are validated and mined using proof of work or proof of state mechanisms. Each new block contains the link to the parent block, an address, and the information about the winning miner together with their list of transactions.
Blockchain properties
Peer-to-peer
Blockchains are governed and validated by (usually) a majority of nodes that are able to communicate with each other directly with no central authority involved in their communication. The governing nodes introduce a definition of what a valid block looks like and all nodes participating in the blockchain follow it. For a transaction to be valid, it must be included in a valid block, in practice at least in 6 or 7 blocks.
Distributed ledger
Blockchain is a type of distributed ledger. Distributed ledgers are virtually lists of transactions distributed over all nodes of the blockchain. Each block carries its own transactions and a link to its predecessor block so the whole history of transactions can be easily reconstructed. Distributed ledger is a subordinate term to distributed database, in which two nodes work together. A good example of a distributed database is Google Search, where the data is distributed over several nodes that work together to satisfy the search query. However, the information updates of Google Search are centralized. In blockchain, a majority of nodes must agree on an action.
Immutability
New blocks of a blockchain can only be appended to the end of the blockchain. The blockchain, or the individual blocks, can never be changed retroactively, because all the subsequent blocks would have to change too. You could, theoretically, rewrite the blockchain’s history if you governed at least 51% of the hashing power in the network (Bitcoin).
Ethereum
Ethereum is a public blockchain platform with its own programming language Solidity and its own cryptocurrency, called Ether (ETH). It was launched in 2015. The original Ethereum whitepaper was written by Vitaly Dmitriyevich “Vitalik” Buterin already in 2013. Unlike Bitcoin, Ethereum is not only a storage database for transactions but contains its own Turing-complete programming code as well. It has two account types – normal and smart-contract account.
Ethereum Virtual Machine (EVM)
The Ethereum Virtual Machine is the environment for transaction execution in Ethereum. Ethereum is not just a distributed ledger like other blockchains. Because it uses smart contracts, it’s a more complex system. The Ethereum website describes it as a distributed state machine and more thoroughly as follows: “[It] is a large data structure which holds not only all accounts and balances, but a machine state, which can change from block to block according to a predefined set of rules, and which can execute arbitrary machine code.” Unlike Bitcoin, which is simply a chain of blocks, Ethereum also includes program states that change. The EVM code cannot be changed, unless there is consensus of the nodes during the proposal phase. The number of tokens stored by individual accounts is contained in the account storage. The smart contracts are also included.
Mnemonic (back-up phrase)
A mnemonic phrase is a set of 12 or more memorable words generated when a cryptocurrency wallet is created. In case your wallet died, was lost or stolen, you can use the mnemonic phrase to restore it. Store your mnemonic carefully! Ethereum mnemonic uses BIP39 protocol.
Test Networks
Testnest are networks used by developers to test both protocol upgrades as well as potential smart contracts in a production-like environment before deployment to Mainnet. Most testnets use a proof-of-authority consensus mechanism. Some well known testnets are Goerli, Sepolia or Holesky. Goerli is depracated, using the old proof-of-work minig consensus. Rinkeby uses the proof-of-authority mechanism and the so-called faucets to acquire ETH. Sepolia uses proof-of-stake mechanism and is the current testnet best suitable for developing smart contracts. Holesky serves as testnet for protocol upgrades.
Side chains (Layer 2)
There exist multiple sidechains, build on the Ethereum layer (layer 1), called layer 2s. They have custom consensus algorithm and their native tokens (not ETH). Mostly they aim to solve scalability issues of Ethereum, but offering a full EVM compatibility. The most notable are Optimism, Base, Arbitrum or Polygon.
Nodes
There are three types of nodes. Full nodes contain the history of all the blocks in a blockchain, which is very demanding as regards storage and processor performance. They receive new transactions and blocks during validation and verify all blocks and states. Fast nodes save only recent history. Light nodes enable you to connect to a different node that gives you access to its history on demand. They are useful for low capacity devices. One of the most common clients for running nodes is Geth – you’ll learn about it in the next chapter.
Hands-on excercise
Tutorial objectives
We will lern how to generate wallets and how to start an own testnet on Ethereum blockchain.
Tutorial pre-requirements
- Install geth version 1.10.25 - https://geth.ethereum.org/downloads (click on SHOW OLDER RELEASES)
- Install Metamask - https://metamask.io/download.html
Caution:
Computers in the laboratory don’t allow to install programs and save their state between login sessions. Please bring your own hardware for these tutorials.
Interact with Sepolia testnet
Let’s create a crypto wallet! We will choose the most common MetaMask wallet. In this example, we’re going to use the Goerli testnet.
- Create a new wallet in Metamask
- Set up a password
- Save Secret Backup Phrase (mnemonic)
- Click on show test networks in Networks dropdown
- Switch to Sepolia Test Network
- Get Sepolia faucet
- For example here https://www.sepoliafaucet.io/
- Find your Faucet transaction on Etherscan (hint: https://sepolia.etherscan.io)
- Verify balance in your wallet
- Send your first SepoliaETH transaction
- Get an address of your colleague
- Send him some SepoliaETH
- Verify on Etherscan
Run a local (private) blockchain
Geth is a command line tool to interact with Ethereum blockchain. It has several subcommands, see yourself. We create a local EVM chain just for us.
$ geth --help
Warning:
Verify the geth version, we must use archaic 1.10.25 for proof of work support.
$ geth version
Geth command Account enables to create new accounts or display local accounts. You can also create new wallets or retrieve the folder in which your wallet is stored.
- List local accounts
$ geth account list
- Create a new account
$ geth account new
- Check the path if it contains a public and a private key of your new wallet
- List local accounts to verify
$ geth account list
Wallets from Metamask can be exported to Geth. Now, you can import your wallet to Geth using the import command and your private key.
- Import Metamask account to Geth
- Go to Metamask
- Click on the three dots in the upper right corner
- Choose account details
- Click to reveal the private key (never share your private key with anyone!)
- Save the private key to a text file file.key
$ geth account import file.key
- Check the public address
- Check the balance
- List local accounts to verify
$ geth account list
Create a Genesis block (genesis.json). You can set initial balance to pre-defined wallets (identified by a public key). Note that the public key address in your file is different from the example. It should correspond to the public key of your exported Metamask wallet.
Define
genesis.json
{ "config": { "chainId": 15, "homesteadBlock": 0, "eip150Block": 0, "eip155Block": 0, "eip158Block": 0 }, "difficulty": "0x40000", "gasLimit": "2100000", "alloc": { "public_address_new_account": { "balance": "30000000000" }, "public_address_metamask_account": { "balance": "40000000000" } } }
- Delete the
geth
folder (/Users/xxx/Library/Ethereum/geth
). - Initialise a local chain with genesis block.
$ geth init genesis.json
- Start a local chain (not discoverable, why?)
$ geth console --nodiscover
- Check existing accounts using integrated web3 library
eth.accounts;
- Check account balance
eth.getBalance(eth.accounts[0])
- Unlock your account
personal.unlockAccount(eth.accounts[0])
- Send a transaction
eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: 111, gas: 60000})
(it’s a trap, why?)
- Mine some Ether
- Set base account for mining
miner.setEtherbase(eth.accounts[0])
- Start mining
miner.start()
- Stop the miner after you commit new mining work
miner.stop()
- Check account balances
eth.getBalance(eth.accounts[0])
eth.getBalance(eth.accounts[1])
- Send (again) the transaction
eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: 111, gas: 60000})
(it’s a trap, why?) - Check account balances
eth.getBalance(eth.accounts[0])
eth.getBalance(eth.accounts[1])
- See pending transactions
eth.pendingTransactions
- Start mining
miner.start()
- Stop the miner after you commit new mining work
miner.stop()
- See pending transactions
eth.pendingTransactions
- Check account balances
eth.getBalance(eth.accounts[0])
eth.getBalance(eth.accounts[1])
- Set base account for mining
- Explore integrated web3 library (geth console)
- Try
eth.
and see the list of commands - Silence network updates
debug.verbosity(0)
- Check last block number
eth.blockNumber;
- Try
- Connect Metamask to your local chain
- Start RPC server
$geth console --nodiscover --http
- DIY
- Check the balance in Metamask
- Mine some more tokens
- Check the balance in Metamask
- Start RPC server