Create a chunk chain from scratch with Python

Source: Internet
Author: User

The author thinks that the quickest way to learn a block chain is to create one by yourself, and this article follows the author using Python to create a block chain.

We are curious about the rise of the digital currency and want to know the technology behind it-how the block chain is achieved.

But fully understand the block chain is not easy, I like to learn in practice, by writing code to learn technology will be more firmly mastered. By building a block chain can deepen the understanding of the block chain.

Preparatory work
This article requires readers to have a basic understanding of Python, read and write basic python, and need to have a basic understanding of HTTP requests.

We know that a block chain is an immutable, orderly chain of records made up of chunks, records can be transactions, files, or any data you want, and it is important that they are linked by a hash value (hashes).

If you are not quite familiar with hashing, you can view this article

Environmental preparedness
Environmental readiness to ensure that python3.6+, Pip, flask, requests have been installed
Installation method:

Pip Install flask==0.12.2 requests==2.18.4

You also need an HTTP client, such as a postman,curl or other client.

Reference source code (the original code in my translation, can not run, I fork a copy, fixed the error, and added a translator, thank star)

Start creating blockchain
Create a new file blockchain.py, all the code in this article is written in this file, you can refer to the source code at any time

Blockchain class
First, you create a blockchain class that creates two lists in the constructor, one for storing block chains, and one for storing transactions.

The following is the framework for the Blockchain class:

Class Blockchain (object):
def __init__ (self):
Self.chain = []
Self.current_transactions = []
def new_block (self):
# Creates a new block and adds it to the chain
Pass
def new_transaction (self):
# Adds A new transaction to the list of transactions
Pass
@staticmethod
def hash (block):
# Hashes a block
Pass
@property
def last_block (self):
# Returns The last blocks in the chain
Pass

The blockchain class is used to manage the chain, it can store transactions, add new blocks, and so on, let's further refine these methods.

Block structure
Each chunk contains attributes: Index, UNIX timestamp (timestamp), transaction list (transactions), work proof (explained later), and hash value of the previous block.

The following is the structure of a block:

Block = {
' Index ': 1,
' Timestamp ': 1506057125.900785,
' Transactions ': [
{
' Sender ': "8527147fe1f5426f9dd545de4b27ee00",
' Recipient ': "a77f5cdfa2934df3954a5c7c7da5df1f",
' Amount ': 5,
}
],
' Proof ': 324984774000,
' Previous_hash ': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}

Here, the concept of the block chain is clear, each new block contains the hash of the previous block, which is the key point, it protects the block chain is not denatured. If an attacker destroys one of the previous blocks, then the hash of all the blocks that follow will become incorrect. Do not understand the words, slowly digestion, can refer to block chain accounting principle

Join trade
Next we need to add a deal to refine the New_transaction method

 class Blockchain (object): 
   .
   def new_transaction (self, sender, recipient, amount):
        ""
  &nbs P     Generate new transaction information, the information will be added to the next block to be dug
Related Article

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.