Skip to content

Commit

Permalink
Add getDestinationTransactionByMessage and getSourceTransactionByMess…
Browse files Browse the repository at this point in the history
…age (#15)
  • Loading branch information
dungeon-master-666 authored Nov 17, 2022
1 parent eff3c10 commit 373acac
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
29 changes: 29 additions & 0 deletions indexer/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def __init__(self, lt, hash):
def __str__(self):
return f"Transaction ({self.lt}, {self.hash}) not found in DB"

class MessageNotFound(DataNotFound):
def __init__(self, source, destination, created_lt):
self.source = source
self.destination = destination
self.created_lt = created_lt

def __str__(self):
return f"Message not found source: {self.source} destination: {self.destination} created_lt: {self.created_lt}"

async def get_existing_seqnos_from_list(session, seqnos):
seqno_filters = [Block.seqno == seqno for seqno in seqnos]
seqno_filters = or_(*seqno_filters)
Expand Down Expand Up @@ -285,6 +294,26 @@ def get_transactions_by_in_message_hash(session: Session, msg_hash: str, include
query = query.order_by(Transaction.utime.desc()).limit(500)
return query.all()

def get_source_transaction_by_message(session: Session, source: str, destination: str, msg_lt: int):
query = session.query(Transaction).join(Transaction.out_msgs).options(contains_eager(Transaction.out_msgs))
query = query.filter(and_(Message.destination == destination, Message.source == source, Message.created_lt == msg_lt))
query = query.options(joinedload(Transaction.in_msg).joinedload(Message.content)) \
.options(joinedload(Transaction.out_msgs).joinedload(Message.content))
transaction = query.first()
if transaction is None:
raise MessageNotFound(source, destination, msg_lt)
return transaction

def get_destination_transaction_by_message(session: Session, source: str, destination: str, msg_lt: int):
query = session.query(Transaction).join(Transaction.in_msg).options(contains_eager(Transaction.in_msg))
query = query.filter(and_(Message.destination == destination, Message.source == source, Message.created_lt == msg_lt))
query = query.options(joinedload(Transaction.in_msg).joinedload(Message.content)) \
.options(joinedload(Transaction.out_msgs).joinedload(Message.content))
transaction = query.first()
if transaction is None:
raise MessageNotFound(source, destination, msg_lt)
return transaction

def get_blocks_by_unix_time(session: Session, start_utime: Optional[int], end_utime: Optional[int], workchain: Optional[int], shard: Optional[int], limit: int, offset: int, sort: str):
query = session.query(BlockHeader).join(BlockHeader.block).options(contains_eager(BlockHeader.block))
if start_utime is not None:
Expand Down
27 changes: 27 additions & 0 deletions webserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ async def get_transaction_by_in_message_hash(
db_transactions = await db.run_sync(crud.get_transactions_by_in_message_hash, msg_hash, include_msg_body)
return [schemas.Transaction.transaction_from_orm(t, include_msg_body) for t in db_transactions]

@app.get('/getSourceTransactionByMessage', response_model=schemas.Transaction)
async def get_source_transaction_by_message(
source: str = Query(..., description="Source address"),
destination: str = Query(..., description="Destination address"),
msg_lt: int = Query(..., description="Creation lt of the message"),
db: Session = Depends(get_db)
):
"""
Get transaction of `source` address by incoming message on `destination` address.
"""
db_transaction = await db.run_sync(crud.get_source_transaction_by_message, source, destination, msg_lt)
return schemas.Transaction.transaction_from_orm(db_transaction, True)

@app.get('/getDestinationTransactionByMessage', response_model=schemas.Transaction)
async def get_destination_transaction_by_message(
source: str = Query(..., description="Sender address"),
destination: str = Query(..., description="Receiver address"),
msg_lt: int = Query(..., description="Creation lt of the message"),
db: Session = Depends(get_db)
):
"""
Get transaction of `destination` address by outcoming message on `source` address.
"""
db_transaction = await db.run_sync(crud.get_destination_transaction_by_message, source, destination, msg_lt)
return schemas.Transaction.transaction_from_orm(db_transaction, True)


@app.get('/getBlocksByUnixTime', response_model=List[schemas.Block])
async def get_blocks_by_unix_time(
start_utime: Optional[int] = Query(None, description="UTC timestamp to start searching blocks"),
Expand Down

0 comments on commit 373acac

Please sign in to comment.