(self, peer)
Call this method when a peer comes online.
Call this method when a peer comes online.
Call this method when a peer comes online .
def add_peer(self, peer):
        """
        Call this method when a peer comes online.
        """
        @asyncio.coroutine
        def _run_handle_get(next_message):
            while True:
                name, data = yield from next_message()
                if name == 'getblocks':
                    continue
                    # TODO: get this working
                    # block_hashes = _prep_block_hashes(data.get("hash_stop"))
                    # if block_hashes:
                    #    peer.send_msg("headers", headers=block_hashes)
                if name == 'getheaders':
                    headers = self._prep_headers(data.get("hash_stop"))
                    if headers:
                        logging.debug("sending %d headers", len(headers))
                        peer.send_msg("headers", headers=headers)
                if name == 'getdata':
                    inv_items = data["items"]
                    not_found = []
                    blocks_found = []
                    for inv_item in inv_items:
                        if inv_item.item_type != ITEM_TYPE_BLOCK:
                            continue
                        block = self.block_store.get(inv_item.data)
                        if block:
                            blocks_found.append(block)
                        else:
                            not_found.append(inv_item)
                    if not_found:
                        logging.debug("could not find %d blocks", len(not_found))
                        peer.send_msg("notfound", items=not_found)
                    logging.debug("sending %d blocks", len(blocks_found))
                    for block in blocks_found:
                        logging.debug("sending block %s", block.id())
                        peer.send_msg("block", block=block)

        next_message = peer.new_get_next_message_f(
            lambda name, data: name in ['getheaders', 'getblocks', 'getdata'])
        peer.add_task(_run_handle_get(next_message))
def add_peer ( self , peer ) : @ asyncio . coroutine def _run_handle_get ( next_message ) : while True : name , data = yield from next_message ( ) if name == 'getblocks' : continue # TODO: get this working # block_hashes = _prep_block_hashes(data.get("hash_stop")) # if block_hashes: #    peer.send_msg("headers", headers=block_hashes) if name == 'getheaders' : headers = self . _prep_headers ( data . get ( "hash_stop" ) ) if headers : logging . debug ( "sending %d headers" , len ( headers ) ) peer . send_msg ( "headers" , headers = headers ) if name == 'getdata' : inv_items = data [ "items" ] not_found = [ ] blocks_found = [ ] for inv_item in inv_items : if inv_item . item_type != ITEM_TYPE_BLOCK : continue block = self . block_store . get ( inv_item . data ) if block : blocks_found . append ( block ) else : not_found . append ( inv_item ) if not_found : logging . debug ( "could not find %d blocks" , len ( not_found ) ) peer . send_msg ( "notfound" , items = not_found ) logging . debug ( "sending %d blocks" , len ( blocks_found ) ) for block in blocks_found : logging . debug ( "sending block %s" , block . id ( ) ) peer . send_msg ( "block" , block = block ) next_message = peer . new_get_next_message_f ( lambda name , data : name in [ 'getheaders' , 'getblocks' , 'getdata' ] ) peer . add_task ( _run_handle_get ( next_message ) )