区块链Python:从头开始构建的具有不同端点的公共分散网络,以支持典型区块链的必要功能

  • m9_366978
    了解作者
  • 5.9KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-29 06:10
    上传日期
区块链Python:从头开始构建的具有不同端点的公共分散网络,以支持典型区块链的必要功能
blockchain_python-master.zip
  • blockchain_python-master
  • blockchain_server.py
    7.7KB
  • .idea
  • misc.xml
    415B
  • blockchain_python.iml
    466B
  • dictionaries
  • ThanhTran.xml
    151B
  • inspectionProfiles
  • profiles_settings.xml
    174B
  • modules.xml
    286B
  • .gitignore
    38B
  • vcs.xml
    180B
  • initialization.py
    76B
  • .gitignore
    30B
内容介绍
import time from hashlib import sha256 import json from flask import Flask, request import requests class Block: def __init__(self, index, transactions, timestamp, prev_hash, nonce=0): self.index = index self.transactions = transactions self.timestamp = timestamp self.prev_hash = prev_hash self.nonce = nonce # create the hash of the block def calculate_hash(self): block_string = json.dumps(self.__dict__, sort_keys=True) return sha256(block_string.encode()).hexdigest() class Blockchain: difficulty = 2 # for proof of work def __init__(self): self.unconfirmed_transactions = [] self.chain = [] # create the first block (genesis) def create_genesis(self): genesis = Block(0, [], time.time(), "0") genesis.hash = genesis.calculate_hash() self.chain.append(genesis) @property def get_last_block(self): return self.chain[-1] # brute force to figure out the nonce def create_proof_of_work(self, block): block.nonce = 0 calculated_hash = block.calculate_hash() while not calculated_hash.startswith("0" * Blockchain.difficulty): block.nonce = block.nonce + 1 calculated_hash = block.calculate_hash() return calculated_hash def add_block(self, new_block, proof_of_work): curr_prev_hash = self.get_last_block.hash # verify the new block before adding it to the chain if curr_prev_hash != new_block.prev_hash or not self.is_valid_proof_work(new_block, proof_of_work): return False else: new_block.hash = proof_of_work self.chain.append(new_block) return True # check the validity of the block hash and to see if it satisfies the difficulty @classmethod def is_valid_proof_work(cls, new_block, new_block_hash): return (new_block_hash.startswith('0' * Blockchain.difficulty) and new_block_hash == new_block.calculate_hash()) def add_transaction(self, new_transaction): self.unconfirmed_transactions.append(new_transaction) # add pending transactions to the block and calculate the proof of work def mine(self): if self.unconfirmed_transactions: last_block = self.get_last_block new_block = Block(index=last_block.index+1, transactions=self.unconfirmed_transactions, timestamp=time.time(), prev_hash=last_block.hash) proof = self.create_proof_of_work(new_block) self.add_block(new_block, proof) self.unconfirmed_transactions = [] # broadcast this to the network broadcast_added_block(new_block) return new_block.index else: return False @classmethod def is_chain_valid(cls, chain): result = True prev_hash = '0' for block in chain: block_hash = block.hash # remove and recompute the hash delattr(block, "hash") if not cls.is_valid_proof_work(block, block.hash) or prev_hash != block.prev_hash: result = False break block.hash, prev_hash = block_hash, block_hash return result app = Flask(__name__) # my copy of the blockchain blockchain = Blockchain() blockchain.create_genesis() # create an address so other can join the network peers = set() # BACKEND operations # receive new transaction and add to the (pending) blockchain @app.route('/new_transaction', methods=['POST']) def receive_new_transaction(): new_transaction = request.get_json() required_fields = ['author', 'content'] for field in required_fields: if not new_transaction.get(field): return 'Invalid transaction data', 404 new_transaction["timestamp"] = time.time() blockchain.add_transaction(new_transaction) return 'Success', 201 # return the node's copy of the chain by querying all of the posts @app.route('/chain', methods=['GET']) def get_chain(): # get the longest chain consensus() chain = [] for block in blockchain.chain: chain.append(block.__dict__) return json.dumps({'length': len(chain), 'chain': chain, 'peers': list(peers)}) # receive request to mine unconfirmed transactions @app.route('/mine', methods=['GET']) def mine_unconfirmed_transactions(): result = blockchain.mine() if result: return 'Block #{} is mined.'.format(result) return 'No transactions to mine' # query unconfirmed transactions @app.route('/pending_transactions') def get_pending_transactions(): return json.dumps(blockchain.unconfirmed_transactions) # receive request to join the network @app.route('/add_nodes', methods=['POST']) def register_new_peers(): node_address = request.get_json()['node_address'] if node_address: peers.add(node_address) return get_chain() else: return 'Invalid Data', 400 # add a new block to the node's chain @app.route('/add_block', methods=['POST']) def validate_and_add_block(): new_block_data = request.get_json() new_block = Block(new_block_data['index'], new_block_data['transactions'], new_block_data['timestamp'], new_block_data['prev_hash']) proof = new_block_data['hash'] added = blockchain.add_block(new_block, proof) if added: return 'A new block was added to the blockchain', 201 return 'Failed to add the new block to the blockchain', 400 @app.route('/register_with', methods=['POST']) def register_with_existing_node(): node_address = request.get_json()['node_address'] if node_address: data = {'node_address': request.host_url} headers = {'Content-Type:': 'application/json'} response = requests.post(node_address + '/add_nodes', data=json.dumps(data), headers=headers) if response.status_code == 200: global blockchain global peers # update the chain chain_dump = response.json()['chain'] blockchain = create_chain_from_dump(chain_dump) peers.update(response.json()['peers']) return 'Registration Successful', 200 else: return response.content, response.status_code else: return 'Invalid data', 400 def create_chain_from_dump(chain_dump): blockchain = Blockchain() for index, block_data in enumerate(chain_dump): block = Block(block_data['index'], block_data['transactions'], block_data['timestamp'], block_data['prev_hash']) proof = block_data['hash'] if index > 0: added = blockchain.add_block(block, proof) if not added: raise Exception('The chain dump is tampered!') else: blockchain.chain.append(block) return blockchain def consensus(): global blockchain longest_chain = None curr_length = len(blockchain) for peer in peers: print('{}/chain'.format(peer)) response = requests.get('http://{}/chain'.format(peer)) print('Content', response.content) length = response.json()['length'] chain = response.json()['chain'] if length > curr_length and blockchain.is_chain_valid(chain): curr_length = length longest_chain = chain # if a longer valid chain is found, it becomes the new global blockchain if longest_chain: blockchain = longest_chain return True return False def broadcast_added_block(new_block): for peer in peers: url = 'http://{}/add_block'.format(peer) requests.post(url, data=json.dumps(new_block.__dict__, sort_keys=True)) # app.run(debug=True, port=8000)
评论
    相关推荐
    • 15个区块链Python
      15个区块链Python 依存关系: 必须在您的操作系统(任何版本5或7)上安装PHP。 您将需要克隆hd-wallet-derive工具。 位Python比特币库。 web3.py Python以太坊库。 库: 将熊猫作为pd导入将numpy导入为np 导入子...
    • 区块链python:在Python中创建多区块链钱包
      区块链Python 设置 1.创建一个名为wallet的项目目录,并进入cd。 将hd-wallet派生工具克隆到此文件夹中,并按照其README.md上的说明进行安装。 一种。 git clone b。 cd高清钱包衍生 C。 php -r“ readfile(' '...
    • 区块链python:用Python实现的简单BlockChain
      Python实现的简单BlockChain 这是我在[在Python中构建简单的区块链]中的帖子的源代码。 要求 Python3.5 烧瓶 python请求 贡献 请随时提出拉取请求:)
    • chia-blockchain:Chia区块链python实现(完整节点,农民,收割机,时间领主和钱包)
      通过键入python3确保您的默认python版本> = 3.7。 如果您使用的是NAT,则子网外的对等点启动时可能很难到达您。 您可以在路由器上启用或添加NAT(针对IPv4,但不针对IPv6)和防火墙规则,以允许TCP端口8444访问...
    • Blockchain-Python:区块链Python作业
      区块链Python HD(分层确定性)钱包可以允许从单个种子短语生成许多crpyto钱包。这是一个从根节点开始的公共/私有密钥树,该根节点是主节点。任何节点都可以有多个子代。主私钥的唯一目的是重新生成钱包树,并且...
    • Chia区块链python实现(完整节点,农民,收割机,时间领主和钱包)-Python开发
      Chia是一种从头开始构建的现代加密货币,旨在高效,分散和安全。 chia-blockchain当前版本/主开发部门/ dev Chia是一种从头开始构建的现代加密货币,旨在高效,分散和安全。 这里有一些功能和好处:基于时空共识的...
    • Blockchain-Python:Exemplo2区块链Usando Python
      区块链Python Exemplo2区块链Usando Python
    • Python区块链应用
      Python区块链应用 具有UI的完整区块链应用程序。 可以添加多个节点,每个节点都有自己的本地存储的区块链,一个公钥和一个私钥。 区块链存储在本地的.txt文件中。 钱包保存,加载和交易签名验证 块的真实SHA256...
    • 区块链Python
      区块链Python 遵循@dvf有关准则,为学习目的而。 使用Flask通过REST公开服务。 安装 确保已安装 。 安装 。 $ pip install pipenv 安装要求 $ pipenv install 运行服务器: $ pipenv run python blockchain.py ...
    • GaussDB_100_1.0.1-DATABASE-REDHAT-64bit.tar.gz
      guassdb100在redhat上安装包,单机部署的包,安装步骤请看我的文中介绍,经过大量实验搭建总结出来的文档