# 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 get_headers_hashes(peer, until_block_hash):
#     hashes = [until_block_hash]
#     peer.send_msg(message_name="getheaders", version=1, hashes=hashes, hash_stop=until_block_hash)
#     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


import asyncio
import logging
import time
from pycoinnet.InvItem import InvItem, ITEM_TYPE_BLOCK
from pycoinnet.helpers.standards import get_headers_hashes
    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, blockchain):
        next_message = peer.new_get_next_message_f(lambda msg, data: msg == 'block')
        ops = []
        for h in blockchain.chain_finder.missing_parents():
            peer.send_msg("getdata", items=[InvItem(ITEM_TYPE_BLOCK, h)])
            msg, data = yield from next_message()
            block = data["block"]
            ops = blockchain.add_headers([block])
            if len(ops) > 0:
                break
        return ops

    @asyncio.coroutine
    def _run_ff(blockchain):
        # this kind of works, but it's lame because we put
        # peers into a queue, so they'll never be garbage collected
        # even if they vanish. I think.
        while 1:
            priority, (peer, lbi, rate_dict) = yield from peer_queue.get()
            if lbi - blockchain.length() > 0:
                # let's get some headers from this guy!
                start_time = time.time()
                h = blockchain.last_block_hash()
                try:

# 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 get_headers_hashes(peer, until_block_hash):
#     hashes = [until_block_hash]
#     peer.send_msg(message_name="getheaders", version=1, hashes=hashes, hash_stop=until_block_hash)
#     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/peergroup/fast_forwarder.py
import asyncio
import logging
import time
from pycoinnet.InvItem import InvItem, ITEM_TYPE_BLOCK
from pycoinnet.helpers.standards import get_headers_hashes

    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, blockchain):
        next_message = peer.new_get_next_message_f(lambda msg, data: msg == 'block')
        ops = []
        for h in blockchain.chain_finder.missing_parents():
            peer.send_msg("getdata", items=[InvItem(ITEM_TYPE_BLOCK, h)])
            msg, data = yield from next_message()
            block = data["block"]
            ops = blockchain.add_headers([block])
            if len(ops) > 0:
                break
        return ops

    @asyncio.coroutine
    def _run_ff(blockchain):
        # this kind of works, but it's lame because we put
        # peers into a queue, so they'll never be garbage collected
        # even if they vanish. I think.
        while 1:
            priority, (peer, lbi, rate_dict) = yield from peer_queue.get()
            if lbi - blockchain.length() > 0:
                # let's get some headers from this guy!
                start_time = time.time()
                h = blockchain.last_block_hash()
                try:

                    headers = yield from asyncio.wait_for(get_headers_hashes(peer, h), timeout=10)