[howto] A blockchain from scratch | Block and Blockchain

in #cryptocurrency6 years ago

Hey Folks, 

Today we will do some really important stuff for a blockchain.

  1. We will put some new lines on our Block.h class to make it a real block
  2. We will create a Blockchain.h to fit all blocks together to a chain

Finish the block class

To fit the blocks to our needs we need some last lines.
As you can see i added the public and private sections in our block class.

At the public section, i added a constructor, a function to return the hash of the actual block and a function to mine the block. Don't worry, we will fill the functions with code later on. And i also added the hash of the previous block on the public section.

In the private section we find the index, timestamp, data and the hash of the actual block. And a function to calculate the hash of the block. We declare it as const, so it will not accidental change the actual block.

With this lines, we finished our block class and can now start to talk about a chain.

Creating a blockchain

So what did our chain class need? First of all a file =D Create a new file and call it Blockchain.h.

By the way, i created a folder called PigChain and in this folder a created a folder called src and in this folder i created the to file and will create all future files in.

Ok back to topic. What did our blockchain class need?

  1. A constructor to create itself
  2. A function to add a new block
  3. A Difficulty
  4. A variable to hold the the chain in
  5. A function to return the last block

Ok lets type the following in:

Ok thats it. In our case the chain variable is a vector, because it is easy to use and it is nearly a chain and we can iterate through it. Everything else should be self explained.

So i think we got it for now. On the next tutorial we will fill the classes with code and start to fill up our chain with life.

So stay tuned and interested =D