예제 #1
0
class TestColorSet(unittest.TestCase):
    def setUp(self):
        self.colormap = MockColorMap()
        d = self.colormap.d
        self.colorset0 = ColorSet(self.colormap, [d[0]])
        self.colorset1 = ColorSet(self.colormap, [d[1]])
        self.colorset2 = ColorSet(self.colormap, [d[2]])
        self.colorset3 = ColorSet(self.colormap, [d[1], d[2]])
        self.colorset4 = ColorSet(self.colormap, [d[3], d[2]])
        self.colorset5 = ColorSet(self.colormap, [d[3], d[1]])
        self.colorset6 = ColorSet(self.colormap, [])

    def test_repr(self):
        self.assertEquals(self.colorset0.__repr__(), "['']")
        self.assertEquals(self.colorset1.__repr__(),
                          "['obc:color_desc_1:0:0']")
        self.assertEquals(self.colorset3.__repr__(),
                          "['obc:color_desc_1:0:0', 'obc:color_desc_2:0:1']")

    def test_uncolored_only(self):
        self.assertTrue(self.colorset0.uncolored_only())
        self.assertTrue(not self.colorset1.uncolored_only())
        self.assertTrue(not self.colorset3.uncolored_only())

    def test_get_data(self):
        self.assertEquals(self.colorset0.get_data(), [""])
        self.assertEquals(self.colorset1.get_data(), ["obc:color_desc_1:0:0"])

    def test_get_hash_string(self):
        self.assertEquals(
            self.colorset0.get_hash_string(),
            "055539df4a0b804c58caf46c0cd2941af10d64c1395ddd8e50b5f55d945841e6")
        self.assertEquals(
            self.colorset1.get_hash_string(),
            "ca90284eaa79e05d5971947382214044fe64f1bdc2e97040cfa9f90da3964a14")
        self.assertEquals(
            self.colorset3.get_hash_string(),
            "09f731f25cf5bfaad512d4ee6f37cb9481f442df3263b15725dd1624b4678557")

    def test_get_earliest(self):
        self.assertEquals(self.colorset5.get_earliest(),
                          "obc:color_desc_1:0:0")
        self.assertEquals(self.colorset4.get_earliest(),
                          "obc:color_desc_2:0:1")
        self.assertEquals(self.colorset6.get_earliest(), "\x00\x00\x00\x00")

    def test_get_color_string(self):
        self.assertEquals(self.colorset0.get_color_hash(), "JNu4AFCBNmTE1")
        self.assertEquals(self.colorset1.get_color_hash(), "CP4YWLr8aAe4Hn")
        self.assertEquals(self.colorset3.get_color_hash(), "ZUTSoEEwZY6PB")

    def test_has_color_id(self):
        self.assertTrue(self.colorset0.has_color_id(0))
        self.assertTrue(self.colorset3.has_color_id(1))
        self.assertTrue(not self.colorset1.has_color_id(0))
        self.assertTrue(not self.colorset4.has_color_id(1))

    def test_intersects(self):
        self.assertTrue(not self.colorset0.intersects(self.colorset1))
        self.assertTrue(self.colorset1.intersects(self.colorset3))
        self.assertTrue(self.colorset3.intersects(self.colorset1))
        self.assertTrue(self.colorset4.intersects(self.colorset3))
        self.assertTrue(not self.colorset2.intersects(self.colorset0))
        self.assertTrue(not self.colorset1.intersects(self.colorset4))

    def test_equals(self):
        self.assertTrue(not self.colorset1.equals(self.colorset0))
        self.assertTrue(self.colorset3.equals(self.colorset3))
        self.assertTrue(not self.colorset4.equals(self.colorset5))

    def test_from_color_ids(self):
        self.assertTrue(
            self.colorset0.equals(ColorSet.from_color_ids(self.colormap, [0])))
        self.assertTrue(
            self.colorset3.equals(
                ColorSet.from_color_ids(self.colormap, [1, 2])))
        tmp = ColorSet.from_color_ids(self.colormap, [1, 2, 3])
        self.assertTrue(tmp.has_color_id(1))
        self.assertTrue(tmp.has_color_id(2))
        self.assertTrue(tmp.has_color_id(3))
예제 #2
0
파일: asset.py 프로젝트: F483/ngcccbase
class AssetDefinition(object):
    """Stores the definition of a particular asset, including its color set,
    it's name (moniker), and the wallet model that represents it.
    """
    def __init__(self, colormap, params):
        """Create an Asset for a color map <colormap> and configuration
        <params>. Note params has the color definitions used for this
        Asset.
        """
        self.colormap = colormap
        self.monikers = params.get('monikers', [])
        # currently only single-color assets are supported
        assert len(params.get('color_set')) == 1
        self.color_set = ColorSet(colormap, params.get('color_set'))
        self.unit = int(params.get('unit', 1))

    def __repr__(self):
        return "%s: %s" % (self.monikers, self.color_set)

    def get_id(self):
        return self.color_set.get_color_hash()

    def get_all_ids(self):
        return [self.get_id()]

    def get_monikers(self):
        """Returns the list of monikers for this asset.
        """
        return self.monikers

    def get_color_id(self):
        return list(self.get_color_set().color_id_set)[0]

    def has_color_id(self, color_id):
        return self.get_color_set().has_color_id(color_id)

    def get_color_set(self):
        """Returns the list of colors for this asset.
        """
        return self.color_set

    def get_color_def(self):
        color_set = self.get_color_set()
        assert len(color_set.color_desc_list) == 1
        return self.colormap.get_color_def(color_set.color_desc_list[0])

    def get_null_colorvalue(self):
        cd = self.get_color_def()
        return SimpleColorValue(colordef=cd, value=0)

    def get_colorvalue(self, utxo):
        """ return colorvalue for a given utxo"""
        if utxo.colorvalues:
            for cv in utxo.colorvalues:
                if self.has_color_id(cv.get_color_id()):
                    return cv
        raise Exception("Cannot get colorvalue for UTXO!")

    def validate_value(self, portion):
        """Returns True if the portion is an exact multiple of the Asset atoms.
        """
        if isinstance(portion, ColorValue) or isinstance(portion, AssetValue):
            portion = portion.get_value()
        atom = Decimal("1") / Decimal(self.unit)
        return Decimal(portion) % atom == Decimal("0")

    def parse_value(self, portion):
        """Returns actual number of Satoshis for this Asset
        given the <portion> of the asset.
        """
        return int(Decimal(portion) * Decimal(self.unit))

    def format_value(self, value):
        """Returns a string representation of the portion of the asset.
        can involve rounding.  doesn't display insignificant zeros
        """
        if isinstance(value, ColorValue) or isinstance(value, AssetValue):
            value = value.get_value()
        return str(Decimal(value) / Decimal(self.unit))

    def get_atom(self):
        return self.format_value(1)

    def get_data(self):
        """Returns a JSON-compatible object that represents this Asset
        """
        return {
            "monikers": self.monikers,
            "assetid" : self.get_color_set().get_color_hash(),
            "color_set": self.color_set.get_data(),
            "unit": self.unit
            }
예제 #3
0
class AssetDefinition(object):
    """Stores the definition of a particular asset, including its color set,
    it's name (moniker), and the wallet model that represents it.
    """
    def __init__(self, colormap, params):
        """Create an Asset for a color map <colormap> and configuration
        <params>. Note params has the color definitions used for this
        Asset.
        """
        self.colormap = colormap
        self.monikers = params.get('monikers', [])
        # currently only single-color assets are supported
        assert len(params.get('color_set')) == 1
        self.color_set = ColorSet(colormap, params.get('color_set'))
        self.unit = int(params.get('unit', 1))

    def __repr__(self):
        return "%s: %s" % (self.monikers, self.color_set)

    def get_id(self):
        return self.color_set.get_color_hash()

    def get_all_ids(self):
        return [self.get_id()]

    def get_monikers(self):
        """Returns the list of monikers for this asset.
        """
        return self.monikers

    def has_color_id(self, color_id):
        return self.get_color_set().has_color_id(color_id)

    def get_color_set(self):
        """Returns the list of colors for this asset.
        """
        return self.color_set

    def get_null_colorvalue(self):
        color_set = self.get_color_set()
        assert len(color_set.color_desc_list) == 1
        cd = self.colormap.get_color_def(color_set.color_desc_list[0])
        return SimpleColorValue(colordef=cd, value=0)

    def get_colorvalue(self, utxo):
        """ return colorvalue for a given utxo"""
        if utxo.colorvalues:
            for cv in utxo.colorvalues:
                if self.has_color_id(cv.get_color_id()):
                    return cv
        raise Exception("cannot get colorvalue for UTXO: "
                        "no colorvalues available")

    def parse_value(self, portion):
        """Returns actual number of Satoshis for this Asset
        given the <portion> of the asset.
        """
        return int(float(portion) * self.unit)

    def format_value(self, value):
        """Returns a string representation of the portion of the asset.
        can involve rounding.  doesn't display insignificant zeros
        """
        if isinstance(value, ColorValue) or isinstance(value, AssetValue):
            atoms = value.get_value()
        else:
            atoms = value
        return '{0:g}'.format(atoms / float(self.unit))

    def get_data(self):
        """Returns a JSON-compatible object that represents this Asset
        """
        return {
            "monikers": self.monikers,
            "color_set": self.color_set.get_data(),
            "unit": self.unit
        }
예제 #4
0
class TestColorSet(unittest.TestCase):
    def setUp(self):
        self.colormap = MockColorMap()
        d = self.colormap.d
        self.colorset0 = ColorSet(self.colormap, [""])
        self.colorset1 = ColorSet(self.colormap, [d[1]])
        self.colorset2 = ColorSet(self.colormap, [d[2]])
        self.colorset3 = ColorSet(self.colormap, [d[1], d[2]])
        self.colorset4 = ColorSet(self.colormap, [d[3], d[2]])
        self.colorset5 = ColorSet(self.colormap, [d[3], d[1]])
        self.colorset6 = ColorSet(self.colormap, [])

    def test_repr(self):
        self.assertEquals(self.colorset0.__repr__(), "['']")
        self.assertEquals(self.colorset1.__repr__(), "['obc:color_desc_1:0:0']")
        self.assertEquals(self.colorset3.__repr__(), "['obc:color_desc_1:0:0', 'obc:color_desc_2:0:1']")

    def test_uncolored_only(self):
        self.assertTrue(self.colorset0.uncolored_only())
        self.assertFalse(self.colorset1.uncolored_only())
        self.assertFalse(self.colorset3.uncolored_only())

    def test_get_data(self):
        self.assertEquals(self.colorset0.get_data(), [""])
        self.assertEquals(self.colorset1.get_data(), ["obc:color_desc_1:0:0"])

    def test_get_hash_string(self):
        self.assertEquals(
            self.colorset0.get_hash_string(), "055539df4a0b804c58caf46c0cd2941af10d64c1395ddd8e50b5f55d945841e6"
        )
        self.assertEquals(
            self.colorset1.get_hash_string(), "ca90284eaa79e05d5971947382214044fe64f1bdc2e97040cfa9f90da3964a14"
        )
        self.assertEquals(
            self.colorset3.get_hash_string(), "09f731f25cf5bfaad512d4ee6f37cb9481f442df3263b15725dd1624b4678557"
        )

    def test_get_earliest(self):
        self.assertEquals(self.colorset5.get_earliest(), "obc:color_desc_1:0:0")
        self.assertEquals(self.colorset4.get_earliest(), "obc:color_desc_2:0:1")
        self.assertEquals(self.colorset6.get_earliest(), "\x00\x00\x00\x00")

    def test_get_color_string(self):
        self.assertEquals(self.colorset1.get_color_hash(), "CP4YWLr8aAe4Hn")
        self.assertEquals(self.colorset3.get_color_hash(), "ZUTSoEEwZY6PB")

    def test_has_color_id(self):
        self.assertTrue(self.colorset0.has_color_id(0))
        self.assertTrue(self.colorset3.has_color_id(1))
        self.assertFalse(self.colorset1.has_color_id(0))
        self.assertFalse(self.colorset4.has_color_id(1))

    def test_intersects(self):
        self.assertFalse(self.colorset0.intersects(self.colorset1))
        self.assertTrue(self.colorset1.intersects(self.colorset3))
        self.assertTrue(self.colorset3.intersects(self.colorset1))
        self.assertTrue(self.colorset4.intersects(self.colorset3))
        self.assertFalse(self.colorset2.intersects(self.colorset0))
        self.assertFalse(self.colorset1.intersects(self.colorset4))

    def test_equals(self):
        self.assertFalse(self.colorset1.equals(self.colorset0))
        self.assertTrue(self.colorset3.equals(self.colorset3))
        self.assertFalse(self.colorset4.equals(self.colorset5))

    def test_from_color_ids(self):
        self.assertTrue(self.colorset0.equals(ColorSet.from_color_ids(self.colormap, [0])))
        self.assertTrue(self.colorset3.equals(ColorSet.from_color_ids(self.colormap, [1, 2])))
        tmp = ColorSet.from_color_ids(self.colormap, [1, 2, 3])
        self.assertTrue(tmp.has_color_id(1))
        self.assertTrue(tmp.has_color_id(2))
        self.assertTrue(tmp.has_color_id(3))