Skip to content

Commit ab8b836

Browse files
authored
[Fix] Set default item type early (#298)
Fix for the previous fix. Some messages end up with check_message set to False, bypassing the validation in `incoming`. We now set the default value of `item_type` as the first thing in the function. Note that this is a temporary fix that will disappear as soon as we use Pydantic models everywhere.
1 parent 9e685b7 commit ab8b836

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/aleph/chains/common.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from enum import IntEnum
66
from typing import Dict, Optional, Tuple, List
77

8-
from aleph_message.models import MessageType
8+
from aleph_message.models import MessageType, ItemType
99
from bson import ObjectId
1010
from pymongo import UpdateOne
1111

@@ -28,6 +28,7 @@
2828
from aleph.storage import get_json, pin_hash, add_json, get_message_content
2929
from .tx_context import TxContext
3030
from ..schemas.pending_messages import BasePendingMessage
31+
from ..utils import item_type_from_hash
3132

3233
LOGGER = logging.getLogger("chains.common")
3334

@@ -104,6 +105,23 @@ async def mark_message_for_retry(
104105
LOGGER.debug(f"Update result {result}")
105106

106107

108+
def update_message_item_type(message_dict: Dict) -> Dict:
109+
"""
110+
Ensures that the item_type field of a message is present.
111+
Sets it to the default value if the field is not specified.
112+
"""
113+
if "item_type" in message_dict:
114+
return message_dict
115+
116+
if "item_content" in message_dict:
117+
item_type = ItemType.inline
118+
else:
119+
item_type = item_type_from_hash(message_dict["item_hash"])
120+
121+
message_dict["item_type"] = item_type
122+
return message_dict
123+
124+
107125
async def incoming(
108126
message: Dict,
109127
chain_name: Optional[str] = None,
@@ -120,6 +138,10 @@ async def incoming(
120138
if existing in database, created if not.
121139
"""
122140

141+
# TODO: this is a temporary fix to set the item_type of the message to the correct
142+
# value. This should be replaced by a full use of Pydantic models.
143+
message = update_message_item_type(message)
144+
123145
item_hash = message["item_hash"]
124146
sender = message["sender"]
125147
ids_key = (item_hash, sender, chain_name)

src/aleph/network.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ async def check_message(
6969

7070
message = parse_message(message_dict)
7171

72-
# TODO: this is a temporary fix to set the item_type of the message to the correct
73-
# value. This should be replaced by a full use of Pydantic models.
74-
message_dict["item_type"] = message.item_type.value
75-
7672
if trusted:
7773
# only in the case of a message programmatically built here
7874
# from legacy native chain signing for example (signing offloaded)

0 commit comments

Comments
 (0)