Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve logging system in terminal and UI #540

Merged
Merged
Show file tree
Hide file tree
Changes from 113 commits
Commits
Show all changes
114 commits
Select commit Hold shift + click to select a range
7fb3269
refactor: id to hash
Sep 4, 2024
b7df785
refactor: db migration, generate tx hashes
Sep 5, 2024
7309d9a
refactor: adapt frontend for hashes (ui, sorting, indexeddb, mappings…
Sep 5, 2024
b973d5b
fix: omitted ids to be replaced by hash
Sep 5, 2024
1931392
fix: make hash column unique
Sep 5, 2024
1881868
fix: delete broken migration file
Sep 5, 2024
8745104
fix: add safe migration
Sep 5, 2024
95ff7cd
fix: add tx at top of the list by default
Sep 5, 2024
13fb239
Merge branch 'staging' into 451-refactor-transaction-id-with-transact…
Sep 6, 2024
d8def67
refactor: extract get hash method
Sep 6, 2024
537874b
fix: update some method namings and cleanup
Sep 6, 2024
2aa6a6b
refactor: cleanup comments
Sep 6, 2024
f7eefe2
fix: formatting
Sep 6, 2024
477b276
refactor: cleanup migration
Sep 6, 2024
f97cae3
Merge branch 'staging' into 451-refactor-transaction-id-with-transact…
Sep 6, 2024
914e0b3
refactor: add todo
Sep 6, 2024
b156acc
refactor: cleanup comments
Sep 6, 2024
ca673fd
Merge branch 'staging' into 451-refactor-transaction-id-with-transact…
Sep 9, 2024
3efe889
refactor: better migration
Sep 9, 2024
4d631fb
fix: button clickable area bug
Sep 9, 2024
865fa59
fix: Merge branch 'staging' into 451-refactor-transaction-id-with-tra…
Sep 10, 2024
21e979f
fix: make migration subsequent of tx value migration, add primary key…
Sep 10, 2024
78d7788
fix: improve migration
Sep 10, 2024
f63a58b
test: update integration test with hashes
Sep 10, 2024
80bd814
Merge branch 'main' into 451-refactor-transaction-id-with-transaction…
Sep 11, 2024
2cae2ee
refactor: add some description to the db migration file, cleanup prin…
Sep 11, 2024
e21bae3
test: update get tx by hash unit test
Sep 11, 2024
c273489
refactor: remove id property from type interface
Sep 11, 2024
4345a92
refactor: cleanup comments
Sep 11, 2024
5839ade
Merge remote-tracking branch 'origin/main' into 451-refactor-transact…
Sep 12, 2024
0878fc2
fix: remove duplicated property due to merge
Sep 12, 2024
eb9067b
fix: missing hash key reference
Sep 12, 2024
df4c5b6
fix: don't pass data when None
Sep 12, 2024
af1d3c2
fix: update expected test tx structure with hash
Sep 12, 2024
df57923
fix: print execution logs in the backend terminal
Sep 12, 2024
b98af12
Merge remote-tracking branch 'origin/main' into 451-refactor-transact…
Sep 13, 2024
0d053df
refactor: remove unused endpoint
Sep 13, 2024
cb2eae5
feat: improve logging system
Sep 13, 2024
a3cf563
feat: basic filtering system
Sep 13, 2024
8893e30
feat: search filter
Sep 16, 2024
7b10c0e
fix: move migration description
Sep 16, 2024
ba933c9
fix: limit tx hashes to 66 chars in db
Sep 16, 2024
4f7ef93
fix: remove unused import
Sep 16, 2024
bba659d
add foreign key constraint to tx hash in tx audits table
Sep 16, 2024
f0a502a
add hash logic to migration
Sep 16, 2024
c433b68
fix: foreign key definition
Sep 16, 2024
78d1e46
Revert "add hash logic to migration"
Sep 16, 2024
21d774d
fix: implement hash in migration, fix constraints
Sep 16, 2024
7c62bd4
fix: remove unecessary safety check
Sep 16, 2024
78bbc6f
fix: dexie migration: populate hashes and remove id columns
Sep 16, 2024
0fe421a
Merge branch 'main' into 451-refactor-transaction-id-with-transaction…
Sep 16, 2024
cdbbda7
Merge remote-tracking branch 'origin/main' into 451-refactor-transact…
Sep 17, 2024
321cca8
fix: move revision head
Sep 17, 2024
d0f1bf1
fix: print execution logs in the backend terminal
Sep 12, 2024
fa2d509
feat: improve logging system
Sep 13, 2024
3b1f100
feat: basic filtering system
Sep 13, 2024
9506023
feat: search filter
Sep 16, 2024
d72826c
Merge branch '448-show-captured-outputs-also-in-the-dev-terminal' of …
Sep 17, 2024
68ae363
Merge branch 'main' into 448-show-captured-outputs-also-in-the-dev-te…
Sep 18, 2024
639ccf0
Merge branch 'main' into 448-show-captured-outputs-also-in-the-dev-te…
Sep 23, 2024
a011123
Merge branch 'main' into 448-show-captured-outputs-also-in-the-dev-te…
Sep 23, 2024
7f01289
fix: contract deployed notification when deployment failed
Sep 24, 2024
9eaf354
all events ok but bug on get data
Sep 24, 2024
2612a2d
improve messages, add some todos an clean up
Sep 24, 2024
df6aa12
improve messages, add category isolation
Sep 24, 2024
d654582
improve ui
Sep 24, 2024
7ee51fa
filters ui
Sep 24, 2024
335d1c0
go back to separated filter ui
Sep 24, 2024
45c3872
clear search text on reset
Sep 24, 2024
289c5b9
tweak ui
Sep 24, 2024
f647feb
filter by tx hash, clear search text, cleanup
Sep 24, 2024
ae9d371
tweak status colors
Sep 24, 2024
60a8a50
search in data as well
Sep 24, 2024
7bc6ab6
implement loguru and improve terminal logs
Sep 25, 2024
91cc92a
add contract deployed event
Sep 25, 2024
6459efd
cleanup, fix tutorial
Sep 25, 2024
d509351
remove unused logic in the tutorial
Sep 25, 2024
2f0d182
cleanup and update integration tests
Sep 25, 2024
57ed997
Merge branch 'main' into 448-show-captured-outputs-also-in-the-dev-te…
Sep 25, 2024
0ffce0b
cleanup
Sep 25, 2024
19b872e
cleanup error handling and message sending in genvm
Sep 25, 2024
4d33f69
cleanup and move types to separate file
Sep 25, 2024
cdd3ab9
cleanup, tweak colors
Sep 25, 2024
5ac3453
cleanup
Sep 25, 2024
ebb8319
Merge branch 'main' into 448-show-captured-outputs-also-in-the-dev-te…
Sep 26, 2024
6152700
cleanup imports
Sep 26, 2024
812a680
fix merge
Sep 26, 2024
ab7bcc5
move tx status update logs to consensus
Sep 27, 2024
0cc1dd2
improve logging and error handling in genvm
Sep 27, 2024
50877f4
improve consensus events
Sep 27, 2024
945c71a
use success for contract deployment
Sep 27, 2024
2510755
refactor stdout printing and bypass terminal
Sep 27, 2024
7468c44
commit forgotten changes, update blue for info logs
Sep 27, 2024
28bfcba
Merge branch 'main' into 448-show-captured-outputs-also-in-the-dev-te…
Sep 30, 2024
055f216
cleanup merge conflicts
Sep 30, 2024
c2d20d5
cleanup merge
Sep 30, 2024
728b6f1
don't split lines on outputs
Sep 30, 2024
2712a5b
display line breaks in logs
Sep 30, 2024
66fc1b7
Merge branch 'main' into 448-show-captured-outputs-also-in-the-dev-te…
Sep 30, 2024
65f216a
allow passing self in get_contract_data to allow logging again
Sep 30, 2024
c06380c
prefix private methods with _
Sep 30, 2024
3f91465
remove useless renaming
Oct 1, 2024
5519fc4
Revert "remove useless renaming"
Oct 1, 2024
803ca3e
add mock msg handlers
Oct 1, 2024
0650e1c
avoid updating tx status twice, fixing unit tests
Oct 1, 2024
d35fc74
revert commit
Oct 1, 2024
147a1bc
add client_id to log events
Oct 1, 2024
23de0b7
add client id to rpc requests
Oct 1, 2024
a4ff542
handle client session ids in the backend
AgustinRamiroDiaz Oct 1, 2024
d66fd15
fix: wait for connection
AgustinRamiroDiaz Oct 1, 2024
2232cb2
format
AgustinRamiroDiaz Oct 1, 2024
79fbd74
fix compile error
AgustinRamiroDiaz Oct 1, 2024
dbc7754
fix e2e tests
AgustinRamiroDiaz Oct 1, 2024
9119094
address comment
AgustinRamiroDiaz Oct 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 104 additions & 18 deletions backend/consensus/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
from backend.node.base import Node
from backend.node.genvm.types import ExecutionMode, Receipt, Vote
from backend.protocol_rpc.message_handler.base import MessageHandler
from backend.protocol_rpc.message_handler.types import (
LogEvent,
EventType,
EventScope,
)


def node_factory(
Expand Down Expand Up @@ -146,6 +151,9 @@ async def exec_transaction(
Node,
] = node_factory,
):
msg_handler = self.msg_handler.with_client_session(
transaction.client_session_id
)
if (
transactions_processor.get_transaction_by_hash(transaction.hash)["status"]
!= TransactionStatus.PENDING.value
Expand All @@ -161,8 +169,8 @@ async def exec_transaction(
# If transaction is a transfer, execute it
# TODO: consider when the transfer involves a contract account, bridging, etc.
if transaction.type == TransactionType.SEND:
return self.execute_transfer(
transaction, transactions_processor, accounts_manager
return ConsensusAlgorithm.execute_transfer(
transaction, transactions_processor, accounts_manager, msg_handler
)

# Select Leader and validators
Expand All @@ -180,8 +188,11 @@ async def exec_transaction(

for validators in rotate(involved_validators):
# Update transaction status
transactions_processor.update_transaction_status(
transaction.hash, TransactionStatus.PROPOSING
ConsensusAlgorithm.dispatch_transaction_status_update(
transactions_processor,
transaction.hash,
TransactionStatus.PROPOSING,
msg_handler,
)

[leader, *remaining_validators] = validators
Expand All @@ -199,16 +210,19 @@ async def exec_transaction(
ExecutionMode.LEADER,
contract_snapshot,
None,
self.msg_handler,
msg_handler,
contract_snapshot_factory,
)

# Leader executes transaction
leader_receipt = await leader_node.exec_transaction(transaction)
votes = {leader["address"]: leader_receipt.vote.value}
# Update transaction status
transactions_processor.update_transaction_status(
transaction.hash, TransactionStatus.COMMITTING
ConsensusAlgorithm.dispatch_transaction_status_update(
transactions_processor,
transaction.hash,
TransactionStatus.COMMITTING,
msg_handler,
)

# Create Validators
Expand All @@ -218,7 +232,7 @@ async def exec_transaction(
ExecutionMode.VALIDATOR,
contract_snapshot,
leader_receipt,
self.msg_handler,
msg_handler,
contract_snapshot_factory,
)
for validator in remaining_validators
Expand All @@ -235,9 +249,11 @@ async def exec_transaction(

for i, validation_result in enumerate(validation_results):
votes[validator_nodes[i].address] = validation_result.vote.value
transactions_processor.update_transaction_status(
ConsensusAlgorithm.dispatch_transaction_status_update(
transactions_processor,
transaction.hash,
TransactionStatus.REVEALING,
msg_handler,
)

if (
Expand All @@ -252,13 +268,27 @@ async def exec_transaction(

else: # this block is executed if the loop above is not broken
print("Consensus not reached for transaction: ", transaction)
transactions_processor.update_transaction_status(
transaction.hash, TransactionStatus.UNDETERMINED
msg_handler.send_message(
LogEvent(
"consensus_failed",
EventType.ERROR,
EventScope.CONSENSUS,
"Failed to reach consensus",
)
)
ConsensusAlgorithm.dispatch_transaction_status_update(
transactions_processor,
transaction.hash,
TransactionStatus.UNDETERMINED,
msg_handler,
)
return

transactions_processor.update_transaction_status(
transaction.hash, TransactionStatus.ACCEPTED
ConsensusAlgorithm.dispatch_transaction_status_update(
transactions_processor,
transaction.hash,
TransactionStatus.ACCEPTED,
msg_handler,
)

final = False
Expand All @@ -269,6 +299,16 @@ async def exec_transaction(
validators=validation_results,
).to_dict()

msg_handler.send_message(
LogEvent(
"consensus_reached",
EventType.SUCCESS,
EventScope.CONSENSUS,
"Reached consensus",
consensus_data,
)
)

# Register contract if it is a new contract
if transaction.type == TransactionType.DEPLOY_CONTRACT:
new_contract = {
Expand All @@ -280,10 +320,27 @@ async def exec_transaction(
}
contract_snapshot.register_contract(new_contract)

msg_handler.send_message(
LogEvent(
"deployed_contract",
EventType.SUCCESS,
EventScope.GENVM,
"Contract deployed",
new_contract,
)
)

# Update contract state if it is an existing contract
else:
contract_snapshot.update_contract_state(leader_receipt.contract_state)

ConsensusAlgorithm.dispatch_transaction_status_update(
transactions_processor,
transaction.hash,
TransactionStatus.FINALIZED,
msg_handler,
)

# Finalize transaction
transactions_processor.set_transaction_result(
transaction.hash,
Expand All @@ -303,13 +360,15 @@ async def exec_transaction(
value=0, # No value gets transferred?
type=TransactionType.RUN_CONTRACT.value,
leader_only=transaction.leader_only, # Cascade
client_session_id=transaction.client_session_id,
)

@staticmethod
def execute_transfer(
self,
transaction: Transaction,
transactions_processor: TransactionsProcessor,
accounts_manager: AccountsManager,
msg_handler: MessageHandler,
):
"""
Executes a native token transfer between Externally Owned Accounts (EOAs).
Expand All @@ -333,8 +392,10 @@ def execute_transfer(

# If the sender does not have enough balance, set the transaction status to UNDETERMINED
if from_balance < transaction.value:
transactions_processor.update_transaction_status(
transaction.hash, TransactionStatus.UNDETERMINED
MessageHandler.dispatch_transaction_status_update(
cristiam86 marked this conversation as resolved.
Show resolved Hide resolved
transactions_processor,
transaction.hash,
TransactionStatus.UNDETERMINED,
)
return

Expand All @@ -352,8 +413,33 @@ def execute_transfer(
transaction.to_address, to_balance + transaction.value
)

transactions_processor.update_transaction_status(
transaction.hash, TransactionStatus.FINALIZED
ConsensusAlgorithm.dispatch_transaction_status_update(
transactions_processor,
transaction.hash,
TransactionStatus.FINALIZED,
msg_handler,
)

@staticmethod
def dispatch_transaction_status_update(
transactions_processor: TransactionsProcessor,
transaction_hash: str,
new_status: TransactionStatus,
msg_handler: MessageHandler,
):
transactions_processor.update_transaction_status(transaction_hash, new_status)

msg_handler.send_message(
LogEvent(
"transaction_status_updated",
EventType.INFO,
EventScope.CONSENSUS,
f"{str(new_status.value)} {str(transaction_hash)}",
{
"hash": str(transaction_hash),
"new_status": str(new_status.value),
},
)
)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""add client session id to transactions

Revision ID: a32f85df2806
Revises: 3566595124f6
Create Date: 2024-10-01 12:09:19.995482

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "a32f85df2806"
down_revision: Union[str, None] = "3566595124f6"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"transactions",
sa.Column("client_session_id", sa.String(length=255), nullable=True),
cristiam86 marked this conversation as resolved.
Show resolved Hide resolved
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("transactions", "client_session_id")
# ### end Alembic commands ###
3 changes: 3 additions & 0 deletions backend/database_handler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class Transactions(Base):
created_at: Mapped[Optional[datetime.datetime]] = mapped_column(
DateTime(True), server_default=func.current_timestamp(), init=False
)
client_session_id: Mapped[Optional[str]] = mapped_column(
String(255)
) # Used to identify the client session that is subscribed to this transaction's events
leader_only: Mapped[bool] = mapped_column(Boolean)
r: Mapped[Optional[int]] = mapped_column(Integer)
s: Mapped[Optional[int]] = mapped_column(Integer)
Expand Down
13 changes: 7 additions & 6 deletions backend/database_handler/transactions_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@


class TransactionsProcessor:
def __init__(self, session: Session):
def __init__(
self,
session: Session,
):
self.session = session

@staticmethod
Expand All @@ -31,6 +34,7 @@ def _parse_transaction_data(transaction_data: Transactions) -> dict:
"v": transaction_data.v,
"created_at": transaction_data.created_at.isoformat(),
"leader_only": transaction_data.leader_only,
"client_session_id": transaction_data.client_session_id,
}

@staticmethod
Expand Down Expand Up @@ -75,6 +79,7 @@ def insert_transaction(
value: float,
type: int,
leader_only: bool,
client_session_id: str | None,
) -> int:
nonce = (
self.session.query(Transactions)
Expand Down Expand Up @@ -103,6 +108,7 @@ def insert_transaction(
s=None,
v=None,
leader_only=leader_only,
client_session_id=client_session_id,
)

self.session.add(new_transaction)
Expand Down Expand Up @@ -149,9 +155,4 @@ def set_transaction_result(self, transaction_hash: str, consensus_data: dict):
transaction.status = TransactionStatus.FINALIZED
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason behind this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was redundant because we also update the status in the update status method, and I thought it was causing the unit test to fail. But actually updating the mocking in the test was enough, so I'm reverting this! nevermind

transaction.consensus_data = consensus_data

print(
"Updating transaction status",
transaction_hash,
TransactionStatus.FINALIZED.value,
)
self.session.commit()
3 changes: 3 additions & 0 deletions backend/domain/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Transaction:
leader_only: bool = (
False # Flag to indicate if this transaction should be processed only by the leader. Used for fast and cheap execution of transactions.
)
client_session_id: str | None = None

def to_dict(self):
return {
Expand All @@ -97,6 +98,7 @@ def to_dict(self):
"s": self.s,
"v": self.v,
"leader_only": self.leader_only,
"client_session_id": self.client_session_id,
}


Expand All @@ -117,4 +119,5 @@ def transaction_from_dict(input: dict) -> Transaction:
s=input.get("s"),
v=input.get("v"),
leader_only=input.get("leader_only", False),
client_session_id=input["client_session_id"],
)
15 changes: 1 addition & 14 deletions backend/node/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,14 @@ async def run_contract(
def get_contract_data(
self, code: str, state: str, method_name: str, method_args: list
):
output_buffer = io.StringIO()

result = GenVM.get_contract_data(
result = self.genvm.get_contract_data(
code,
state,
method_name,
method_args,
self.contract_snapshot_factory,
output_buffer,
)

if self.genvm.contract_runner.mode == ExecutionMode.LEADER:
cristiam86 marked this conversation as resolved.
Show resolved Hide resolved
# Retrieve the captured stdout and stderr
captured_out = output_buffer.getvalue()
if captured_out:
socket_message = {
"function": "intelligent_contract_execution",
"response": {"status": "info", "message": captured_out},
}
self.msg_handler.socket_emit(socket_message)

return result

def get_contract_schema(self, code: str):
Expand Down
Loading
Loading