Blockchain Programming Fundamentals
Ritu Sharma
I Promise Small businesses to book 50+ appointments/month for High-Value Services with a 100% guarantee through Website Development, Mobile App Development and Digital Marketing.
In order to understand Blockchain deeply, let us first talk about the concept of a Digital Signature or a Hash.
Digital Signature is basically a function that takes a string as input and returns a fixed-size alphanumeric string. The output string is known as the?Digital Signature?or the Hash of the input message. The important point to note here is that the function via which we obtain the Digital Signature is “irreversible” in that given an input string, it can compute the Hash. However, given the Hash, it is virtually impossible to compute the input string. Further, it is also virtually impossible to find 2 values that have the same Hash.
hash1 = Hash(input1)
hash2 = Hash(input2)
Here, what we are essentially trying to say is the following:
Such Hashing functions are carefully designed by cryptographers after years of research. Most programming languages have a built-in library function to compute the Hash of a particular input string.
Why are we talking about the Hash function?
Well, Blockchain as a concept relies heavily on Hashing. The idea is that in a Blockchain, we have an ordered chain of blocks such that each block contains the following information:
Let us take an example. Consider the following simple block: [0, “X paid $100 to Y”, 91b452].
Here, since this is the first block of the Blockchain, the Hash of the previous block is 0. The list of transactions contains just 1 transaction - X paid $100 to Y. The Hash of itself is computed by the following way:
hash_itself = Hash(List of transactions, Hash of the previous block)
Basically, we combine the List of transactions and the Hash of the previous block as a single input string and feed it to the Hash function to get the hash_itself value.
Such blocks where the Hash of the previous block is 0 are termed as Generic Blocks. A Genesis block is basically the very first block in a Blockchain.
Now, suppose we want to add some more blocks to this Blockchain. Let us have block1 = [91b452, “Y paid $20 to Z, X paid $10 to P”, 8ab32k].
Here, 91b452 is nothing but the Hash of the previous block (the Genesis block). There are 2 transactions:
Finally, we have the hash_itself value which is basically Hash(“Y paid $20 to Z, X paid $10 to P”, 91b452). This turns out to be 8ab32k.
What’s so special about this “data structure”?
Well, the idea is that if suppose someone were to mutilate the Blockchain by say altering the transaction in the Genesis Block - changing “X paid $100 to Y” to “Y paid $100 to X”, this will change the hash value of 91b452. As a result, there will be a mismatch in the value of this hash in block1 (remember, the first value of each block is the hash value of its parent block). As a result, the chain becomes invalid. This effectively holds for each block in the Blockchain because as soon as we modify a block, the hashes of all subsequent blocks become invalid and so, the chain collapses. Therefore Blockchain provides a high level of data security.
Blockchain Programming Code Example
From the above picture of Blockchain, it is clear that we can code it in pretty much any programming language. For instance, the above concept can be implemented in C++, Python, Java and even JavaScript. Let us take a look at a sample Python code:
# A block is stored as a tuple of# (parent_hash, transactions, hash_itself)
领英推荐
def get_parent_hash(block): return block[0]
def get_transactions(block): return block[1]
def get_hash_itself(block): return block[2]
# function to create a block in a blockchaindef create_block(transactions, parent_hash): hash_itself = hash((transactions, parent_hash)) return (parent_hash, transactions, hash_itself)
# function to create the genesis blockdef create_genesis_block(transactions): return create_block(transactions, 0)
# we create our genesis blockgenesis_block = create_genesis_block("X paid $100 to Y")
# print the hash of the genesis_blockgenesis_block_hash = get_hash_itself(genesis_block)print "genesis_block_hash:", genesis_block_hash
# create another blockblock1 = create_block("Y paid $20 to Z, X paid $10 to P", genesis_block_hash)
# print the hash of block1block1_hash = get_hash_itself(block1)print "block1_hash:", block1_hash
snippet1.py? hosted with ? by?GitHub
Now suppose, if we were to mutilate the genesis_block
genesis_block[1] = "Y paid $100 to X"genesis_block_hash = get_hash_itself(genesis_block)
print "genesis_block_hash:", genesis_block_hashprint "block1_parent_hash:", get_parent_hash(block1)
The output will be as follows:
genesis_block_hash: 3495953456182427352block1_hash: -554281952046316805genesis_block_hash: 3220110016770526666block1_parent_hash: 3495953456182427352
Here, the value of genesis_block_hash and block1_parent_hash are clearly different while they should actually be the same in the correct Blockchain. As a result, the Blockchain is now corrupted.
Summary
Think of Blockchain as a distributed and secured data structure that can be used in places where?no?middlemen are involved. The decentralised nature of Blockchain is what helps in removing the middlemen and it comes from the above immutability of Blockchain. It is an interesting data structure and as we all have seen cryptocurrency is a real-life implementation of it.
Engineer
2 年?????? good app.
Great information and very interesting too !
--
2 年Hi ritu