diff --git a/apps/cic-cache/cic_cache/cache.py b/apps/cic-cache/cic_cache/cache.py index 33d23f8b..c8fae935 100644 --- a/apps/cic-cache/cic_cache/cache.py +++ b/apps/cic-cache/cic_cache/cache.py @@ -113,35 +113,21 @@ class BloomCache(Cache): class DataCache(Cache): - def load_transactions_with_data_index(self, offset, limit, block_offset=None, block_limit=None, oldest=False): - if limit == 0: - limit = DEFAULT_LIMIT - rows = list_transactions_mined_with_data_index(self.session, offset, limit, block_offset, block_limit, oldest=oldest) - return self.__load_transactions(rows) - - def load_transactions_with_data(self, offset, limit, block_offset=None, block_limit=None, oldest=False): if limit == 0: limit = DEFAULT_LIMIT rows = list_transactions_mined_with_data(self.session, offset, limit, block_offset, block_limit, oldest=oldest) - return self.__load_transactions(rows) + return self.__process_rows(rows) - def load_transactions_account_with_data_index(self, address, offset, limit, block_offset=None, block_limit=None, oldest=False): - if limit == 0: - limit = DEFAULT_LIMIT - rows = list_transactions_account_mined_with_data_index(self.session, address, offset, limit, block_offset, block_limit, oldest=oldest) - return self.__load_transactions(rows) - - def load_transactions_account_with_data(self, address, offset, limit, block_offset=None, block_limit=None, oldest=False): if limit == 0: limit = DEFAULT_LIMIT rows = list_transactions_account_mined_with_data(self.session, address, offset, limit, block_offset, block_limit, oldest=oldest) - return self.__load_transactions(rows) + return self.__process_rows(rows) - def __load_transactions(self, rows): + def __process_rows(self, rows): tx_cache = [] highest_block = -1; lowest_block = -1; diff --git a/apps/cic-cache/cic_cache/tasks/tx.py b/apps/cic-cache/cic_cache/tasks/tx.py index 525dac84..7524fe6f 100644 --- a/apps/cic-cache/cic_cache/tasks/tx.py +++ b/apps/cic-cache/cic_cache/tasks/tx.py @@ -45,15 +45,9 @@ def tx_filter_content(self, offset, limit, address=None, block_offset=None, bloc c = DataCache(session) b = None if address == None: - if block_offset: - (lowest_block, highest_block, tx_cache) = c.load_transactions_with_data(offset, limit, block_offset=block_offset, block_limit=block_limit, oldest=oldest) - else: - (lowest_block, highest_block, tx_cache) = c.load_transactions_with_data_index(offset, limit, block_offset=block_offset, block_limit=block_limit) + (lowest_block, highest_block, tx_cache) = c.load_transactions_with_data(offset, limit, block_offset=block_offset, block_limit=block_limit, oldest=oldest) else: - if block_offset: - (lowest_block, highest_block, tx_cache) = c.load_transactions_account_with_data(address, offset, limit, block_offset=block_offset, block_limit=block_limit) - else: - (lowest_block, highest_block, tx_cache) = c.load_transactions_account_with_data_index(address, offset, limit, block_offset=block_offset, block_limit=block_limit) + (lowest_block, highest_block, tx_cache) = c.load_transactions_account_with_data_index(address, offset, limit, block_offset=block_offset, block_limit=block_limit) session.close() diff --git a/apps/cic-cache/tests/conftest.py b/apps/cic-cache/tests/conftest.py index 61db702f..c158034e 100644 --- a/apps/cic-cache/tests/conftest.py +++ b/apps/cic-cache/tests/conftest.py @@ -64,7 +64,6 @@ def txs( dt.timestamp(), ) - tx_number = 42 tx_hash_second = '0x' + os.urandom(32).hex() tx_signed_second = '0x' + os.urandom(128).hex() @@ -93,6 +92,43 @@ def txs( ] +@pytest.fixture(scope='function') +def more_txs( + init_database, + list_defaults, + list_actors, + list_tokens, + txs, + ): + + session = init_database + + tx_number = 666 + tx_hash = '0x' + os.urandom(32).hex() + tx_signed = '0x' + os.urandom(128).hex() + nonce = 3 + + dt = datetime.datetime.utcnow() + dt += datetime.timedelta(hours=1) + db.add_transaction( + session, + tx_hash, + list_defaults['block']+2, + tx_number, + list_actors['alice'], + list_actors['diane'], + list_tokens['bar'], + list_tokens['bar'], + 2048, + 4096, + False, + dt.timestamp(), + ) + + session.commit() + + return txs + [tx_hash] + @pytest.fixture(scope='function') def tag_txs( init_database, diff --git a/apps/cic-cache/tests/test_cache.py b/apps/cic-cache/tests/test_cache.py index 8b3146fc..c3eed0ca 100644 --- a/apps/cic-cache/tests/test_cache.py +++ b/apps/cic-cache/tests/test_cache.py @@ -58,33 +58,11 @@ def test_cache_ranges( list_defaults, list_actors, list_tokens, - txs, + more_txs, ): session = init_database - - tx_number = 666 - tx_hash_second = '0x' + os.urandom(32).hex() - tx_signed_second = '0x' + os.urandom(128).hex() - nonce = 3 - - dt = datetime.datetime.utcnow() - dt += datetime.timedelta(hours=1) - db.add_transaction( - session, - tx_hash_second, - list_defaults['block']+2, - tx_number, - list_actors['alice'], - list_actors['diane'], - list_tokens['bar'], - list_tokens['bar'], - 2048, - 4096, - False, - dt.timestamp(), - ) - + oldest = list_defaults['block'] - 1 mid = list_defaults['block'] newest = list_defaults['block'] + 2 @@ -149,4 +127,77 @@ def test_cache_ranges( assert b[1] == oldest +def test_cache_ranges_data( + init_database, + list_defaults, + list_actors, + list_tokens, + txs, + ): + session = init_database + + oldest = list_defaults['block'] - 1 + mid = list_defaults['block'] + newest = list_defaults['block'] + 2 + + c = DataCache(session) + b = c.load_transactions_with_data(0, 100) + assert len(b) == 3 + + +# b = c.load_transactions(1, 2) +# assert b[0] == oldest +# assert b[1] == mid +# +# b = c.load_transactions(0, 2) +# assert b[0] == mid +# assert b[1] == newest +# +# b = c.load_transactions(0, 1) +# assert b[0] == newest +# assert b[1] == newest +# +# b = c.load_transactions(0, 100, oldest=True) +# assert b[0] == oldest +# assert b[1] == newest +# +# b = c.load_transactions(0, 100, block_offset=list_defaults['block']) +# assert b[0] == mid +# assert b[1] == newest +# +# b = c.load_transactions(0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block']) +# assert b[0] == oldest +# assert b[1] == mid +# +# # now check when supplying account +# b = c.load_transactions_account(list_actors['alice'], 0, 100) +# assert b[0] == oldest +# assert b[1] == newest +# +# b = c.load_transactions_account(list_actors['bob'], 0, 100) +# assert b[0] == mid +# assert b[1] == mid +# +# b = c.load_transactions_account(list_actors['diane'], 0, 100) +# assert b[0] == oldest +# assert b[1] == newest +# +# # add block filter to the mix +# b = c.load_transactions_account(list_actors['alice'], 0, 100, block_offset=list_defaults['block']) +# assert b[0] == mid +# assert b[1] == newest +# +# b = c.load_transactions_account(list_actors['alice'], 0, 100, block_offset=list_defaults['block']) +# assert b[0] == mid +# assert b[1] == newest +# +# b = c.load_transactions_account(list_actors['bob'], 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block']) +# assert b[0] == mid +# assert b[1] == mid +# +# b = c.load_transactions_account(list_actors['diane'], 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block']) +# assert b[0] == oldest +# assert b[1] == oldest +# +#