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.
This basic blockchain has a linked list that is composed of blocks. Each block has the following properties.
- Previous Hash
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.
mix phx.new ex_chain --no-html --no-ecto
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
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
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
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