Beispiel #1
0
 async def on_latest_block(self, sid, data):
     """Client informs us of its new block"""
     # from yadacoin.block import Block  # Circular reference. Not good! - Do we need the object here?
     self.app_log.info('WS latest-block: {} {}'.format(
         sid, json.dumps(data)))
     # TODO: handle a dict here to store the consensus state
     # self.latest_peer_block = Block.from_dict(data)
     if not self.peers.syncing:
         async with self.session(sid) as session:
             peer = Peer(session['ip'], session['port'])
         self.app_log.debug("Trying to sync on latest block from {}".format(
             peer.to_string()))
         my_index = self.config.BU.get_latest_block()['index']
         if data['index'] == my_index + 1:
             self.app_log.debug(
                 "Next index, trying to merge from {}".format(
                     peer.to_string()))
             if await self.consensus.process_next_block(data, peer):
                 pass
                 # if ok, block was inserted and event triggered by import block
                 # await self.peers.on_block_insert(data)
         elif data['index'] > my_index + 1:
             self.app_log.debug(
                 "Missing blocks between {} and {} , asking more to {}".
                 format(my_index, data['index'], peer.to_string()))
             data = {
                 "start_index": my_index + 1,
                 "end_index": my_index + 1 + CHAIN.MAX_BLOCKS_PER_MESSAGE
             }
             await self.emit('get_blocks', data=data, room=sid)
         else:
             # Remove later on
             self.app_log.debug(
                 "Old or same index, ignoring {} from {}".format(
                     data['index'], peer.to_string()))
 async def send_it(self, txn_dict: dict, peer: Peer):
     try:
         if self.config.debug:
             self.app_log.debug(
                 'Transmitting pool payout transaction to: {}'.format(
                     peer.to_string()))
         await peer.client.client.emit('newtransaction',
                                       data=txn_dict,
                                       namespace='/chat')
     except Exception as e:
         if self.config.debug:
             self.app_log.debug(e)