2022-03-12 09:48:19 +01:00
|
|
|
# standard imports
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import unittest
|
2022-03-12 13:49:38 +01:00
|
|
|
import math
|
2022-03-12 09:48:19 +01:00
|
|
|
|
|
|
|
# external imports
|
|
|
|
from hexathon import add_0x
|
2022-03-12 13:49:38 +01:00
|
|
|
from chainlib.chain import ChainSpec
|
2022-03-12 09:48:19 +01:00
|
|
|
|
|
|
|
# local imports
|
|
|
|
from chainqueue import QueueEntry
|
|
|
|
from chainqueue.cache import (
|
|
|
|
CacheTokenTx,
|
2022-03-12 13:49:38 +01:00
|
|
|
CacheFilter,
|
2022-03-12 09:48:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# test imports
|
|
|
|
from tests.base_shep import TestShepBase
|
2022-03-12 15:12:02 +01:00
|
|
|
from tests.common import (
|
|
|
|
MockTokenCache,
|
2022-03-12 15:22:55 +01:00
|
|
|
MockCacheTokenTx,
|
2022-03-12 15:12:02 +01:00
|
|
|
)
|
2022-03-12 09:48:19 +01:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
2022-03-12 13:49:38 +01:00
|
|
|
class MockNormalizer:
|
|
|
|
|
|
|
|
def address(self, v):
|
|
|
|
return 'address' + v
|
|
|
|
|
|
|
|
|
|
|
|
def value(self, v):
|
|
|
|
dv = int(math.log10(v) + 1)
|
|
|
|
return float(v / (10 ** dv))
|
|
|
|
|
|
|
|
|
|
|
|
def hash(self, v):
|
|
|
|
return 'ashbashhash' + v
|
2022-03-12 13:02:45 +01:00
|
|
|
|
|
|
|
|
2022-03-12 09:48:19 +01:00
|
|
|
class TestCache(TestShepBase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TestCache, self).setUp()
|
2022-03-12 13:49:38 +01:00
|
|
|
self.chain_spec = ChainSpec('foo', 'bar', 42, 'baz')
|
|
|
|
self.cache = MockTokenCache()
|
|
|
|
|
|
|
|
|
|
|
|
def test_cache_instance(self):
|
|
|
|
normalizer = MockNormalizer()
|
|
|
|
a = b'foo'
|
|
|
|
tx = MockCacheTokenTx(normalizer=normalizer)
|
|
|
|
tx.deserialize(a)
|
|
|
|
self.assertTrue(isinstance(tx.value, float))
|
|
|
|
self.assertEqual(tx.sender[:4], 'addr')
|
|
|
|
self.assertEqual(tx.recipient[:4], 'addr')
|
|
|
|
self.assertEqual(tx.tx_hash[:11], 'ashbashhash')
|
2022-03-12 09:48:19 +01:00
|
|
|
|
2022-03-12 13:49:38 +01:00
|
|
|
|
|
|
|
def test_cache_putget(self):
|
2022-03-12 09:48:19 +01:00
|
|
|
a = b'foo'
|
2022-03-12 13:49:38 +01:00
|
|
|
tx = MockCacheTokenTx()
|
|
|
|
tx.deserialize(a)
|
|
|
|
self.cache.put(self.chain_spec, tx)
|
|
|
|
tx_retrieved = self.cache.get(self.chain_spec, tx.tx_hash)
|
|
|
|
self.assertEqual(tx, tx_retrieved)
|
|
|
|
|
|
|
|
|
|
|
|
def test_cache_filter(self):
|
|
|
|
normalizer = MockNormalizer()
|
|
|
|
fltr = CacheFilter(normalizer=normalizer)
|
|
|
|
|
|
|
|
sender = os.urandom(20).hex()
|
|
|
|
fltr.add_senders(sender)
|
|
|
|
|
|
|
|
recipient_one = os.urandom(20).hex()
|
|
|
|
recipient_two = os.urandom(20).hex()
|
|
|
|
fltr.add_recipients([recipient_one, recipient_two])
|
|
|
|
|
|
|
|
self.assertEqual(fltr.senders[0][:4], 'addr')
|
|
|
|
self.assertEqual(fltr.recipients[1][:4], 'addr')
|
|
|
|
|
|
|
|
|
|
|
|
def test_cache_query(self):
|
|
|
|
a = os.urandom(20).hex()
|
|
|
|
fltr = CacheFilter(nonce=42)
|
|
|
|
self.cache.count(fltr)
|
|
|
|
self.assertEqual(self.cache.last_filter, fltr)
|
2022-03-12 09:48:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|