# Path: pycoinnet/util/debug_help.py
# def _done_callback(f):
# def Task(*args, **kwargs):
# 
# 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):


from pycoinnet.util.debug_help import asyncio
from pycoinnet.InvItem import InvItem, ITEM_TYPE_BLOCK
    from pycoin.block import BlockHeader
import io
import logging

If a new block is mined, it should be added via the add_block
method of this class. It will then advertise it to peers so
it will propogate.

When a new peer comes online, invoke add_peer.

This object will then watch for getheaders, getblock and getdata messages
and handle them appropriately.
"""




# TODO: move to pycoin
def _header_for_block(block):
    f = io.BytesIO()
    block.stream(f)
    f.seek(0)
    return BlockHeader.parse(f)


class BlockHandler:
    def __init__(self, inv_collector, block_chain, block_store,
                 should_download_f=lambda block_hash, block_index: True,
                 block_validator=lambda block: True):
        self.inv_collector = inv_collector
        self.block_chain = block_chain
        self.block_store = block_store
        self.q = inv_collector.new_inv_item_queue()

# Path: pycoinnet/util/debug_help.py
# def _done_callback(f):
# def Task(*args, **kwargs):
# 
# 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/peergroup/BlockHandler.py
from pycoinnet.util.debug_help import asyncio
from pycoinnet.InvItem import InvItem, ITEM_TYPE_BLOCK
    from pycoin.block import BlockHeader
import io
import logging


If a new block is mined, it should be added via the add_block
method of this class. It will then advertise it to peers so
it will propogate.

When a new peer comes online, invoke add_peer.

This object will then watch for getheaders, getblock and getdata messages
and handle them appropriately.
"""




# TODO: move to pycoin
def _header_for_block(block):
    f = io.BytesIO()
    block.stream(f)
    f.seek(0)
    return BlockHeader.parse(f)


class BlockHandler:
    def __init__(self, inv_collector, block_chain, block_store,
                 should_download_f=lambda block_hash, block_index: True,
                 block_validator=lambda block: True):
        self.inv_collector = inv_collector
        self.block_chain = block_chain
        self.block_store = block_store
        self.q = inv_collector.new_inv_item_queue()

        self._watch_invcollector_task = asyncio.Task(self._watch_invcollector(block_validator))