示例#1
0
def check_block(block):
    """检查收到的块"""
    result = True
    try:
        string = block["header"].__str__() + block["trans_list"].__str__()
        sig = block["sign"]
        user_name = block["header"]["sender"]
        hash_value = block["block_hash"]
        if not check_sign(string, sig, user_name):
            result = False
        if not check_hash(string, hash_value):
            result = False
        block_index = int(block["header"]["index"])
        db_top_index = BlockChain().get_top_block_index()
        if block_index != db_top_index + 1:
            result = False
        if block["header"]["pr_block_hash"] != BlockChain().get_top_block_hash():
            result = False
        sender = block["header"]["sender"]
        coin = block["tran"]["coin"]
        sender_obj = AccountOpertion(sender)
        coin_list = sender_obj.show_coins()
        if coin in coin_list:
            return False
    except Exception as e:
        Log().error("检查区块合法性失败:" + str(e))
        result = False
    finally:
        return result
示例#2
0
def check_net_block(block, ip):
    """检查块"""
    result = True
    try:
        string = block["header"].__str__() + block["trans_list"].__str__()
        sig = block["sign"]
        user_name = block["header"]["sender"]
        hash_value = block["block_hash"]
        if not check_sign(string, sig, user_name):
            result = False
        if not check_hash(string, hash_value):
            result = False
        block_index = int(block["header"]["index"])
        db = Mongodb(ip)
        db_top_index = db.get_max_index()
        if block_index != db_top_index + 1:
            result = False
        if block["header"]["pr_block_hash"] != db.get_top_block_hash():
            result = False
        sender = block["header"]["sender"]
        coin = block["tran"]["coin"]
        coin_list = db.show_coins(sender)
        if coin in coin_list:
            return False
    except Exception as e:
        Log().error("检查区块合法性失败:" + str(e))
        result = False
    finally:
        return result
示例#3
0
 def __init__(self):
     try:
         myclient = pymongo.MongoClient("mongodb://localhost:27017/")
         dblist = myclient.list_database_names()
         if "skin_chain" not in dblist:
             Log().info("数据库不存在,正在创建--------")
             print("数据库不存在,正在创建--------")
             mydb = myclient["skin_chain"]
             Log().info("已创建skin_chain数据库")
             print("已创建skin_chain数据库")
         else:
             mydb = myclient["skin_chain"]
         self.mycol = mydb["chain"]
     except Exception as e:
         print("数据库创建失败:" + str(e))
         Log().error("数据库创建失败:" + str(e))
示例#4
0
 def insert(self, block):
     """在数据库插入新的块"""
     try:
         self.mycol.insert_one(block.block_dict)
         return True
     except Exception as e:
         Log().error("区块插入失败:" + str(e))
         print(e)
         return False
示例#5
0
def move_file(srcfile, dstpath):
    """复制文件到指定文件夹"""
    try:
        if not os.path.exists(dstpath):
            os.mkdir(dstpath)
            shutil.move(srcfile, dstpath)
        return True
    except Exception as e:
        Log().error("文件不存在:" + str(e))
        print("文件不存在:" + str(e))
        return False
示例#6
0
 def get_block_list_by_user(self, user):
     """获取含有某一用户的所有块"""
     try:
         block_list_raw = json.loads(json_util.dumps(self.mycol.find({"header.sender": user}, {"_id": False}))) \
                          + json.loads(json_util.dumps(self.mycol.find({"tran.recive": user}, {"_id": False})))
         block_list = []
         for item in block_list_raw:
             if item not in block_list:
                 block_list.append(item)
         return block_list
     except Exception as e:
         Log().error("通过用户获取块失败:" + str(e))
示例#7
0
def response_sync():
    """同步请求(守护进程)"""
    udp_server_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udp_server_sock.bind((get_host_ip(), Config.SYNCPORT))
    udp_server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    while True:
        mesg, addr = udp_server_sock.recvfrom(1024)
        mesg = json.loads(mesg.decode('utf-8'))
        try:
            request = mesg["request"]
            if request == "sync":
                request_index = int(mesg["data"])
                host_index = BlockChain().get_top_block_index()
                if request_index < host_index:
                    block_list = []
                    for index in range(request_index, host_index):
                        block_list.append(BlockChain().get_block_by_index(index+1))
                    Client().push_blocks(block_list)
        except Exception as e:
            Log().error("同步请求响应失败:" + str(e))
            pass
示例#8
0
def send_file(path):
    """向全网广播文件,输入文件路径"""
    addr = (Config.IPPOOL, Config.FILEPORT)
    udp_cli_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        f = open(path, 'rb')
    except Exception as e:
        Log().error("路径文件不存咋:" + str(e))
    count = 0
    while True:
        if count == 0:
            data = bytes(path,  encoding="utf8")
            udp_cli_sock.sendto(data, addr)
        data = f.read(1024)
        if str(data) != "b''":
            udp_cli_sock.sendto(data, addr)
        else:
            udp_cli_sock.sendto('end'.encode('utf-8'), addr)
            break
        count += 1
    udp_cli_sock.close()
示例#9
0
 def get_block_list_by_coin(self, coin):
     """获取含有某一资产的所有块"""
     try:
         return json.loads(json_util.dumps(self.mycol.find({"tran.coin": coin}, {"_id": False})))
     except Exception as e:
         Log().error("通过资产获取块失败:" + str(e))
示例#10
0
 def get_block_by_index(self, index):
     """通过索引获取最新的块"""
     try:
         return json.loads(json_util.dumps(self.mycol.find({"header.index": index}, {"_id": False})))[0]
     except Exception as e:
         Log().error("获取最新块失败:" + str(e))
示例#11
0
 def clean(self):
     """清空数据库"""
     try:
         self.mycol.delete_many({})
     except Exception as e:
         Log().error("清空数据库失败:" + str(e))
示例#12
0
 def delete(self, index):
     """根据索引删除块"""
     try:
         self.mycol.delete_one({"header.index": index})
     except Exception as e:
         Log().error("删除失败:" + str(e))