From 00f8c4736435cb870bba8c2179d58cbfb9f9ed8d Mon Sep 17 00:00:00 2001 From: James Riehl Date: Mon, 21 Oct 2024 13:28:55 +0100 Subject: [PATCH] chore: move contract version check and turn off contract registration if unsupported --- .../08-local-network-interaction/agent1.py | 2 +- python/src/uagents/agent.py | 11 ------ python/src/uagents/config.py | 2 +- python/src/uagents/network.py | 35 ++++++++++++++++--- python/src/uagents/registration.py | 12 +++++-- 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/python/examples/08-local-network-interaction/agent1.py b/python/examples/08-local-network-interaction/agent1.py index 209e4cff..d96c835c 100644 --- a/python/examples/08-local-network-interaction/agent1.py +++ b/python/examples/08-local-network-interaction/agent1.py @@ -10,7 +10,7 @@ class Message(Model): bob = Agent( name="bob", port=8001, - seed="bob secret phrase", + seed="bosdfsdfdfb sewewewewcret phrase", endpoint=["http://127.0.0.1:8001/submit"], ) diff --git a/python/src/uagents/agent.py b/python/src/uagents/agent.py index eb833b6d..cf9e226f 100644 --- a/python/src/uagents/agent.py +++ b/python/src/uagents/agent.py @@ -26,7 +26,6 @@ from uagents.asgi import ASGIServer from uagents.communication import Dispenser from uagents.config import ( - ALMANAC_CONTRACT_VERSION, AVERAGE_BLOCK_INTERVAL, LEDGER_PREFIX, MAINNET_PREFIX, @@ -765,16 +764,6 @@ async def register(self): if necessary. """ - # Check if the deployed contract version matches the supported version - deployed_version = self._almanac_contract.get_contract_version() - if deployed_version != ALMANAC_CONTRACT_VERSION: - self._logger.warning( - "Mismatch in almanac contract versions: supported (%s), deployed (%s). " - "Update uAgents to the latest version for compatibility.", - ALMANAC_CONTRACT_VERSION, - deployed_version, - ) - await self._registration_policy.register( self.address, list(self.protocols.keys()), self._endpoints, self._metadata ) diff --git a/python/src/uagents/config.py b/python/src/uagents/config.py index de457572..0a8f9a7a 100644 --- a/python/src/uagents/config.py +++ b/python/src/uagents/config.py @@ -13,7 +13,7 @@ "fetch1mezzhfj7qgveewzwzdk6lz5sae4dunpmmsjr9u7z0tpmdsae8zmquq3y0y" ) TESTNET_CONTRACT_ALMANAC = ( - "fetch135h26ys2nwqealykzey532gamw4l4s07aewpwc0cyd8z6m92vyhsplf0vp" + "fetch1tjagw8g8nn4cwuw00cf0m5tl4l6wfw9c0ue507fhx9e3yrsck8zs0l3q4w" ) MAINNET_CONTRACT_NAME_SERVICE = ( "fetch1479lwv5vy8skute5cycuz727e55spkhxut0valrcm38x9caa2x8q99ef0q" diff --git a/python/src/uagents/network.py b/python/src/uagents/network.py index 39647a63..abdc3468 100644 --- a/python/src/uagents/network.py +++ b/python/src/uagents/network.py @@ -21,6 +21,7 @@ from cosmpy.crypto.address import Address from uagents.config import ( + ALMANAC_CONTRACT_VERSION, AVERAGE_BLOCK_INTERVAL, MAINNET_CONTRACT_ALMANAC, MAINNET_CONTRACT_NAME_SERVICE, @@ -44,6 +45,10 @@ class InsufficientFundsError(Exception): """Raised when an agent has insufficient funds for a transaction.""" +class UnsupportedContractVersionError(Exception): + """Raised when the contract version is not supported.""" + + def get_ledger(test: bool = True) -> LedgerClient: """ Get the Ledger client. @@ -150,6 +155,24 @@ class AlmanacContract(LedgerContract): registration, and getting the endpoints associated with an agent's registration. """ + def check_version(self) -> bool: + """ + Check if the contract version supported by this version of uAgents matches the + deployed version. + + Returns: + bool: True if the contract version is supported, False otherwise. + """ + deployed_version = self.get_contract_version() + if deployed_version != ALMANAC_CONTRACT_VERSION: + logger.warning( + f"The deployed version of the Almanac Contract is {deployed_version} " + f"and you are using version {ALMANAC_CONTRACT_VERSION}. " + "Update uAgents to the latest version for compatibility.", + ) + return False + return True + def query_contract(self, query_msg: Dict[str, Any]) -> Any: """ Execute a query with additional checks and error handling. @@ -356,7 +379,7 @@ def get_sequence(self, address: str) -> int: ) -def get_almanac_contract(test: bool = True) -> AlmanacContract: +def get_almanac_contract(test: bool = True) -> Optional[AlmanacContract]: """ Get the AlmanacContract instance. @@ -364,11 +387,15 @@ def get_almanac_contract(test: bool = True) -> AlmanacContract: test (bool): Whether to use the testnet or mainnet. Defaults to True. Returns: - AlmanacContract: The AlmanacContract instance. + AlmanacContract: The AlmanacContract instance if version is supported. """ if test: - return _testnet_almanac_contract - return _mainnet_almanac_contract + if _testnet_almanac_contract.check_version(): + return _testnet_almanac_contract + return None + if _mainnet_almanac_contract.check_version(): + return _mainnet_almanac_contract + return None class NameServiceContract(LedgerContract): diff --git a/python/src/uagents/registration.py b/python/src/uagents/registration.py index a6032c5c..bacf2108 100644 --- a/python/src/uagents/registration.py +++ b/python/src/uagents/registration.py @@ -265,9 +265,12 @@ def __init__( self._api_policy = AlmanacApiRegistrationPolicy( identity, almanac_api=almanac_api, logger=logger ) - self._ledger_policy = LedgerBasedRegistrationPolicy( - identity, ledger, wallet, almanac_contract, testnet, logger=logger - ) + if almanac_contract is None: + self._ledger_policy = None + else: + self._ledger_policy = LedgerBasedRegistrationPolicy( + identity, ledger, wallet, almanac_contract, testnet, logger=logger + ) async def register( self, @@ -286,6 +289,9 @@ async def register( f"Failed to register on Almanac API: {e.__class__.__name__}" ) + if self._ledger_policy is None: + return + # schedule the ledger registration try: await self._ledger_policy.register(