Blockchain And How To Build One

in blockchain •  7 years ago 

What is a Blockchain?

Essentially, we can say, the Blockchain is a way we store information using cryptographic functions, linked data structure, and certain governance protocols in a decentralized manner. Basically, that’s it. But let’s break down this simple definition using the keywords visible.
1_qNrMEkp5VpsJAVVGeZPjxA.png

Cryptographic Functions


The blockchain uses cryptography to create hashes for data units on the blockchain. Hashes are unique long complex numbers. They are usually represented as hexadecimal generated from cryptographic algorithms. They work in a sense that when you put A in F(A). It returns X. If you do it for an infinite number of times, F(A) will still return X. Unless you change the contents of A and call it Aa. Then, when used the modified data inside F(Aa) produces a different result, for example, Y. With this, we can identify a particular piece of data as original and the other as fake. An example of such cryptographic function is the SHA-256 hash.

Linked Data Structure


Hashed information on the blockchain is stored in a linked manner. Such that each data block relies on the previous data block to create a new hash identity. Except the first data block called the genesis block. This is what we call a chain. Imagine hashed data organized in blocks. And linked together to produce a single axis called the blockchain. By this way, we realize a hereditary pattern. Because the hash identifier of a previous block added to the created block generates the hash identifier of the next data block. Hence we can witness the parent-child relationship between blocks.

Decentralized Manner


One important feature of the blockchain is how data decentralizes among the nodes. It means each node in the network has a copy of the blockchain and that enhances the security of the blockchain. Data syncs among the nodes and if for some reason, one node goes rogue, it is easy to identify and actions are taken. The more the nodes, the more secure the blockchain.

Let’s build one !


Now that we have a fair idea of what the blockchain is, why don’t we build our own blockchain solution for business use. I will use python to build. The source code can be found here. Note this part requires you to have basic programming concepts to grasp.

Case


Our business firm stores critical business documents that are vital to our clients. They cannot be tampered with. We want to take advantage of the security and distributed nature of a blockchain. This will keep our documents secure and away from loss and tampering. So we are building a new way of storing our documents on the blockchain. Which will make our storage immutable, distributed and practically uncrackable.

Design


Our blockchain is for private and business use. So it should be permission based. This means one has to be registered on the network to be able to add documents to the blockchain. Also, since we are building j small-scale solution and an experimental one. I will use very simple protocols to govern how the blockchain validates data added. I will use a protocol I came up with called the Block Limit Protocol to validate blocks. Block Limit Protocol maintains that documents in a single block should be less than 5 and shouldn’t be empty. Else the system will reject the block. Since it is permission-based, only registered users can create and submit new docs signed in their name.

The Code-
GitHub

Creating the blockchain

I have created a blueprint for creating our blockchain. On instantiation, the creator must register authors/users since it is permission-based. Also, we can give it a name. While we set by default the block size to 5 and create an empty chain. As well as empty docs buffer to hold current docs added to create a block. You can see that we create a genesis block and add to the chain right after we create the chain. Our genesis block is hashed but has no parent hash so we give it a value of 1(this means we don’t have a parent hash)

Creating hashes for docs and blocks

We need to define a static method named hash_it() that we will use to hash our blocks and documents to be able to link them. Remember hashes help us identify the true identity of these items. This helps us to know that our items have tampered or not. We dwell on python’s hashing library to draw in the sha256 hashing function. This is what the Bitcoin blockchain uses too.

Creating and adding a document

This part is where we define how we create our document. So a document is of a timestamp, author, and content, and a doc item is of the document and the document hash. We store all this information in a dictionary format. For a document to be valid, it needs to be signed by a registered user.
So we define the create_doc() add_doc() is_valid_doc() methods to deal with this for us.

Creating and adding a block

Here, we create functions that deal with collecting docs from the current docs pool. If the block passes the Block Limit Protocol, it gets added to the chain. The added block contains the hash of the previous block and also its own hash, index, and collection of docs in the block. So here we see the connection between all blocks in the chain down to the genesis block.

Interacting with our Blockchain

Initialize Blockchain with new users
BasicX = Blockchain(name="BasicX", authors=["Kwaku", "Ama"])

create 3 documents and add them to the pool
BasicX.add_doc(BasicX.create_doc("Kwaku", "The days of Ra"))
BasicX.add_doc(BasicX.create_doc("Ama", "20012 rock ages"))
BasicX.add_doc(BasicX.create_doc("Ama", "Freedom for all"))

Create a new block and add to the blockchain
BasicX.add_block(Mufasa.create_block())

See contents of the Blockchain
print(BasicX.chain)

Create more documents and add them to the blockchain. Try to break the protocol and see what happens. Also, try changing document contents in the blockchain. Witness that the chain will break and deemed invalid because the hashes will change. A good way to interact with the blockchain is to build API endpoints to access the methods on the blockchain. Security is more strengthened when we create Consensus protocol. Where by we add more nodes and sync the data between them so that. When data on one node changes or goes rogue , it will be easy to track and verify, but we can do that later ;) .

Source

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @jasonkwm! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

I like this. Need to read through in more detail later.