Ethereum ÐApps Programming Distilled — Part 1

A list of concepts and tips I learnt from my first decentralised app on the Ethereum blockchain, with answers to the following questions: What is a blockchain and Ethereum? What is a Smart Contract and how to write one with Solidity? How do I create a frontend for a ÐApp with html, JavaScript, Web3.js and MetaMask? How do I deploy all this in a private blockchain, or live for other people to use it?

Marco Bellinaso
9 min readMar 5, 2018

This is a multi-part article. Here are the links to the other parts:

Unless you’ve been living under a rock for the past year or two, you must know that every person, website and magazine in IT/tech is talking about blockchain technologies (together with AI, which continues to be the other hot topic). The interest for many is as a potentially lucrative investment (still? who knows…), but being a developer I’ve always been more interested in the tech part of it, and just like I did for React Native, I recently decided to dig a bit deeper by getting my hands dirty and creating an application.

The app, msgblocks.com, is meant to be a super easy way for people to store messages on the Ethereum blockchain: this could be a love message for someone, or a prediction of the future, or any other “bold statement” that you want to make persistent forever. A message can be optionally password-encrypted before being sent to the blockchain, and you have the option to let people reply to it (nice in the case of a love letter for example). It’s not meant or going to change the world, but it served well the purpose of learning how to create something from start to finish :)

In this article I’ll try to provide the simplest possible summary of how all this works together behind the scenes, what are the components that come into play for a developer to create a decentralised application, and what are some limitations (and the workarounds) that I’ve found while developing. The article is long, but it also cover a lot!
Albeit a simple project, it was a fun journey that let me learn and discover quite a few things!

msgblocks.com — go and give it a try

A quick recap of what a blockchain is and how it works

Before I start talking about developing a custom app, let’s recap some of the basic concepts about blockchains, transactions, blocks etc. No doubt this will be simplistic and possibly imprecise at times, but I hope it’ll give you a decent starting point from which you can dig deeper. Feel free to skip this section or skim through it if you’re already comfortable with these things though.

  1. There are many blockchain networks and technologies, each with different characteristics, objectives and typically coins/currencies. The two most popular ones are the Bitcoin blockchain/network (with bitcoin [all lowercase], BTC, as its currency) and Ethereum (with Ether, ETH, as its currency). A search for something similar to “blockchain technologies comparison” will give you plenty to read…
    In this article the focus is on Ethereum, with many general concepts of this section being applicable to Bitcoin as well though (until we start talking about development, at which point it becomes 100% Ethereum-only).
  2. The blockchain is like a database, that rather than living in centralised servers owned by a company (think Microsoft Azure, Amazon AWS, Google Cloud, or any other hosting), it lives in the computers of tens of thousands of people around the world, each storing a copy of the same data.
  3. The machines storing the blockchain data are called “nodes”, and they form the blockchain network. At the time of writing, the Ethereum network has around 21,000 nodes (half of it being in USA + China), but this goes up and down of course (according to the visibility of the technology in publications, the cost of hardware required to operate it, etc.). It was 22K a few days ago, and 25K around 10 days ago. Check out ethernodes.org to see updated numbers.
    In order to transform your machine into a node you just need to install an Ethereum client, which will replicate the blockchain locally and will start making new blocks (more about this in a moment). The most popular client is Geth, written in Go language (in fact it’s also called go-ethereum).
  4. The blockchain is, guess what, a chain of blocks of data, where every block contains a number of transactions (how many depends on the size of each transaction, which depends on how much data it contains). Each block has a signature, which is the result of hashing the transactions’ data + a “nonce”. The nonce is a number, but not just any number…it must be a number that makes the hashing function (SHA256 for Bitcoin and ETHash for Ethereum) generate a signature in a specific shape (eg: the signature must begin with X zeroes). The computer will try finding the correct nonce starting from 1 and incrementing it by 1 at every interaction of a loop…until it finds it…or until it gets notified that another node of the network found it and produced a valid block. This process of finding the valid nonce and therefore hash is called “proof-of-work” (PoW).
  5. When a valid block is found, the node that finds it broadcasts it to its closest nodes of the network, and they do the same with their closest nodes, and so on…until all the nodes in the network have a local copy of it. This is what it means to be decentralised: the “database” is not stored in a central location, but is fully replicated in all nodes.
  6. Finding a valid signature for a new block is called “mining”, and since it requires a lot of computational effort (which means powerful and expensive graphics cards to crunch the numbers, and lots of electricity to fuel them) the winning miner is awarded with some cryptocurrency (Ether in case of Ethereum, or bitcoin or others in other blockchains) as an incentive. An alternative for PoW, that will require less energy and normal hardware, as well as improve the block confirmation latency time, is proof-of-stake (PoS) — the differences are outlined in this article by Karthik Radhakrishnan.
  7. Every block also includes a header with the hash of the previous block. If the previous block is tampered with, the malicious node would not only need to regenerate the hash of that block according to the new data…but also do the same for all the subsequent blocks (because the next block depends on the hacked block’s hash…and so on in cascade), which would be highly impractical because of all the computation effort it would take. Even if done….that node’s blockchain data would be different from the blockchain stored in the other tens of thousands nodes of the network, and therefore not considered valid (the valid blockchain is the one stored in at least 51% of nodes). This is what guarantees that the blockchain is immutable — you can’t go back and modify stuff without making it invalid.
  8. At the moment the reward for finding a valid block for the Bitcoin blockchain is 12.5 BTC, which will however become 6.25 BTC in 2020 (it halves every 210,000 blocks). The reward for a valid block on Ethereum is 5 ETH (to be reduced to 3 ETH or even lower, see here)…but things are more complex here because the miner also gets some “transaction fees” (more on this later) and there is also some compensation for miners that do find a valid block, but only after another valid block was confirmed (more about this here).
  9. Finding the correct hash for a block takes a lot of effort, and that translates to a long time and a lot of money…all for nothing if someone else finds it sooner. That’s why rather than doing “solo mining”, people join “mining pools”: all the participants contribute to finding the magic number that solves the puzzle, and when it’s solved, they earn proportionally to how much computational power they put it with their hardware. Some reward, but more or less constant, is better than nothing after all. (To get an idea of how much competition there is, just know that there are companies renting entire Boing 747 airplanes to get new mining hardware as fast as possible.)
  10. But what are these “transactions”, after all? Anyone can read data from the blockchain, but to write data you need to execute a transaction. You can think of them as documents, or more generally pieces of data, that describe a transfer of value between two addresses (eg: when you transfer some bitcoin or Ether to a friend), or (in case of Ethereum) a call to an app’s function with some input parameters, which will change some “state” in the blockchain.
    This document is then signed with the sender’s private key and sent to his local node (or potentially to a remote one, in case you don’t have a local node and use the MetaMask browser extension); the receiving node validates the transaction to make sure it was really sent from that account (the account’s address is the public key, which is used to validate the signature), and then it broadcasts it to the network (similarly to how new valid blocks are passed from node to node until they are everywhere). The transaction ends up in a “transaction pool” (called “Mempool” in Bitcoin), and at some point it will be included in the next block being mined by someone. When that happens depends on how many transaction there are before yours, and how much “fee” you’re willing to pay to the miner. Yes, sending money or executing a function in an app does have a cost! The miner gets that fee + the reward, as mentioned before.
    So, even if a block takes on average 10 minutes to be mined in Bitcoin and 15 seconds in Ethereum, your transaction might take a lot longer to be confirmed according to the factors just described.
    Refer to this article by CodeTract to see in details what’s inside a transaction, and to this other one by Mahesh Murthy to better understand its life cycle.

I also recommend watching this video for a very clear representation of some of the points above.

Why is it interesting?

  1. It’s forever: as long as the blockchain exists, the data you store in it does as well. And with 22K nodes (in case of Ethereum)+ a bright future ahead, it’s unlikely the whole blockchain will disappear any time soon.
  2. Datetime stamp: your data is saved in a block of the blockchain, which has a date and time. That certifies when you wrote and saved that data, and no one can argue that.
  3. No tampering: a block in the blockchain cannot be modified (otherwise the blockchain would become invalid from thereafter), and so is for your data. Once it’s saved, it can’t be modified.
  4. No censorship: since a block can’t be modified, it also can’t be deleted, which means your data can’t be censored. Is there any centralised website that can guarantee you that?

There are many apps where transparency and no tampering would be or are a must: poll votes and results, education records and awards, government and public datasets, property ownership records are all things that could benefit from this…but really you might want to store any kind of data in the blockchain, just to be sure that there’s no central authority that can take it down or modify (be it the company hosting the centralised server, a government, or hackers).

Of course not every type of data is suitable for a public blockchain (medical records?), but private enterprise blockchains are also a possibility that many companies (banks, insurances, hospitals etc.) are investigating. As an example check out Quorum by JP Morgan if interested, and this article about how to set it up and work with it. The focus is this article is the public Ethereum blockchain though.

Who am I / what do I do? I proudly work as a Solutions Architect in the Mobile Team @ ASOS.com (iOS app | Android app), and we’re always looking for strong, friendly and talented developers that want to have an impact on how tens of millions of customers shop online. ASOS is the biggest online-only retailer in UK and, let’s be real, the best tech+fashion company in the world. Some of the technologies we use are Swift for iOS, Kotlin for Android, React and Node on the web frontend, .NET and Azure on the backend. If that sounds interesting to you, and you happen to live in beautiful London (or are willing to move here…after all it’s the best city in Europe except for some in Italy!), do get in touch with me!

--

--

Marco Bellinaso

Principal Architect @ASOS.com (and iOS / full-stack dev for fun)