Image for post
Image for post

How to build a blockchain in Elixir part-1

coding guy
Sep 12 · 3 min read

Blockchain is a technology that allows data to be stored and exchanged on a peer-to-peer (P2P) basis. Structurally, blockchain data can be consulted, shared, and secured thanks to consensus-based algorithms. It is used in a decentralized manner and removes the need for intermediaries, or “trusted third parties”

In simple words, Blockchain is a database. What is a database? A database is an organized collection of data. Or, you can say, a data structure that stores the data. Therefore, Blockchain is just a data structure that stores the data. As the name hinted, there will be a chain of blocks.

Image for post
Image for post

This basic blockchain has a linked list that is composed of blocks. Each block has the following properties.

  • Index
  • Timestamp
  • Previous Hash
  • Hash
  • Data

Today we are going to build a blockchain in Elixir. We will build everything whatever a basic blockchain provides, concepts are simple and I’m assuming that you are a little familiar with the elixir and blockchain concepts, but even if you are not then I’ll try to explain every detail about blockchain.

We will build blocks, hash, wallets, consensus step by step so in this part we will build a blockchain with having a chain of blocks. Then in later parts, we will see how we can build transactions, mining, and other blockchain-related stuff.

So let’s set up the project quickly and start doing it quickly.

This will initialize our application and let’s go into the project and start navigating.

now let’s create a new file lib/blockchain/block.ex this will contain the block related functions and logic.

let’s build the struct for the block, so our block will be having 4 basic things as of now then slowly we will keep adding new stuff as we proceed further.

so let’s say that in a very basic block in a blockchain we have timestamp, hash of block, block data and previous block hash . So let’s add it to the block.

so this will give us a basic block struct. which has details about the block.

let’s add some basic functions into the block.exby which we can create new blocks and a genesis block (Genesis Block is the first block in any blockchain), Genesis block doesn’t have any previous hash in it so we will leave the previous hash field empty.

So let’s add some tests first then we will implement the functions to pass those tests. create a file in test/blockchain/block_test.exs

Now run mix test both tests will fail so let’s write the functions so that it can pass.

Let’s modify the lib/blockchain/block.ex with the following functions.

Here we have created 2 new functions in which are new/4 and genesis/0 and now our tests should pass.

our next task is to create the mine function and adding the hash functions so that our data which we have passed can be validated and we can build the foundations of the hash.

Now we will add the function hash/3 which will accept timestamp, last_hash and data so that we can get the current block hash. So let’s modify the tests and functions.

Here we have modified the existing functions to generate block hash from the new/3 function and return us back the fresh %Block{} struct.

We have added a mine_block/2 the function which mines the new blocks when we give the last_hash so it sets the params accordingly and creates the new blocks.

Let’s stop this part over here and in the next part, we will create the blockchain from these blocks.

Here is the link for the second post link

Image for post
Image for post

Coinmonks is a non-profit Crypto educational publication.

Sign up for Coinmonks

By Coinmonks

A newsletter that brings you week's best crypto and blockchain stories and trending news directly in your inbox, by Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

coding guy

Written by



coding guy

Written by

I write code


Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project —

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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