Please enable JavaScript to use CodeHS

What is Blockchain

This tutorial covers the basics of what blockchain technology is and how it works. It also will demonstrate a basic blockchain algorithm.

By David Burnham

What is Blockchain Technology

You have most likely heard of things like Bitcoin, Dogecoin, NFTs (non-fungible tokens), and of course blockchain technology, but how are these things related? Things like digital currency and NFTs use blockchain technology as the backbone to implement the technology, but the concept of blockchain is not the same thing as a digital currency.

Blockchain is essentially a decentralized ledger system that can be used to track transactions and assets. So what does that mean? Think of a ledger like a bank account. You start with a set amount of money, then you can add money, or take money out. Your bank account ledger will then track each of these transactions and keep track of your balance.

Blockchain technology allows for this to happen in a decentralized way, which is to say that there is no centralized ‘bank’ keeping track of everything. Instead, the blockchain is used to verify transactions.

How Does Blockchain Technology Work?

Blockchains generally work as their name suggests, they are a chain of blocks. Each block contains some basic information, including a hash value to the previous block, a timestamp, and data about the current transaction. Using this data, you can essentially create a digital trail for any item.

Let’s look at an example. Let’s start with a Bitcoin as your first entry in the blockchain. Maybe you then go out and buy movie tickets and pay for them with your Bitcoin. A new block is added to represent that purchase and then it sends a portion of your Bitcoin to the movie theater. Next, you buy some pizza, so a new block is made representing that purchase. Each time you make a purchase, the transaction data is recorded and the remaining portion of your bitcoin is available.

By looking at all of these transactions, you can figure out not only how much Bitcoin you have, but also how much others have since both the movie theater and pizza place will have entries to their own ledger to spend the Bitcoin you gave them.

Eventually, you get a large web of transactions for each coin, but how do you know each of these is accurate? If the ledger is decentralized, what’s to say that the movie theater doesn’t update their transaction to say that they received more Bitcoin than you said you sent them?

This is the magic of the blockchain. Remember above how we said that each block contains a timestamp and a hash to the previous block? If those values are ever broken, the chain will be broken and the next transaction will not be able to be verified.

A Peek Behind The Scenes

Let’s break this down and start looking at the code. To best understand this, we are going to look at a Java implementation of blockchain technology. Out implementation has a Runner class, a Block class, and a StringUtil class.

Runner - This will kick off our program, store all of our blocks in an ArrayList, and validate that a block is valid. For a block to be valid, the previousBlock hash value needs to match the previous block’s hash value.

StringUtil - This program will use a SHA256 encryption logic. SHA256 stands for Secure Hash Algorithm 256, which is a 256 bit encrypted hash generator.

Block - This class will create new blocks and store the information for each block.

As mentioned above, each block is linked and if any of the information in the block is changed, the hash value will not match, thus breaking the chain. Why is that? The hash function is a one-way function, that takes in the previous hash value, a timestamp, data (such as transaction amount), and a nonce (number used one time). If any of that data were to change, a new hash value would be created and that hash value will not match the previous value stored in the next block.

That is the essence of blockchain technology! Explore the code below to see this in action.

Mining a new Bitcoin

Now that you understand how a blockchain works, let’s go one step further and understand how a Bitcoin is mined. Each Bitcoin in the ledger builds off the previous bitcoin, just like you say with the blockchain technology. In other words, the next bitcoin to be mined will need to use the previously mined Bitcoin’s hash value as an input.

Once you have this input, you can start mining, however there is one more catch. The next hash value also has to be lower than or equal to the current target value. This target value is known as the difficulty. Given these inputs, a user can use brute force to calculate the next value in the chain by changing the nonce. Each time the nonce is changed, a new hash value is created and if the new value meets the target condition, then the coin can be mined.

While it sounds simple, the level of difficulty slowly increases causing the process to take longer and longer.

The program below simulates this process. In the Runner class, you will now see a difficulty set at 4. At a difficulty of 4, you should see the program successfully mine a coin in less than a second. Try increasing to 5, then 6 (you may need to stop it for 6). You will notice the time quickly ramps up. In our simulation, to mine a block, the program needs to find a hash value with as many leading zeros as the difficulty.

Check out the program below to see this in action!

Departing Thoughts

The concept of mining a bitcoin is relatively straightforward, but as you probably saw, increasing the difficulty drastically increases the time it takes to mine a coin. Bitcoin miners are using much more powerful computers to churn through the algorithm quickly. Even so, it still takes them on average about 20 minutes to mine a coin, which is pretty quick considering the current difficulty is over 17 trillion!