This blog post is part of a whole series:
- Part 1: Implementing a basic blockchain
- Part 2: Implementing proof-of-work.
- Part 3: Transactions & mining rewards
A blockchain is a public database that consists out of blocks that anyone can read. Nothing special, but they have an interesting property: they are immutable. Once a block has been added to the chain, it cannot be changed anymore without invalidating the rest of the chain.
That is the reason why cryptocurrencies are based on blockchains. You don't want people changing their transactions after they've made them!
Building a Block
A blockchain consists out of many blocks that are linked together (that makes a lot of sense, right?). The chaining of blocks happens in a way that allows us to detect when someone has manipulated any of the previous blocks.
So how do we ensure the integrity? Well each block contains a hash that is computed based on its contents. It also contains the hash of the previous block.
index that tells us at what position the block sits on the chain. We also include a timestamp, some data to store in our block and finally the hash of the previous block.
Building the chain
In the constructor I initialize the chain by creating an array that contains the genesis block. The first block is special because it cannot point to a previous block. I've also added two methods:
getLatestBlock()returns the latest block on our blockchain.
addBlock()is responsible for adding a new block to our chain. To do that we add the hash of the previous block to our new block. That way we preserve the integrity of the chain. Because we changed the contents of our new block, we need to recalculate it's hash. When that's done, I push the block onto the chain (array).
Finally I've created a method
isChainValid() to make sure that nobody has messed with the blockchain. It loops over all the blocks and checks if the hash of each block is correct. It also checks if each block points to the correct previous block by comparing the
previousHash value. If everything checks out it returns true and if something is wrong it returns false.
Using the blockchain
With our Blockchain class finished, we can actually start using it!
Here I'm just creating a new instance of a Blockchain and naming it SavjeeCoin. Afterwards I add some dummy blocks onto the chain. Blocks can contain any data that you want, but in this case I opted for an object with an
Trying to manipulate it
In the introduction I said that blockchains are immutable. Blocks cannot be changed once they are added. Let's test that!
I'll start by verifying the integrity of our chain by running
isChainValid(). I haven't manipulated any blocks so it returns
After that I take the first block on the chain (index = 1) and I manipulate the amount. I then recheck the integrity of the chain and this time it detects that something is wrong. Our chain is no longer valid.
This implementation is far from complete. It doesn't implement proof-of-work or a P2P network to communicate with other miners.
It does however demonstrate how a blockchain works. Many people think that it's very complex, but this post demonstrates that the basic concepts of a blockchain are easy to understand and to implement.
This blockchain is not complete and not fully secure. Keep reading: