Skip to content

Commit

Permalink
chore: move contract version check and turn off contract registration…
Browse files Browse the repository at this point in the history
… if unsupported
  • Loading branch information
jrriehl committed Oct 21, 2024
1 parent e8457a2 commit 00f8c47
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion python/examples/08-local-network-interaction/agent1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)

Expand Down
11 changes: 0 additions & 11 deletions python/src/uagents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion python/src/uagents/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"fetch1mezzhfj7qgveewzwzdk6lz5sae4dunpmmsjr9u7z0tpmdsae8zmquq3y0y"
)
TESTNET_CONTRACT_ALMANAC = (
"fetch135h26ys2nwqealykzey532gamw4l4s07aewpwc0cyd8z6m92vyhsplf0vp"
"fetch1tjagw8g8nn4cwuw00cf0m5tl4l6wfw9c0ue507fhx9e3yrsck8zs0l3q4w"
)
MAINNET_CONTRACT_NAME_SERVICE = (
"fetch1479lwv5vy8skute5cycuz727e55spkhxut0valrcm38x9caa2x8q99ef0q"
Expand Down
35 changes: 31 additions & 4 deletions python/src/uagents/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -356,19 +379,23 @@ 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.
Args:
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):
Expand Down
12 changes: 9 additions & 3 deletions python/src/uagents/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit 00f8c47

Please sign in to comment.