Lab 1 - Setting up Ethereum
Eager to start out? First we'll do a quick intro to what Ethereum is and how you can get up to speed by creating our own account and transfer tokens to other users.
Last updated
Eager to start out? First we'll do a quick intro to what Ethereum is and how you can get up to speed by creating our own account and transfer tokens to other users.
Last updated
On the official Ethereum, the description reads as follows:
Ethereum is a decentralized platform that runs smart contracts: applications that run exactly as programmed without any possibility of downtime, censorship, fraud or third-party interference.
Decentralized in this case means that there are no single entity that controls the platform. If you did any course in distributed computing you probably learned about techniques like distributed hash tables, database sharding, MapReduce and network file systems. There is an important distinction to make between decentralized and distributed. In this completely decentralized version there are no control organs, instead everything is done in consensus, kind of like a democracy. If at least half of the nodes in the system are honest we can be sure that the system will behave well.
A smart contract is simply a program that runs in a decentralized environment. We pay for execution in terms of gas. Every operation has a certain gas-cost and we can choose to pay a certain amount of Ether for every unit of gas.
First we need to install Ethereum on your local machine. The easiest way is to download the latest version of Geth from the official website, geth.ethereum.org/downloads.
You can also clone the repo from GitHub and build it from the source. To build it you need a C-compiler and a GO-compiler. You most certainly already have a C-compiler on your system but you might need to install one for GO. The easiest way to do so on a Mac using Homebrew. Homebrew is an awesome package manager for Mac that you can install by running the following command:
Then install the Go-compiler by running:
Once it finished installing just clone down the repo and run build it:
If you prefer running it inside a docker container there are instructions for that as well on the Go Ethereum website.
Once you successfully installed geth you should be able to run
And see an output similar to this, don't worry if your version number differs:
When using the real Ethereum network, everything is being done with real Ether as the currency. We need to pay for every operation in terms of gas as explained in the introduction. This means that it's very costly and inefficient to use the Ethereum mainnet when developing and testing your blockchain application. Also, when we deployed a smart contract to the Ethereum blockchain it will stay there forever, we can't simply change the source code of a contract. To deal with this issues there are something called testnets in the Ethereum ecosystem. A testnet is just like the real Ethereum mainnet, except that the Ethers here are useless. Using a testnet is a great way to familiarize yourself with the technology without using any real money. In some sense it can be compared to joining a trading competition where you buy fake stocks but on real markets.
There are three main testnets, namely Ropsten, Kovan and Rinkeby. Without going into too much detail about them you can say that they all have their own pros and cons. In this tutorial we're using Ropsten, the main reason for this is that Ropsten is the network that resembles the main network the most. Ropsten is not only a cape in the eastern part of Stockholm but also a widely used testnet. You can get free Ether on Ropsten using the faucet at faucet.ropsten.be and you can also try out mining your own Ether on Ropsten. Apart from these widely used testnets, there are also some deprecated ones that are no longer in active use, for example Morden. You can also quite easily set up your own testnet running on your local machine. We'll do this later when we start developing the DAPP
So, first things first, let's connect to the Ropsten network with geth. In the terminal, run the commands:
The first command, removedb
is just for removing any old databases, if it's the first time you sync to the network you can skip this. In the second command we are syncing to the Ropsten testnet. The syncing will take a while and you will see an output for every new block that is imported.
We pass in a three flags to tell geth, the first one, --testnet is used to connect to the Ropsten testnet (instead of the mainnet). --syncmode=fast means that we just want to do the validation for the last blocks, not the whole chain of block. What this does means in practice is that we validate all transactions in last 64 blocks instead of the whole chain. If we want to validate the whole chain we can instead pass in --syncmode=full and then we'll validate every transaction all the way back to the genesis block. Since we're only operating on the Ropsten testnet and just using it for testing purposes we choose to trust that the preceding blocks are valid.
The last flag, --bootnodes is for telling geth which nodes we want to connect to. If you're reading this from the future (welcome!) they might have changed. In that case, just head over to the official Ethereum repo at GitHub to get the new ones.
To see the syncing process, open up a new terminal and attach to the geth console using geth --testnet attach
, then run eth.status
to see your current block and the highest block. The syncing will probably take a few hours so I recommend letting it run over night. Also, make sure that you have enough space on your laptop to store the blockchain since it will be a couple of gigabytes (a bit over 30 GB by the time of writing).
If you quit the syncing and want to resume you can simply run the last command once again:
This will resume the syncing process, picking up from your last downloaded block.
While waiting for the blockchain to sync we will create our own ethereum account, also using geth
. To create the account, open up a new terminal and run:
You will now get back your account address as a 40 character hexadecimal number. This is your public address that you can give out to anyone. To get some test ether to play with we will use the Ropsten Ethereum Faucet at faucet.ropsten.be. Enter your address that you just received in the terminal and add 0x before. The 0x means that we are entering the address in hexadecimal format. After waiting a few seconds you should see a confirmation with the transaction hash
If you click on the transaction hash you'll be taken to the transaction view at Etherscan and it will probably look something like this:
After a minute or so you should see the one Ether you just requested from the faucet appear on your account. You can check the balance of your account using geth by using its interactive JavaScript REPL. Attach to it by running geth --testnet attach
, then get your account balance by running eth.getBalance(eth.accounts[0])
:
Now send 0.01 ether to my account on Ropsten by using the function sendTransaction in the eth-instance:
The function call should be pretty much self-explanatory. You're sending from your account which you get from calling eth.accounts[0]
to my account. All transactions are done in the smallest unit, wei, so we are using a helper function from the web3 library to convert 0.01 wei to ether. Depending on your setup you will probably need to unlock your account using the passphrase you chose when first creating it.
That's it, now you know how to sync to the Ropsten testnet using geth, giving yourself some test-Ether to play with and transfer Ether from one account to another.
Now that we are somewhat familiar with geth and we will try using another tool called MetaMask. MetaMask runs as a browser extension in Chrome, Firefox, Opera and Brave. I recommend using Chrome so if you don't already have the Chrome browser installed you can download it from google.com/chrome. You also need to install the MetaMask plugin by going to metamask.io, go to "Get Chrome Extension" which will take you the the Chrome Web Store page for MetaMask. Then click the button "Add to Chrome" and then "Add Extension".
After installing MetaMask you will be taken to the initial setup where you create a new account. Since we already created an account using geth and deposited some Ether to it we will use this in the rest of the tutorial. You can import new accounts into MetaMask by providing the JSON file in your keystore. Depending on your operating system the location may differ. To find it, just run geth account list
and look for the keystore. Use the open
command to open up the keystore and find the JSON-file. For example, on Mac OS you can open the folder by running open /Users/[USER]/Library/Ethereum/testnet/keystore
.
In MetaMask, import this account by clicking the menu in the upper right corner and choose "Import Account"
Choose JSON File under "Select Type" and locate the JSON file in the folder you just opened. You also need to provide the password:
After successfully importing your account to MetaMask you should be able to switch to it by clicking on the round icon in the upper right corner, notice that it should have an "Imported" tag right next to the name.
We will now once again send 0.01 ether from this account to my address, you can do this by clicking "Send" on the start screen and enter in the address to my account: 0x867F7c9693573B75122e1Ff57Ddb44d55cAF1fA0
We'll come back to MetaMask in the third lab when we develop the frontend for our DAPP. There's just one final thing to do before we move on to the last part of this lab, we need to export the mnemonic for our account. The mnemonic is a set of words you can use to restore your wallet. The idea of having a set of words instead of a password is that it's easier to remember than a safe password, if you're interested in how it actually works I recommend watching this video. To export your seed words, go to settings and click "Reveal Seed Words". Save these in a safe place.
Now it's time to start our very own testnet. To do this we'll use an application called Ganache, it's available for both Mac, Linux and Windows and is available for download at https://truffleframework.com/ganache.
When you fire up Ganache you will start a private blockchain using the Ethereum protocol but running locally on your machine. You can see something that looks like this:
Now to prepare for the future labs, we'll import our the same account that we've been playing around with in both geth and MetaMask. You can import that account by going to settings, click Accounts & Keys and fill in the seedwords as in the screenshot:
That's it for the first lab, hopefully you were able to follow through without encountering and any major obstacles. We set up geth, synced it to the Ropsten testnet and created our very own account. We also familiarized ourselves with the browser extension MetaMask and finally we set up our very own private testnet.
In the next lab we will start doing the actual development of our decentralized application.