In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use python code to achieve block chain". In daily operation, I believe many people have doubts about how to use python code to achieve block chain. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to use python code to achieve block chain". Next, please follow the editor to study!
Block chain
Blockchain is a public digital ledger that records transactions in bitcoin or other cryptocurrencies in chronological order.
More generally, a blockchain is a public database, the new data will be stored in a container called "block", and then the block will be added to an immutable chain, so it is called "blockchain". When we talk about bitcoin or other cryptocurrencies, these data refer to transactions. Of course, you can store any type of data in the block chain.
Blockchain technology has spawned new, fully digital currencies, such as Bitcoin and Lettercoin, which are not issued or managed by the central government. This technology brings new freedom to those who do not approve of the current banking system. Blockchain also brings revolutionary innovation to distributed computing. For example, Etaifang blockchain introduces some interesting concepts, such as intelligent contracts.
In this article, I will use less than 50 lines of Python 2 code to make a simple blockchain. I call it SnakeCoin.
We will first define the data structure of the "block". In the blockchain, each block stores a timestamp and an optional index. In SnakeCoin, we will store both. To ensure the integrity of the entire block chain, each block has a hash for self-identification. Like Bitcoin, the hash of each block will be the encrypted hash value calculated for the block index, timestamp, data, and previous block hash. In which you can save anything in the data.
Import hashlib as hasherclass Block: 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 () def hash_block (self): sha = hasher.sha256 () sha.update (str (self.index) + str (self.timestamp) + str (self.data) + str (self.previous_hash)) return sha.hexdigest ()
great! Now we have a data structure for blocks, but our goal is to implement a chain of blocks, so we need to add blocks to a chain. As mentioned earlier, each block requires information about the previous block. But according to this statement, there is a question, how to add the first block of the blockchain? Well, the first block in the chain, or Genesis Block, is a special block. In many cases, you need to add manually or use separate processing logic.
Next, you will create a function that simply returns a creation block. The index of this block is 0:
Import datetime as datedef create_genesis_block (): # Manually construct a block with # index zero and arbitrary previous hash return Block (0, date.datetime.now (), "Genesis Block", "0")
Now that you have created the creation block, you need a function to generate subsequent blocks in the block chain. This function will generate and return a new block with the previous block in the chain as an argument. When the hash value of the new block is calculated using the information from the previous block, the integrity of the block chain increases with each new block. If not, it is easier for external organizations to "change the past" and replace the blocks in the chain with their forged blocks. This series of block hashes can be used as evidence of encryption to help ensure that once a block 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! Imam block" + str (this_index) this_hash = last_block.hash return Block (this_index, this_timestamp, this_data, this_hash)
Most of the important work has been done, and now you can create the blockchain! In our implementation, the blockchain is a simple Python list. The first element of the list is the Genesis Block. Of course, we also need to add subsequent blocks. Because SnakeCoin is arguably the world's most mini blockchain, we only add 20 new blocks here. You can use a for loop to generate a new block:
# 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 = 2percent Add blocks to the chainfor i in range (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 # {} has been added to the blockchain!" .format (block_to_add.index) print "Hash: {}\ n" .format (block_to_add.hash)
Let's test our blockchain.
At this point, the study on "how to use python code to achieve block chain" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.