@@ -54,18 +54,17 @@ async def mark_confirmed_data(chain_name, tx_hash, height):
5454 }
5555
5656
57- async def delayed_incoming (message , chain_name = None , tx_hash = None , height = None ):
57+ async def delayed_incoming (
58+ message , tx_context : Optional [TxContext ] = None , check_message : bool = True
59+ ):
5860 if message is None :
5961 return
62+
6063 await PendingMessage .collection .insert_one (
6164 {
6265 "message" : message ,
63- "source" : dict (
64- chain_name = chain_name ,
65- tx_hash = tx_hash ,
66- height = height ,
67- check_message = True , # should we store this?
68- ),
66+ "tx_context" : asdict (tx_context ) if tx_context is not None else None ,
67+ "check_message" : check_message ,
6968 }
7069 )
7170
@@ -78,25 +77,13 @@ class IncomingStatus(IntEnum):
7877
7978async def mark_message_for_retry (
8079 message : Dict ,
81- chain_name : Optional [str ],
82- tx_hash : Optional [str ],
83- height : Optional [int ],
80+ tx_context : Optional [TxContext ],
8481 check_message : bool ,
8582 retrying : bool ,
8683 existing_id ,
8784):
8885 if not retrying :
89- await PendingMessage .collection .insert_one (
90- {
91- "message" : message ,
92- "source" : dict (
93- chain_name = chain_name ,
94- tx_hash = tx_hash ,
95- height = height ,
96- check_message = check_message , # should we store this?
97- ),
98- }
99- )
86+ await delayed_incoming (message , tx_context , check_message )
10087 else :
10188 LOGGER .debug (f"Incrementing for { existing_id } " )
10289 result = await PendingMessage .collection .update_one (
@@ -124,9 +111,7 @@ def update_message_item_type(message_dict: Dict) -> Dict:
124111
125112async def incoming (
126113 message : Dict ,
127- chain_name : Optional [str ] = None ,
128- tx_hash : Optional [str ] = None ,
129- height : Optional [int ] = None ,
114+ tx_context : Optional [TxContext ] = None ,
130115 seen_ids : Optional [Dict [Tuple , int ]] = None ,
131116 check_message : bool = False ,
132117 retrying : bool = False ,
@@ -144,11 +129,12 @@ async def incoming(
144129
145130 item_hash = message ["item_hash" ]
146131 sender = message ["sender" ]
132+ chain_name = tx_context .chain if tx_context is not None else None
147133 ids_key = (item_hash , sender , chain_name )
148134
149- if chain_name and tx_hash and height and seen_ids is not None :
135+ if tx_context is not None and seen_ids is not None :
150136 if ids_key in seen_ids .keys ():
151- if height > seen_ids [ids_key ]:
137+ if tx_context . height > seen_ids [ids_key ]:
152138 return IncomingStatus .MESSAGE_HANDLED , []
153139
154140 filters = {
@@ -167,7 +153,7 @@ async def incoming(
167153 # check/sanitize the message if needed
168154 try :
169155 message = await check_message_fn (
170- message , from_chain = (chain_name is not None )
156+ message , from_chain = (tx_context is not None )
171157 )
172158 except InvalidMessageError :
173159 return IncomingStatus .FAILED_PERMANENTLY , []
@@ -188,16 +174,14 @@ async def incoming(
188174 # existing = await Message.collection.find_one(
189175 # filters, projection={'confirmed': 1, 'confirmations': 1, 'time': 1})
190176
191- if chain_name and tx_hash and height :
177+ if tx_context is not None :
192178 # We are getting a confirmation here
193- new_values = await mark_confirmed_data (chain_name , tx_hash , height )
194-
195179 updates = {
196180 "$set" : {
197181 "confirmed" : True ,
198182 },
199183 "$min" : {"time" : message ["time" ]},
200- "$addToSet" : {"confirmations" : new_values [ "confirmations" ][ 0 ] },
184+ "$addToSet" : {"confirmations" : asdict ( tx_context ) },
201185 }
202186 else :
203187 updates = {
@@ -210,14 +194,14 @@ async def incoming(
210194 # new_values = {'confirmed': False} # this should be our default.
211195 should_commit = False
212196 if existing :
213- if seen_ids is not None and height is not None :
197+ if seen_ids is not None and tx_context is not None :
214198 if ids_key in seen_ids .keys ():
215- if height > seen_ids [ids_key ]:
199+ if tx_context . height > seen_ids [ids_key ]:
216200 return IncomingStatus .MESSAGE_HANDLED , []
217201 else :
218- seen_ids [ids_key ] = height
202+ seen_ids [ids_key ] = tx_context . height
219203 else :
220- seen_ids [ids_key ] = height
204+ seen_ids [ids_key ] = tx_context . height
221205
222206 # THIS CODE SHOULD BE HERE...
223207 # But, if a race condition appeared, we might have the message twice.
@@ -227,7 +211,7 @@ async def incoming(
227211
228212 LOGGER .debug ("Updating %s." % item_hash )
229213
230- if chain_name and tx_hash and height :
214+ if tx_context is not None :
231215 # we need to update messages adding the confirmation
232216 # await Message.collection.update_many(filters, updates)
233217 should_commit = True
@@ -248,9 +232,7 @@ async def incoming(
248232 LOGGER .exception ("Can't get content of object %r" % item_hash )
249233 await mark_message_for_retry (
250234 message = message ,
251- chain_name = chain_name ,
252- tx_hash = tx_hash ,
253- height = height ,
235+ tx_context = tx_context ,
254236 check_message = check_message ,
255237 retrying = retrying ,
256238 existing_id = existing_id ,
@@ -290,9 +272,7 @@ async def incoming(
290272 LOGGER .debug ("Message type handler has failed, retrying later." )
291273 await mark_message_for_retry (
292274 message = message ,
293- chain_name = chain_name ,
294- tx_hash = tx_hash ,
295- height = height ,
275+ tx_context = tx_context ,
296276 check_message = check_message ,
297277 retrying = retrying ,
298278 existing_id = existing_id ,
@@ -310,14 +290,14 @@ async def incoming(
310290 LOGGER .warning ("Invalid sender for %s" % item_hash )
311291 return IncomingStatus .MESSAGE_HANDLED , []
312292
313- if seen_ids is not None and height is not None :
293+ if seen_ids is not None and tx_context is not None :
314294 if ids_key in seen_ids .keys ():
315- if height > seen_ids [ids_key ]:
295+ if tx_context . height > seen_ids [ids_key ]:
316296 return IncomingStatus .MESSAGE_HANDLED , []
317297 else :
318- seen_ids [ids_key ] = height
298+ seen_ids [ids_key ] = tx_context . height
319299 else :
320- seen_ids [ids_key ] = height
300+ seen_ids [ids_key ] = tx_context . height
321301
322302 LOGGER .debug ("New message to store for %s." % item_hash )
323303 # message.update(new_values)
0 commit comments