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

Trade announcement #30

Merged
merged 34 commits into from
Jan 27, 2024
Merged

Conversation

thompalex
Copy link
Collaborator

Created initial functionality for peers to announce and respond to trades

thompalex and others added 30 commits January 16, 2024 15:18
…catch rpc_store calls. Added a callback function in peer which is called when we catch a new key value pair on the network.
…p instances of NewServer and check that we are catching set commands in the DHT, triggering our callback function \n Testing coverage for the server module is at 100%
updated commission object and peer initialization
Copy link
Owner

@zimmermatt zimmermatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly good. I really like the readability with the self-documenting names and the literate programming effect that results.

A couple of things to look at though and some optionals to consider.

Nothing severe enough to hold this up, so I'm going to merge it to keep things moving for our Monday deadline. Please address the necessary issues in a follow-up PR.

return self.commissions[key]
raise KeyError(f"No commission found for key: {key}")

def get_owned_artwork(self, key: bytes):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional/Opportunistic Improvement

Similar to a discussion from Yen's code review, Python has a different approach to/view of encapsulation. It doesn't have true private members, though you can emulate by preceding class & instance variables with either a single or double underscore.

Full discussion of this topic is too much for here. But, all of that context to was set up a comment that this, and the above, getter could be removed. Python convention considers it okay to access a member like commissions and owned_artworks directly by external callers.

I would keep is_owned_artwork below though. I think it adds to the readability.

Comment on lines +149 to +151
self.logger.info("Trade announced")
else:
self.logger.error("Trade failed to announce")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address, if appropriate for debug-ability.

These two log messages are the same as below in announce_trade. Shouldn't they differ in some manner?

trade_key, artwork_to_trade.key, self.keys["public"]
)
response_key = utils.generate_random_sha1_hash()
self.inventory.add_pending_trade(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address.

The pending trade should only be added on success. So this should be moved to the if set_success branch below.

Comment on lines +222 to +230
async def handle_accept_trade(self, response: OfferResponse):
"""Handle an accepted trade"""

self.logger.info(response)

async def handle_reject_trade(self, response: OfferResponse):
"""Handle a rejected trade"""

self.logger.info(response)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take it these are a TODO?

Comment on lines +238 to +240
if trade_key in self.inventory.pending_trades:
self.inventory.remove_pending_trade(trade_key)
if response.trade_id in self.inventory.pending_trades:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remind me... What's the scenario where trade_key would be different from response.trade_id? Why would they both be in pending_trades?


def generate_random_sha1_hash():
"""
Generates a random SHA-1 hash as a file descriptor for the trade response.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional/Opportunistic Improvement

Given this function is now in a utils module, the doc should be more general.

Generates a random SHA-1 hash as a file descriptor for the trade response.

Something like:

Generates a random SHA-1 hash that can be used as an ID for cases where there's no obvious attribute on which to base the ID.

Comment on lines +27 to +37
def get_artwork_ledger_key(self):
"""
Returns the artwork to be traded.
"""
return self.artwork_ledger_key

def get_originator_public_key(self):
"""
Returns the public key of the originator of the trade announcement.
"""
return self.originator_public_key
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment as above about Python encapsulation, getters.

Comment on lines +28 to +44
def get_trade_id(self):
"""
Returns the trade id to respond to.
"""
return self.trade_id

def get_originator_public_key(self):
"""
Returns the public key of the originator of the trade response.
"""
return self.originator_public_key

def get_offer_ledger_id(self):
"""
Returns the offer ledger id to respond to.
"""
return self.offer_ledger_id
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment as above about Python encapsulation, getters.

"""


class OfferResponse:
Copy link
Owner

@zimmermatt zimmermatt Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional/Opportunistic Improvement

This could also be a @dataclass (see above).

"""


class OfferAnnouncement:
Copy link
Owner

@zimmermatt zimmermatt Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional/Opportunistic Improvement

Consider making this a @dataclass. That generates a lot of boilerplate code associated with classes like this.

See https://docs.python.org/3/library/dataclasses.html

@zimmermatt zimmermatt merged commit 65c786c into zimmermatt:main Jan 27, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants