My Artisanal, Small Batch Blockchain
By: Kat Leight
Tags: Software Development, Small Batch, Blockchain, Web Development, Node

I’ve been interested in Blockchain technology since before I knew I was really interested in Software Engineering. I attended a panel discussion at a Denver Startup Week discussing Blockchain basics, and was immediately fascinated, mostly because I didn’t understand most of what the panel was talking about. But it seemed like such a rebellious, anti-establishment piece of tech and I knew I had to learn more.

As I began to dig into coding, my interest in Blockchain only grew. For my final capstone project for Flatiron School, I decided to build my own Blockchain so I could better understand the technology. I ended up building BelayChain, which is a blockchain that stores belay certifications for climbing gyms.

What is a Blockchain?

Most people think of cryptocurrency when they hear the word, “blockchain” but in short, it’s just another way to store data, another version of a database. What makes this database special is that you can’t change or delete the information it’s storing. It’s immutable. A blockchain consists of a series of blocks (or records). Each record contains a hash of all of its information, including the hash of the previous block. Change something in the block, and you change the hash, which then no long matches the previous hash of the next block. Change something in a block, and the whole chain becomes invalid.

Unlike a traditional database, a blockchain is decentralized, meaning its information isn’t stored in one place. Usually, everyone operating on the blockchain has a copy of the ledger (list of blocks) and when a new block is added, the majority of the users must agree that it is valid.

While cryptocurrency is the most well known application of blockchain, there are other potential use cases such as peer-to-peer loans via smart contracts, or supply chain tracking, even providing food assistance to refugees.

Making My Own Blockchain

There are tons of tutorials on the internet on how to build a simple blockchain via Javascript. When I first started my capstone project, this is where I began.

I started by making a block class with a constructor that included the index (or position on the blockchain), timestamp, hash of the previous block, it’s own hash, and a nonce (for mining). The class also includes a function for calculating a hash (this.calculateHash()) and function for mining a new block (using the nonce).

The data I stored in each block was the actual information for the belay certification. It included the ID of the gym issuing the certification, the ID of the member getting the certification, and the certification type, which was either “Top Rope”, “Lead”, or “Revoke Previous Certification” (because you can’t delete a certification once it’s on the chain).

const data = { gym_id user_member_number cert_type }

The Blockchain Class has a constructor that sets a new chain of blocks, a difficulty (for mining), and record of its own URL, as well as an array of URLs of any other nodes of the chain it’s connected to.

I then added some methods, like creating a genesis block to start my chain (it’s previous hash = 0 because there is no previous block), getting the last block on the chain, and adding a new block.

I also created a method to check to see if the chain is valid by comparing hashes across blocks (remember- a block has a record of the previous block’s hash and those must match, meaning the data must be unchanged for the chain to be valid).

Finally, I added some methods specifically useful to this project, like being able to find all blocks (or belay records) specific to a gym or a member.

Building A Node.js and Express API to interact with the BlockChain

In order to interact with my blockchain, I built an API in Node.js and Express. I started with three basic routes: one to fetch the chain, one to add a block, and one to check if the chain is valid:

Making the Chain Persist with MongoDB

This basic structure was all well and good, but because I wasn’t storing the data anywhere, anytime I restarted my server, my Blockchain started all over from zero. I needed a way to make sure the information persisted from session to session. I decided to use MongoDB to store each block. This is where my homemade blockchain starts to deviate from true blockchain technology. Utilizing a database to store all of my blocks certainly is decentralized.

In order to keep with the spirit of being decentralized, I decided to run the blockchain on more than one node (aka more than one server). Each node stores the blocks in a different MongoDB collection. For ease of setting up this project, all of my collections are in the same cluster, but it would be very easy to add a node running on a different cluster.

I then created a function that ran when the server started to grab all of the blocks in the chain from MongoDB and used this to create a new instance of my BlockChain class:

And BOOM! Now I had a blockchain that was actually persisting from session to session. I had to go back and tweak my function for adding certifications to the chain to also include creating a new block on MongoDB.

Just a Tasting

I ended up adding a great deal of other routes and functions once I started connecting nodes and making sure every time a new certification was posted, it was posted and validated by every node in the network. This was additionally complicated by the fact that I was also running a Ruby on Rails API to for my user information (gym and member ids and login information) and was interacting with the Blockchain API almost exclusively through my Rails backend. But that’s a blogpost for another day.

Additional Resources