Skip to content

Commit

Permalink
[Fix] Set default item type early (#298)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
odesenfans authored Jun 6, 2022
1 parent 9e685b7 commit ab8b836
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
24 changes: 23 additions & 1 deletion src/aleph/chains/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from enum import IntEnum
from typing import Dict, Optional, Tuple, List

from aleph_message.models import MessageType
from aleph_message.models import MessageType, ItemType
from bson import ObjectId
from pymongo import UpdateOne

Expand All @@ -28,6 +28,7 @@
from aleph.storage import get_json, pin_hash, add_json, get_message_content
from .tx_context import TxContext
from ..schemas.pending_messages import BasePendingMessage
from ..utils import item_type_from_hash

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

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


def update_message_item_type(message_dict: Dict) -> Dict:
"""
Ensures that the item_type field of a message is present.
Sets it to the default value if the field is not specified.
"""
if "item_type" in message_dict:
return message_dict

if "item_content" in message_dict:
item_type = ItemType.inline
else:
item_type = item_type_from_hash(message_dict["item_hash"])

message_dict["item_type"] = item_type
return message_dict


async def incoming(
message: Dict,
chain_name: Optional[str] = None,
Expand All @@ -120,6 +138,10 @@ async def incoming(
if existing in database, created if not.
"""

# TODO: this is a temporary fix to set the item_type of the message to the correct
# value. This should be replaced by a full use of Pydantic models.
message = update_message_item_type(message)

item_hash = message["item_hash"]
sender = message["sender"]
ids_key = (item_hash, sender, chain_name)
Expand Down
4 changes: 0 additions & 4 deletions src/aleph/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ async def check_message(

message = parse_message(message_dict)

# TODO: this is a temporary fix to set the item_type of the message to the correct
# value. This should be replaced by a full use of Pydantic models.
message_dict["item_type"] = message.item_type.value

if trusted:
# only in the case of a message programmatically built here
# from legacy native chain signing for example (signing offloaded)
Expand Down

0 comments on commit ab8b836

Please sign in to comment.