# Path: pycoinnet/InvItem.py
# ITEM_TYPE_TX, ITEM_TYPE_BLOCK, ITEM_TYPE_MERKLEBLOCK = (1, 2, 3)
#         INV_TYPES = [None, "Tx", "Block", "Merkle"]
# class InvItem(object):
#     def __init__(self, item_type, data):
#     def __str__(self):
#     def __repr__(self):
#     def __hash__(self):
#     def __eq__(self, other):
#     def __ne__(self, other):
#     def stream(self, f):
#     def parse(self, f):
# 
# Path: pycoinnet/helpers/standards.py
# @asyncio.coroutine
# def do_get_headers(peer, block_locator_hashes, hash_stop=b'\0'*32):
#     peer.send_msg(message_name="getheaders", version=1, hashes=block_locator_hashes, hash_stop=hash_stop)
#     next_message = peer.new_get_next_message_f(lambda name, data: name == 'headers')
#     name, data = yield from next_message()
#     headers = [bh for bh, t in data["headers"]]
#     return headers
# 
# Path: pycoinnet/util/BlockChainView.py
# HASH_INITIAL_BLOCK = b'\0' * 32


import asyncio
import logging
import time
from pycoinnet.InvItem import InvItem, ITEM_TYPE_BLOCK
from pycoinnet.helpers.standards import do_get_headers
from pycoinnet.util.BlockChainView import HASH_INITIAL_BLOCK
"""
This code takes care of calling "getheaders" repeatedly to quickly
catch up the local copy of the block chain. It does not get full blocks.
"""




def getheaders_add_peer_f(blockchain_view, improved_blockchain_view_callback_f):
    """
    returns a function "add_peer"
        add_peer(peer, last_block_index)
    which accepts a BitcoinPeerProtocol peer and the last_block_index
    as reported in the initial handshake.
    """
    peer_queue = asyncio.PriorityQueue()

    @asyncio.coroutine
    def _fetch_missing(peer, header):
        next_message = peer.new_get_next_message_f(lambda msg, data: msg == 'block')
        the_hash = header.previous_block_hash

# Path: pycoinnet/InvItem.py
# ITEM_TYPE_TX, ITEM_TYPE_BLOCK, ITEM_TYPE_MERKLEBLOCK = (1, 2, 3)
#         INV_TYPES = [None, "Tx", "Block", "Merkle"]
# class InvItem(object):
#     def __init__(self, item_type, data):
#     def __str__(self):
#     def __repr__(self):
#     def __hash__(self):
#     def __eq__(self, other):
#     def __ne__(self, other):
#     def stream(self, f):
#     def parse(self, f):
# 
# Path: pycoinnet/helpers/standards.py
# @asyncio.coroutine
# def do_get_headers(peer, block_locator_hashes, hash_stop=b'\0'*32):
#     peer.send_msg(message_name="getheaders", version=1, hashes=block_locator_hashes, hash_stop=hash_stop)
#     next_message = peer.new_get_next_message_f(lambda name, data: name == 'headers')
#     name, data = yield from next_message()
#     headers = [bh for bh, t in data["headers"]]
#     return headers
# 
# Path: pycoinnet/util/BlockChainView.py
# HASH_INITIAL_BLOCK = b'\0' * 32


# Path: pycoinnet/peergroup/getheaders.py
import asyncio
import logging
import time
from pycoinnet.InvItem import InvItem, ITEM_TYPE_BLOCK
from pycoinnet.helpers.standards import do_get_headers
from pycoinnet.util.BlockChainView import HASH_INITIAL_BLOCK

"""
This code takes care of calling "getheaders" repeatedly to quickly
catch up the local copy of the block chain. It does not get full blocks.
"""




def getheaders_add_peer_f(blockchain_view, improved_blockchain_view_callback_f):
    """
    returns a function "add_peer"
        add_peer(peer, last_block_index)
    which accepts a BitcoinPeerProtocol peer and the last_block_index
    as reported in the initial handshake.
    """
    peer_queue = asyncio.PriorityQueue()

    @asyncio.coroutine
    def _fetch_missing(peer, header):
        next_message = peer.new_get_next_message_f(lambda msg, data: msg == 'block')
        the_hash = header.previous_block_hash

        peer.send_msg("getdata", items=[InvItem(ITEM_TYPE_BLOCK, the_hash)])