使用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 block
    def get_previous_block(self):
        return self.chain[-1] 
    
    def proof_of_work(self,previous_proof):
        new_proof=1
        check_proof=False
        while check_proof is False:
            hash_oparation=hashlib.sha256(str(new_proof**2-previous_proof**2).encode()).hexdigest()
            if hash_oparation[:4]=='0000':
                check_proof=True
            else:
                new_proof+=1
        return new_proof
    
    def 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=1
        while block_index<len(chain):
          block=chain[block_index]
          if block['previous_hash'] !=self.hash(previous_block):
              return False
          previous_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 False
          previous_block=block
          block_index+=1
        return True
    
    def 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']+1
            
    def add_node(self,address):
        parsed_url=urlparse(address)
        self.nodes.add(parsed_url.netloc)
        
    def replace_chain(self):
        network = self.nodes
        longest_chain = None
        max_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}")
                continue
    
            if response.status_code == 200:
                length = response.json()['length']
                chain = response.json()['chain']
                if length > max_length and self.is_chain_valid(chain):
                    max_length = length
                    longest_chain = chain
    
        if longest_chain:
            self.chain = longest_chain
            return True
        return 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',400
    index=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", 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)
    }
    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.mfbz.cn/a/574129.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Activiti——将绘制的流程图存入act数据库并进行流程推进与状态流转

文章目录 前言流程图入库操作 RepositoryService项目结构数据库连接配置文件入库Java测试代码zip 方式进行流程的批量部署 流程启动 RuntimeService待处理任务查看 TaskService流程状态的扭转查询流程定义信息 RepositoryService查询正在执行的流程实例 RuntimeService已部署流…

Autosar AP的基本构成

1. 引言 Autosar AP的体系结构是怎样的呢&#xff1f;从整体的宏观的方向上划分&#xff0c;分为 1&#xff09;应用层&#xff0c;其中放置各种应用组件SWCs。2&#xff09;运行时基本功能软件族群&#xff0c;提供基本AutoSar基本软件中间件&#xff0c;比如经典的通信服务…

玩转nginx的配置文件3

1. limit_req_zone配置限流 limit_req_zone $binary_remote_addr zonemylimit:10m rate10r/s;upstream myweb {server 10.0.105.196:80 weight1 max_fails1 fail_timeout1;}server {listen 80;server_name localhost;location /login {limit_req zonemylimit;proxy_pass http:…

嵌入式UBoot如何跳转Kernel—uboot与linux交界分析

不知道你是否有这种感觉,就是学习了Uboot,学习了kernel,学习了安卓。但是有时候总感觉是各自孤立的,将三者连续不起来? • 不知道你是否在做启动方案的时候,在宏观上知道了整个启动链路流程,但是却在汪洋的代码中迷了路? 那么这篇文章必定对你有点用处。 如果没有,那请…

恭喜!喜提美国匹兹堡大学儿童医院访问学者邀请函

➡️【院校简介】 匹兹堡UPMC儿童医院该院是匹兹堡大学医学中心的一部分&#xff0c;也是大匹兹堡唯一一家专门护理26岁以下婴儿&#xff0c;儿童&#xff0c;青少年和年轻人的医院。该医院隶属于匹兹堡大学医学院&#xff0c;设有一个获得州级认证的一级儿科创伤中心&#xf…

Linux的自动化脚本:使用crul命令的从某个网站获取数据(从url获取数据),并将其写入一个文件中

目录 一、要求 二、思路 三、shell脚本实现演练 &#xff08;一&#xff09;脚本实现 &#xff08;二&#xff09;脚本代码说明 &#xff08;三&#xff09;脚本执行 &#xff08;四&#xff09;数据内容 一、要求 Linux的一个进程需要获取一个网站上的最新数据&#xf…

JavaScript:将input标签中的内容打印到控制台

使用浏览器进行开发时&#xff0c;按F12可以查看网页信息。 目标&#xff1a;实现将input标签中的内容&#xff0c;打印到控制台&#xff08;console&#xff09; HTML页面的关键代码实现&#xff1a; 登录功能&#xff1a; HTML代码&#xff1a; <div class"form-…

FastAPI从入门到实战(16)——请求参数汇总

FastAPI有各种各样的参数,包括: url参数(定义在url中的参数)param参数(使用url后面?xxxx)定义的参数body参数(在请求主体中携带的json参数)form参数(在请求主体中携带的web表单参数)cookie参数(在请求的cookie中携带的参数)file参数(客户端上传的文件) 1. url参数 from fas…

【A-034】基于SSH的电影订票系统(含论文)

【A-034】基于SSH的电影订票系统&#xff08;含论文&#xff09; 开发环境&#xff1a; Jdk7(8)Tomcat7(8)MySQLIntelliJ IDEA(Eclipse) 数据库&#xff1a; MySQL 技术&#xff1a; SpringStruts2HiberanteJSPJquery 适用于&#xff1a; 课程设计&#xff0c;毕业设计&…

户外指南——时代产物

分类 一级分类&#xff1a; 衣&#xff1a;除了上述提到的&#xff0c;还包括衣物的材质、款式多样性、与身份地位的关联等。 食&#xff1a;还包括饮食的文化内涵、地域特色、对特殊饮食需求的满足等。 住&#xff1a;还包括居住空间的合理布局、智能家居的应用、与自然环境…

mysql的约束和表关系

根据查询的结果&#xff0c;复制出一个新表 create table newTable AS select * from oldTable; create table newPeople AS select * from day2_test.people; 约束 引入&#xff1a;如果某一列如id列&#xff0c;有重复的数据&#xff0c;无法准确定位&#xff0c;有的列有空…

安装VCenter 7 对硬件资源的需求

安装VMware vCenter Server 7.x 对硬件资源的需求主要包括以下方面&#xff1a; 服务器硬件&#xff1a; 处理器&#xff1a;64位 x86架构&#xff0c;推荐采用多核CPU以支持高并发管理和运行多个虚拟机。具体数量取决于vCenter Server将管理的虚拟机规模及复杂度。内存&#x…

vue系统指令二

vue系统指令二 v-model:双向数据绑定 重点&#xff1a;双向数据绑定&#xff0c;只能用于表单元素&#xff0c;或者用于自定义组件。 之前的文章里&#xff0c;我们通过v-bind&#xff0c;给<input>标签绑定了data对象里的name属性。当data里的name的值发生改变时&#…

未来五十年,智能科技将如何改变传统行业格局?

未来五十年内&#xff0c;随着人工智能&#xff08;AI&#xff09;和智能科技的不断发展&#xff0c;许多行业将面临被取代的风险。虽然这种趋势可能会带来一些担忧&#xff0c;但也将为人类社会带来巨大的变革。下面将详细探讨哪些行业可能会在未来被智能科技所取代。 ▶ 制造…

攻防世界fileclude题解

攻防世界fileclude题解 ​​ 题目要求file1和file2参数不能为空 且file2这个文件内容值为hello ctf&#xff0c;用php://input 然后POST体内输入hello ctf即可满足这个if条件 满足这个条件后就会包含file1变量所指定的那个文件。用php伪协议来跨目录包含一下flag.php文件就可以…

群组分析方法

目录 1.什么是群组分析方法 2.基本原理 3.群组分析方法分类 3.1.层次方法 3.2.划分方法 3.3.密度基方法 ​​​​​​​3.4.模型基方法 4.群组评估 5.应用步骤 1.什么是群组分析方法 群组分析&#xff08;Cluster Analysis&#xff09;是数据分析中的一种重要方法&…

【第3节】“茴香豆“:搭建你的 RAG 智能助理

目录 1 基础知识1.1.RAG技术的概述1.2 RAG的基本结构有哪些呢&#xff1f;1.3 RAG 工作原理&#xff1a;1.4 向量数据库(Vector-DB )&#xff1a;1.5 RAG常见优化方法1.6RAG技术vs微调技术 2、茴香豆介绍2.1应用场景2.2 场景难点2.3 茴香豆的构建&#xff1a; 3 论文快读4 实践…

Swift - Playground

文章目录 Swift - Playground1. 新建Playground2. View3. 图片4. ViewController5. Playground - 多Page6. 注释6.1 Playground的注释支持markup语法&#xff08;与markdown相似&#xff09;6.1.1 语法 Swift - Playground Playground可以快速预览代码效果&#xff0c;是学习语…

SpringCloud系列(15)--Eureka自我保护

前言&#xff1a;在上一章节中我们说明了一些关于Eureka的服务发现功能&#xff0c;也用这个功能进行接口的实现&#xff0c;在本章节则介绍一些关于Eureka的自我保护 1、Eureka保护模式概述 保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。默认情况…

chrome插件 脚本 使用和推荐

chrome插件使用 在极简插件中可以进行下载并进行安装, 内部有安装教程在极简插件中搜索"油猴",下载一个油猴插件,并安装,可以用于下载很多的用户脚本用户脚本下载地址Greasy Fork,里面有很多实用的用户脚本供下载,并在油猴中进行管理 推荐的插件 Tampermonkey 篡改…
最新文章