-
Notifications
You must be signed in to change notification settings - Fork 7
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
Send/Receive functions + tests working #617
Conversation
WalkthroughThe pull request introduces significant changes to the message handling and transaction fetching mechanisms in the prediction market agent. The modifications involve removing the Changes
Sequence DiagramsequenceDiagram
participant Agent
participant AgentCommunicationContract
participant MessageContainer
Agent->>AgentCommunicationContract: fetch_unseen_transactions(consumer_address)
AgentCommunicationContract-->>Agent: Count of unprocessed transactions
Agent->>AgentCommunicationContract: Fetch messages by indices
AgentCommunicationContract-->>MessageContainer: Create message containers
Agent->>MessageContainer: Parse messages
Possibly related PRs
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (4)
prediction_market_agent/tools/message_utils.py (1)
25-29
: Ensure proper parsing for multi-line output.
Using triple-quoted f-strings can result in unintended whitespace in the final string. Depending on the consumer of this function, consider stripping or adjusting format to avoid unexpected padding in logs or displays.prediction_market_agent/db/agent_communication.py (1)
8-23
: Parallel map usage infetch_unseen_transactions
is appropriate; verify error handling.
This function effectively fetches unseen messages in parallel. However, consider logging or handling exceptions within thepar_map
calls to prevent partial failures from halting the entire fetch process.prediction_market_agent/agents/microchain_agent/nft_treasury_game/app_nft_treasury_game.py (1)
243-253
: UI code successfully fetches and displays unseen transactions.
- Good use of
fetch_unseen_transactions
to retrieve messages.- Consider adding error handling or empty checks to avoid breaking the UI if the contract call fails.
- Marking the sender, message, and value is clear, but ensure user input is sanitized if displayed.
prediction_market_agent/agents/microchain_agent/nft_treasury_game/messages_functions.py (1)
88-110
: Consider enhancing error handling and configurationThe implementation looks good but could benefit from some improvements:
- The treasury accumulation percentage is hardcoded. Consider making it configurable.
- Add validation for the transaction value before treasury accumulation.
- Add error handling for failed treasury transactions.
Here's a suggested improvement:
class ReceiveMessage(Function): - # Percentage of message value that goes to the treasury. - TREASURY_ACCUMULATION_PERCENTAGE = 0.7 + def __init__(self, treasury_percentage: float = 0.7): + """ + Initialize ReceiveMessage function + Args: + treasury_percentage: Percentage of message value that goes to the treasury (0.0 to 1.0) + """ + if not 0 <= treasury_percentage <= 1: + raise ValueError("Treasury percentage must be between 0 and 1") + self.treasury_percentage = treasury_percentage def __call__(self) -> str: keys = MicrochainAgentKeys() count_unseen_messages = self.get_count_unseen_messages() if count_unseen_messages == 0: logger.info("No messages to process.") return "No new messages" popped_message = pop_message( api_keys=APIKeys_PMAT(BET_FROM_PRIVATE_KEY=keys.bet_from_private_key), ) + if popped_message.value <= 0: + logger.warning("Received message with zero or negative value") + return parse_message_for_agent(message=popped_message) + # Accumulate a percentage of the message value in the treasury. - tx_receipt = send_xdai_to( - web3=ContractOnGnosisChain.get_web3(), - from_private_key=keys.bet_from_private_key, - to_address=TREASURY_SAFE_ADDRESS, - value=wei_type( - self.TREASURY_ACCUMULATION_PERCENTAGE * popped_message.value - ), - ) + try: + tx_receipt = send_xdai_to( + web3=ContractOnGnosisChain.get_web3(), + from_private_key=keys.bet_from_private_key, + to_address=TREASURY_SAFE_ADDRESS, + value=wei_type( + self.treasury_percentage * popped_message.value + ), + ) + logger.info( + f"Funded the treasury with xDai, tx_hash: {HexBytes(tx_receipt['transactionHash']).hex()}" + ) + except Exception as e: + logger.error(f"Failed to fund treasury: {str(e)}") + # Continue processing the message even if treasury funding fails - logger.info( - f"Funded the treasury with xDai, tx_hash: {HexBytes(tx_receipt['transactionHash']).hex()}" - ) return parse_message_for_agent(message=popped_message)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (2)
poetry.lock
is excluded by!**/*.lock
,!**/*.lock
pyproject.toml
is excluded by!**/*.toml
📒 Files selected for processing (9)
prediction_market_agent/agents/microchain_agent/nft_treasury_game/app_nft_treasury_game.py
(2 hunks)prediction_market_agent/agents/microchain_agent/nft_treasury_game/messages_functions.py
(4 hunks)prediction_market_agent/db/agent_communication.py
(1 hunks)prediction_market_agent/db/blockchain_message_table_handler.py
(0 hunks)prediction_market_agent/db/blockchain_transaction_fetcher.py
(0 hunks)prediction_market_agent/tools/message_utils.py
(2 hunks)tests/agents/microchain/test_messages_functions.py
(4 hunks)tests/conftest.py
(0 hunks)tests/db/test_blockchain_message_table_handler.py
(0 hunks)
💤 Files with no reviewable changes (4)
- tests/conftest.py
- prediction_market_agent/db/blockchain_transaction_fetcher.py
- tests/db/test_blockchain_message_table_handler.py
- prediction_market_agent/db/blockchain_message_table_handler.py
🔇 Additional comments (13)
prediction_market_agent/tools/message_utils.py (1)
3-5
: Imports look correct and consistent.
These updated imports accurately reference the correct modules, ensuring that HexBytes
, MessageContainer
, and wei_to_xdai
come from the intended packages.
prediction_market_agent/db/agent_communication.py (3)
1-6
: Imports are concise and appear correct.
The imported resources from eth_typing
, config
, tools.contract
, tools.data_models
, and tools.parallelism
are valid dependencies for the new transaction management utilities.
25-30
: fetch_count_unprocessed_transactions
function is straightforward.
The logic is clean and returns the unseen message count. Ensure that calling code gracefully handles a large count, or potential network errors.
32-38
: pop_message
correctness check.
The pop_message
method retrieves a single message and is transactionally altering the contract state. Confirm that calling code gracefully handles a scenario where no messages exist or the contract call reverts.
tests/agents/microchain/test_messages_functions.py (5)
6-8
: New imports align with the updated message handling flow.
These additional imports for wei_type
, AgentCommunicationContract
, and MessageContainer
ensure that the tests integrate well with the changes in agent_communication.py
.
38-41
: Constants defined for clarity.
Defining MOCK_SENDER
and MOCK_COUNT_UNPROCESSED_TXS
at the top helps readability. No issues found.
54-62
: patch_count_unseen_messages
fixture is well-structured.
This fixture mock is straightforward and supports testing the new fetch_count_unprocessed_transactions
function without external dependencies.
83-88
: Ensuring the test verifies fetch_count_unprocessed_transactions
.
The test reads the count of unseen messages after patching. Confirm that edge cases (e.g., zero messages, large counts) are also tested in separate tests if needed.
96-112
: test_receive_message_call
integrates well with the new pop flow.
Mocking pop_message
and verifying its usage is a good approach. Ensure that the test also validates the returned MessageContainer
content where critical fields (e.g., sender
, message
, value
) are used in the application logic.
prediction_market_agent/agents/microchain_agent/nft_treasury_game/app_nft_treasury_game.py (1)
40-49
: Imports are consistent with the removal of older classes.
Replacing references to BlockchainTransactionFetcher
with fetch_unseen_transactions
and unzip_message_else_do_nothing
is aligned with the new design.
prediction_market_agent/agents/microchain_agent/nft_treasury_game/messages_functions.py (3)
16-22
: LGTM! Clean import organization
The new imports align well with the architectural changes, replacing the blockchain transaction fetcher with more focused message handling functions.
71-73
: LGTM! Clean implementation of message count retrieval
The implementation is concise and follows single responsibility principle.
94-96
: Verify message handling integration
Let's verify that all required components for message handling are properly integrated.
✅ Verification successful
Message handling integration is properly configured
Based on the verification results:
- The
pop_message
function is correctly implemented inagent_communication.py
and properly imported inmessages_functions.py
- Required message utility functions (
compress_message
,parse_message_for_agent
) are correctly imported frommessage_utils.py
- The old message handling classes (
BlockchainTransactionFetcher
,BlockchainMessageTableHandler
) have been completely removed - The
APIKeys_PMAT
is properly imported fromprediction_market_agent_tooling.config
The message handling integration appears to be complete and correctly structured. All necessary components are in place and properly connected.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify the integration of message handling components
# Check for the existence of required modules and their exports
echo "Checking for required modules..."
# Check agent_communication.py
rg -l "def (fetch_count_unprocessed_transactions|pop_message)" --type py
# Check message_utils.py
rg -l "def (parse_message_for_agent|compress_message)" --type py
# Check for any remaining references to old classes
echo "Checking for old class references..."
rg -l "class (BlockchainTransactionFetcher|BlockchainMessageTableHandler)" --type py
Length of output: 483
Script:
#!/bin/bash
# Let's verify the actual implementation of pop_message and its integration
# Check the implementation of pop_message
echo "Checking pop_message implementation:"
rg "def pop_message" -A 10 prediction_market_agent/db/agent_communication.py
# Check how APIKeys_PMAT is defined and used
echo -e "\nChecking APIKeys_PMAT definition:"
rg "class APIKeys_PMAT" -A 5 --type py
# Check for any imports of pop_message in the codebase
echo -e "\nChecking pop_message usage:"
rg "from.*agent_communication.*import.*pop_message" --type py
# Check for any other direct usage of pop_message
echo -e "\nChecking other pop_message references:"
rg "pop_message\(" --type py
Length of output: 1163
Script:
#!/bin/bash
# Let's check for APIKeys_PMAT and MessageContainer definitions, and AgentCommunicationContract usage
# Check for APIKeys_PMAT definition
echo "Checking for APIKeys_PMAT definition:"
rg "class APIKeys_PMAT" -B 2 -A 10 --type py
# Check for MessageContainer definition
echo -e "\nChecking for MessageContainer definition:"
rg "class MessageContainer" -B 2 -A 10 --type py
# Check AgentCommunicationContract implementation
echo -e "\nChecking AgentCommunicationContract implementation:"
rg "class AgentCommunicationContract" -B 2 -A 15 --type py
# Check for imports in the messages_functions.py
echo -e "\nChecking imports in messages_functions.py:"
rg "^from|^import" prediction_market_agent/agents/microchain_agent/nft_treasury_game/messages_functions.py
Length of output: 1484
Script:
#!/bin/bash
# Let's check the specific imports from agent_communication and message_utils
echo "Checking specific imports from agent_communication:"
rg "from prediction_market_agent.db.agent_communication import" -A 3 prediction_market_agent/agents/microchain_agent/nft_treasury_game/messages_functions.py
echo -e "\nChecking specific imports from message_utils:"
rg "from prediction_market_agent.tools.message_utils import" -A 3 prediction_market_agent/agents/microchain_agent/nft_treasury_game/messages_functions.py
# Let's also check if APIKeys_PMAT is properly imported and has the expected attribute
echo -e "\nChecking APIKeys_PMAT in prediction_market_agent_tooling:"
fd -e py -x rg "class APIKeys.*bet_from_private_key" -A 5 {} prediction_market_agent_tooling/
Length of output: 10997
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (7)
prediction_market_agent/agents/microchain_agent/nft_treasury_game/messages_functions.py (4)
2-2
: Consider naming consistency for imported alias.
RenamingAPIKeys
toAPIKeys_PMAT
is fine, but ensure it aligns with existing naming conventions. If there's no strong reason for the alias, you might keep usage consistent asAPIKeys
.
51-55
: Validate fee logic and empty message scenarios.
While sending the message, consider handling scenarios where:
- The fee is insufficient.
- The message is empty (or exceedingly large).
Add error handling or validations to avoid transaction failures.
83-87
: Returning a string on no messages is fine, but confirm the user experience.
The user sees "No new messages" if none are available. This is acceptable, but if future usage requires structured data (e.g., JSON), consider returning an informative object.
89-93
: Add robustness for popped messages.
Currently, the code pops and parses the message, but lacks explicit error handling. Ifpop_message
returns a malformed or empty message,parse_message_for_agent
might fail. Wrap it in a try/except or validate the message content if needed.prediction_market_agent/db/agent_communication.py (3)
1-11
: Recommend docstrings for new methods.
Adding docstrings for the introduced methods will clarify usage, expected parameters, and potential exceptions, improving maintainability.
13-28
: Consider ordering and performance for fetching unseen transactions.
Usingpar_map
is efficient for parallel fetching. However, if strict message order matters, ensure the order is handled or documented. Also, confirm performance for large message counts.
37-44
: Ensurepop_message
handles empty queues gracefully.
If no messages are available, confirm thatagent_comm_contract.pop_message
returns a useful error or an empty container, rather than raising an unhandled exception.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (2)
poetry.lock
is excluded by!**/*.lock
,!**/*.lock
pyproject.toml
is excluded by!**/*.toml
📒 Files selected for processing (2)
prediction_market_agent/agents/microchain_agent/nft_treasury_game/messages_functions.py
(4 hunks)prediction_market_agent/db/agent_communication.py
(1 hunks)
🔇 Additional comments (6)
prediction_market_agent/agents/microchain_agent/nft_treasury_game/messages_functions.py (4)
6-6
: Import for xdai_to_wei
looks good.
No issues observed here.
11-14
: Imports from agent_communication
align well with the new architecture.
These imports facilitate the shift toward the abstracted message handling approach.
16-18
: Imports for message utilities are correctly used.
Bringing in compress_message
and parse_message_for_agent
keeps message processing modular.
66-68
: Check exception handling for unreadable addresses.
When calling fetch_count_unprocessed_transactions
, confirm that invalid or null addresses are handled gracefully by the underlying contract method.
prediction_market_agent/db/agent_communication.py (2)
30-35
: Validate edge cases in counting unprocessed transactions.
Explicitly handle invalid consumer addresses or potential unexpected negative counts returned by the contract.
46-56
: Confirm success/failure states for send_message
.
Returning a TxReceipt
is great, but consider verifying or logging transaction success/failure. If the transaction fails on-chain, ensure there's a fallback or clear error reporting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (8)
prediction_market_agent/agents/microchain_agent/deploy.py (1)
110-112
: Consider concurrency and cooperative cancellation.This loop checks
agent.do_stop
on each iteration, which is fine for single-threaded logic. However, if other threads or processes can setagent.do_stop
, consider using synchronization primitives (e.g., threading events) to ensure a more robust cancellation flow. Otherwise, there is a possibility of race conditions if multiple threads access and mutateagent.do_stop
concurrently.prediction_market_agent/db/agent_communication.py (4)
13-28
: Add error handling and batch processing for robustness.While the parallel mapping implementation is efficient, consider these improvements:
- Add error handling for contract interactions
- Implement batch processing to prevent potential memory issues with large message counts
def fetch_unseen_transactions( consumer_address: ChecksumAddress, ) -> list[MessageContainer]: agent_comm_contract = AgentCommunicationContract() + try: count_unseen_messages = fetch_count_unprocessed_transactions(consumer_address) + BATCH_SIZE = 100 + message_containers = [] + + for start_idx in range(0, count_unseen_messages, BATCH_SIZE): + batch_size = min(BATCH_SIZE, count_unseen_messages - start_idx) + batch_indices = list(range(start_idx, start_idx + batch_size)) - message_containers = par_map( - items=list(range(count_unseen_messages)), + batch_containers = par_map( + items=batch_indices, func=lambda idx: agent_comm_contract.get_at_index( agent_address=consumer_address, idx=idx ), ) + message_containers.extend(batch_containers) return message_containers + except Exception as e: + logger.error(f"Error fetching unseen transactions: {str(e)}") + raise
30-34
: Add error handling for contract interaction.The function should handle potential contract call failures gracefully.
def fetch_count_unprocessed_transactions(consumer_address: ChecksumAddress) -> int: agent_comm_contract = AgentCommunicationContract() + try: count_unseen_messages = agent_comm_contract.count_unseen_messages(consumer_address) return count_unseen_messages + except Exception as e: + logger.error(f"Error fetching unprocessed transaction count: {str(e)}") + raise
37-43
: Add validation for api_keys parameter.The function should validate the API keys before attempting to use them.
def pop_message(api_keys: APIKeys_PMAT) -> MessageContainer: + if not api_keys or not api_keys.bet_from_address: + raise ValueError("Invalid API keys provided") + agent_comm_contract = AgentCommunicationContract() popped_message = agent_comm_contract.pop_message( api_keys=api_keys, agent_address=api_keys.bet_from_address, ) return popped_message
46-59
: Add validation for amount_wei and message parameters.The function should validate the payment amount and message before sending.
def send_message( api_keys: APIKeys_PMAT, recipient: ChecksumAddress, message: HexBytes, amount_wei: Wei, ) -> TxReceipt: + if not message: + raise ValueError("Message cannot be empty") + if amount_wei <= 0: + raise ValueError("Amount must be greater than 0") + agent_comm_contract = AgentCommunicationContract() return agent_comm_contract.send_message( api_keys=api_keys, agent_address=recipient, message=message, amount_wei=amount_wei, web3=ContractOnGnosisChain.get_web3(), )prediction_market_agent/agents/microchain_agent/nft_treasury_game/messages_functions.py (3)
52-57
: Add address validation before sending message.Validate the address format before attempting to convert it to checksum address.
api_keys = APIKeys_PMAT(BET_FROM_PRIVATE_KEY=keys.bet_from_private_key) + if not Web3.is_address(address): + raise ValueError(f"Invalid address format: {address}") + send_message( api_keys=api_keys, recipient=Web3.to_checksum_address(address), message=HexBytes(compress_message(message)), amount_wei=xdai_to_wei(keys.cap_sending_xdai(xdai_type(fee))), )
85-95
: Add error handling for message retrieval.The message retrieval process should handle potential failures gracefully.
count_unseen_messages = self.get_count_unseen_messages() if count_unseen_messages == 0: logger.info("No messages to process.") return "No new messages" - popped_message = pop_message( - api_keys=APIKeys_PMAT(BET_FROM_PRIVATE_KEY=keys.bet_from_private_key), - ) - - return parse_message_for_agent(message=popped_message) + try: + popped_message = pop_message( + api_keys=APIKeys_PMAT(BET_FROM_PRIVATE_KEY=keys.bet_from_private_key), + ) + return parse_message_for_agent(message=popped_message) + except Exception as e: + logger.error(f"Failed to retrieve message: {str(e)}") + return "Failed to retrieve message"
85-95
: Consider potential race condition in message processing.There's a potential race condition between checking the message count and popping the message. Another consumer could process messages between these operations.
Consider implementing an atomic operation or using a message queue system to prevent race conditions in message processing. This could be addressed in a future enhancement.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
prediction_market_agent/agents/microchain_agent/deploy.py
(1 hunks)prediction_market_agent/agents/microchain_agent/nft_treasury_game/messages_functions.py
(4 hunks)prediction_market_agent/db/agent_communication.py
(1 hunks)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
tests/agents/microchain/test_messages_functions.py (2)
45-51
: Consider adding docstring to the fixture.The fixture is well-implemented following pytest best practices. Consider adding a docstring to document its purpose and return value.
@pytest.fixture(scope="module") def patch_count_unseen_messages() -> Generator[PropertyMock, None, None]: + """Fixture that mocks the count_unseen_messages method of AgentCommunicationContract. + + Returns: + Generator[PropertyMock, None, None]: Mock object returning MOCK_COUNT_UNPROCESSED_TXS + """ with patch.object( AgentCommunicationContract, "count_unseen_messages", return_value=MOCK_COUNT_UNPROCESSED_TXS, ) as mock: yield mock
71-79
: Enhance test coverage with additional assertions.While the test verifies the count in the description, it could be more thorough.
Consider adding these assertions:
def test_receive_message_description( patch_public_key: PropertyMock, patch_count_unseen_messages: PropertyMock, ) -> None: r = ReceiveMessage() description = r.description count_unseen_messages = fetch_count_unprocessed_transactions( patch_public_key.return_value ) assert str(count_unseen_messages) in description + # Verify the complete description format + assert "You have" in description + assert "unprocessed message" in description + # Verify the count matches the mock + assert count_unseen_messages == MOCK_COUNT_UNPROCESSED_TXS
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
tests/agents/microchain/test_messages_functions.py
(3 hunks)
🔇 Additional comments (2)
tests/agents/microchain/test_messages_functions.py (2)
Line range hint 6-41
: LGTM! Well-structured imports and constants.
The imports are properly organized, and the constants are well-defined using appropriate Web3 utilities and test accounts from Anvil.
Line range hint 1-100
: Verify contract integration with actual AgentCommunicationContract.
While the tests cover the basic functionality, it would be beneficial to verify that the mocked contract methods match the actual contract interface.
✅ Verification successful
Contract integration is correctly implemented and verified
The verification shows that the contract integration is properly implemented:
-
The actual contract methods
count_unseen_messages
andpop_message
are used in the production code:- In
prediction_market_agent/db/agent_communication.py
, both methods are called on theAgentCommunicationContract
instance - The mocked methods in the test match the actual usage in production
- In
-
The test mocks align with the real implementation:
- Test fixtures mock the same methods that are used in production
- The method signatures and return types are consistent between test and production code
-
The integration flow is correct:
ReceiveMessage
class uses these contract methods through the proper abstraction layers- The contract interaction is properly wrapped in the database layer (
agent_communication.py
)
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify that the mocked contract methods exist in the actual contract
# Search for the contract interface definition
ast-grep --pattern 'class AgentCommunicationContract {
$$$
count_unseen_messages($$$) {
$$$
}
$$$
pop_message($$$) {
$$$
}
$$$
}'
Length of output: 153
Script:
#!/bin/bash
# Let's try a different approach to find the contract implementation
# First, let's locate the contract file
fd "contract.py" --type f
# Then let's use ripgrep to find the methods we're interested in
rg -A 5 "class AgentCommunicationContract"
# Also search for the specific methods
rg "count_unseen_messages|pop_message" -A 3
Length of output: 6692
patch_public_key: PropertyMock, | ||
patch_spice: typing.Any, | ||
patch_dune_api_key: PropertyMock, | ||
patch_send_xdai: PropertyMock, | ||
patch_count_unseen_messages: PropertyMock, | ||
) -> None: | ||
# Idea here is to fetch the next message, and then fetch the count of unseen messages, asserting that | ||
# this number decreased by 1. | ||
r = ReceiveMessage() | ||
|
||
initial_count_unseen_messages = ( | ||
BlockchainTransactionFetcher().fetch_count_unprocessed_transactions( | ||
patch_public_key.return_value | ||
) | ||
) | ||
|
||
r() | ||
current_count_unseen_messages = ( | ||
BlockchainTransactionFetcher().fetch_count_unprocessed_transactions( | ||
patch_public_key.return_value | ||
) | ||
mock_log_message = MessageContainer( | ||
sender=MOCK_SENDER, | ||
recipient=MOCK_SENDER, | ||
message=HexBytes("0x123"), # dummy message | ||
value=wei_type(10000), | ||
) | ||
assert current_count_unseen_messages == initial_count_unseen_messages - 1 | ||
with patch.object( | ||
AgentCommunicationContract, | ||
"pop_message", | ||
return_value=mock_log_message, | ||
): | ||
r = ReceiveMessage() | ||
|
||
blockchain_message = r() | ||
assert blockchain_message is not None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance test coverage for message handling.
The test could be more comprehensive by verifying the returned message properties and testing error cases.
Consider expanding the test:
def test_receive_message_call(
patch_public_key: PropertyMock,
patch_count_unseen_messages: PropertyMock,
) -> None:
mock_log_message = MessageContainer(
sender=MOCK_SENDER,
recipient=MOCK_SENDER,
message=HexBytes("0x123"), # dummy message
value=wei_type(10000),
)
with patch.object(
AgentCommunicationContract,
"pop_message",
return_value=mock_log_message,
):
r = ReceiveMessage()
blockchain_message = r()
assert blockchain_message is not None
+ # Verify message properties
+ assert blockchain_message.sender == MOCK_SENDER
+ assert blockchain_message.recipient == MOCK_SENDER
+ assert blockchain_message.message == HexBytes("0x123")
+ assert blockchain_message.value == wei_type(10000)
+
+def test_receive_message_call_no_messages(
+ patch_public_key: PropertyMock,
+ patch_count_unseen_messages: PropertyMock,
+) -> None:
+ """Test behavior when no messages are available."""
+ with patch.object(
+ AgentCommunicationContract,
+ "pop_message",
+ return_value=None,
+ ):
+ r = ReceiveMessage()
+ blockchain_message = r()
+ assert blockchain_message is None
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
patch_public_key: PropertyMock, | |
patch_spice: typing.Any, | |
patch_dune_api_key: PropertyMock, | |
patch_send_xdai: PropertyMock, | |
patch_count_unseen_messages: PropertyMock, | |
) -> None: | |
# Idea here is to fetch the next message, and then fetch the count of unseen messages, asserting that | |
# this number decreased by 1. | |
r = ReceiveMessage() | |
initial_count_unseen_messages = ( | |
BlockchainTransactionFetcher().fetch_count_unprocessed_transactions( | |
patch_public_key.return_value | |
) | |
) | |
r() | |
current_count_unseen_messages = ( | |
BlockchainTransactionFetcher().fetch_count_unprocessed_transactions( | |
patch_public_key.return_value | |
) | |
mock_log_message = MessageContainer( | |
sender=MOCK_SENDER, | |
recipient=MOCK_SENDER, | |
message=HexBytes("0x123"), # dummy message | |
value=wei_type(10000), | |
) | |
assert current_count_unseen_messages == initial_count_unseen_messages - 1 | |
with patch.object( | |
AgentCommunicationContract, | |
"pop_message", | |
return_value=mock_log_message, | |
): | |
r = ReceiveMessage() | |
blockchain_message = r() | |
assert blockchain_message is not None | |
def test_receive_message_call( | |
patch_public_key: PropertyMock, | |
patch_count_unseen_messages: PropertyMock, | |
) -> None: | |
mock_log_message = MessageContainer( | |
sender=MOCK_SENDER, | |
recipient=MOCK_SENDER, | |
message=HexBytes("0x123"), # dummy message | |
value=wei_type(10000), | |
) | |
with patch.object( | |
AgentCommunicationContract, | |
"pop_message", | |
return_value=mock_log_message, | |
): | |
r = ReceiveMessage() | |
blockchain_message = r() | |
assert blockchain_message is not None | |
# Verify message properties | |
assert blockchain_message.sender == MOCK_SENDER | |
assert blockchain_message.recipient == MOCK_SENDER | |
assert blockchain_message.message == HexBytes("0x123") | |
assert blockchain_message.value == wei_type(10000) | |
def test_receive_message_call_no_messages( | |
patch_public_key: PropertyMock, | |
patch_count_unseen_messages: PropertyMock, | |
) -> None: | |
"""Test behavior when no messages are available.""" | |
with patch.object( | |
AgentCommunicationContract, | |
"pop_message", | |
return_value=None, | |
): | |
r = ReceiveMessage() | |
blockchain_message = r() | |
assert blockchain_message is None |
No description provided.