Skip to content

Commit

Permalink
fix: resolve consensus base test fails
Browse files Browse the repository at this point in the history
  • Loading branch information
kstroobants committed Jan 7, 2025
1 parent bcbcaf1 commit 3fd519e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion backend/consensus/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ async def handle(self, context):
# Set the contract snapshot for the transaction
if context.transaction.contract_snapshot is None:
context.transactions_processor.set_transaction_contract_snapshot(
context.transaction.hash, contract_snapshot_supplier()
context.transaction.hash, contract_snapshot_supplier().to_dict()
)

# Get the contract snapshot for the transaction, to not use the overwritten one
Expand Down
2 changes: 1 addition & 1 deletion backend/database_handler/contract_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def to_dict(self):
}

@classmethod
def from_dict(cls, input: dict) -> Optional["ContractSnapshot"]:
def from_dict(cls, input: dict | None) -> Optional["ContractSnapshot"]:
if input:
instance = cls.__new__(cls)
instance.session = None
Expand Down
8 changes: 3 additions & 5 deletions backend/database_handler/transactions_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,19 +403,17 @@ def get_newer_transactions(self, transaction_hash: str):
]

def set_transaction_contract_snapshot(
self, transaction_hash: str, contract_snapshot: ContractSnapshot | None
self, transaction_hash: str, contract_snapshot: dict | None
):
transaction = (
self.session.query(Transactions).filter_by(hash=transaction_hash).one()
)
transaction.contract_snapshot = (
contract_snapshot.to_dict() if contract_snapshot else None
)
transaction.contract_snapshot = contract_snapshot
self.session.commit()

def get_transaction_contract_snapshot(
self, transaction_hash: str
) -> ContractSnapshot:
) -> ContractSnapshot | None:
transaction = (
self.session.query(Transactions).filter_by(hash=transaction_hash).one()
)
Expand Down
4 changes: 3 additions & 1 deletion backend/domain/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ def to_dict(self):
"to_address": self.to_address,
"input_data": self.input_data,
"data": self.data,
"consensus_data": self.consensus_data.to_dict(),
"consensus_data": (
self.consensus_data.to_dict() if self.consensus_data else None
),
"nonce": self.nonce,
"value": self.value,
"gaslimit": self.gaslimit,
Expand Down
35 changes: 28 additions & 7 deletions tests/unit/consensus/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from backend.node.base import Node
from backend.node.types import ExecutionMode, ExecutionResultStatus, Receipt, Vote
from backend.protocol_rpc.message_handler.base import MessageHandler
from typing import Optional

DEFAULT_FINALITY_WINDOW = 5
DEFAULT_EXEC_RESULT = b"\x00\x00" # success(null)
Expand Down Expand Up @@ -99,12 +100,12 @@ def get_newer_transactions(self, transaction_hash: str):
return []

def set_transaction_contract_snapshot(
self, transaction_hash: str, contract_snapshot: ContractSnapshot
self, transaction_hash: str, contract_snapshot: dict
):
transaction = self.get_transaction_by_hash(transaction_hash)
transaction["contract_snapshot"] = (
contract_snapshot.to_dict() if contract_snapshot else None
)
pass

def get_transaction_contract_snapshot(self, transaction_hash: str):
return None


class SnapshotMock:
Expand All @@ -123,7 +124,9 @@ def transaction_to_dict(transaction: Transaction) -> dict:
"to_address": transaction.to_address,
"input_data": transaction.input_data,
"data": transaction.data,
"consensus_data": transaction.consensus_data.to_dict(),
"consensus_data": (
transaction.consensus_data.to_dict() if transaction.consensus_data else None
),
"nonce": transaction.nonce,
"value": transaction.value,
"type": transaction.type.value,
Expand All @@ -138,7 +141,11 @@ def transaction_to_dict(transaction: Transaction) -> dict:
"timestamp_awaiting_finalization": transaction.timestamp_awaiting_finalization,
"appeal_failed": transaction.appeal_failed,
"appeal_undetermined": transaction.appeal_undetermined,
"contract_snapshot": transaction.contract_snapshot,
"contract_snapshot": (
transaction.contract_snapshot.to_dict()
if transaction.contract_snapshot
else None
),
}


Expand All @@ -157,6 +164,20 @@ def update_contract_state(
):
pass

def to_dict(self):
return {
"address": (self.address if self.address else None),
}

@classmethod
def from_dict(cls, input: dict | None) -> Optional["ContractSnapshotMock"]:
if input:
instance = cls.__new__(cls)
instance.address = input.get("address", None)
return instance
else:
return None

return ContractSnapshotMock()


Expand Down

0 comments on commit 3fd519e

Please sign in to comment.