# Path: coloredcoinlib/colordata.py
# class ThickColorData(StoredColorData):
#     """ Color data which needs access to the whole blockchain state"""
#     def __init__(self, *args, **kwargs):
#         super(ThickColorData, self).__init__(*args, **kwargs)
#         self.mempool_cache = []
# 
#     def get_colorvalues(self, color_id_set, txhash, outindex):
#         blockhash, found = self.blockchain_state.get_tx_blockhash(txhash)
#         if not found:
#             raise UnfoundTransactionError("Transaction %s not found!" % txhash)
#         if blockhash:
#             self.cdbuilder_manager.ensure_scanned_upto(color_id_set, blockhash)
#             return self._fetch_colorvalues(color_id_set, txhash, outindex)
#         else:
#             # not in the blockchain, but might be in the memory pool
#             best_blockhash = None
#             while 1:
#                 best_blockhash_prev = self.blockchain_state.get_best_blockhash()
#                 mempool = self.blockchain_state.get_mempool_txs()
#                 best_blockhash = self.blockchain_state.get_best_blockhash()
#                 if best_blockhash_prev == best_blockhash:
#                     break
#             if txhash not in [tx.hash for tx in mempool]:
#                 raise UnfoundTransactionError("Transaction %s not found in mempool!" % txhash)
#             # the preceding blockchain
#             self.cdbuilder_manager.ensure_scanned_upto(
#                 color_id_set, best_blockhash)
#             # scan everything in the mempool
#             for tx in mempool:
#                 self.cdbuilder_manager.scan_tx(color_id_set, tx)
#             return self._fetch_colorvalues(color_id_set, txhash, outindex)
# 
# class ThinColorData(StoredColorData):
#     """ Color data which needs access to the blockchain state up to the genesis of
#         color."""
#     def get_colorvalues(self, color_id_set, txhash, outindex):
#         """
#         for a given transaction <txhash> and output <outindex> and color
#         <color_id_set>, return a list of dicts that looks like this:
#         {
#         'color_id': <color id>,
#         'value': <colorvalue of this output>,
#         'label': <currently unused>,
#         }
#         These correspond to the colorvalues of particular color ids for this
#         output. Currently, each output should have a single element in the list.
#         """
#         color_def_map = self.cdbuilder_manager.get_color_def_map(color_id_set)
# 
#         scanned_outputs = set()
# 
#         def process(current_txhash, current_outindex):
#             """For any tx out, process the colorvalues of the affecting
#             inputs first and then scan that tx.
#             """
#             if (current_txhash, current_outindex) in scanned_outputs:
#                 return
#             scanned_outputs.add((current_txhash, current_outindex))
# 
#             if self._fetch_colorvalues(color_id_set, current_txhash, current_outindex):
#                 return
# 
#             current_tx = self.blockchain_state.get_tx(current_txhash)
#             if not current_tx:
#                 raise UnfoundTransactionError("Transaction %s not found!" % current_txhash)
# 
#             # note a genesis tx will simply have 0 affecting inputs
#             inputs = set()
#             for color_id, color_def in color_def_map.items():
#                 inputs = inputs.union(
#                     color_def.get_affecting_inputs(current_tx,
#                                                    [current_outindex]))
#             for i in inputs:
#                 process(i.prevout.hash, i.prevout.n)
#             self.cdbuilder_manager.scan_tx(color_id_set, current_tx, [current_outindex])
# 
#         process(txhash, outindex)
#         return self._fetch_colorvalues(color_id_set, txhash, outindex)
# 
# class UnfoundTransactionError(Exception):
#     pass


import unittest
from coloredcoinlib.colordata import (ThickColorData, ThinColorData,
                                      UnfoundTransactionError)
from test_colormap import MockColorMap
from test_txspec import MockTX

class MockStore:
    def get_any(self, a, b):
        return [(1, 5, ''), (1, 6, '')]


class TestColorData(unittest.TestCase):

    def setUp(self):
        builder = MockBuilder()
        blockchain = MockBlockchain()
        store = MockStore()
        colormap = MockColorMap()
        self.thick = ThickColorData(builder, blockchain, store, colormap)
        self.thin = ThinColorData(builder, blockchain, store, colormap)

    def test_thick(self):
        self.assertRaises(UnfoundTransactionError, self.thick.get_colorvalues,
                          set([1,2]), '', 0)
        cvs = self.thick.get_colorvalues(set([1,2]), '1', 0)
        self.assertEquals(cvs[0].get_value(), 5)
        self.assertEquals(cvs[1].get_value(), 6)
        cvs = self.thick.get_colorvalues(set([1,2]), '2', 0)
        self.assertEquals(cvs[0].get_value(), 5)
        self.assertEquals(cvs[1].get_value(), 6)

        self.assertRaises(UnfoundTransactionError, self.thick.get_colorvalues,
                          set([1,2]), '9', 0)

    def test_thin(self):

# Path: coloredcoinlib/colordata.py
# class ThickColorData(StoredColorData):
#     """ Color data which needs access to the whole blockchain state"""
#     def __init__(self, *args, **kwargs):
#         super(ThickColorData, self).__init__(*args, **kwargs)
#         self.mempool_cache = []
# 
#     def get_colorvalues(self, color_id_set, txhash, outindex):
#         blockhash, found = self.blockchain_state.get_tx_blockhash(txhash)
#         if not found:
#             raise UnfoundTransactionError("Transaction %s not found!" % txhash)
#         if blockhash:
#             self.cdbuilder_manager.ensure_scanned_upto(color_id_set, blockhash)
#             return self._fetch_colorvalues(color_id_set, txhash, outindex)
#         else:
#             # not in the blockchain, but might be in the memory pool
#             best_blockhash = None
#             while 1:
#                 best_blockhash_prev = self.blockchain_state.get_best_blockhash()
#                 mempool = self.blockchain_state.get_mempool_txs()
#                 best_blockhash = self.blockchain_state.get_best_blockhash()
#                 if best_blockhash_prev == best_blockhash:
#                     break
#             if txhash not in [tx.hash for tx in mempool]:
#                 raise UnfoundTransactionError("Transaction %s not found in mempool!" % txhash)
#             # the preceding blockchain
#             self.cdbuilder_manager.ensure_scanned_upto(
#                 color_id_set, best_blockhash)
#             # scan everything in the mempool
#             for tx in mempool:
#                 self.cdbuilder_manager.scan_tx(color_id_set, tx)
#             return self._fetch_colorvalues(color_id_set, txhash, outindex)
# 
# class ThinColorData(StoredColorData):
#     """ Color data which needs access to the blockchain state up to the genesis of
#         color."""
#     def get_colorvalues(self, color_id_set, txhash, outindex):
#         """
#         for a given transaction <txhash> and output <outindex> and color
#         <color_id_set>, return a list of dicts that looks like this:
#         {
#         'color_id': <color id>,
#         'value': <colorvalue of this output>,
#         'label': <currently unused>,
#         }
#         These correspond to the colorvalues of particular color ids for this
#         output. Currently, each output should have a single element in the list.
#         """
#         color_def_map = self.cdbuilder_manager.get_color_def_map(color_id_set)
# 
#         scanned_outputs = set()
# 
#         def process(current_txhash, current_outindex):
#             """For any tx out, process the colorvalues of the affecting
#             inputs first and then scan that tx.
#             """
#             if (current_txhash, current_outindex) in scanned_outputs:
#                 return
#             scanned_outputs.add((current_txhash, current_outindex))
# 
#             if self._fetch_colorvalues(color_id_set, current_txhash, current_outindex):
#                 return
# 
#             current_tx = self.blockchain_state.get_tx(current_txhash)
#             if not current_tx:
#                 raise UnfoundTransactionError("Transaction %s not found!" % current_txhash)
# 
#             # note a genesis tx will simply have 0 affecting inputs
#             inputs = set()
#             for color_id, color_def in color_def_map.items():
#                 inputs = inputs.union(
#                     color_def.get_affecting_inputs(current_tx,
#                                                    [current_outindex]))
#             for i in inputs:
#                 process(i.prevout.hash, i.prevout.n)
#             self.cdbuilder_manager.scan_tx(color_id_set, current_tx, [current_outindex])
# 
#         process(txhash, outindex)
#         return self._fetch_colorvalues(color_id_set, txhash, outindex)
# 
# class UnfoundTransactionError(Exception):
#     pass


# Path: coloredcoinlib/tests/test_colordata.py
import unittest
from coloredcoinlib.colordata import (ThickColorData, ThinColorData,
                                      UnfoundTransactionError)
from test_colormap import MockColorMap
from test_txspec import MockTX


class MockStore:
    def get_any(self, a, b):
        return [(1, 5, ''), (1, 6, '')]


class TestColorData(unittest.TestCase):

    def setUp(self):
        builder = MockBuilder()
        blockchain = MockBlockchain()
        store = MockStore()
        colormap = MockColorMap()
        self.thick = ThickColorData(builder, blockchain, store, colormap)
        self.thin = ThinColorData(builder, blockchain, store, colormap)

    def test_thick(self):
        self.assertRaises(UnfoundTransactionError, self.thick.get_colorvalues,
                          set([1,2]), '', 0)
        cvs = self.thick.get_colorvalues(set([1,2]), '1', 0)
        self.assertEquals(cvs[0].get_value(), 5)
        self.assertEquals(cvs[1].get_value(), 6)
        cvs = self.thick.get_colorvalues(set([1,2]), '2', 0)
        self.assertEquals(cvs[0].get_value(), 5)
        self.assertEquals(cvs[1].get_value(), 6)

        self.assertRaises(UnfoundTransactionError, self.thick.get_colorvalues,
                          set([1,2]), '9', 0)

    def test_thin(self):

        self.assertRaises(UnfoundTransactionError, self.thin.get_colorvalues,