11import asyncio
22import json
33import logging
4- from dataclasses import asdict
54from enum import IntEnum
65from typing import Dict , Optional , Tuple , List
76
8- from aleph_message .models import MessageConfirmation
97from bson import ObjectId
108from pymongo import UpdateOne
119
2523from aleph .model .pending import PendingMessage , PendingTX
2624from aleph .network import verify_signature
2725from aleph .permissions import check_sender_authorization
28- from aleph .storage import get_json , pin_hash , add_json , get_message_content
29- from .tx_context import TxContext
30- from aleph .schemas .pending_messages import (
31- BasePendingMessage ,
32- )
26+ from aleph .schemas .pending_messages import BasePendingMessage
3327from aleph .schemas .validated_message import (
3428 validate_pending_message ,
3529 ValidatedStoreMessage ,
3630 ValidatedForgetMessage ,
3731 make_confirmation_update_query ,
38- make_message_upsert_query ,
32+ make_message_upsert_query ,
3933)
34+ from ..schemas .message_confirmation import MessageConfirmation
35+ from aleph .storage import get_json , pin_hash , add_json , get_message_content
36+ from .tx_context import TxContext
4037
4138LOGGER = logging .getLogger ("chains.common" )
4239
@@ -64,21 +61,17 @@ async def mark_confirmed_data(chain_name, tx_hash, height):
6461
6562async def delayed_incoming (
6663 message : BasePendingMessage ,
67- chain_name : Optional [str ] = None ,
68- tx_hash : Optional [str ] = None ,
69- height : Optional [int ] = None ,
64+ tx_context : Optional [TxContext ] = None ,
65+ check_message : bool = True ,
7066):
7167 if message is None :
7268 return
69+
7370 await PendingMessage .collection .insert_one (
7471 {
7572 "message" : message .dict (exclude = {"content" }),
76- "source" : dict (
77- chain_name = chain_name ,
78- tx_hash = tx_hash ,
79- height = height ,
80- check_message = True , # should we store this?
81- ),
73+ "tx_context" : tx_context .dict () if tx_context else None ,
74+ "check_message" : check_message ,
8275 }
8376 )
8477
@@ -91,27 +84,15 @@ class IncomingStatus(IntEnum):
9184
9285async def mark_message_for_retry (
9386 message : BasePendingMessage ,
94- chain_name : Optional [str ],
95- tx_hash : Optional [str ],
96- height : Optional [int ],
87+ tx_context : Optional [TxContext ],
9788 check_message : bool ,
9889 retrying : bool ,
9990 existing_id ,
10091):
10192 message_dict = message .dict (exclude = {"content" })
10293
10394 if not retrying :
104- await PendingMessage .collection .insert_one (
105- {
106- "message" : message_dict ,
107- "source" : dict (
108- chain_name = chain_name ,
109- tx_hash = tx_hash ,
110- height = height ,
111- check_message = check_message , # should we store this?
112- ),
113- }
114- )
95+ await delayed_incoming (message , tx_context , check_message )
11596 else :
11697 LOGGER .debug (f"Incrementing for { existing_id } " )
11798 result = await PendingMessage .collection .update_one (
@@ -122,9 +103,7 @@ async def mark_message_for_retry(
122103
123104async def incoming (
124105 pending_message : BasePendingMessage ,
125- chain_name : Optional [str ] = None ,
126- tx_hash : Optional [str ] = None ,
127- height : Optional [int ] = None ,
106+ tx_context : Optional [TxContext ] = None ,
128107 seen_ids : Optional [Dict [Tuple , int ]] = None ,
129108 check_message : bool = False ,
130109 retrying : bool = False ,
@@ -139,16 +118,23 @@ async def incoming(
139118 item_hash = pending_message .item_hash
140119 sender = pending_message .sender
141120 confirmations = []
121+ chain_name = tx_context .chain if tx_context is not None else None
142122 ids_key = (item_hash , sender , chain_name )
143123
144- if chain_name and tx_hash and height :
124+ if tx_context :
145125 if seen_ids is not None :
146126 if ids_key in seen_ids .keys ():
147- if height > seen_ids [ids_key ]:
127+ if tx_context . height > seen_ids [ids_key ]:
148128 return IncomingStatus .MESSAGE_HANDLED , []
149129
150130 confirmations .append (
151- MessageConfirmation (chain = chain_name , hash = tx_hash , height = height )
131+ MessageConfirmation (
132+ chain = tx_context .chain ,
133+ hash = tx_context .hash ,
134+ height = tx_context .height ,
135+ time = tx_context .time ,
136+ publisher = tx_context .publisher ,
137+ )
152138 )
153139
154140 filters = {
@@ -178,14 +164,14 @@ async def incoming(
178164 updates : Dict [str , Dict ] = {}
179165
180166 if existing :
181- if seen_ids is not None and height is not None :
167+ if seen_ids is not None and tx_context is not None :
182168 if ids_key in seen_ids .keys ():
183- if height > seen_ids [ids_key ]:
169+ if tx_context . height > seen_ids [ids_key ]:
184170 return IncomingStatus .MESSAGE_HANDLED , []
185171 else :
186- seen_ids [ids_key ] = height
172+ seen_ids [ids_key ] = tx_context . height
187173 else :
188- seen_ids [ids_key ] = height
174+ seen_ids [ids_key ] = tx_context . height
189175
190176 LOGGER .debug ("Updating %s." % item_hash )
191177
@@ -205,17 +191,17 @@ async def incoming(
205191 LOGGER .exception ("Can't get content of object %r" % item_hash )
206192 await mark_message_for_retry (
207193 message = pending_message ,
208- chain_name = chain_name ,
209- tx_hash = tx_hash ,
210- height = height ,
194+ tx_context = tx_context ,
211195 check_message = check_message ,
212196 retrying = retrying ,
213197 existing_id = existing_id ,
214198 )
215199 return IncomingStatus .RETRYING_LATER , []
216200
217201 validated_message = validate_pending_message (
218- pending_message = pending_message , content = content , confirmations = confirmations
202+ pending_message = pending_message ,
203+ content = content ,
204+ confirmations = confirmations ,
219205 )
220206
221207 # warning: those handlers can modify message and content in place
@@ -244,9 +230,7 @@ async def incoming(
244230 LOGGER .debug ("Message type handler has failed, retrying later." )
245231 await mark_message_for_retry (
246232 message = pending_message ,
247- chain_name = chain_name ,
248- tx_hash = tx_hash ,
249- height = height ,
233+ tx_context = tx_context ,
250234 check_message = check_message ,
251235 retrying = retrying ,
252236 existing_id = existing_id ,
@@ -264,14 +248,14 @@ async def incoming(
264248 LOGGER .warning ("Invalid sender for %s" % item_hash )
265249 return IncomingStatus .MESSAGE_HANDLED , []
266250
267- if seen_ids is not None and height is not None :
251+ if seen_ids is not None and tx_context is not None :
268252 if ids_key in seen_ids .keys ():
269- if height > seen_ids [ids_key ]:
253+ if tx_context . height > seen_ids [ids_key ]:
270254 return IncomingStatus .MESSAGE_HANDLED , []
271255 else :
272- seen_ids [ids_key ] = height
256+ seen_ids [ids_key ] = tx_context . height
273257 else :
274- seen_ids [ids_key ] = height
258+ seen_ids [ids_key ] = tx_context . height
275259
276260 LOGGER .debug ("New message to store for %s." % item_hash )
277261
@@ -386,5 +370,5 @@ async def incoming_chaindata(content: Dict, context: TxContext):
386370 For now we only add it to the database, it will be processed later.
387371 """
388372 await PendingTX .collection .insert_one (
389- {"content" : content , "context" : asdict ( context )}
373+ {"content" : content , "context" : context . dict ( )}
390374 )
0 commit comments