This article describes how to use Python to build a small blockchain technology that is implemented with Python2 with less than 50 lines of code.
Although some think blockchain is a solution waiting for problems, there's no doubt that this novel technology is a Mar Vel of computing. But, what's exactly is a blockchain?
While some people think the blockchain itself still has a lot of problems to solve, there is no doubt that this novel technology is a marvel of the computer world. But what exactly is a blockchain?
Blockchain
A digital ledger in which transactions made in Bitcoin or another cryptocurrency is recorded Chrono Logically and publicly.
Blockchain a digital bookkeeping in which transactions are made in a chronological and publicly recorded manner in bitcoin or other encryption.
In terms, it's a public database where new data is stored in a container called a block and is Adde D to a immutable chain (hence blockchain) with data added in the past. In the case of Bitcoin and other cryptocurrencies, these data is groups of transactions. But, the data can is of any type, of course.
in layman's words, a blockchain is a public database in which newly generated data is stored in a container called a block and added to a chain of chunks with existing data. In the case of Bitcoin and other cryptocurrency, the data is a set of transactions. The data can also be of any type.
Blockchain technology have given rise to new, fully digital currencies like Bitcoin and litecoin that aren ' t issued or mana Ged by a central authority. This brings new freedom to individuals who believe that today's banking systems is a scam or subject to failure. Blockchain have also revolutionized distributed computing in the form of technologies like Ethereum, which have introduced I Nteresting concepts like smart contracts.
Blockchain technology has brought new, fully digitized currencies, such as Bitcoin and Wright coins, that are not issued or managed by central authorities, which gives the belief that today's banking system is a scam or a failure of the individual to bring in fresh freedom. Blockchain also revolutionized distributed computing in the form of technology like Ethereum, which introduces interesting concepts such as smart contracts.
In this article, I'll make a simple blockchain in less than lines of Python 2 code. It ' ll be called Snakecoin.
In this article, I'll make a simple blockchain in Python 2 code in less than 50 lines. This will be called Snakecoin.
We'll start by first defining, what's our blocks would look like. In blockchain, each block is stored with a timestamp and, optionally, an index. In Snakecoin, we ' re going to store both. Ensure integrity throughout the blockchain, each block would have a self-identifying hash. Like Bitcoin, each block ' s hash would be a cryptographic hash of the Block's index, timestamp, data, and the hash of the PR Evious block ' s hash. Oh, and the data can be anything want.
Let's start by defining what our chunks will look like. In a blockchain, each block is stored with a timestamp and an optional index. In Snakecoin, we will store both. And to help ensure the integrity of the entire block chain, each block will have the ability to self-identify the hash value. Like Bitcoin, each chunk will include the hash value of the index as a chunk, the timestamp, the data, and the hash value of the previous block. Oh, the data can be anything you want.
ImportHashlib as HasherclassBlock:def __init__(self, index, timestamp, data, Previous_hash): Self.index=Index Self.timestamp=timestamp Self.data=Data Self.previous_hash=Previous_hash Self.hash=Self.hash_block ()defHash_block (self): Sha=hasher.sha256 () sha.update (str (self.index)+Str (self.timestamp)+Str (self.data)+str (self.previous_hash))returnsha.hexdigest ()
awesome! We have the block structure, but we ' re creating a block chain. We need to start adding blocks to the actual chain. As I mentioned earlier, each block requires information from the previous block. But with this being said, a question Arises:how does the first block in the blockchain get there? Well, the first block, or Genesis block, is a special block. In many cases, it's added manually or has a unique logic allowing it to be added.
That's great! We've got a block structure, but we're creating a blockchain. We need to start adding chunks to the actual chain. As mentioned earlier, each block requires information from the previous block. But here's a question: How did the first chunk in the chunk come from? Well, the first chunk, or the Genesis block, is a special block. In many cases, it is either manually added or has the only logic allowed to add.
We ' ll create a function that simply returns a Genesis block to make things easy. This block is of index 0, and it had an arbitrary data value and a arbitrary value in the "previous hash" parameter.
to simplify, we will create a function that simply returns a creation block with an index of 0, which has any data value and any value in the "previous hash" parameter.
import datetime as date def Create_genesis_block (): # manually construct a block with # index zero and arbitrary previous hash return Block (0, Date.datetime.now (), " genesis Block ", " 0 ")
Now so we ' re able to create a Genesis block, we need a function that would generate succeeding blocks in the Blockchai N. This function would take the previous block in the chain as a parameter and create the data for the block to be generated, and return the new block with its appropriate data. When new blocks hashes information from previous blocks, the integrity of the blockchain increases with each new block. If we didn ' t do this, it would is easier for a outside party to "change the past" and replace our chain with an entirely New one of their own. This chain of hashes acts as cryptographic proof and helps ensure that once a block are added to the blockchain it cannot b e replaced or removed.
Now that we have created a Genesis block, we need a function to generate subsequent chunks in the blockchain. The function takes the previous chunk in the chain as an argument, creates the data for the chunk to be generated, and returns a new block with its corresponding data. The newly generated chunks store the hashes in the previous chunks, and the integrity of the blockchain increases with each new chunk. If we don't, others will easily tamper with the history and replace our chain with their new data. This Hashiki, as a cryptographic proof, helps ensure that once a new chunk is added to the blockchain, it cannot be replaced or deleted.
def Next_block (last_block): This_index = Last_block.index + 1 This_timestamp = Date.datetime.now () this_data = " hey! I ' m block " + str (this_index) This_hash = Last_block.hash return block ( This_index, This_timestamp, This_data, This_hash)
That's the majority of the hard work. Now, we can create our blockchain! In our case, the blockchain itself are a simple Python list. The first element of the list is the Genesis block. And of course, we need to add the succeeding blocks. Because Snakecoin is the tiniest blockchain, we'll only add new blocks. We can do the with a For loop.
This is the focus of the mission. Now we can create our blockchain! In our case, the blockchain itself is a simple python list. The first element of the list is the Genesis block. Of course, we need to add subsequent chunks. Because Snakecoin is the smallest blockchain, we only added 20 new blocks. We can do this with a for loop.
#Create The blockchain and add the Genesis blockBlockchain =[Create_genesis_block ()]previous_block=Blockchain[0]#How many blocks should we add to the chain#After the Genesis blockNum_of_blocks_to_add = 20#ADD blocks to the chain forIinchRange (0, Num_of_blocks_to_add): Block_to_add=Next_block (Previous_block) blockchain.append (block_to_add) Previous_block=Block_to_add#Tell everyone about it! Print "Block #{} have been added to the blockchain!". Format (block_to_add.index)Print "Hash: {}\n". Format (Block_to_add.hash)
Let's test what we've made so far.
Let's test what has been achieved.
There we go! Our blockchain works. If you want to see more information in the console, you could edit the complete source file and print each block ' s Timesta MP or data.
I'll go! Our blockchain is in effect! If you want to see more information in the console, you can edit the full source file and print the timestamp or data for each block.
That's about all that snakecoin have to offer. To make Snakecoin scale to the size of today's production blockchains, we ' d has to add more features like a server layer To track changes to the chain on multiple machines and a proof-of-work algorithm to limit the amount of blocks added in a Given time period.
That's all that Snakecoin has to offer. To reduce the size of the snakecoin to the current production block chain, we must add more features, such as the server layer, to track changes in the chain on multiple machines, and provide a working proof algorithm to limit the number of blocks in a given time period.
If you ' d like to get more technical, you can view the original Bitcoin whitepaper here. Best of luck and happy hacking!
If you want more technical details, you can view the original Bitcoin white paper. Good luck!
Thank very much for reading!
Thanks for reading!
English Original: Https://dev.to/aunyks/lets-build-the-tiniest-blockchain
Original translation: http://www.cnblogs.com/fangbei/
50 lines of Python code to build a small blockchain