Python blockchain coin digging
In 2009, bencong invented bitcoin, and set that there are only 21 million bitcoins. By participating in the proction of blocks and providing proof of work (POW), he can get the reward of bitcoin network. This process is mining
the concept of "mining" comes from the existing concepts in our real economic life, such as gold mining and silver mining. Because minerals are valuable, it drives people to pay labor to dig
another important point of bitcoin mining is that the miners who participate in mining recognize the value of bitcoin, and some of them are willing to spend money in the bitcoin market. Therefore, the mining of bitcoin is meaningful
extended data
currency characteristics of bitcoin
1, decentralized
bitcoin is the first distributed virtual currency, and the whole network is composed of users without central bank. Decentralization is the guarantee of bitcoin's security and freedom
2, bitcoin can be managed on any computer connected to the Internet. No matter where you are, anyone can dig, buy, sell or collect bitcoin
3, exclusive ownership
controlling bitcoin requires a private key, which can be stored in any storage medium in isolation. No one can get it except the user himself
4, low transaction cost
bitcoin can be remitted free of charge, but in the end, about 1 bitfen transaction fee will be charged for each transaction to ensure faster transaction execution
5, no hidden cost
as a means of payment from a to B, bitcoin has no cumbersome limit of quota and proceres. If you know the other party's bitcoin address, you can pay
6, cross platform mining
users can explore the computing power of different hardware on many platforms
features:
blockchain is an immutable and orderly chain record composed of block records. The main features are as follows:
1: decentralization
e to the use of Distributed Accounting and storage, there is no centralized hardware or management organization, the rights and obligations of any node are equal, and the data blocks in the system are jointly maintained by the nodes with maintenance function in the whole system. Thanks to the decentralization of blockchain, bitcoin also has the characteristics of decentralization
2: openness
the system is open, in addition to the private information of the parties to the transaction is encrypted, the data of the blockchain is open to all, and anyone can query the blockchain data and develop related applications through the open interface, so the information of the whole system is highly transparent
3: autonomy
blockchain adopts consensus based specifications and Protocols (such as a set of open and transparent algorithms) to enable all nodes in the whole system to exchange data freely and safely in a de trusted environment, so that the trust in "people" is changed into the trust in machines, and any human intervention does not work
4: information cannot be tampered with
once the information is verified and added to the blockchain, it will be stored permanently. Unless more than 51% of the nodes in the system can be controlled at the same time, the modification of the database on a single node is invalid, so the data stability and reliability of the blockchain are extremely high
5: anonymity
since the exchange between nodes follows a fixed algorithm, their data interaction does not need to be trusted (the program rules in the blockchain will judge whether the activity is effective by themselves), so the counterparties do not need to make the other party trust themselves by disclosing their identities, which is very helpful for credit accumulation
compared with the traditional distributed database, there are two main differences:
1: the traditional distributed database supports adding, deleting, checking and modifying, while the blockchain only supports searching and inserting, and blocks cannot be deleted or modified
2: the traditional distributed database is generally a master-slave structure: master and slaves structure. In order to ensure high availability, it is realized by standby master, while the blockchain is a decentralized database. There is no master-slave structure
blockchain and bitcoin:
when it comes to blockchain, most people talk about bitcoin. However, blockchain is not equal to bitcoin. It is now the era of blockchain 3.0, and bitcoin is only the proct of blockchain 1.0
the evolution mode of blockchain is:
& ᦇ 9642; Blockchain 1.0 digital currency; Blockchain 2.0 digital assets and smart contracts; Blockchain 3.0 - Implementation of distributed applications in various instries
classification of blockchain:
public blockchains
public blockchain means that any indivial or group in the world can send transactions, and transactions can be effectively confirmed by the blockchain, and anyone can participate in its consensus process. Public blockchain is the earliest and the most widely used blockchain. The virtual digital currencies of bitcoin series are all based on public blockchain. There is only one blockchain corresponding to this currency in the world
consortium blockchains
instry blockchains: multiple preselected nodes are designated as bookkeepers within a certain group, and the generation of each block is jointly determined by all preselected nodes (preselected nodes participate in the consensus process). Other access nodes can participate in transactions, but not the bookkeeping process (essentially, trusteeship bookkeeping), It just becomes distributed accounting, the number of preselected nodes, and how to determine the bookkeeper of each block to become the main risk point of the blockchain). Anyone else can conct limited query through the open API of the blockchain
private blockchain: it only uses the general ledger technology of blockchain for bookkeeping. It can be a company or an indivial, enjoying the write permission of the blockchain. This blockchain is not very different from other distributed storage schemes Dec2015) conservative giants (traditional finance) want to experiment with private blockchain, while the application of public chain, such as bitcoin, has been instrialized, and the application procts of private chain are still groping.
Can be
blockchain to achieve a simple description of the principle of
blockchain technology as a digital accounting technology, its core is to save the transaction data block, in the form of encryption, according to the order of time chain record. Blockchain itself is a public database. The system stores the new business data in a container called block, and adds the block to the chain composed of existing blocks. It's a bit like a snake. The more you eat, the longer the snake's body; In the application scenario of bitcoin, the data is a set of transfer transaction records. In the application scenario of bike sharing, these data can be the transaction records of car borrowing and returning
The simple implementation code ofblockchain
in the above code, the data structure stored in the core of blockchain is list, which can be accessed through
new_ New blocks generated by block () are constantly added to the end of the blockchain. The hash value of each block contains all the data information of the block. In the process of calculating the hash
value, the hash
value of the previous block needs to be referenced, so tamper proof is realized. The biggest value of blockchain database is this highly tamper proof trusted computing. This has been successfully demonstrated in our simple blockchain implementation. In commercial level blockchain applications, the process of creating new blocks is called smart contract, and blockchain is growing through smart contract
the following is the running result of the code, which is different at different times
running results
< img / >
is a computer language. Blockchain is a decentralized database protocol without a center. Most blockchains are developed with C + + (because of performance). There is no direct relationship between Python and blockchain, and few people use Python to develop blockchain.
1 import hashlib as hasher
2 import datetime as date
3
4 # Define what a Snakecoin block is
5 class Block:
6 def __init__(self, index, timestamp, data, previous_hash):
7 self.index = index
8 self.timestamp = timestamp
9 self.data = data
10 self.previous_hash = previous_hash
11 self.hash = self.hash_block()
12
13 def hash_block(self):
14 sha = hasher.sha256()
15 sha.update(str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash))
16 return sha.hexdigest()
17
18 # Generate genesis block
19 def create_genesis_block():
20 # Manually construct a block with
21 # index zero and arbitrary previous hash
22 return Block(0, date.datetime.now(), "Genesis Block", "0")
23
24 # Generate all later blocks in the blockchain
25 def next_block(last_block):
26 this_index = last_block.index + 1
27 this_timestamp = date.datetime.now()
28 this_data = "Hey! I'm block " + str(this_index)
29 this_hash = last_block.hash
30 return Block(this_index, this_timestamp, this_data, this_hash)
31
32 # Create the blockchain and add the genesis block
33 blockchain = [create_genesis_block()]
34 previous_block = blockchain[0]
35
36 # How many blocks should we add to the chain
37 # after the genesis block
38 num_of_blocks_to_add = 20
39
40 # Add blocks to the chain
41 for i in range(0, num_of_blocks_to_add):
42 block_to_add = next_block(previous_block)
43 blockchain.append(block_to_add)
44 previous_block = block_to_add
45 # Tell everyone about it!
46 print "Block #{} has been added to the blockchain!".format(block_to_add.index)
47 print "Hash: {}\n".format(block_to_add.hash)