DEV Community

Cover image for Ruby & blockchain
Davide Santangelo
Davide Santangelo

Posted on

Ruby & blockchain

A blockchain is a decentralized and distributed digital ledger that is used to record transactions across a network of computers. It is a type of distributed database that is composed of blocks of data that are linked and secured using cryptography.

Here is a simple example of a blockchain implemented in Ruby:

# This class represents a block in the blockchain
class Block
  attr_reader :data, :timestamp, :prev_hash, :hash

  def initialize(data, prev_hash = nil)
    @data = data
    @timestamp = Time.now
    @prev_hash = prev_hash
    @hash = calculate_hash
  end

  def calculate_hash
    Digest::SHA256.hexdigest("#{@prev_hash}#{@timestamp}#{@data}")
  end
end

# This class represents the blockchain
class Blockchain
  attr_reader :chain

  def initialize
    @chain = [genesis_block]
  end

  def genesis_block
    Block.new("This is the first block in the chain.", "0")
  end

  def last_block
    @chain[-1]
  end

  def add_block(data)
    new_block = Block.new(data, last_block.hash)
    @chain << new_block
  end

  def valid?
    (1...@chain.length).all? do |i|
      @chain[i].prev_hash == @chain[i - 1].hash
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

In this code, the Block class represents a single block in the blockchain. Each block has data, a timestamp, a hash, and a reference to the previous block's hash. The hash of a block is calculated using the SHA-256 cryptographic hash function, which takes the previous block's hash, the block's timestamp, and its data as inputs.

The Blockchain class represents the entire blockchain. It maintains a list of blocks in the @chain instance variable, and it provides methods for adding new blocks to the chain, checking the validity of the chain, and accessing the last block in the chain.

To create a new blockchain, you would simply create a new instance of the Blockchain class:

# Create a new blockchain
blockchain = Blockchain.new
Enter fullscreen mode Exit fullscreen mode

To add a new block to the blockchain, you would use the add_block method:

# Add a new block to the blockchain
blockchain.add_block("This is the second block in the chain.")
Enter fullscreen mode Exit fullscreen mode

To check the validity of the blockchain, you would use the valid? method:

# Check if the blockchain is valid
if blockchain.valid?
  puts "The blockchain is valid."
else
  puts "The blockchain is not valid."
end
Enter fullscreen mode Exit fullscreen mode

Here are some tests that you can use to verify the functionality of the Ruby blockchain code that was provided in the previous answer:

# Test the Block class
describe Block do
  let(:block) { Block.new("test data") }

  it "has data" do
    expect(block.data).to eq "test data"
  end

  it "has a timestamp" do
    expect(block.timestamp).to be_a Time
  end

  it "has a previous hash" do
    expect(block.prev_hash).to be_nil
  end

  it "has a hash" do
    expect(block.hash).to be_a String
  end
end

# Test the Blockchain class
describe Blockchain do
  let(:blockchain) { Blockchain.new }

  it "has a chain" do
    expect(blockchain.chain).to be_an Array
  end

  it "has a genesis block" do
    expect(blockchain.chain[0].data).to eq "This is the first block in the chain."
    expect(blockchain.chain[0].prev_hash).to eq "0"
  end

  it "can add blocks" do
    blockchain.add_block("test data")
    expect(blockchain.chain.length).to eq 2
  end

  it "can check the validity of the chain" do
    blockchain.add_block("test data")
    expect(blockchain.valid?).to be true
  end
end
Enter fullscreen mode Exit fullscreen mode

These tests use the rspec testing framework, which is a popular choice for testing Ruby code. The tests are written in a descriptive style, using the describe and it methods to define the tests and specify their expected behavior.

To run these tests, you would need to have the rspec gem installed on your system. You can then run the tests by typing the following command at the command line:

rspec blockchain_tests.rb
Enter fullscreen mode Exit fullscreen mode

This will run the tests and output the results to the terminal. If all of the tests pass, then you should see a message indicating that all of the tests passed. If any of the tests fail, then you will see a detailed error message that indicates which test failed and why. You can use this information to debug your code and fix any issues that are preventing the tests from passing.

Here are some links that you can use to learn more about blockchain:

Blockchain Basics
Introduction to Blockchain
A Beginner's Guide to Blockchain
Blockchain for Dummies
The Ultimate Guide to Blockchain Development
The Blockchain Academy

These resources provide a range of information about Ruby, blockchain, and related topics. You can use them to learn more about the basics of Ruby, how to use it to build web applications

Top comments (2)

Collapse
 
pedrogrande profile image
Pete Argent

Hi there. Just letting you know that all the links at the bottom are broken/dead.

Collapse
 
q9 profile image
Afri

And now p2p-networking :D