The basic concept of blockchain is simple: a distributed database that stores an ever-growing list,list contains many sequential records. The following article mainly introduces you to the Python learning of the blockchain of the relevant information, the text through the sample code introduced in very detailed, the needs of the friends below to see together.
Objective
This article will give you a brief introduction to the Blockchain (BlockChain) of the relevant knowledge, and Python to do a simple implementation. The following words do not say, come together to see the detailed introduction:
What is a blockchain
Simply put, a blockchain is a permanent, non-reversible record of the generation of encrypted data (chunks) that are superimposed (chained) in chronological order. Specifically, the blockchain is made up of a string of data blocks that are generated using a cryptographic method, each containing a hash of the previous chunk (hash), starting from the founding block (Genesis Block) and connecting to the current block, forming a block chain. Each chunk is guaranteed to occur after the previous chunk in chronological order, otherwise the hash value of the previous chunk is unknown. It is an important concept of Bitcoin.
Characteristics
Blockchain has the following characteristics:
Decentralized: The blockchain does not depend on a central node, but rather on the distributed nodes.
No need to trust the system: The blockchain is based on cryptography algorithms, the data need to be approved by other users in the network, so there is not a set of third-party intermediary structure or trust agency endorsement.
Non-tamper and encrypted security: The blockchain takes a one-way hashing algorithm, and each new block is pushed in a linear sequence, and the irreversible nature of the time causes any attempt to tamper with the data information within the blockchain to be easily traced, leading to rejection by other nodes, which can limit the related wrongful behavior.
These features make blockchain more and more widely used in many fields such as banking, securities market and finance.
How Blockchain works
Blockchain a series of encrypted data blocks. These chunks consist of a chunk header containing metadata and a long list of transactions immediately following the constituent block body. The block structure in Bitcoin is as follows:
Block size
The block size contains information such as connection information, timestamps, and nonce in other chunks in the blockchain, as follows:
Block identifiers
Chunks have two identifiers, one is the hash value of the chunk header, and the other is the block height. The hash value of the chunk header is the number obtained by hashing the chunk header two times with the SHA256 algorithm. A chunk hash can uniquely and unambiguously identify a chunk, and any node can obtain the chunk hash independently by simply hashing the chunk header. Block height refers to the location of the chunk in the blockchain. Block height is not a unique identifier. Although a single block will always have a definite, fixed block height, but the reverse is not true, a block height does not always identify a single block. Two or more blocks may have the same block height and compete for the same position in the blockchain.
After understanding the above foundation, we begin to implement a simple blockchain with python.
Python implementation of Blockchain
I. Defining the BLOCK structure
In [16]:
# block.pyimport hashlibimport Uuidclass Block (object): Def __init__ (self, data=none, previous_ Hash=none): Self.identifier = Uuid.uuid4 (). Hex # Generates a unique flag self.nonce = None # nonce Value self.data = data # chunk content SE Lf.previous_hash = previous_hash # parent hash value def hash (self, Nonce=none): "' computes the hash value of the chunk ' ' message = hashlib.sha256 () m Essage.update (Self.identifier.encode (' Utf-8 ')) message.update (str (nonce). Encode (' Utf-8 ')) Message.update (str ( Self.data). Encode (' Utf-8 ')) message.update (str (self.previous_hash). Encode (' Utf-8 ')) return Message.hexdigest () def Hash_is_valid (self, The_hash): ' Verify chunk hash value is valid ' ' Return the_hash.startswith (' 0000 ') def __repr__ (self): return ' Blo Ck