@@ -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
122125def 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
139136def 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