Skip to content

Commit f6c0f92

Browse files
committed
fixes for review
1 parent 8ab3ecf commit f6c0f92

File tree

9 files changed

+36
-33
lines changed

9 files changed

+36
-33
lines changed

src/aleph/chains/common.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import asyncio
22
import json
33
import logging
4-
from dataclasses import asdict
54
from enum import IntEnum
65
from typing import Dict, Optional, Tuple, List
76

@@ -27,12 +26,12 @@
2726
from aleph.schemas.pending_messages import BasePendingMessage
2827
from aleph.schemas.validated_message import (
2928
validate_pending_message,
30-
MessageConfirmation,
3129
ValidatedStoreMessage,
3230
ValidatedForgetMessage,
3331
make_confirmation_update_query,
3432
make_message_upsert_query,
3533
)
34+
from ..schemas.message_confirmation import MessageConfirmation
3635
from aleph.storage import get_json, pin_hash, add_json, get_message_content
3736
from .tx_context import TxContext
3837

@@ -71,7 +70,7 @@ async def delayed_incoming(
7170
await PendingMessage.collection.insert_one(
7271
{
7372
"message": message.dict(exclude={"content"}),
74-
"tx_context": asdict(tx_context) if tx_context is not None else None,
73+
"tx_context": tx_context.dict() if tx_context else None,
7574
"check_message": check_message,
7675
}
7776
)
@@ -122,7 +121,7 @@ async def incoming(
122121
chain_name = tx_context.chain if tx_context is not None else None
123122
ids_key = (item_hash, sender, chain_name)
124123

125-
if tx_context is not None:
124+
if tx_context:
126125
if seen_ids is not None:
127126
if ids_key in seen_ids.keys():
128127
if tx_context.height > seen_ids[ids_key]:
@@ -371,5 +370,5 @@ async def incoming_chaindata(content: Dict, context: TxContext):
371370
For now we only add it to the database, it will be processed later.
372371
"""
373372
await PendingTX.collection.insert_one(
374-
{"content": content, "context": asdict(context)}
373+
{"content": content, "context": context.dict()}
375374
)

src/aleph/chains/tx_context.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
from dataclasses import dataclass
1+
from aleph.schemas.message_confirmation import MessageConfirmation
22

33

4-
@dataclass
5-
class TxContext:
6-
chain: str
7-
hash: str
8-
height: int
9-
# Transaction timestamp, in Unix time (number of seconds since epoch).
10-
time: int
11-
publisher: str
4+
# At the moment, confirmation = chain transaction. This might change, but in the meantime
5+
# having TxContext inherit MessageConfirmation avoids code duplication.
6+
class TxContext(MessageConfirmation):
7+
pass

src/aleph/jobs/process_pending_messages.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from aleph.schemas.pending_messages import parse_message
2222
from aleph.services.p2p import singleton
2323
from .job_utils import prepare_loop, process_job_results
24+
from ..chains.tx_context import TxContext
2425

2526
LOGGER = getLogger("jobs.pending_messages")
2627

@@ -60,7 +61,8 @@ async def handle_pending_message(
6061
# If an invalid message somehow ended in pending messages, drop it.
6162
return [delete_pending_message_op]
6263

63-
tx_context = pending.get("tx_context")
64+
tx_context_dict = pending.get("tx_context")
65+
tx_context = TxContext.parse_obj(tx_context_dict) if tx_context_dict else None
6466

6567
async with sem:
6668
status, operations = await incoming(

src/aleph/jobs/process_pending_txs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import asyncio
66
import logging
7-
from dataclasses import asdict
87
from typing import List, Dict, Optional
98
from typing import Set
109

@@ -57,7 +56,7 @@ async def handle_pending_tx(
5756
operation=InsertOne(
5857
{
5958
"message": message.dict(exclude={"content"}),
60-
"tx_context": asdict(tx_context),
59+
"tx_context": tx_context.dict(),
6160
"check_message": True,
6261
}
6362
),
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from aleph_message.models import Chain
2+
from pydantic import BaseModel, Field
3+
4+
5+
class MessageConfirmation(BaseModel):
6+
chain: Chain = Field(..., description="Chain from which the confirmation was fetched.")
7+
height: int = Field(..., description="Block in which the confirmation was published.")
8+
hash: str = Field(
9+
...,
10+
description="Hash of the transaction/block in which the confirmation was published.",
11+
)
12+
time: float = Field(
13+
...,
14+
description="Transaction timestamp, in Unix time (number of seconds since epoch).",
15+
)
16+
publisher: str = Field(..., description="Publisher of the confirmation on chain.")

src/aleph/schemas/validated_message.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
MessageType,
1313
PostContent,
1414
ProgramContent,
15-
StoreContent, Chain,
16-
)
15+
StoreContent, )
1716
from pydantic import BaseModel, Field
1817

1918
from aleph.schemas.base_messages import AlephBaseMessage, ContentType, MType
@@ -25,17 +24,10 @@
2524
PendingProgramMessage,
2625
PendingStoreMessage,
2726
)
27+
from .message_confirmation import MessageConfirmation
2828
from .message_content import MessageContent
2929

3030

31-
class MessageConfirmation(BaseModel):
32-
chain: Chain
33-
height: int
34-
hash: str
35-
time: float
36-
publisher: str
37-
38-
3931
class EngineInfo(BaseModel):
4032
hash: str = Field(alias="Hash")
4133
size: int = Field(alias="Size")

tests/chains/test_confirmation.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
from dataclasses import asdict
32
from typing import Dict
43

54
import pytest
@@ -72,7 +71,7 @@ async def test_confirm_message(test_db):
7271

7372
assert message_in_db is not None
7473
assert message_in_db["confirmed"]
75-
expected_confirmations = [asdict(tx_context)]
74+
expected_confirmations = [tx_context.dict()]
7675
assert message_in_db["confirmations"] == expected_confirmations
7776

7877
capped_message_after_confirmation = await CappedMessage.collection.find_one(
@@ -111,7 +110,7 @@ async def test_process_confirmed_message(test_db):
111110
assert message_in_db is not None
112111
assert message_in_db["confirmed"]
113112

114-
expected_confirmations = [asdict(tx_context)]
113+
expected_confirmations = [tx_context.dict()]
115114
assert message_in_db["confirmations"] == expected_confirmations
116115

117116
capped_message_in_db = await CappedMessage.collection.find_one(
@@ -120,4 +119,4 @@ async def test_process_confirmed_message(test_db):
120119

121120
assert remove_id_key(message_in_db) == remove_id_key(capped_message_in_db)
122121
assert capped_message_in_db["confirmed"]
123-
assert capped_message_in_db["confirmations"] == [asdict(tx_context)]
122+
assert capped_message_in_db["confirmations"] == [tx_context.dict()]

tests/schemas/test_validated_messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
from aleph.schemas.validated_message import (
1717
validate_pending_message,
1818
BaseValidatedMessage,
19-
MessageConfirmation,
2019
ValidatedAggregateMessage,
2120
ValidatedStoreMessage,
2221
)
22+
from aleph.schemas.message_confirmation import MessageConfirmation
2323

2424

2525
def check_basic_message_fields(message: BaseValidatedMessage, message_dict: Dict):

tests/storage/test_store_message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from aleph.handlers.storage import handle_new_storage
66
from aleph.schemas.message_content import ContentSource, RawContent
77
from aleph.schemas.validated_message import (
8-
MessageConfirmation,
98
ValidatedStoreMessage,
109
StoreContentWithMetadata,
1110
)
11+
from aleph.schemas.message_confirmation import MessageConfirmation
1212
from message_test_helpers import make_validated_message_from_dict
1313

1414

0 commit comments

Comments
 (0)