(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) +