Building a minimal block chain with Python

Source: Internet
Author: User

(Click on the public number above to be quick attention)


Compiling: Linux China/xingyu.wang English: Gerald Nash

Https://linux.cn/article-9453-1.html


While some people think that block chains are a solution to problems sooner or later, there is no doubt that this innovative technology is a miracle of computer technology. So, what exactly is a block chain? block Chain

A digital ledger that records transactions publicly in chronological order in Bitcoin (Bitcoin) or other encrypted currency.

In a more popular sense, it is a public database in which new data is stored in a container called block, and is added to an immutable chain (chain) (so called a block chain (blockchain)), and the previously added data is also in the chain. For Bitcoin or other encrypted currencies, the data is a group of transactions, but it can be any other type of data.

Block-chain technology brings new, fully digitized currencies, such as Bitcoin and Litecoin, which are not managed by any central institution. This gives freedom to those who think that today's banking system is a hoax and will eventually fail. The block chain also revolutionized the technology of distributed computing, such as the Ethereum, which introduced an interesting concept: Intelligent contract (smart contract).

In this article, I'm going to implement a simple block chain with less than 50 lines of Python 2.x code, which I call Snakecoin. block chain with less than 50 lines of code

We will begin by defining what our block is. In a block chain, each chunk is stored with a timestamp and an optional index. In Snakecoin, we store both. To ensure the integrity of the entire block chain, each chunk will have a recognized hash value. In Bitcoin, the hash of each chunk is a cryptographic hash of the data such as the index, timestamp, data, and hash value of the previous block. The "data" mentioned here can be any data 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) +

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.