@@ -51,18 +51,17 @@ async def mark_confirmed_data(chain_name, tx_hash, height):
5151 }
5252
5353
54- async def delayed_incoming (message , chain_name = None , tx_hash = None , height = None ):
54+ async def delayed_incoming (
55+ message , tx_context : Optional [TxContext ] = None , check_message : bool = True
56+ ):
5557 if message is None :
5658 return
59+
5760 await PendingMessage .collection .insert_one (
5861 {
5962 "message" : message ,
60- "source" : dict (
61- chain_name = chain_name ,
62- tx_hash = tx_hash ,
63- height = height ,
64- check_message = True , # should we store this?
65- ),
63+ "tx_context" : asdict (tx_context ) if tx_context is not None else None ,
64+ "check_message" : check_message ,
6665 }
6766 )
6867
@@ -75,25 +74,13 @@ class IncomingStatus(IntEnum):
7574
7675async def mark_message_for_retry (
7776 message : Dict ,
78- chain_name : Optional [str ],
79- tx_hash : Optional [str ],
80- height : Optional [int ],
77+ tx_context : Optional [TxContext ],
8178 check_message : bool ,
8279 retrying : bool ,
8380 existing_id ,
8481):
8582 if not retrying :
86- await PendingMessage .collection .insert_one (
87- {
88- "message" : message ,
89- "source" : dict (
90- chain_name = chain_name ,
91- tx_hash = tx_hash ,
92- height = height ,
93- check_message = check_message , # should we store this?
94- ),
95- }
96- )
83+ await delayed_incoming (message , tx_context , check_message )
9784 else :
9885 LOGGER .debug (f"Incrementing for { existing_id } " )
9986 result = await PendingMessage .collection .update_one (
@@ -104,9 +91,7 @@ async def mark_message_for_retry(
10491
10592async def incoming (
10693 message : Dict ,
107- chain_name : Optional [str ] = None ,
108- tx_hash : Optional [str ] = None ,
109- height : Optional [int ] = None ,
94+ tx_context : Optional [TxContext ] = None ,
11095 seen_ids : Optional [Dict [Tuple , int ]] = None ,
11196 check_message : bool = False ,
11297 retrying : bool = False ,
@@ -120,11 +105,12 @@ async def incoming(
120105
121106 item_hash = message ["item_hash" ]
122107 sender = message ["sender" ]
108+ chain_name = tx_context .chain_name if tx_context is not None else None
123109 ids_key = (item_hash , sender , chain_name )
124110
125- if chain_name and tx_hash and height and seen_ids is not None :
111+ if tx_context is not None and seen_ids is not None :
126112 if ids_key in seen_ids .keys ():
127- if height > seen_ids [ids_key ]:
113+ if tx_context . height > seen_ids [ids_key ]:
128114 return IncomingStatus .MESSAGE_HANDLED , []
129115
130116 filters = {
@@ -143,7 +129,7 @@ async def incoming(
143129 # check/sanitize the message if needed
144130 try :
145131 message = await check_message_fn (
146- message , from_chain = (chain_name is not None )
132+ message , from_chain = (tx_context is not None )
147133 )
148134 except InvalidMessageError :
149135 return IncomingStatus .FAILED_PERMANENTLY , []
@@ -164,16 +150,14 @@ async def incoming(
164150 # existing = await Message.collection.find_one(
165151 # filters, projection={'confirmed': 1, 'confirmations': 1, 'time': 1})
166152
167- if chain_name and tx_hash and height :
153+ if tx_context is not None :
168154 # We are getting a confirmation here
169- new_values = await mark_confirmed_data (chain_name , tx_hash , height )
170-
171155 updates = {
172156 "$set" : {
173157 "confirmed" : True ,
174158 },
175159 "$min" : {"time" : message ["time" ]},
176- "$addToSet" : {"confirmations" : new_values [ "confirmations" ][ 0 ] },
160+ "$addToSet" : {"confirmations" : asdict ( tx_context ) },
177161 }
178162 else :
179163 updates = {
@@ -186,14 +170,14 @@ async def incoming(
186170 # new_values = {'confirmed': False} # this should be our default.
187171 should_commit = False
188172 if existing :
189- if seen_ids is not None and height is not None :
173+ if seen_ids is not None and tx_context is not None :
190174 if ids_key in seen_ids .keys ():
191- if height > seen_ids [ids_key ]:
175+ if tx_context . height > seen_ids [ids_key ]:
192176 return IncomingStatus .MESSAGE_HANDLED , []
193177 else :
194- seen_ids [ids_key ] = height
178+ seen_ids [ids_key ] = tx_context . height
195179 else :
196- seen_ids [ids_key ] = height
180+ seen_ids [ids_key ] = tx_context . height
197181
198182 # THIS CODE SHOULD BE HERE...
199183 # But, if a race condition appeared, we might have the message twice.
@@ -203,7 +187,7 @@ async def incoming(
203187
204188 LOGGER .debug ("Updating %s." % item_hash )
205189
206- if chain_name and tx_hash and height :
190+ if tx_context is not None :
207191 # we need to update messages adding the confirmation
208192 # await Message.collection.update_many(filters, updates)
209193 should_commit = True
@@ -224,9 +208,7 @@ async def incoming(
224208 LOGGER .exception ("Can't get content of object %r" % item_hash )
225209 await mark_message_for_retry (
226210 message = message ,
227- chain_name = chain_name ,
228- tx_hash = tx_hash ,
229- height = height ,
211+ tx_context = tx_context ,
230212 check_message = check_message ,
231213 retrying = retrying ,
232214 existing_id = existing_id ,
@@ -266,9 +248,7 @@ async def incoming(
266248 LOGGER .debug ("Message type handler has failed, retrying later." )
267249 await mark_message_for_retry (
268250 message = message ,
269- chain_name = chain_name ,
270- tx_hash = tx_hash ,
271- height = height ,
251+ tx_context = tx_context ,
272252 check_message = check_message ,
273253 retrying = retrying ,
274254 existing_id = existing_id ,
@@ -286,14 +266,14 @@ async def incoming(
286266 LOGGER .warning ("Invalid sender for %s" % item_hash )
287267 return IncomingStatus .MESSAGE_HANDLED , []
288268
289- if seen_ids is not None and height is not None :
269+ if seen_ids is not None and tx_context is not None :
290270 if ids_key in seen_ids .keys ():
291- if height > seen_ids [ids_key ]:
271+ if tx_context . height > seen_ids [ids_key ]:
292272 return IncomingStatus .MESSAGE_HANDLED , []
293273 else :
294- seen_ids [ids_key ] = height
274+ seen_ids [ids_key ] = tx_context . height
295275 else :
296- seen_ids [ids_key ] = height
276+ seen_ids [ids_key ] = tx_context . height
297277
298278 LOGGER .debug ("New message to store for %s." % item_hash )
299279 # message.update(new_values)
0 commit comments