# -*- 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.rar (22个子文件)
simchain
params.py 904B
datatype.py 7KB
logger.py 188B
wallet.py 1KB
__pycache__
wallet.cpython-37.pyc 2KB
peer.cpython-37.pyc 22KB
ecc.cpython-37.pyc 18KB
__init__.cpython-37.pyc 609B
script.cpython-37.pyc 4KB
datatype.cpython-37.pyc 10KB
network.cpython-37.pyc 6KB
base58.cpython-37.pyc 4KB
params.cpython-37.pyc 830B
consensus.cpython-37.pyc 971B
logger.cpython-37.pyc 275B
ecc.py 18KB
__init__.py 532B
base58.py 4KB
consensus.py 921B
network.py 7KB
peer.py 26KB
script.py 3KB
共 22 条
- 1
weixin_42651887
- 粉丝: 97
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- (源码)基于SpringBoot和Redis的秒杀系统.zip
- yolo数据集.txt
- (源码)基于RabbitMQ的消息队列系统.zip
- (源码)基于SpringBoot框架的Guns后台管理系统.zip
- (源码)基于GoogleCloudIoT的云端烟雾机控制系统CloudSmoker.zip
- JAVA的SpringBoot 高校宿舍管理系统源码带视频和文档数据库 MySQL源码类型 WebForm
- (源码)基于Python的火车票自动购买系统.zip
- 人工智能-大模型-一个智能问答系统,能够自动将用户的自然语言问题转换成 SQL 查询,并以可视化形式展示结果
- trumtime安装所需的全部资源以及步骤
- (源码)基于JavaWeb的宿舍管理系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0