Skip to content

Commit

Permalink
fix: registration exceptions and retries (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrriehl authored Aug 22, 2023
1 parent a003f14 commit ba7af5e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
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

0 comments on commit ba7af5e

Please sign in to comment.