Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pagination issues for all-transactions #1865

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions safe_transaction_service/history/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@ def __init__(self, request: HttpRequest):

def set_count(self, value):
self.count = value


class DummyPagination(LimitOffsetPagination):
"""
Class to easily get limit and offset from a request, not intended to be used
as a pagination class
"""

def __init__(self, request: HttpRequest):
super().__init__()
self.request = request
self.limit = self.get_limit(request)
self.offset = self.get_offset(request)
self.count: int = 0

def set_count(self, value):
self.count = value
33 changes: 32 additions & 1 deletion safe_transaction_service/history/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@


class TestViews(SafeTestCaseMixin, APITestCase):
def setUp(self):
get_redis().flushall()

def tearDown(self):
get_redis().flushall()

def test_about_view(self):
url = reverse("v1:history:about")
response = self.client.get(url, format="json")
Expand Down Expand Up @@ -394,7 +400,7 @@ def test_all_transactions_ordering(self):
last_result["transaction_hash"], ethereum_tx_2_days_ago.tx_hash
)

def test_all_transactions_cache(self):
def test_all_transactions_cache_view(self):
safe_address = "0x54f3c8e4Bf7bFDFF39B36d1FAE4e5ceBdD93C6A9"
# Older transaction
factory_transactions = [
Expand Down Expand Up @@ -452,6 +458,31 @@ def test_all_transactions_cache(self):
)
self.assertEqual(response.data["count"], 3)

def test_all_transactions_cache_limit_offset_view(self):
"""
Test limit and offset
"""
safe_address = "0x54f3c8e4Bf7bFDFF39B36d1FAE4e5ceBdD93C6A9"
number_transactions = 100

for _ in range(number_transactions):
MultisigTransactionFactory(safe=safe_address)

for limit, offset in ((57, 12), (13, 24)):
with self.subTest(limit=limit, offset=offset):
# all-txs:{safe}:{executed}{queued}{trusted}:{limit}:{offset}:{ordering}:{relevant_elements}
cache_key = f"all-txs:0x54f3c8e4Bf7bFDFF39B36d1FAE4e5ceBdD93C6A9:100:{limit}:{offset}:execution_date:{number_transactions}"
redis = get_redis()
self.assertFalse(redis.exists(cache_key))

response = self.client.get(
reverse("v1:history:all-transactions", args=(safe_address,))
+ f"?executed=True&queued=False&trusted=False&ordering=execution_date&limit={limit}&offset={offset}"
)
self.assertEqual(response.data["count"], number_transactions)
self.assertEqual(len(response.data["results"]), limit)
self.assertTrue(redis.exists(cache_key))

def test_all_transactions_wrong_transfer_type_view(self):
# No token in database, so we must trust the event
safe_address = Account.create().address
Expand Down
5 changes: 3 additions & 2 deletions safe_transaction_service/history/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
SafeMasterCopy,
TransferDict,
)
from .pagination import ListPagination
from .pagination import DummyPagination
from .serializers import get_data_decoded_from_data
from .services import (
BalanceServiceProvider,
Expand Down Expand Up @@ -357,6 +357,7 @@ def get_cached_page_tx_identifiers(
self.paginator.offset = offset
self.paginator.request = self.request
return page

page = self.get_page_tx_identifiers(
safe, executed, queued, trusted, ordering, limit, offset
)
Expand All @@ -372,7 +373,7 @@ def list(self, request, *args, **kwargs):
executed, queued, trusted = self.get_parameters()
ordering = self.get_ordering_parameter()
# Trick to get limit and offset
list_pagination = ListPagination(self.request)
list_pagination = DummyPagination(self.request)
limit, offset = list_pagination.limit, list_pagination.offset

tx_identifiers_page = self.get_cached_page_tx_identifiers(
Expand Down
Loading