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

Added send_xdai to treasury #595

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from microchain import Function
from prediction_market_agent_tooling.gtypes import wei_type
from prediction_market_agent_tooling.loggers import logger
from prediction_market_agent_tooling.tools.contract import ContractOnGnosisChain
from prediction_market_agent_tooling.tools.hexbytes_custom import HexBytes
from prediction_market_agent_tooling.tools.web3_utils import send_xdai_to, xdai_to_wei
from web3 import Web3

from prediction_market_agent.agents.microchain_agent.agents_nft_game import (
TREASURY_SAFE_ADDRESS,
)
from prediction_market_agent.agents.microchain_agent.microchain_agent_keys import (
MicrochainAgentKeys,
)
Expand Down Expand Up @@ -54,6 +59,9 @@ def __call__(self, address: str, message: str) -> str:


class ReceiveMessage(Function):
# Percentage of message value that goes to the treasury.
TREASURY_ACCUMULATION_PERCENTAGE = 0.7

@staticmethod
def get_count_unseen_messages() -> int:
return BlockchainTransactionFetcher().fetch_count_unprocessed_transactions(
Expand All @@ -77,9 +85,22 @@ def __call__(self) -> BlockchainMessage | None:
keys.bet_from_address
)
)
# ToDo - Fund the treasury with xDai.

if not message_to_process:
logger.info("No messages to process.")
else:
# 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 * message_to_process.value_wei
),
)
logger.info(
f"Funded the treasury with xDai, tx_hash: {HexBytes(tx_receipt['transactionHash']).hex()}"
)
Comment on lines +88 to +103
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Race conditions and transaction ordering need attention

Based on the code analysis, there are potential race conditions in the message processing implementation:

  1. The fetch_unseen_transactions_df queries transactions without proper row-level locking
  2. No explicit transaction isolation level or database-level synchronization is visible
  3. The message processing and treasury transfer could be processed multiple times if concurrent instances are running

Consider implementing:

  1. Database-level row locking using SELECT FOR UPDATE
  2. Explicit transaction boundaries with proper isolation level
  3. Unique constraints or idempotency checks for treasury transfers
🔗 Analysis chain

Verify transaction ordering and race conditions.

The current implementation might be susceptible to race conditions if multiple instances process messages simultaneously. Consider:

  1. Adding transaction nonce management
  2. Implementing proper synchronization mechanisms

Let's check for existing synchronization mechanisms:


Let me check the implementation of the fetcher to verify the synchronization mechanism.


Let me check the implementation of fetch_unseen_transactions_df to verify if there's any database-level synchronization.


Let me check the blockchain table handler implementation for any database-level locking or transaction isolation.


Let me try to find the correct path for the blockchain table handler.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Look for synchronization patterns in message processing
rg -A 5 "fetch_one_unprocessed_blockchain_message_and_store_as_processed"

Length of output: 1368


Script:

#!/bin/bash
# Check the implementation of the fetcher method
rg -B 5 -A 15 "def fetch_one_unprocessed_blockchain_message_and_store_as_processed" prediction_market_agent/db/blockchain_transaction_fetcher.py

Length of output: 1062


Script:

#!/bin/bash
# Check the implementation of fetch_unseen_transactions_df
rg -B 5 -A 15 "def fetch_unseen_transactions_df" prediction_market_agent/db/blockchain_transaction_fetcher.py

Length of output: 1140


Script:

#!/bin/bash
# Check the blockchain table handler implementation
rg -B 5 -A 15 "def fetch_latest_blockchain_message" prediction_market_agent/db/blockchain_table_handler.py

Length of output: 203


Script:

#!/bin/bash
# Find the blockchain table handler file
fd blockchain_table_handler

# Look for database-related files that might contain transaction handling
rg -l "BEGIN TRANSACTION|SELECT.*FOR UPDATE" 

Length of output: 76

return message_to_process


Expand Down
Loading