使用Python,结合Flask框架,创建一个可以处理交易、挖矿新区块、验证区块链有效性,并能在网络节点间同步的区块链网络。(持续更新)

目录

前言

二、代码注释

1.添加新交易到区块链

2.连接新节点

3、替换区块链为最长链

总结


前言

本篇文章将从一个实践者的角度出发,通过构建一个简单的区块链系统,揭开区块链技术的神秘面纱。我们将使用Python语言,结合Flask框架,创建一个可以处理交易、挖矿新区块、验证区块链有效性,并能在网络节点间同步的区块链网络。。


一、代码展示

 # Module 1 -Create a Cryptocurrency
#To be installed:
#Flask==0.12.2:pip install Flask==0.12.2
#Postman HTrp Client:https://www.getpostman.com
#requests==2.18.4:pip install requests=-2.18.4
#时间戳
import datetime
import hashlib
import json
#Flask可以定义Web应用的路由(URL到Python函数的映射),并处理HTTP请求和响应。jsonify是一个函数,用于将Python对象转换为JSON格式的响应。当你在Flask路由函数中返回一个jsonify对象时,Flask会自动将该对象对应的数据转换为JSON格式,并设置合适的HTTP响应头,以便客户端可以正确解析响应内容。
from flask import Flask, jsonify,request
import requests
from uuid import uuid4
from urllib.parse import urlparse#  1******Building a Blockchain
class Blockchain:def __init__(self):self.transactions=[]self.chain=[]self.create_block(proof=1,previous_hash='0')self.nodes=set()def create_block(self,proof,previous_hash):block={'index':len(self.chain)+1,'timestamp':str(datetime.datetime.now()),'proof':proof,'previous_hash':previous_hash,'transactions':self.transactions}self.transactions=[]self.chain.append(block) return blockdef get_previous_block(self):return self.chain[-1] def proof_of_work(self,previous_proof):new_proof=1check_proof=Falsewhile check_proof is False:hash_oparation=hashlib.sha256(str(new_proof**2-previous_proof**2).encode()).hexdigest()if hash_oparation[:4]=='0000':check_proof=Trueelse:new_proof+=1return new_proofdef hash(self, block):encode_block = json.dumps(block, sort_keys=True).encode()return hashlib.sha256(encode_block).hexdigest()def is_chain_valid(self,chain):previous_block=chain[0]block_index=1while block_index<len(chain):block=chain[block_index]if block['previous_hash'] !=self.hash(previous_block):return Falseprevious_proof=previous_block['proof']proof=block['proof']hash_oparation=hashlib.sha256(str(proof**2-previous_proof**2).encode()).hexdigest()if hash_oparation[:4] !='0000':return Falseprevious_block=blockblock_index+=1return Truedef add_transaction(self,sender,receiver,amount):self.transactions.append({'sender':sender,'receiver':receiver,'amount':amount})previous_block=self.get_previous_block()return previous_block['index']+1def add_node(self,address):parsed_url=urlparse(address)self.nodes.add(parsed_url.netloc)def replace_chain(self):network = self.nodeslongest_chain = Nonemax_length = len(self.chain)for node in network:try:response = requests.get(f'http://{node}/get_chain')response.raise_for_status()  # 这将抛出异常,如果请求失败except requests.exceptions.RequestException as e:print(f"Failed to get the chain from {node}. Exception: {e}")continueif response.status_code == 200:length = response.json()['length']chain = response.json()['chain']if length > max_length and self.is_chain_valid(chain):max_length = lengthlongest_chain = chainif longest_chain:self.chain = longest_chainreturn Truereturn False
#Part 2 -Mining our Blockchain#Creating a Web App
app = Flask(__name__)#Creating an address for the node on Port 5000
node_address=str(uuid4()).replace('-', '')#Creating a Blockchain
blockchain=Blockchain()
#Mining a new block
@app.route('/mine_block',methods=['GET'])
def mine_block():previous_block=blockchain.get_previous_block()previous_proof=previous_block['proof']proof=blockchain.proof_of_work(previous_proof)previous_hash=blockchain.hash(previous_block)blockchain.add_transaction(sender=node_address,receiver='Lanzhile',amount=1)block=blockchain.create_block(proof, previous_hash)response={'message':'Congratulation,you just mined a block','index':block['index'],'timestamp':block['timestamp'],'proof':block['proof'],'previous_hash':block['previous_hash'],'transactions':block['transactions']}return jsonify(response),200#Getting the full Blockchain
@app.route('/get_chain',methods=['GET'])
def get_chain():response={'chain':blockchain.chain,'length':len(blockchain.chain)}return jsonify(response),200      #Checking if the Blockchain is valid
@app.route('/is_valid',methods=['GET']) 
def get_valid():is_valid=blockchain.is_chain_valid(blockchain.chain)if is_valid:response={'message':'All good. The Blockchain is valid.'}else:response={'message':'Houston,we have a problem.The Blockchain is not valid.'}return jsonify(response),200#Addling a new transaction to the Blockchain
@app.route('/add_transaction',methods=['POST']) 
def add_transaction():json =request.get_json()transaction_keys=['sender','receiver','amount']if not all(key in json for key in transaction_keys):return 'Some elements of the transaction are missing',400index=blockchain.add_transaction(json['sender'], json['receiver'],json['amount'])response={'message':f'This transaction will be added to Block {index}'}return jsonify(response),201#Connecting new nodes
@@app.route('/connect_node', methods=['POST'])
def connect_node():json = request.get_json()nodes = json.get('nodes')if nodes is None:return "No nodes provided", 400for node in nodes:blockchain.add_node(node)# 将响应构建移到循环外,并在所有节点添加后才返回response = {'message': 'All the nodes are now connected. The Lancoin Blockchain now contains the following nodes:','total_nodes': list(blockchain.nodes)}return jsonify(response), 201#Replacing the chain by the longest chain if needed
@app.route('/replace_chain', methods=['GET'])
def replace_chain():is_chain_replaced = blockchain.replace_chain()if is_chain_replaced:response = {'message': 'The nodes had different chains so the chain was replaced by the longest one.','new_chain': blockchain.chain}else:response = {'message': 'All good. the chain is the largest one.','actual_chain': blockchain.chain}return jsonify(response), 200 app.run(host='0.0.0.0',port=5000)         

二、代码注释

注:下面对/add_transaction、/connect_node、/replace_chain路由和对应的视图函数进行讲解,其他函数的详解在我的另外两篇我的文章里“创建一个简单的区块链,并使用 Flask 框架提供一个简单的 Web 接口来与区块链交互。-CSDN博客”“使用了Python语言和Flask框架。创建一个区块链网络,允许用户通过HTTP请求进行交互,如包括创建区块链、挖矿、验证区块链等功能。-CSDN博客

1.添加新交易到区块链

@app.route('/add_transaction', methods=['POST'])
def add_transaction():# 获取请求的JSON数据json = request.get_json()# 定义交易必须包含的键transaction_keys = ['sender', 'receiver', 'amount']# 检查JSON数据中是否包含所有必要的交易键if not all(key in json for key in transaction_keys):# 如果缺少任何一个键,返回错误信息和400状态码return 'Some elements of the transaction are missing', 400# 添加交易到区块链,并获取返回的区块索引index = blockchain.add_transaction(json['sender'], json['receiver'], json['amount'])# 构建响应消息,告知交易将被添加到的区块索引response = {'message': f'This transaction will be added to Block {index}'}# 返回JSON格式的响应和201状态码,表示交易已成功创建return jsonify(response), 201

2.连接新节点

@app.route('/connect_node', methods=['POST'])
def connect_node():
# 获取请求的JSON数据json = request.get_json()
# 尝试从JSON数据中获取节点列表nodes = json.get('nodes')
# 如果节点列表不存在,返回错误信息和400状态码if nodes is None:return "No nodes provided", 400
# 遍历节点列表,并将每个节点添加到区块链网络中for node in nodes:blockchain.add_node(node)# 构建响应消息,列出所有已连接的节点response = {'message': 'All the nodes are now connected. The Lancoin Blockchain now contains the following nodes:','total_nodes': list(blockchain.nodes)}# 返回JSON格式的响应和201状态码,表示节点已成功连接return jsonify(response), 201

3、替换区块链为最长链

@app.route('/replace_chain', methods=['GET'])
def replace_chain():# 调用 Blockchain 类的 replace_chain 方法# 这个方法会遍历所有节点,并决定是否需要用更长的链替换当前链is_chain_replaced = blockchain.replace_chain()# 如果链被替换了if is_chain_replaced:# 构建一个包含替换信息的响应字典response = {'message': 'The nodes had different chains so the chain was replaced by the longest one.',# 提供替换后区块链的完整信息'new_chain': blockchain.chain}else:# 如果当前区块链已经是最长的,没有被替换# 构建一个包含当前区块链信息的响应字典response = {'message': 'All good. the chain is the largest one.',# 提供当前区块链的完整信息'actual_chain': blockchain.chain}# 使用 jsonify 将 Python 字典转换成 JSON 格式的响应体# 返回 200 OK HTTP 状态码return jsonify(response), 200

总结

这纸片文章主要介绍三个路由:/add_transaction/connect_node/replace_chain,分别用于添加交易、连接新节点和替换链。在处理请求时,代码首先获取请求中的JSON数据,然后根据不同的路由执行相应的操作,最后返回响应消息和状态码。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/3460.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

uniapp H5实现签名

第一种&#xff1a;跳转签名页面 1、创建审核页面audit.vue <template><view><uni-section title""><view class"auditClass"><uni-forms :model"baseFormData" ref"baseFormRef" :rules"rules&quo…

【Flink入门修炼】2-3 Flink Checkpoint 原理机制

如果让你来做一个有状态流式应用的故障恢复&#xff0c;你会如何来做呢&#xff1f; 单机和多机会遇到什么不同的问题&#xff1f; Flink Checkpoint 是做什么用的&#xff1f;原理是什么&#xff1f; 一、什么是 Checkpoint&#xff1f; Checkpoint 是对当前运行状态的完整记…

elementui el-date-picker禁止选择今年、今天、之前、时间范围限制18个月

1、禁止选择今年之前的所有年份 <el-date-pickerv-if"tabsActive 0":clearable"false"v-model"yearValue"change"yearTimeChange"type"year"placeholder"选择年"value-format"yyyy":picker-options…

03 OLED显示屏实现

文章目录 前言一、软件模拟IIC协议1.开启IIC协议2.结束IIC协议3.传输数据 二、OLED的操作1.传输数据的准备2.写入命令3.写入数据4.初始化函数5.设置光标6.显示字符7.显示字符串8.清屏9.显示汉字10.显示图片11.显示动图 三、完整代码总结 前言 这一章主要是上一节没有讲完的项目…

前端项目中使用插件prettier/jscodeshift/json-stringify-pretty-compact格式化代码或json数据

同学们可以私信我加入学习群&#xff01; 正文开始 前言一、json代码格式化-选型二、json-stringify-pretty-compact简单试用三、prettier在前端使用四、查看prettier支持的语言和插件五、使用prettier格式化vue代码最终效果如图&#xff1a; ![在这里插入图片描述](https://im…

中文语音识别实战(ASR)

写在前面的话 本博客主要介绍了 1. 语音识别基础知识 2. 中文语音识别数据集 3. 语音识别常用模型方法 4. 自己训练一个中文语音识别模型 主意: 代码中所涉及的模型及数据集,均可从huggingface下载得到,代码中的路劲,需要根据自身实际情况稍做调整。 目录 语音识别基…

服务器之间传递数据脚本

服务器之间的数据复制传递 准备 Python 环境&#xff1a; 确保你的计算机上安装了 Python&#xff0c;并安装了 Paramiko 库。你可以使用 pip 命令来安装 Paramiko&#xff0c;如下所示&#xff1a; pip install paramiko 修改脚本&#xff1a; 将脚本中的以下变量替换为你的…

LLM应用实战:当KBQA集成LLM(二)

1. 背景 又两周过去了&#xff0c;本qiang~依然奋斗在上周提到的项目KBQA集成LLM&#xff0c;感兴趣的可通过传送门查阅先前的文章《LLM应用实战&#xff1a;当KBQA集成LLM》。 本次又有什么更新呢&#xff1f;主要是针对上次提到的缺点进行优化改进。主要包含如下方面&#…

多客圈子交友系统 uniapp+thinkphp6适配小程序/H5/app/api全开源,多款插件自选,支持个性定制!

网上交友的优点包括&#xff1a; 1. 方便&#xff1a;网上交友可以随时随地进行&#xff0c;不受时间和空间的限制&#xff0c;方便且高效。 2. 匿名性&#xff1a;网上交友可以实现匿名性&#xff0c;用户可以匿名地搜索、聊天或交换信息&#xff0c;保护个人隐私和安全。 3.…

COOIS 生产订单显示系统增强

需求说明&#xff1a;订单系统显示页面新增批量打印功能 增强点&#xff1a;CL_COIS_DISP_LIST_NAVIGATION -->TOOLBAR方法中新增隐式增强添加自定义打印按钮 增强点&#xff1a;BADI-->WORKORDER_INFOSYSTEM新增增强实施 实现位置&#xff1a;IF_EX_WORKORDER_INFOSYS…

制造型企业 如何实现便捷的机台文件统一管理?

机台文件统一管理&#xff0c;这是生产制造型企业都需要去做的&#xff0c;机台文件需要统一管理的原因主要包括以下几点&#xff1a; 1、提高效率&#xff1a;统一管理可以简化文件的访问和使用过程&#xff0c;提高工作效率&#xff0c;尤其是在需要频繁访问或更新机台文件的…

MySQL中什么情况下会出现索引失效?如何排查索引失效?

目录 1-引言&#xff1a;什么是MySQL的索引失效&#xff1f;(What、Why)1-1 索引失效定义1-2 为什么排查索引失效 2- 索引失效的原因及排查&#xff08;How&#xff09;2-1 索引失效的情况① 索引列参与计算② 对索引列进行函数操作③ 查询中使用了 OR 两边有范围查询 > 或 …

基于单片机的家居智能系统设计与实现

摘 要:采用STC89C52 单片机为主控制芯片的智能家居系统,能给用户提供一个安全、智能、舒适的家居环境。通过DHT11 温湿度传感器检测当前室内的温度和湿度,可以按键设置温度和湿度的范围,当检测到温度或者湿度不在设置的范围内时,可自动调节。此外,具有防盗功能,通过红外…

HTML中meta标签属性详解

文章目录 name 属性⚡️1. viewport⚡️2. keywords⚡️3. description4. author5. application-name6. generator7. robots8. copyright http-equiv 属性⚡️1. content-type⚡️2. Cache-Control3. refresh4. default-style charset 属性⚡️UTF-8 <meta> 标签是一种用…

USB设备的音频类UAC

一、UAC简介 UAC&#xff08;USB Audio Class&#xff09;是USB设备的音频类&#xff0c;它定义了USB音频设备与主机计算机通信的方式。UAC标准是USB规范的一部分&#xff0c;并受到各种操作系统&#xff08;包括Windows、macOS和Linux&#xff09;的支持。 UAC是基于libusb,实…

kotlin根据文件的filePath转化为uri

方法实现 使用File类来创建一个文件对象&#xff0c;然后通过FileProvider来获取文件的URI import android.content.Context import android.net.Uri import androidx.core.content.FileProvider import java.io.Filefun getFileUri(context: Context, filePath: String): Ur…

图像在神经网络中的预处理与后处理的原理和作用(最详细版本)

1. 问题引出及内容介绍 相信大家在学习与图像任务相关的神经网络时&#xff0c;经常会见到这样一个预处理方式。 self.to_tensor_norm transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) 具体原理及作用稍后解释&…

java8 Stream流常用方法(持续更新中...)

java8 Stream流常用方法 1.过滤数据中年龄大于等于十八的学生2.获取对象中其中的一个字段并添加到集合(以学生姓名&#xff08;name&#xff09;为例)3.获取对象中其中的一个字段并转为其他数据类型最后添加到集合(以学生性别&#xff08;sex&#xff09;为例&#xff0c;将Str…

Apache Doris 2.x 版本【保姆级】安装+使用教程

Doris简介 Apache Doris 是一个基于 MPP 架构的高性能、实时的分析型数据库&#xff0c;以极速易用的特点被人们所熟知&#xff0c;仅需亚秒级响应时间即可返回海量数据下的查询结果&#xff0c;不仅可以支持高并发的点查询场景&#xff0c;也能支持高吞吐的复杂分析场景。基于…

【论文速读】|大语言模型(LLM)智能体可以自主利用1-day漏洞

本次分享论文&#xff1a; LLM Agents can Autonomously Exploit One-day Vulnerabilities 基本信息 原文作者&#xff1a;Richard Fang, Rohan Bindu, Akul Gupta, Daniel Kang 作者单位&#xff1a;无详细信息提供 关键词&#xff1a;大语言模型, 网络安全, 1-day漏洞, …