Take your hand and make your own ' Bitcoin ' (on)

Source: Internet
Author: User
Tags python list

This article I use Python to implement a local block, to achieve a complete block chain also need to do a distributed server, a lot of things. First, the transactions are stored in chronological order, and the transaction of the bits and other encrypted currencies that are traded using the block chain are saved in time and are publicly stored. In layman's parlance, a block chain is a public database in which new data is stored in a container called a block and added to an immutable chain (hence a block chain) with previously added data. In the case of Bitcoin and other encrypted currencies, the data is a set of transactions, and of course, the data can be of any type. For example, a block chain of 2.0 of the etheric square, can be built on the chain application, not just a pure digital currency encryption. In this article, each block has a timestamp and an optional index, and in Snakecoin, both are stored, and each block will have a self identifiable hash to help ensure the integrity of the entire block chain. Like Bitcoin, the hash of each block will be the index of the block, the timestamp, the data, and the hashed hash of the previous block.

ImportHashlib asHasherImportDatetime asDateclassBlock: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). Encode (' utf-8 ') + St R (Self.timestamp). Encode (' utf-8 ') + str (self.data). Encode (' utf-8 ') + str (Self.previo Us_hash). Encode (' Utf-8 ')) returnSha.hexdigest ()def__str__ (self): output = str (self.index) + "/" + \ str (self.timestamp) + "/" + \ STR (self.data) + "/" + \ str (self.previous_hash[:10]) returnOutput

Now we have a block structure, but we're creating a block chain that needs to be added to the actual chain. As mentioned earlier, each block requires information about the previous block. In other words, there is a problem: how the first block in the block gets there. So the first block, or the origin block, is a special block. In many cases, it is manually added or has a unique logical value that is allowed to be added.

We will create a function to return a block of origin, making things easier. The block has an index of 0, which has arbitrary data values and arbitrary values in the previous hash argument.

Def create_genesis_block ():
    #手动构造带有索引零和任意先前散列的块 return blocks
    (0, Date.datetime.now (), "Genesis Block", "0")

Now we can create a block of origin, and we need a function to generate a subsequent block in the block chain. The function takes the previous block in the chain as a parameter, creates the data for the block to be generated, and returns a new block with its corresponding data. When the new block gets the hash information in the previous block, the integrity of the block chain increases with each new block. If we do not do this, the external information will be more likely to "change the past" and replace our chain with our own updated changes. This Hashiki is used as a cryptographic proof to help ensure that once a block is added to the block chain, 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)
    Previous_hash = Last_block.hash return block
    (This_index, This_timestamp, This_ Data, Previous_hash)

The above is the necessary work. Now we can create our blockchain. In our example, the blockchain itself is a simple python list. The first element of the list is the Origin block. Of course, we need to add the following blocks. Because Cchcoin is the smallest block, we add only 20 new blocks. We can do this with a for loop.

# Create block chains and add creation blocks
blockchain = [Create_genesis_block ()]
previous_block = blockchain[0]

# How many pieces do we need
# Number of blocks behind the Genesis block
Num_of_blocks_to_add =

# 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

    Print (" Block #{} has been added to the blockchain!. Format (Block_to_add.index)) print (
    block_to_add) print (
    "Hash: {}\n". Format (Block_to_add.hash))

Run Result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.