区块结构的详细介绍,容易入手:
【区块链时代】如何理解区块链中共识机制以及在区块链中分叉是如何解决的
一、区块链开发环境准备
HTTP协议
Postman / curl 测试工具
Python
Pycharm
Pip工具
Flask/ request
二、区块的结构
{
"index":0, #索引
"timestamp":"", #时间戳
"translations": [ #交易信息
{
"sender":"", #交易发送者
"recipient": "", #交易接收者
"amount": 5, #交易金额
}
],
"proof": "", #工作量证明
"previous_hash":"", #上一个区块hash值
}
三、创建一个区块
import hashlib
import json
from time import time,sleep
class Blockchain:
def __init__(self):
self.chain = []
self.current_translations = []
# 创建创世区块
self.new_block(proof = 100 , previous_hash = 1)
def new_block(self,proof,previous_hash = None):
block = {
'index': len(self.chain) + 1,
'timestamp': time(),
'translations': self.current_translations,
'proof': proof,
'previous_hash': previous_hash or self.hash(self.last_block),
}
self.current_translations = []
self.chain.append(block)
return block
# 实现交易
def new_translation(self,sender,recipient,amount) -> int:
self.current_translations.append(
{
"sender":sender,
"recipient":recipient,
"amount":amount
}
)
return self.last_block['index'] + 1
@staticmethod
def hash(block):
block_string = json.dumps(block,sort_keys=True).encode() # 将block(json对象)转换为字符串
return hashlib.sha256(block_string).hexdigest()
@property
def last_block(self):
return self.chain(-1)
def proof_of_work(self,last_block:int) -> int:
proof = 0
while self.valid_proof(last_block,proof) is False:
proof += 1
print("proof is ",proof)
return proof
def valid_proof(self, last_proof:int, proof:int) -> bool:
guess = f'{last_proof}{proof}'.encode()
guess_hash = hashlib.sha256(guess).hexdigest()
sleep(1)
print("guess_hash is ",guess_hash)
# return guess_hash[0:4] == "0000"
if guess_hash[0:4] == "0000":
return True
else:
return False
# 测试
testPow = Blockchain()
testPow.proof_of_work(100)
结果如下:文章来源:https://www.toymoban.com/news/detail-557430.html
guess_hash is ccbde76c925125361a6ab2ec97dbb0bb3376bd5f165ca71b8cf9b1851fce1251
guess_hash is 6fe3a4d4ab5e4dbf25ad2e6c5a9e177b3e8e9728fcd60f1bbf75e30592cc7d61
guess_hash is 7b501a4208b6b1a3afe9249a145f73df675eb13a0888b44d6e746f285320fd08
guess_hash is 08936563aaa2e39c721bfb074529904ffae85d2869493405d51442e25b5f1551
guess_hash is 81c5cd1bcca9c78d8d51c76dfb1770bf6509f09c3760179060f12057cf2281ee
guess_hash is caf42c350b933954e087b008bb5f8457f6cd247f4c06788ecd804a3a8c35cfe1
guess_hash is c20da575724013406f330c1e2646d7b8cf2bda2abd60c150da1226c9bfda64cc
guess_hash is 7ee9410967a25bb5fa1e216346ae62567594f8c866bfbb1f4c750799cf178cda
四、实现节点通信功能
from flask import Flask
from blockchain import Blockchain
app = Flask(__name__)
@app.route('/index',methods=['GET'])
def index():
return "Hello BlockChain"
# 添加新的交易
@app.route('/transactions/new',methods=['POST'])
def new_transaction():
return "我们添加新的交易"
# 挖矿
@app.route('/mine',methods=['GET'])
def mine():
return "挖矿进行中~~~~~~~~~~~~~~~~~~~"
# 返回完整区块链信息
@app.route('/chain',methods=['GET'])
def full_chain():
return "返回完整区块链信息"
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5000)
在下一篇博客中将重点介绍节点之间通信过程中如何实现各个功能模块。文章来源地址https://www.toymoban.com/news/detail-557430.html
到了这里,关于【区块链时代】如何用Python实现区块链中的区块结构(快速入手01)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!