Exemplo n.º 1
0
    def test_local_cache(self):
        """
    Installing a cache only for a single instance of
    :py:class:`AddressGenerator`.
    """
        mock_generate_address = Mock(return_value=self.addy)

        with patch(
                'cornode.crypto.addresses.AddressGenerator._generate_address',
                mock_generate_address,
        ):
            generator1 = AddressGenerator(Seed.random())

            # Install the cache locally.
            generator1.cache = MemoryAddressCache()

            addy1 = generator1.get_addresses(42)
            mock_generate_address.assert_called_once()

            # The second time we try to generate the same address, it is
            # fetched from the cache.
            addy2 = generator1.get_addresses(42)
            mock_generate_address.assert_called_once()
            self.assertEqual(addy2, addy1)

            # Create a new instance to verify it has its own cache.
            generator2 = AddressGenerator(generator1.seed)

            # The generator has its own cache instance, so even though the
            # resulting address is the same, it is not fetched from cache.
            addy3 = generator2.get_addresses(42)
            self.assertEqual(mock_generate_address.call_count, 2)
            self.assertEqual(addy3, addy1)
Exemplo n.º 2
0
    def test_global_cache(self):
        """
    Installing a cache that affects all :py:class:`AddressGenerator`
    instances.
    """
        # Install the cache globally.
        AddressGenerator.cache = MemoryAddressCache()

        mock_generate_address = Mock(return_value=self.addy)

        with patch(
                'cornode.crypto.addresses.AddressGenerator._generate_address',
                mock_generate_address,
        ):
            generator1 = AddressGenerator(Seed.random())

            addy1 = generator1.get_addresses(42)
            mock_generate_address.assert_called_once()

            # The second time we try to generate the same address, it is
            # fetched from the cache.
            addy2 = generator1.get_addresses(42)
            mock_generate_address.assert_called_once()
            self.assertEqual(addy2, addy1)

            # Create a new AddressGenerator and verify it uses the same
            # cache.
            generator2 = AddressGenerator(generator1.seed)

            # Cache is global, so the cached address is returned again.
            addy3 = generator2.get_addresses(42)
            mock_generate_address.assert_called_once()
            self.assertEqual(addy3, addy1)
Exemplo n.º 3
0
    def test_get_addresses_error_step_zero(self):
        """
    Providing a ``step`` value of 0 to ``get_addresses``.
    """
        ag = AddressGenerator(seed=b'')

        with self.assertRaises(ValueError):
            ag.get_addresses(start=0, step=0)
Exemplo n.º 4
0
    def test_get_addresses_error_count_too_small(self):
        """
    Providing a ``count`` value less than 1 to ``get_addresses``.

    :py:class:`AddressGenerator` can potentially generate an infinite
    number of addresses, so there is no "end" to offset against.
    """
        ag = AddressGenerator(seed=b'')

        with self.assertRaises(ValueError):
            ag.get_addresses(start=0, count=0)
Exemplo n.º 5
0
    def test_cache_miss_seed(self):
        """
    Cached addresses are keyed by seed.
    """
        AddressGenerator.cache = MemoryAddressCache()

        mock_generate_address = Mock(return_value=self.addy)

        with patch(
                'cornode.crypto.addresses.AddressGenerator._generate_address',
                mock_generate_address,
        ):
            generator1 = AddressGenerator(Seed.random())
            generator1.get_addresses(42)
            mock_generate_address.assert_called_once()

            generator2 = AddressGenerator(Seed.random())
            generator2.get_addresses(42)
            self.assertEqual(mock_generate_address.call_count, 2)
Exemplo n.º 6
0
    def test_get_addresses_single(self):
        """
    Generating a single address.
    """
        # Seed is not important for this test; it is only used by
        # :py:class:`KeyGenerator`, which we will mock in this test.
        ag = AddressGenerator(seed=b'')

        # noinspection PyUnresolvedReferences
        with patch.object(ag, '_get_digest', self._mock_get_digest):
            addresses = ag.get_addresses(start=0)

        self.assertListEqual(addresses, [self.addy0])

        # noinspection PyUnresolvedReferences
        with patch.object(ag, '_get_digest', self._mock_get_digest):
            # You can provide any positive integer as the ``start`` value.
            addresses = ag.get_addresses(start=2)

        self.assertListEqual(addresses, [self.addy2])
Exemplo n.º 7
0
    def test_get_addresses_multiple(self):
        """
    Generating multiple addresses in one go.
    """
        # Seed is not important for this test; it is only used by
        # :py:class:`KeyGenerator`, which we will mock in this test.
        ag = AddressGenerator(seed=b'')

        # noinspection PyUnresolvedReferences
        with patch.object(ag, '_get_digest', self._mock_get_digest):
            addresses = ag.get_addresses(start=1, count=2)

        self.assertListEqual(addresses, [self.addy1, self.addy2])
Exemplo n.º 8
0
    def _find_addresses(self, seed, index, count):
        """
    Find addresses matching the command parameters.
    """
        # type: (Seed, int, Optional[int]) -> List[Address]
        generator = AddressGenerator(seed)

        if count is None:
            # Connect to Tangle and find the first address without any
            # transactions.
            for addy in generator.create_iterator(start=index):
                response = FindTransactionsCommand(
                    self.adapter)(addresses=[addy])

                if not response.get('hashes'):
                    return [addy]

        return generator.get_addresses(start=index, count=count)
Exemplo n.º 9
0
    def test_get_addresses_step_negative(self):
        """
    Providing a negative ``step`` value to ``get_addresses``.

    This is probably a weird use case, but what the heck.
    """
        # Seed is not important for this test; it is only used by
        # :py:class:`KeyGenerator`, which we will mock in this test.
        ag = AddressGenerator(seed=b'')

        # noinspection PyUnresolvedReferences
        with patch.object(ag, '_get_digest', self._mock_get_digest):
            addresses = ag.get_addresses(start=1, count=2, step=-1)

        self.assertListEqual(
            addresses,

            # This is the same as ``ag.get_addresses(start=0, count=2)``, but
            # the order is reversed.
            [self.addy1, self.addy0],
        )
Exemplo n.º 10
0
 def get_address():
     generator = AddressGenerator(seed)
     generated.extend(generator.get_addresses(0))