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

fix: registration exceptions and retries #128

Merged
merged 2 commits into from
Aug 22, 2023
Merged
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/.pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ disable=missing-module-docstring,
too-many-instance-attributes,
duplicate-code,
too-many-arguments,
logging-fstring-interpolation
logging-fstring-interpolation,
broad-exception-caught

20 changes: 12 additions & 8 deletions python/src/uagents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
from uagents.mailbox import MailboxClient
from uagents.config import (
REGISTRATION_FEE,
MIN_REGISTRATION_TIME,
REGISTRATION_UPDATE_INTERVAL_SECONDS,
LEDGER_PREFIX,
REGISTRATION_RETRY_INTERVAL_SECONDS,
parse_endpoint_config,
parse_agentverse_config,
get_logger,
Expand Down Expand Up @@ -241,7 +242,8 @@ async def register(self):
# or anything has changed from the last registration
if (
not self._almanac_contract.is_registered(self.address)
or self._schedule_registration() < MIN_REGISTRATION_TIME
or self._almanac_contract.get_expiry(self.address)
< REGISTRATION_UPDATE_INTERVAL_SECONDS
or self._endpoints != self._almanac_contract.get_endpoints(self.address)
or list(self.protocols.keys())
!= self._almanac_contract.get_protocols(self.address)
Expand Down Expand Up @@ -271,15 +273,17 @@ async def register(self):
self._logger.info("Almanac registration is up to date!")

async def _registration_loop(self):
await self.register()
# schedule the next registration
time_until_next_registration = REGISTRATION_UPDATE_INTERVAL_SECONDS
try:
await self.register()
except Exception as ex:
self._logger.exception(f"Failed to register on almanac contract: {ex}")
time_until_next_registration = REGISTRATION_RETRY_INTERVAL_SECONDS
# schedule the next registration update
self._loop.create_task(
_delay(self._registration_loop(), self._schedule_registration())
_delay(self._registration_loop(), time_until_next_registration)
)

def _schedule_registration(self):
return self._almanac_contract.get_expiry(self.address)

def on_interval(
self,
period: float,
Expand Down
5 changes: 3 additions & 2 deletions python/src/uagents/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class AgentNetwork(Enum):
)
REGISTRATION_FEE = 500000000000000000
REGISTRATION_DENOM = "atestfet"
MIN_REGISTRATION_TIME = 3600
BLOCK_INTERVAL = 5
REGISTRATION_UPDATE_INTERVAL_SECONDS = 3600
REGISTRATION_RETRY_INTERVAL_SECONDS = 60
AVERAGE_BLOCK_INTERVAL = 5.7
AGENT_NETWORK = AgentNetwork.FETCHAI_TESTNET

AGENTVERSE_URL = "https://agentverse.ai"
Expand Down
8 changes: 4 additions & 4 deletions python/src/uagents/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
CONTRACT_ALMANAC,
CONTRACT_NAME_SERVICE,
AGENT_NETWORK,
BLOCK_INTERVAL,
AVERAGE_BLOCK_INTERVAL,
REGISTRATION_FEE,
REGISTRATION_DENOM,
get_logger,
Expand Down Expand Up @@ -80,19 +80,19 @@ def is_registered(self, address: str) -> bool:
return False
return True

def get_expiry(self, address: str):
def get_expiry(self, address: str) -> int:
query_msg = {"query_records": {"agent_address": address}}
response = self.query(query_msg)

if not response["record"]:
contract_state = self.query({"query_contract_state": {}})
expiry = contract_state.get("state").get("expiry_height")
return expiry * BLOCK_INTERVAL
return expiry * AVERAGE_BLOCK_INTERVAL

expiry = response.get("record")[0].get("expiry")
height = response.get("height")

return (expiry - height) * BLOCK_INTERVAL
return (expiry - height) * AVERAGE_BLOCK_INTERVAL

def get_endpoints(self, address: str):
query_msg = {"query_records": {"agent_address": address}}
Expand Down
Loading