Building the Simplest Blockchain with Ruby

Blockchain is a revolutionary technology that is starting to have a significant impact on businesses worldwide. Despite its abstractness and complexity, blockchain brings various benefits to industries, such as greater transparency and enhanced security.

However, it takes time to study the design and master specific programming languages for blockchain. Appropriate learning materials help shorten this process, which is why since the emergence of Bitcoin, a barrage of popular online content have been released to explain how it works.

In October 2017, Mr. Haseeb Qureshi, a well-known blockchainist, posted a blockchain demo and source code for making a simple blockchain using Ruby. In my opinion, this is one of the best materials available online to help the public better understand blockchain. In this article, we’ll be taking a closer look at Mr. Qureshi’s demo. Specifically, we will be talking about the six stages involved in making a functional blockchain with Ruby.

Stage 1: Money Transfer

Balance check and money transfer are implemented at this stage. The transfer is addition or subtraction performed based on the account balance.

GET, and POST of the HTTP protocol are the most suitable options to implement this functionality. GET obtains data from the server and POST changes data on the server.

Here, the presentation of the UI does not require HTML protocol. You can use the Ruby Web framework Sinatra to organize URLs and related methods and view the transfer information in the command line by using UE.

Methods on the client and URLs on the server are quite simple:

Client: client.rb

Server: haseebcoin.rb

Required knowledge for this layer: ruby, HTTP GET, and POST, Sinatra

Stage 2: Building a Gossip Network

The blockchain has a decentralized structure, known as “Gossip Protocol.” The word “gossip” here is not referring to rumors but information that gets disseminated in a decentralized network.

Let us build a Gossip network where movie names can get exchanged.

client.rb sends messages to a specified port.

gossip.rb receives two parameters: the source port and the destination port. Exchange information through specific ports on the source end, such as ports 1111 and 2222.

In a real decentralized network, the two ports are essentially two network nodes. Exchanging information between different local ports represents communication between different nodes in a simulated network.

On each node:

Speak out your favorite movie name every 3 seconds.

Change the favorite movie name every 8 seconds.

The server receives and processes the data.

In a four-person network:

  1. Run gossip.rb 1111 at the initial node. The first node speaks out a favorite movie name at port 1111.
  2. Run gossip.rb 2222 1111. The second node speaks a favorite movie name at port 2222 to the first node (port 1111).
  3. Run gossip.rb 3333 2222. The third node speaks a favorite movie name at port 3333 to the second node (port 2222).
  4. Run gossip.rb 4444 3333. The fourth node speaks a favorite movie name at port 4444 to the third node (port 3333).

After running for a period, the four nodes finally obtain information of peer ends, and the data keeps changing. This is a simple Gossip network.

Stage 3: Data Encryption and Decryption

Top-level encryption algorithms are the foundation of blockchain. The asymmetric encryption technology is used at this layer to implement blockchain accounts. The RSA algorithm can generate public keys, private keys, and enforce asymmetric encryption.

Thanks to the OpenSSL module in Ruby language, we can quickly implement asymmetric encryption and signature verification. In blockchain, public keys are accounts, and private keys are passwords. Each pair of keys is one blockchain account.

Decrypt the ciphertext:

Verify whether the ciphertext is a message:

Required knowledge for this layer: asymmetric encryption algorithm

Stage 4: Data Mining

At this stage, proof-of-work gets implemented, and blocks get generated for the blockchain. It is a time-consuming and arduous process. Hash functions are irreversible and free of conflicts. The calculation process is straightforward. A result is obtained by merely performing a hash operation on the input.

The input is the information about money transfer, including the transfer amount, the remitter’s name, the recipient’s name, and so on. Hash operations have various algorithms.

Here, the SHA256 algorithm gets used:

When the same information gets hashed, different results come up each time. We keep performing operations until the obtained result meets some features, for example, starting with several digits of 0.

Verify whether the result starts with several digits of 0:

It is not easy to perform operations to meet the above conditions. They consume a lot of time. The entire process is called mining:

The input includes the result of the previous hash operation. Therefore, each hash operation gets affected by the previous one. In other words, this is a chain structure. That is why it is called blockchain.

Stage 5: The Longest Chain Rule

At this stage, the first block gets initialized and accordingly a chain structure gets generated to form a blockchain. Blockchain can get stored in the Array structure. During storage, the blocks must get validated.

Initialize the block: class Block

The most challenging work during mining is finding a nonce.

A complete block gets compacted in this way.

Initialize the blockchain: class BlockChain

Merely use Array to store it!

Add blocks to the chain. The entire blockchain is continuously growing.

You must validate rigorously whether the blocks are healthy.

Stage 6: Combining the Pieces

Finally, Blockchain works its magic through a harmonious collaboration with every component in the network. At Stage one, the transfer is of the Transaction class and needs to use a private key to sign the information.

The reward for the miner who gets the first block is 500,000 pieces of silver.

Verify whether spending charged to the account is valid.

Add unknown node $PEERS to maintain the growth of the network.

Data processing between nodes reads and then updates the blockchain and peers.

Processing a received block focuses on whether the chain is getting longer.

Update peers so far as they are new.

When transferring money, get the recipient’s pub_key and then transfer the funds through the remitter’s pub_key.

Put the blockchain into a Gossip network and assemble all functional components. There you go. You have successfully created a working blockchain!

You can find more information about this Demo on Github:

To learn more about blockchain and other innovative technologies, visit


Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store