• RoyalJJ
    了解作者
  • Python
    开发工具
  • 50KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 10
    下载次数
  • 2019-06-12 21:51
    上传日期
区块链simchain代码,模拟区块链的运行过程
simchain.rar
  • simchain
  • __pycache__
  • ecc.cpython-37.pyc
    17.7KB
  • script.cpython-37.pyc
    3.8KB
  • wallet.cpython-37.pyc
    2.3KB
  • datatype.cpython-37.pyc
    10KB
  • params.cpython-37.pyc
    830B
  • base58.cpython-37.pyc
    3.8KB
  • __init__.cpython-37.pyc
    609B
  • logger.cpython-37.pyc
    275B
  • peer.cpython-37.pyc
    21.9KB
  • consensus.cpython-37.pyc
    971B
  • network.cpython-37.pyc
    6.3KB
  • base58.py
    3.8KB
  • ecc.py
    18.3KB
  • peer.py
    26.4KB
  • __init__.py
    532B
  • network.py
    6.6KB
  • script.py
    3.3KB
  • params.py
    904B
  • consensus.py
    921B
  • wallet.py
    1.5KB
  • logger.py
    188B
  • datatype.py
    7.2KB
内容介绍
# -*- coding: utf-8 -*- import random from .ecc import VerifyingKey,build_message from .datatype import Pointer,Vin,Vout,UTXO,Tx,Block,get_merkle_root_of_txs from .params import Params from .consensus import mine,caculate_target from .logger import logger from .wallet import Wallet from .script import LittleMachine class Peer(object): def __init__(self,coords = None): self.coords = coords self.network = None self.txs = [] self.candidate_block_txs = [] self.candidate_block = None self.blockchain = [] self.orphan_block = [] self.utxo_set = {} self.mem_pool = {} self.orphan_pool = {} self.pid = None self.fee = Params.FIX_FEE_PER_TX self.tx_choice_method = 'whole' self.current_tx = None self.machine = LittleMachine() self.generate_wallet() self._is_block_candidate_created = False self._is_current_tx_created = False self._is_current_tx_sent = False self._delayed_tx = None self._delayed_block = None self._utxos_from_vins = None self._pointers_from_vouts = None self._utxos_from_vouts = None self._txs_removed = None ############################################################ # peer as wallet ############################################################ """ Generate wallet """ def generate_wallet(self): self.wallet = Wallet() @property def sk(self): return self.wallet.keys[-1].sk.to_string() if self.wallet.keys else None @property def pk(self): return self.wallet.keys[-1].pk.to_string() if self.wallet.keys else None @property def addr(self): return self.wallet.addrs[-1] if self.wallet.addrs else None @property def key_base_len(self): return len(self.sk) """ your bank balance """ def get_balance(self): utxos = self.get_utxo() return sum(utxo.vout.value for utxo in utxos) """ your output """ def get_utxo(self): return [utxo for utxo in self.utxo_set.values() if (utxo.vout.to_addr in self.wallet.addrs) and utxo.unspent] def get_unconfirmed_utxo(self): utxos = self.get_utxo() return [utxo for utxo in utxos if not utxo.confirmed] def get_confirmed_utxo(self): utxos = self.get_utxo() return [utxo for utxo in utxos if utxo.confirmed] def set_fee(self,value): self.fee = value def get_fee(self): return self.fee def calculate_fees(self,txs): fee = 0 if txs: for tx in txs: fee += tx.fee return fee return 0 def get_block_subsidy(self): return Params.FIX_BLOCK_SUBSIDY """ create a transaction """ def create_transaction(self,to_addr, value, tx_type = "normal"): if tx_type == 'normal': outputs = create_normal_tx(self,to_addr,value) if outputs: tx_in,tx_out,fee = outputs self.current_tx = Tx(tx_in,tx_out,fee = fee,nlocktime = 0) self.txs.append(self.current_tx) self._is_current_tx_created = True logger.info('{0}(pid={1}) created a transaction'.format(self,self.pid)) return True return False """ if this is a recorder peer, build rewards for self after won """ def create_coinbase(self,value): return Tx.create_coinbase(self.wallet.addrs[-1],value = value) ############################################################ # peer as route ############################################################ """ broadcast a transaction """ def send_transaction(self): if not self.txs: return False if self._is_current_tx_created: sign_utxo_from_tx(self.utxo_set,self.current_tx) add_tx_to_mem_pool(self,self.current_tx) self._is_current_tx_created = False self._is_current_tx_sent = True logger.info("{0}(pid={1}) sent a transaction to network".format(self,self.pid)) return True return False def recieve_transaction(self,tx): if tx and (tx not in self.mem_pool): if self.verify_transaction(self,tx,self.mem_pool): add_tx_to_mem_pool(self,tx) return True return False def broadcast_transaction(self,tx = None): if not self._is_current_tx_sent: self.send_transaction() self._is_current_tx_created = False self._is_current_tx_sent = False tx = tx or self.current_tx if tx: peers = self.network.peers[:] peers.remove(self) number = broadcast_tx(peers,tx) self.current_tx = None logger.info("{0}(pid={1})'s transaction verified by {2} peers".format(self,self.pid,number)) return number return 0 """ broadcast a transaction """ def broadcast_block(self,block): peers = self.network.peers[:] peers.remove(self) number = broadcast_winner_block(peers,block) logger.info('{0} received by {1} peers'.format(block,number)) def locate_block(self,block_hash): return locate_block_by_hash(self,block_hash) def recieve_block(self,block): if not self.verify_block(block): return False return try_to_add_block(self,block) """ verify a transaction """ def verify_transaction(self,tx,pool={}): if tx in self.txs: return True return verify_tx(self,tx,pool) """ verify a block """ def verify_block(self,block): if self._delayed_tx: fill_mem_pool(self) if self.orphan_pool: check_orphan_tx_from_pool(self) pool = get_unknown_txs_from_block(self.mem_pool,block.txs) if block == self.candidate_block: return True if verify_winner_block(self,block,pool): return True else: return False """ peer links to p2p network """ def login(self): assert self in self.network.off_peers,( "This peer does not connect to network or online" ) repeat_log_in(self,self.network) self.update_blockchain() """ peer logs out """ def logout(self): assert self in self.network.peers,( "This peer does not connect to network" ) log_out(self,self.network) def update_blockchain(self,other): return update_chain(self,other) def update_mem_pool(self,other): if other._delayed_tx: fill_mem_pool(other) return update_pool(self,other.mem_pool) def update_utxo_set(self,other): self.utxo_set.update(other.utxo_set) ############################################################ # peer as recorder ############################################################ """ if u r a consensus peer, u have create a candid
评论
    相关推荐
    • simchain-master.zip
      区块链simchain代码,模拟区块链的运行过程
    • 区块链技术指南 .zip
      区块链技术指南 ,区块链技术 ,区块链技术指导
    • 区块链原理详解_附代码.zip
      了解区块链,区块链原理详解,还有代码,详情请自行下载,,,希望能帮助大家
    • 自由之神EA源码.rar
      通向财务自由之路的ea很全面,适合要求不高的朋友,自己可以改
    • 10biansuxing1.zip
      构建了一个包含风速模型、功率转换模型、传动链模型等三个部分的10MW变速直驱型风力发电机组的标么值的Matlab模型.传动链模型是基于二质量块——轴数学模型所构建的动态模型.通过标么值的转换及灵活的参数调整,该模型能较准确地模拟各种变速直驱型风力发电机组.仿真分析获取了最大功率跟踪特性曲线,并得到了风力发电机组的四个工作区.结果还能表明,电负荷电磁阻力矩能动态地调整电机转子转速,从而在低速风时能实现风能最大功率的追踪.同时,风涡轮的桨距角能控制高速风的利用率,使风涡轮机工作在额定功率下,有效地防止整个风力发电系统的机械和电负荷容量的过载冲击.作为原动力模型,该模型有助于进一步研究变速直驱风力发电系统的功率特性和并网发电控制技术.
    • Dynamic_partitioning_of_the_simulated_distribution
      ⑴ 用C语言分别实现采用首次适应算法和最佳适应算法的动态分区分配过程alloc()和回收过程free()。其中,空闲分区通过空闲分区链来管理;在进行内存分配时,系统优先使用空闲区低端的空间。 ⑵ 假设初始状态下,可用的内存空间为640KB,并有下列的请求序列:  作业1申请130KB。  作业2申请60KB。  作业3申请100KB。  作业2释放60KB。  作业4申请200KB。  作业3释放100KB。  作业1释放130KB。  作业5申请140KB。  作业6申请60KB。  作业7申请50KB。  作业6释放60KB。 分别采用首次适应算法和最佳适应算法进行内存块的分配和回收,要求每次分配和回收后显示出空闲内存分区链的情况。
    • 从零开始创建区块链(JAVA版).rar
      Java 实现区块链,通过一个详细的例子,一步步说明区块链的概念和java 实现的方法。
    • 论文.zip
      区块链论文集,可以用于研究或者毕业设计参考
    • bitcoinj-master.zip
      区块链比特币的实现代码,基本满足比特币的区块链实现机制以及部署。
    • micro_cplex2.zip
      cplex实例教程,简单易懂,对求解多能源最优帮助最大