Developing Mobile Dapps Part 1

Ayush Bherwani
Coinmonks
6 min readFeb 12, 2020

--

Blockchain has been a hype word for a long time, but often we find it challenging to implement real-world applications. For me, learning Blockchain wasn’t that easy. I had to go through a lot of blogs and Stack Exchange answers to understand the concepts. Don’t worry, in this series of articles we are going to learn how to build Mobile Dapps with Flutter. I’ll attempt to make it as uncomplicated as possible and easy to grasp.

Before we move on to implement Dapps, we must know the basics of Blockchain and Ethereum. So, in this article, we will cover the basics of Blockchain and other terminologies associated with it.

Blockchain

Blockchain in simple terms is a decentralized distributed ledger. As the word suggests, it’s just a collection of blocks linked together, to record the transactions in such a way that it’s not possible to modify these transactions. This characteristic of Blockchain is also known as immutability. The immutability of transactions makes Blockchain a trustworthy solution and decentralized distributed ledger brings the transparency in the system.

Collection of blocks linked together.

Hey, that’s great but what is a block and how does Blockchain achieve immutability?

What is a block?

To make it simple to understand, a block consist of three things, hash of previous block, transactions, and hash of the current block also know as root hash of Merkle tree. A single block can contain more than one transactions.

Representations of blocks in blockchain.

Apart from these three things a block also consist of nonce value, timestamp, difficulty target but let’s not dig into this because it’s a very vast topic and let’s leave it for some another day.

Why Immutable?

Before we discuss how Blockchain achieves immutability, let’s discuss what is a cryptographic hashing.

A cryptographic hashing is a one-way process of generating a series of characters which is usually known as a hash. A hash function takes an input data and produces a corresponding hash. Every time you pass the same input data to the function it generates the same hash. This property of hashing function makes it deterministic. In the context of Blockchain, it uses Secure Hashing Algorithm 256 function a.k.a SHA-256 to generate hash.

SHA-256 is a cryptographic hash function which generates a fixed-sized 256-bit output which is almost unique, no matter how long the given input data is. You can pass a whole Wikipedia, and it will still return a unique 256-bit hash.

32 character Hash of a long paragraph from Wikipedia

A single character variation in the input data results in a significant difference in the generated hash. It is a desirable property of cryptographic functions known as Avalanche effect. In the below images, you can see the difference between generated hash for the word “Blockchain” and “blockchain”.

32 character Hash for Blockchain
32 character Hash for blockchain

SHA-256 also has a property known as Pre-Image Resistance which means you can generate a hash from the given input data, but you can not find the input data from the given hash. The only way that you can find the original input is by using the brute-force method. In the brute-force method, you pick up a random input, hash it and then compare the output with the target hash and repeat until you find a match. It’s infeasible to find the original input data because it will take you 2¹²⁸ attempts to pick the correct random input unless you are a magician.

Magician GIF

Now that we have understood how hashing works, it’s time to discuss how Blockchain achieves immutability. The hash of the previous block is contained in the hash of the new block, the blocks of the Blockchain all build on each other. Without a previous hash, there would be no connection. So due to Avalanche effect, if we change any one piece of this information in the block, the hash is going to change, and it will break the chain.

Mining

If you have heard about Blockchain before, you must have come across the word mining. Mining is an expensive computation calculation that is performed by a miner to generate a particular hash for the block and validate the legitimacy of the transactions in the block. The expensive computation that is performed by the miner is known as Proof of Work which is a trustless and distributed consensus algorithm used by Blockchain.

Every block has a particular difficulty level which defines a threshold level for hash. Every time miner generates a new hash, it is compared with the difficulty threshold. The process is repeated until the generated hash is less than the threshold.

You might be wondering since we have the same transactions inside the block how can we generate a new hash every time. Yes, you are correct we cannot generate a new hash with the same transaction list due to deterministic property of the cryptographic function. But if you remember, we discussed that block also consist of nonce value, timestamp, and difficulty target. A nonce is an arbitrary number that is added with the hash of the block. If the hash is not less than the difficulty threshold, then the nonce is changed, and this keeps on repeating a million times until finally, it is less than the given threshold.

Okay, that’s great, but why do we change the difficulty threshold? Hmm, it’s an interesting question. So, whenever a new block is created, it’s propagated across the network so that every block has a record of this new transaction in the ledger. In theory, it takes almost 10 minutes for a new block to propagated across the network in Bitcoin and 10–19 minutes in Ethereum. The hash will be easily generated if the difficulty threshold is less, and it might be the case that every block does not have the updated ledger, which may cause inconsistency in the network. So, to maintain an average time of block creation to 10 minutes, the difficulty threshold is changed after every 2016 blocks in Bitcoin.

--

--

Ayush Bherwani
Coinmonks

Organizer Flutter Vadodara | GSoC’20 at Dart | Mobile Developer | Solidity Developer