Learn to Code via Tutorials on Repl.it!

← Back to all posts
Basics of Blockchain: How Cryptocurrencies Work
MrEconomical (2205)

Introduction to Blockchain

Money is one of the most important parts of the world, allowing people to exchange goods and services in an economy. The traditional form of currency has been through trusted financial institutions, such as governments and banks. Our money is backed by and goes through these establishments. For example, to buy an apple I might withdraw $5 from a bank. However, any centralized finance system is subject to the decisions of the institutions controlling the currency. If Bank of America decided that they didn't like me anymore, they could block me from withdrawing my money and I won't be able to buy my apple :(.

Cryptocurrency and blockchain technology provides a solution to this problem by introducing a new monetary system that isn't backed by any single government and isn't controlled by a few major banks. Instead, the system is completely peer-to-peer and doesn't require any trusted third party to function. Note that in this tutorial I will be mainly focusing on proof-of-work systems. Also note that much of this tutorial is simplified, but its good enough to understand how blockchains work.

What is Blockchain?

At its core, blockchain is just a transaction ledger, or a giant list of every transaction that has ever occurred on the network. This list is public knowledge, meaning that everybody has access to it and nobody can stop anyone from making transactions.

  • Decentralized - Blockchain doesn't require any centralized power to control money or keep track of transactions. Instead, the network functions as a series of nodes which all keep track of their own blockchain.
  • Universal - Even though the entire network is decentralized, through systems like proof-of-work all nodes are able to agree on a single version of the blockchain which is deemed to be the "correct" version. More on this topic later.
  • Attack-resistant - The same proof-of-work algorithms that make the blockchain universal are able to protect it from attacks. While anybody has access to the ledger, to modify the ledger requires redoing all the proof-of-work.

Hash functions

The most fundamental part of blockchain is hash functions. These are cryptographically secure functions that generate an output of a fixed-size given an input: the SHA256 hash of e is 3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea and the hash of eeeeeeeeeeeeeeeeeeee is 58a791e28b5fc0b9ff6535efaceb9ea4f24cf5ea39c735b0880f6ec184b881df. They have four main properties:

  • Unique - The hash of any two different phrases, no matter how similar, is very unique: Example: The SHA256 hash of hello and the hash of helle are 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 and 5b175fdd8cdfd78c967643ad01f951210364c65642dbf5410f3fb778160585db. Completely different.
  • Collision-resistant - Because hashes are unique, this means even though there are an infinite number of phrases that can be put through a hash function, the probability of finding a hash collision, two phrases that have the same hash, is extremely small. This is what makes hash functions secure.
  • Irreversible - The nature of hash functions like SHA256 and others is that they are one way functions, meaning one cannot get the original plaintext from the hashed result.
  • Fast - It is very fast to calculate a hash of a phrase or a group of text, which is what makes them so useful in cryptography.

Hashes provide a secure way to verify data and because of this are a very important part of blockchain.


To understand how a blockchain works, first we must understand what a block is. Simply put, a block is a collection of transactions along with some information about the block. A blockchain is just a long list of these blocks. An example block (simplified) might look like this:

hash of previous block: ...
hash of transactions: ...
proof of work: ...
transactions: ...

Let's look at each of these components in detail:

  • Hash of previous block - This part of the block is the most critical part and is what forms the chain of the blockchain. Because of how hash functions work, including a hash of the previous block allows a "confirmation" of the chain and also makes the chain very hard to tamper with. If I were to change a block in the middle of the chain, its hash would then change and the irregularity could be easily detected. This feature makes the blockchain extremely secure and attack-resistant.
  • Hash of transactions - This allows all of the transactions in the block to be verified easily. Again, if I were to edit any transaction, the hash of the transactions would change, and then the hash of the block would also change, making this tampering easy to detect.
  • Proof-of-work - More on this in the next section about mining
  • Transactions - The transactions in a block are normal transactions, i.e. Alice sends 5 coins to Bob, and are signed with public key/private key cryptography meaning they can be easily verified.

Proof-of-work Mining

Great, now we have a system in which we can keep track of transactions and also prevent the entire chain from being tampered with, by including the hash of the previous block inside the next block. However, we still don't have a system to decide who gets to make the next block or which transactions are included. This is where mining comes in.

Proof-of-work is a very genius solution to this problem. Essentially, whoever does the most work gets to make the next block. This "work" is to solve a mathematical puzzle, which brings us back to hash functions. To prove that they did some work, each miner attempts to find a value, which when hashed, gives an output lower than the target "difficulty" of the network. Since hash functions are unique and collision resistant, this process is pretty much randomly guessing numbers until one fits the condition. The average amount of hashes or guesses this process takes is known as the difficulty.

This might be a bit hard to understand, but lets look at an example. Let's suppose that the current difficulty requirement is that the hash must start with two 0s. This means that in order to successfully "mine" a block, I need to find something that when hashed starts with two 0s. The probability of this happening is roughly 1/256, as hashes are hexadecimal in form and each digit has 16 values possible. This means if I mine or guess at 16 hashes/s, I will be able to successfully find a hash in about 16 seconds and thus mine the block.

When I mine a block, I get to decide every aspect of the block. I get to decide which transactions are included in the block and which ones aren't. The main job of miners is to validate transactions by generating these blocks and then including transactions in them. To incentivize miners, transactions all carry a small fee to the miner, which means if the miner includes them they can collect the fee.

The first miner which manages to solve this puzzle broadcasts their block out to the network, then all the nodes verify it by checking that it indeed is lower than the target difficulty. At the time of writing, the difficulty for Bitcoin mining is about 15 trillion hashes! Once the miner's block gets accepted, then it is added on to the blockchain for all of these nodes and the transactions are "confirmed." Additionally, the miner gets a reward for being the first to complete the proof-of-work, which is known as the "block reward." This is what makes mining profitable and provides a huge incentive to mine.

Proof-of-work also serves to reinforce the network against attacks. In order to alter a block, the attacker must redo the proof-of-work on the block (which will take as much effort as mining a block) and also all of the blocks after it, since each block includes a hash of the previous block. Essentially, an attacker must re-mine the block and all the blocks after it. This means that as long as the miners have more hashpower than the attackers, the system will outpace attackers and generate more blocks than the attackers can re-mine. This is the genius of the blockchain system.

A Look at the Future of Blockchain

Through hash-functions and proof-of-work mining, blockchain currencies are able to maintain a single public ledger of all transactions in a decentralized manner that is both agreed-upon and also resistant to attacks. Miners drive this system by repeatedly hashing random values until they solve the proof-of-work puzzle and have "mined" a block. Then, this block is validated and accepted as part of the blockchain along with any transactions the miner has included.

I believe that blockchain technology and currencies are the future of the monetary system. No government is required to issue money, and no group of banks is able to control the entire monetary system. It is impossible to seize your funds on the blockchain or to nullify them. While in the future blockchain may not be in the same form as today, this revolutionary new system will allow people to have more financial freedom as well as for the economy to be more secure as a whole.


DynamicSquid (2678)
Mr. E tutorial ---> |----------|
                    |          |
                    |   Good --|--> Upvote
                    |    or    |
                    |   Bad? --|--> Downvote
                    |          |
Mr. E tutorial ---> |----------|
                    |          |
                    |          |
                    |          |--> **Excellent**
                    |          |
                    |          |
AtticusKuhn (232)

Great. We can finally establish an anarcho-capitalist society without fiat money!

awesome10 (212)

@DynamicSquid when you figure that out can i come with you?

AtriDey (159)

Now the entire repl.it community knows about mining. Guess I'll never be able to get that graphics card I wanted ¯_(ツ)_/¯

Roar123 (430)

Very informative but you're getting scammed if you're paying $5 for an apple :)

MrEconomical (2205)

@Roar123 its a premium certified Economical Apple™

xxpertHacker (341)

Hashing is not unique. As one who has dealt with hashing firsthand, I can say with certainty: it is very possible to find that two different strings of characters have the same hash, this is often how passwords are breached. There are a number of places in which your password is merely protected by a hash, thus extremely insecure.

Also what happens when no one can compute the next hash? Say, in the distant future the next hash has an extremely high difficulty?

MrEconomical (2205)

@StudentFires hashing is unique in principle especially with SHA256, nobody has managed to find a way to generate collisions fast yet... as with the difficulty, it automatically adjusts every so often to ensure block times remain at 10 minutes (for bitcoin)

xxpertHacker (341)

@MrEconomical Ah yes, SHA256; it all depends upon the algorithm used, SHA256 is pretty good, but it won't last too much longer. I estimate in about less than 10 years quantum computers will be more commonly available. And when they are, things are gonna change.

MrEconomical (2205)

@StudentFires SHA256 is actually surprisingly considered quantum resistant, which I did not know... however pub key private key cryptography is definitely going to be in trouble

xxpertHacker (341)

@MrEconomical They won't realize that they're wrong until it's too late unfortunately. There's already theoretical algorithms that exist for use with quantum computers.

RohilPatel (810)

Of course a dude with name of Economical must major in economics lol

2009k (11)

WOW. my dad was amazed by this and so am I!

CodingCactus (2855)

wow, really good, thanks for enlightening me about blockchain!

lightningrock (51)

uh no its

great and awesome

but maybe a little complicated @codecademy123

ChezCoder (1484)

@codecademy123 you tried wooshing someone but ended up wooshing yourself