Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build the smallest blockchain with Python code

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to use Python code to build the smallest block chain, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Although some people think that blockchain is a solution to the waiting problem, there is no doubt that this new technology is a computer miracle. But what exactly is blockchain?

Block chain

It is a digital ledger for transactions in Bitcoin or other cryptocurrencies, which are recorded in chronological order and made public.

In more general terms, it is a public database where new data is stored in a container called blocks and is added to an immutable chain (later block chain) to add past data. In the case of bitcoin and other cryptocurrencies, this data is a set of transactions. Of course, the data can be of any type.

Blockchain technology has spawned new, fully digital currencies, such as Bitcoin and Lettercoin, which are not issued or managed by the central government. So it brings new freedom to those who think that today's banking system is a hoax or will eventually fail. The ethernet square technology contained in the blockchain revolutionizes and innovates distributed computing, introducing some interesting concepts, such as intelligent contracts.

In this article, I will use less than 50 lines of Python2 code to make a simple blockchain. I call it SnakeCoin.

The first step is to define what the block will look like. In the blockchain, each block stores a timestamp and an index. In SnakeCoin, you need to store both. In order to ensure the integrity of the entire block chain, each block has an automatic recognition hash. Like Bitcoin, the hash of each block will be the encrypted hash of the block index, timestamp, data, and previous block hash. Data can be anything you want.

Import hashlib as hasher class 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 ()

There is a block structure after this step, but now you are creating a block chain, so you need to add blocks to the actual chain. As mentioned earlier, each block requires information about the previous block. But according to this argument, there is a question: how do the blocks of the blockchain get there? It has to be said that the * block, or the origin block, is a special block. In many cases, it is added manually or has unique logic to allow it to be added.

Next, you will create a function that simply returns an origin block to generate * blocks. This block is index 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 that you have created the origin block, you need a function to generate subsequent blocks in the block chain. This function takes the previous block in the chain as an argument, creates the data for the block to be generated, and returns the new block with the appropriate data. When the new block hash information comes from the previous block, the integrity of the block chain increases with each new block. If not, it will be easier for external organizations to "change the past" and replace existing chains in entirely new ways. This series of hashes can be used as evidence of encryption to help ensure that once a block is added to a 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 work is done, and now you can create the blockchain. In this example, the blockchain itself is a simple Python list. The * * elements of the list are the origin blocks. Of course, subsequent blocks need to be added, because SnakeCoin is the smallest chunk chain, and only 20 new blocks are added here. You can use for loops to generate new blocks.

# Create the blockchain and add the genesis block blockchain = [create_genesis_block ()] previous_block = blockchain [0] # How many blocks should we add to the chain # after the genesis block num_of_blocks_to_add = 20 # Add blocks to the chain for 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 the currently generated blockchain.

See, this is the blockchain. If you want to see more information in the console, you can edit the complete source file and print the timestamp or data for each block.

That's all SnakeCoin has to offer. To bring SnakeCoin to the scale of today's production blockchain, more features, such as server layers, must be added to track chain changes on multiple machines and working algorithms that limit the number of blocks added over a given period of time.

After reading the above, have you learned how to build the smallest blockchain in Python code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report