python创建简单区块链

 1 import time
 2 import hashlib
 3 import json
 4 
 5 #首先创建一个Blockchain类,在构造函数中创建两个列表,一个用于存储区块链,一个用于存储交易。
 6 class BlockChain(object):
 7     def __init__(self):
 8         self.chain=[]
 9         self.current_transactions=[]
10         self.new_block(proof=100,previous_hash=1)
11     def new_block(self,proof,previous_hash=None):   #创建一个新的块并且增加到链中
12         block={
13             'index':len(self.chain) + 1,
14             'time':time.time(),
15             'teansaction':self.current_transactions,
16             'proof':proof,
17             'previous_hash':previous_hash
18         }
19         pass
20     
21     def new_transactions(self,sender,recipient,amount): #创建一个新的交易到交易列表
22         self.current_transactions.append({
23             'sender':sender,
24             'recipient':recipient,
25             'amount':amount
26         })
27 
28 
29     @staticmethod
30     def hash(block):
31         pass
32 
33     @property
34     def last_bolck(self):
35         pass        #返回最后一个区块到链中
36 
37 #每个区块包含属性:索引(index),Unix时间戳(timestamp),交易列表(transaction),工作量证明()以及前一个区块的hash值

未完待续………………

猜你喜欢

转载自www.cnblogs.com/xjmlove/p/9074146.html