diff --git a/itests/test.sh b/itests/test.sh index e9ae75ea..0ea5ac61 100644 --- a/itests/test.sh +++ b/itests/test.sh @@ -12,3 +12,4 @@ pytest -s tests #pytest -s tests -k "test_subscribe_squeaks" #pytest -s tests -k "test_download_squeaks_for_address" #pytest -s tests -k "test_make_squeak" +#pytest -s tests -k "test_download_free_squeak" diff --git a/itests/tests/conftest.py b/itests/tests/conftest.py index d2a6ad27..d98bada9 100644 --- a/itests/tests/conftest.py +++ b/itests/tests/conftest.py @@ -190,3 +190,24 @@ def connected_tcp_peer_id(other_admin_stub): 18777, ) as peer_id: yield peer_id + + +@pytest.fixture +def signing_profile_id_with_free_price(admin_stub, signing_profile_id): + # Set the profile to use_custom_price + admin_stub.SetSqueakProfileUseCustomPrice( + squeak_admin_pb2.SetSqueakProfileUseCustomPriceRequest( + profile_id=signing_profile_id, + use_custom_price=True, + ) + ) + try: + yield signing_profile_id + finally: + # Set the profile to use_custom_price + admin_stub.SetSqueakProfileUseCustomPrice( + squeak_admin_pb2.SetSqueakProfileUseCustomPriceRequest( + profile_id=signing_profile_id, + use_custom_price=False, + ) + ) diff --git a/itests/tests/test_squeak_node.py b/itests/tests/test_squeak_node.py index beb759e6..4e1671e4 100644 --- a/itests/tests/test_squeak_node.py +++ b/itests/tests/test_squeak_node.py @@ -710,6 +710,30 @@ def test_buy_squeak( assert get_payment_summary_response.payment_summary.amount_spent_msat > 0 +def test_download_free_squeak( + admin_stub, + other_admin_stub, + connected_tcp_peer_id, + lightning_client, + signing_profile_id_with_free_price, + saved_squeak_hash, +): + # Download squeak + download_squeak(other_admin_stub, saved_squeak_hash) + time.sleep(5) + + # Download offer + download_offers(other_admin_stub, saved_squeak_hash) + time.sleep(5) + + # Get the squeak display item + get_squeak_display_entry = get_squeak_display( + other_admin_stub, saved_squeak_hash) + assert ( + get_squeak_display_entry.content_str == "Hello from the profile on the server!" + ) + + def test_download_single_squeak( admin_stub, other_admin_stub, diff --git a/squeaknode/network/peer_message_handler.py b/squeaknode/network/peer_message_handler.py index cb4029cb..61bb2040 100644 --- a/squeaknode/network/peer_message_handler.py +++ b/squeaknode/network/peer_message_handler.py @@ -27,6 +27,7 @@ from squeak.messages import msg_inv from squeak.messages import msg_notfound from squeak.messages import msg_offer from squeak.messages import msg_pong +from squeak.messages import msg_secretkey from squeak.messages import msg_squeak from squeak.net import CInv @@ -204,15 +205,46 @@ class PeerMessageHandler: if squeak is not None: return msg_squeak(squeak=squeak) if inv.type == 2: - offer = self.squeak_controller.get_buy_offer( - squeak_hash=inv.hash, - peer_address=self.peer.remote_address, + offer_or_secret_key = self.squeak_controller.get_offer_or_secret_key( + inv.hash, + self.peer.remote_address, ) - if offer is not None: + if offer_or_secret_key is None: + return None + elif type(offer_or_secret_key) is bytes: + return msg_secretkey( + hashSqk=inv.hash, + secretKey=offer_or_secret_key, + ) + elif type(offer_or_secret_key) is Offer: return msg_offer( hashSqk=inv.hash, - nonce=offer.nonce, - strPaymentInfo=offer.payment_request.encode('utf-8'), - host=offer.host.encode('utf-8'), - port=offer.port, + nonce=offer_or_secret_key.nonce, + strPaymentInfo=offer_or_secret_key.payment_request.encode( + 'utf-8'), + host=offer_or_secret_key.host.encode('utf-8'), + port=offer_or_secret_key.port, ) + + # price = self.squeak_controller.get_price_for_squeak(inv.hash) + # if price == 0: + # secret_key = self.squeak_controller.get_squeak_secret_key( + # inv.hash) + # if secret_key is not None: + # return msg_secretkey( + # hashSqk=inv.hash, + # secretKey=secret_key, + # ) + # else: + # offer = self.squeak_controller.get_buy_offer( + # squeak_hash=inv.hash, + # peer_address=self.peer.remote_address, + # ) + # if offer is not None: + # return msg_offer( + # hashSqk=inv.hash, + # nonce=offer.nonce, + # strPaymentInfo=offer.payment_request.encode('utf-8'), + # host=offer.host.encode('utf-8'), + # port=offer.port, + # ) diff --git a/squeaknode/node/squeak_controller.py b/squeaknode/node/squeak_controller.py index 7f868268..639164d7 100644 --- a/squeaknode/node/squeak_controller.py +++ b/squeaknode/node/squeak_controller.py @@ -23,6 +23,7 @@ import logging import threading from typing import List from typing import Optional +from typing import Union import sqlalchemy import squeak.params @@ -162,9 +163,21 @@ class SqueakController: def get_temporary_interest_counter(self, squeak: CSqueak) -> Optional[TemporaryInterest]: return self.temporary_interest_manager.lookup_counter(squeak) - def get_buy_offer(self, squeak_hash: bytes, peer_address: PeerAddress) -> Optional[Offer]: - # Check if there is an existing offer for the hash/peer_address combination - sent_offer = self.get_saved_sent_offer(squeak_hash, peer_address) + def get_offer_or_secret_key(self, squeak_hash: bytes, peer_address: PeerAddress) -> Optional[Union[bytes, Offer]]: + squeak = self.get_squeak(squeak_hash) + if squeak is None: + return None + price = self.get_price_for_squeak(squeak) + if price == 0: + return self.get_squeak_secret_key(squeak_hash) + else: + return self.get_offer( + squeak_hash=squeak_hash, + peer_address=peer_address, + ) + + def get_offer(self, squeak_hash: bytes, peer_address: PeerAddress) -> Optional[Offer]: + sent_offer = self.get_sent_offer_for_peer(squeak_hash, peer_address) if sent_offer is None: return None return self.squeak_core.package_offer( @@ -173,7 +186,7 @@ class SqueakController: self.config.lnd.port, ) - def get_saved_sent_offer(self, squeak_hash: bytes, peer_address: PeerAddress) -> Optional[SentOffer]: + def get_sent_offer_for_peer(self, squeak_hash: bytes, peer_address: PeerAddress) -> Optional[SentOffer]: # Check if there is an existing offer for the hash/peer_address combination sent_offer = self.squeak_db.get_sent_offer_by_squeak_hash_and_peer( squeak_hash, @@ -194,6 +207,17 @@ class SqueakController: self.squeak_db.insert_sent_offer(sent_offer) return sent_offer + def get_price_for_squeak(self, squeak: CSqueak) -> int: + squeak_address = str(squeak.GetAddress()) + logger.info( + "Looking for profile with address: {}".format(squeak_address)) + squeak_profile = self.get_squeak_profile_by_address(squeak_address) + logger.info( + "Checking price for squeak with profile: {}".format(squeak_profile)) + if squeak_profile is not None and squeak_profile.use_custom_price: + return squeak_profile.custom_price_msat + return self.config.node.price_msat + def create_signing_profile(self, profile_name: str) -> int: if len(profile_name) == 0: raise Exception( diff --git a/tests/node/test_squeak_controller.py b/tests/node/test_squeak_controller.py index 443046fc..b8c54c73 100644 --- a/tests/node/test_squeak_controller.py +++ b/tests/node/test_squeak_controller.py @@ -130,7 +130,7 @@ def test_nothing(): def test_get_buy_offer(squeak_controller): - assert squeak_controller.get_buy_offer is not None + assert squeak_controller.get_offer is not None def test_get_network_default(squeak_controller):