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

feat(core): add aname api resolver #585

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion python/src/uagents/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
TESTNET_PREFIX = "test-agent"
MAINNET_PREFIX = "agent"
AGENT_ADDRESS_LENGTH = 65
TESTNET_CHAINID = "dorado-1"

MAINNET_CONTRACT_ALMANAC = (
"fetch1mezzhfj7qgveewzwzdk6lz5sae4dunpmmsjr9u7z0tpmdsae8zmquq3y0y"
Expand All @@ -19,7 +20,7 @@
"fetch1479lwv5vy8skute5cycuz727e55spkhxut0valrcm38x9caa2x8q99ef0q"
)
TESTNET_CONTRACT_NAME_SERVICE = (
"fetch1mxz8kn3l5ksaftx8a9pj9a6prpzk2uhxnqdkwuqvuh37tw80xu6qges77l"
"fetch1kewgfwxwtuxcnppr547wj6sd0e5fkckyp48dazsh89hll59epgpspmh0tn"
)
REGISTRATION_FEE = 500000000000000000
REGISTRATION_DENOM = "atestfet"
Expand Down
3 changes: 2 additions & 1 deletion python/src/uagents/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
MAINNET_CONTRACT_NAME_SERVICE,
REGISTRATION_DENOM,
REGISTRATION_FEE,
TESTNET_CHAINID,
TESTNET_CONTRACT_ALMANAC,
TESTNET_CONTRACT_NAME_SERVICE,
)
Expand Down Expand Up @@ -724,7 +725,7 @@ async def register(
wallet.address(),
records,
domain,
chain_id == "dorado-1",
chain_id == TESTNET_CHAINID,
)

if transaction is None:
Expand Down
80 changes: 71 additions & 9 deletions python/src/uagents/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import requests
from dateutil import parser
from pydantic import BaseModel

from uagents.config import (
AGENT_ADDRESS_LENGTH,
Expand All @@ -24,6 +25,16 @@
LOGGER = get_logger("resolver", logging.WARNING)


class AgentRecord(BaseModel):
address: str
weight: float


class DomainRecord(BaseModel):
name: str
agents: List[AgentRecord]


def weighted_random_sample(
items: List[Any], weights: Optional[List[float]] = None, k: int = 1, rng=random
) -> List[Any]:
Expand Down Expand Up @@ -125,18 +136,18 @@ def query_record(agent_address: str, service: str, test: bool) -> dict:
return result


def get_agent_address(name: str, test: bool) -> Optional[str]:
def _aname_contract_resolve(domain: str, test: bool) -> Optional[str]:
"""
Get the agent address associated with the provided name from the name service contract.
Get the agent address associated with the provided domain from the name service contract.

Args:
name (str): The name to query.
domain (str): The domain to query.
test (bool): Whether to use the testnet or mainnet contract.

Returns:
Optional[str]: The associated agent address if found.
"""
query_msg = {"domain_record": {"domain": f"{name}"}}
query_msg = {"domain_record": {"domain": f"{domain}"}}
result = get_name_service_contract(test).query(query_msg)
if result["record"] is not None:
registered_records = result["record"]["records"][0]["agent_address"]["records"]
Expand Down Expand Up @@ -183,7 +194,7 @@ def __init__(
max_endpoints=self._max_endpoints, almanac_api_url=almanac_api_url
)
self._name_service_resolver = NameServiceResolver(
max_endpoints=self._max_endpoints
max_endpoints=self._max_endpoints, almanac_api_url=almanac_api_url
)

async def resolve(self, destination: str) -> Tuple[Optional[str], List[str]]:
Expand Down Expand Up @@ -333,18 +344,58 @@ async def resolve(self, destination: str) -> Tuple[Optional[str], List[str]]:


class NameServiceResolver(Resolver):
def __init__(self, max_endpoints: Optional[int] = None):
def __init__(
self, max_endpoints: Optional[int] = None, almanac_api_url: Optional[str] = None
):
"""
Initialize the NameServiceResolver.

Args:
max_endpoints (Optional[int]): The maximum number of endpoints to return.
"""
self._max_endpoints = max_endpoints or DEFAULT_MAX_ENDPOINTS
self._almanac_api_url = almanac_api_url
self._almanac_api_resolver = AlmanacApiResolver(
max_endpoints=self._max_endpoints
almanac_api_url=almanac_api_url, max_endpoints=self._max_endpoints
)

async def _api_resolve(self, domain: str) -> Optional[str]:
"""
Resolve the domain using the Alamanac API.

Args:
domain (str): The domain to resolve.

Returns:
Tuple[Optional[str], List[str]]: The address (if available) and resolved endpoints.
"""
try:
response = requests.get(f"{self._almanac_api_url}/domains/{domain}")

if response.status_code != 200:
if response.status_code != 404:
LOGGER.debug(
f"Failed to resolve name {domain} from {self._almanac_api_url}: "
f"{response.status_code}: {response.text}"
)
return None

domain_record = DomainRecord.model_validate(response.json())
agent_records = domain_record.agents
if len(agent_records) == 0:
return None
elif len(agent_records) == 1:
return agent_records[0].address
else:
addresses = [val.address for val in agent_records]
weights = [val.weight for val in agent_records]
return weighted_random_sample(addresses, weights=weights, k=1)[0]

except Exception as ex:
LOGGER.error(f"Error when resolving {domain}: {ex}")

return None

async def resolve(self, destination: str) -> Tuple[Optional[str], List[str]]:
"""
Resolve the destination using the NameService contract.
Expand All @@ -355,11 +406,22 @@ async def resolve(self, destination: str) -> Tuple[Optional[str], List[str]]:
Returns:
Tuple[Optional[str], List[str]]: The address (if available) and resolved endpoints.
"""
prefix, name, _ = parse_identifier(destination)
prefix, domain, _ = parse_identifier(destination)
use_testnet = prefix != MAINNET_PREFIX
address = get_agent_address(name, use_testnet)

if domain == "":
return None, []

# Prefer to resolve using the Almanac API
address = await self._api_resolve(domain)

if address is None:
# If the API resolution fails, fallback to the ANAME contract
address = _aname_contract_resolve(domain, use_testnet)

if address is not None:
return await self._almanac_api_resolver.resolve(address)

return None, []


Expand Down