Skip to content

Commit 30f04bf

Browse files
author
andraz maier
committed
refactor: clean up get_requested_round_id
1 parent 4899454 commit 30f04bf

File tree

5 files changed

+23
-25
lines changed

5 files changed

+23
-25
lines changed

fdc/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ def response(self):
7777
abi = json.loads(self.abi)
7878
a = {"inputs": [abi], "type": "function"}
7979
c = decode_transaction_data(
80-
a, EMPTY_METHOD_IDENTIFIER + self.response_hex, [abi_bytes_to_hex] # type: ignore
81-
)
80+
a,
81+
EMPTY_METHOD_IDENTIFIER + self.response_hex,
82+
[abi_bytes_to_hex], # type: ignore
83+
)
8284

8385
return c["data"]
8486

fdc/serializers/v1/query.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from rest_framework import serializers
22

3-
from configuration.config import config
4-
53

64
class ListAttestationResultV1QuerySerializer(serializers.Serializer):
75
voting_round_id = serializers.IntegerField(

ftso/models.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from eth_abi.abi import encode
55
from web3 import Web3
66

7-
from configuration.config import config
87
from processing.client.types import FtsoRandomResponse, FtsoVotingResponse
98
from processing.utils import un_prefix_0x
109

ftso/serializers/query.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class FeedResultAvailableFeedsQuerySerializer(serializers.Serializer):
77
help_text="Voting round. Defaults to latest.",
88
)
99

10+
1011
class FeedResultFeedsWithProofsQuerySerializer(serializers.Serializer):
1112
voting_round_id = serializers.IntegerField(
1213
required=False,

ftso/views.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ def anchor_feed_names(self, request, *args, **kwargs):
4040
_query_params.is_valid(raise_exception=True)
4141
query_params = _query_params.validated_data
4242

43-
voting_round_id = get_requested_round_id(
44-
query_params.get("voting_round_id", None)
45-
)
43+
voting_round_id = get_requested_round_id(query_params.get("voting_round_id"))
4644
if voting_round_id is None:
47-
return response.Response(None, status=status.HTTP_404_NOT_FOUND)
45+
return response.Response(
46+
data={"error": "no voting rounds in database"},
47+
status=status.HTTP_404_NOT_FOUND,
48+
)
4849

4950
logger.debug(f"Querying for available feeds for round: {voting_round_id}")
5051

@@ -88,7 +89,10 @@ def anchor_feeds_with_proof(self, request, *args, **kwargs):
8889

8990
voting_round_id = get_requested_round_id(query_params.get("voting_round_id"))
9091
if voting_round_id is None:
91-
return response.Response(None, status=status.HTTP_404_NOT_FOUND)
92+
return response.Response(
93+
data={"error": "no voting rounds in database"},
94+
status=status.HTTP_404_NOT_FOUND,
95+
)
9296

9397
feed_ids = list(map(un_prefix_0x, body["feed_ids"]))
9498

@@ -97,7 +101,7 @@ def anchor_feeds_with_proof(self, request, *args, **kwargs):
97101
.filter(voting_round_id=voting_round_id)
98102
.filter(feed_id__in=feed_ids)
99103
)
100-
# TODO: (Andraz) For consistency, consider returning an empty list instead of an error.
104+
# TODO: (andraz) For consistency, consider returning an empty list instead of an error.
101105
if queryset is None:
102106
return response.Response(
103107
data={"error": "anchor feeds not found"},
@@ -118,29 +122,23 @@ def anchor_feeds_with_proof(self, request, *args, **kwargs):
118122

119123

120124
# Utils
121-
# TODO:(luka) Also handle too early rounds
122125
def get_requested_round_id(query_voting_round_id: int | None) -> int | None:
123-
latest_round = FeedResult.objects.latest("voting_round_id")
124-
if query_voting_round_id is None:
125-
if latest_round is None:
126-
# TODO:(luka) we have no data, error/none
127-
return None
126+
if isinstance(query_voting_round_id, int):
127+
return query_voting_round_id
128+
129+
try:
130+
latest_round = FeedResult.objects.latest("voting_round_id")
128131
return latest_round.voting_round_id
129-
query_voting_round_id = int(query_voting_round_id)
130-
if query_voting_round_id > latest_round.voting_round_id:
131-
# Querying for a round that does not exist (ie is not indexed yet)
132-
# TODO:(luka) We can handle this differently
133-
logger.debug("Querying for a round that does not yet exist")
134-
query_voting_round_id = latest_round.voting_round_id
135-
return query_voting_round_id
132+
except FeedResult.DoesNotExist:
133+
return None
136134

137135

138-
# TODO:(luka) WIP
139136
def get_merkle_tree_for_round(voting_round_id: int) -> MerkleTree:
140137
queryset = FeedResult.objects.filter(voting_round_id=voting_round_id)
141138
random = RandomResult.objects.filter(voting_round_id=voting_round_id).first()
142139
if random is None:
143140
raise ValueError("No random result for this round")
141+
144142
a = [v.hash.hex() for v in queryset]
145143
b = random.hash.hex()
146144
return MerkleTree([b, *a])

0 commit comments

Comments
 (0)