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: updates for caboodle demo #30

Merged
merged 50 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
5ab882a
feat: add internal agent protocol
jrriehl Nov 23, 2022
58e22a6
feat: compress protocol digests
jrriehl Nov 23, 2022
05a1137
fix: example 06
Alejandro-Morales Nov 23, 2022
e1ddfd9
feat: add protocol id
jrriehl Nov 23, 2022
ea5e780
Merge branch 'agent-protocol' of ssh://github.com/fetchai/uAgents int…
jrriehl Nov 23, 2022
fd098f1
fix: update internal protocol digest and some names
jrriehl Nov 24, 2022
5853c0a
chore: resolve conflict
jrriehl Nov 24, 2022
ff38ea3
fix: init internal protocol empty
jrriehl Nov 24, 2022
2f8f7d6
feat: add cleaning demo
jrriehl Nov 24, 2022
a69aa61
chore: fix linting errors
jrriehl Nov 24, 2022
bfe9935
chore: uncomment empty digest assertion
jrriehl Nov 24, 2022
283aa90
feat: interval protocol only contains agent registrations
jrriehl Nov 24, 2022
8690ce9
feat: add interval messages to protocol
jrriehl Nov 25, 2022
9a02d2f
fix: add messages to agent decorator
jrriehl Nov 25, 2022
fcc2ba3
chore: fix types for set union
jrriehl Nov 25, 2022
ce78d4e
chore: make private methods private
jrriehl Nov 25, 2022
d872e6a
feat: validate outgoing interval messages
jrriehl Nov 25, 2022
e003713
chore: resolve conflict
jrriehl Nov 25, 2022
6e1dab7
chore: update demo and contract
jrriehl Nov 25, 2022
a01fe5d
fix: include interval messages in default agent context
jrriehl Nov 25, 2022
e357033
Merge branch 'agent-protocol' into cleaning-demo
jrriehl Nov 25, 2022
2b34617
fix: message check and agent addresses
jrriehl Nov 29, 2022
15ea924
feat: start tortoise integration
jrriehl Nov 29, 2022
17e8648
chore: resolve conflict
jrriehl Nov 30, 2022
ca161fa
feat: add tortoise orm
jrriehl Dec 2, 2022
e94020b
chore: fix import
jrriehl Dec 2, 2022
8a85706
chore: move tortoise to orm dependency group
jrriehl Dec 2, 2022
3576815
feat: add on event handlers
jrriehl Dec 2, 2022
bb7e3a5
fix: startup name
jrriehl Dec 2, 2022
95f80a9
feat: add user logic and fix db init
jrriehl Dec 6, 2022
c1bbc6f
Merge branch 'master' into cleaning-demo
jrriehl Dec 6, 2022
4e2f306
feat: add datetimes
jrriehl Dec 6, 2022
218d1a8
Merge branch 'cleaning-demo' of ssh://github.com/fetchai/uAgents into…
jrriehl Dec 6, 2022
9c7c0ea
chore: pr revisions and resolve conflicts
jrriehl Dec 13, 2022
d31dadc
chore: resolve conflict
jrriehl Dec 13, 2022
12e1c16
chore: resolve conflict
jrriehl Dec 13, 2022
428a946
feat: initial connection working
jrriehl Dec 15, 2022
6f9db66
chore: resolve conflict
jrriehl Dec 15, 2022
b5e18d7
fix: do not check for user address on query
jrriehl Dec 15, 2022
00d985d
fix: envelope encoding
jrriehl Dec 15, 2022
d562a3c
fix: get first provider
jrriehl Dec 16, 2022
228e23a
feat: enable signature verification (#31)
Alejandro-Morales Jan 3, 2023
8c648dd
chore: revert to bech32 signature
jrriehl Jan 5, 2023
bc7f381
test: update tests
jrriehl Jan 7, 2023
037d847
test: generate new wallet addresses
jrriehl Jan 9, 2023
1c45c26
chore: revert endpoint and remove print statements
jrriehl Jan 9, 2023
479ddd6
chore: fix linting errors
jrriehl Jan 9, 2023
227faed
test: add asgi server tests (#34)
jrriehl Jan 10, 2023
ce58900
fix: pylint and test event loop
jrriehl Jan 13, 2023
3045b77
fix: set message type to json str
jrriehl Jan 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/10-cleaning-demo/cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async def startup():

await Availability.create(
provider=provider,
time_start=utc.localize(datetime.fromisoformat("2022-12-31 10:00:00")),
time_end=utc.localize(datetime.fromisoformat("2022-12-31 22:00:00")),
time_start=utc.localize(datetime.fromisoformat("2022-01-31 00:00:00")),
time_end=utc.localize(datetime.fromisoformat("2023-02-01 00:00:00")),
max_distance=10,
min_hourly_price=5,
)
Expand Down
2 changes: 1 addition & 1 deletion examples/10-cleaning-demo/protocols/cleaning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def handle_query_request(ctx: Context, sender: str, msg: ServiceRequest):
@cleaning_proto.on_message(model=ServiceBooking, replies=BookingResponse)
async def handle_book_request(ctx: Context, sender: str, msg: ServiceBooking):

provider = await Provider.get(name=ctx.name)
provider = await Provider.filter(name=ctx.name).first()
ejfitzgerald marked this conversation as resolved.
Show resolved Hide resolved
availability = await Availability.get(provider=provider)
services = [int(service.type) for service in await provider.services]

Expand Down
4 changes: 2 additions & 2 deletions src/nexus/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,12 @@ async def _process_message_queue(self):
schema_digest, sender, message = await self._message_queue.get()

# lookup the model definition
model_class = self._models.get(schema_digest)
model_class: Model = self._models.get(schema_digest)
if model_class is None:
continue

# parse the received message
recovered = model_class.parse_raw(message)
recovered = model_class.parse(message)
jrriehl marked this conversation as resolved.
Show resolved Hide resolved

context = Context(
self._identity.address,
Expand Down
7 changes: 4 additions & 3 deletions src/nexus/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async def __call__(self, scope, receive, send):
return

headers = dict(scope.get("headers", {}))
if headers[b"content-type"] != b"application/json":
if b"application/json" not in headers[b"content-type"]:
jrriehl marked this conversation as resolved.
Show resolved Hide resolved
await send(
{
"type": "http.response.start",
Expand Down Expand Up @@ -149,8 +149,9 @@ async def __call__(self, scope, receive, send):
# wait for any queries to be resolved
if expects_response:
response_msg: Model = await self._queries[env.sender]
if datetime.now() > datetime.fromtimestamp(env.expires):
response_msg = ErrorMessage("Query envelope expired")
if env.expires is not None:
if datetime.now() > datetime.fromtimestamp(env.expires):
response_msg = ErrorMessage("Query envelope expired")
sender = env.target
response = enclose_response(response_msg, sender, env.session)
else:
Expand Down
7 changes: 2 additions & 5 deletions src/nexus/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from cosmpy.aerial.wallet import LocalWallet

from nexus.config import DEFAULT_ENVELOPE_TIMEOUT_SECONDS
from nexus.crypto import Identity, is_user_address
from nexus.crypto import Identity
from nexus.dispatch import dispatcher
from nexus.envelope import Envelope
from nexus.models import Model, ErrorMessage
Expand Down Expand Up @@ -110,10 +110,7 @@ async def send(
return

# handle queries waiting for a response
if is_user_address(destination):
if destination not in self._queries:
logging.exception(f"Unable to resolve query to user {destination}")
return
if destination in self._queries:
self._queries[destination].set_result(message)
del self._queries[destination]
return
Expand Down
7 changes: 7 additions & 0 deletions src/nexus/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import hashlib
from typing import Any, Dict

from pydantic import BaseModel

Expand All @@ -14,6 +15,12 @@ def build_schema_digest(model: "Model") -> str:
.hex()
)

@classmethod
def parse(cls, model: Any) -> Dict[str, Any]:
if isinstance(model, dict):
return cls.parse_obj(model)
return cls.parse_raw(model)


class ErrorMessage(Model):
error: str
2 changes: 1 addition & 1 deletion src/nexus/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ def enclose_response(message: Model, sender: str, session: str) -> dict:
session=session,
protocol=Model.build_schema_digest(message),
)
response_env.encode_payload(message.json())
response_env.encode_payload(message.dict())
return response_env.json()
46 changes: 46 additions & 0 deletions tests/test_agent_registration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# pylint: disable=protected-access
import unittest

from nexus import Agent
from nexus.setup import fund_agent_if_low


class TestVerify(unittest.TestCase):
def test_agent_registration(self):

agent = Agent(name="alice")

reg_fee = "500000000000000000atestfet"

fund_agent_if_low(agent.wallet.address())

sequence = agent.get_registration_sequence()

signature = agent._identity.sign_registration(
agent._reg_contract.address, agent.get_registration_sequence()
)

msg = {
"register": {
"record": {
"service": {
"protocols": [],
"endpoints": [{"url": agent._endpoint, "weight": 1}],
}
},
"signature": signature,
"sequence": sequence,
"agent_address": agent.address,
}
}

transaction = agent._reg_contract.execute(msg, agent.wallet, funds=reg_fee)
transaction.wait_to_complete()

is_registered = agent.registration_status()

self.assertEqual(is_registered, True, "Registration failed")


if __name__ == "__main__":
unittest.main()
18 changes: 17 additions & 1 deletion tests/test_verify_msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def encode(message: str) -> bytes:


class TestVerify(unittest.TestCase):
def test_verify_message(self):
def test_sign_and_verify_message(self):
alice = Agent(name="alice", seed="alice recovery password")

alice_msg = "hello there bob"
Expand All @@ -25,6 +25,22 @@ def test_verify_message(self):

self.assertEqual(result, True, "Verification failed")

def test_verify_dart_digest(self):

# Generate public key
address = "agent1qf5gfqm48k9acegez3sg82ney2aa6l5fvpwh3n3z0ajh0nam3ssgwnn5me7"

# Signature
signature = "sig1qyvn5fjzrhjzqcmj2gfg4us6xj00gvscs4u9uqxy6wpvp9agxjf723eh5l6w878p67lycgd3fz77zr3h0q6mrheg48e35zsvv0rm2tsuvyn3l" # pylint: disable=line-too-long

# Message
dart_digest = "a29af8b704077d394a9756dc04f0bb5f1424fc391b3de91144d683c5893ca234"
bytes_dart_digest = bytes.fromhex(dart_digest)

result = Identity.verify_digest(address, bytes_dart_digest, signature)

self.assertEqual(result, True, "Verification failed")


if __name__ == "__main__":
unittest.main()