From 408ec42340b5df5b717824f4408dcedcdebc2c8e Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Fri, 3 Sep 2021 10:45:00 -0700 Subject: [PATCH] Implement download squeaks rpc (#1168) * Implement download squeaks rpc * Handle replyto hash in download squeaks method --- itests/tests/test_squeak_node.py | 63 +++++++++---------- itests/tests/util.py | 9 ++- proto/squeak_admin.proto | 11 ++++ .../admin/squeak_admin_server_handler.py | 23 ++++++- squeaknode/node/squeak_controller.py | 26 +++++++- 5 files changed, 92 insertions(+), 40 deletions(-) diff --git a/itests/tests/test_squeak_node.py b/itests/tests/test_squeak_node.py index 1e5c35d7..9583cde8 100644 --- a/itests/tests/test_squeak_node.py +++ b/itests/tests/test_squeak_node.py @@ -38,6 +38,7 @@ from tests.util import delete_profile from tests.util import delete_squeak from tests.util import download_offers from tests.util import download_squeak +from tests.util import download_squeaks from tests.util import download_squeaks_for_address from tests.util import get_connected_peer from tests.util import get_connected_peers @@ -881,43 +882,39 @@ def test_connect_peer(admin_stub, other_admin_stub): print(item) assert len(item) == 0 -# TODO: Re-enable after DownloadSqueaks RPC method supports params. -# def test_get_squeak_by_lookup( -# admin_stub, -# other_admin_stub, -# connected_tcp_peer_id, -# lightning_client, -# signing_profile_id, -# saved_squeak_hash, -# ): -# # Get the squeak profile -# squeak_profile = get_squeak_profile(admin_stub, signing_profile_id) -# squeak_profile_address = squeak_profile.address -# squeak_profile_name = squeak_profile.profile_name -# # Add the contact profile to the other server and set the profile to be following -# contact_profile_id = create_contact_profile( -# other_admin_stub, squeak_profile_name, squeak_profile_address) -# other_admin_stub.SetSqueakProfileFollowing( -# squeak_admin_pb2.SetSqueakProfileFollowingRequest( -# profile_id=contact_profile_id, -# following=True, -# ) -# ) +def test_get_squeak_by_lookup( + admin_stub, + other_admin_stub, + connected_tcp_peer_id, + lightning_client, + signing_profile_id, + saved_squeak_hash, +): + # Get the squeak profile + squeak_profile = get_squeak_profile(admin_stub, signing_profile_id) + squeak_profile_address = squeak_profile.address + squeak_profile.profile_name -# # Get the squeak display item -# squeak_display_entry = get_squeak_display( -# other_admin_stub, saved_squeak_hash) -# assert squeak_display_entry is None + # Get the squeak display item + squeak_display_entry = get_squeak_display( + other_admin_stub, saved_squeak_hash) + assert squeak_display_entry is None -# # Sync squeaks -# download_squeaks(other_admin_stub) -# time.sleep(5) + # Sync squeaks + download_squeaks( + other_admin_stub, + [squeak_profile_address], + -1, + -1, + None, + ) + time.sleep(5) -# # Get the squeak display item -# squeak_display_entry = get_squeak_display( -# other_admin_stub, saved_squeak_hash) -# assert squeak_display_entry.squeak_hash == saved_squeak_hash + # Get the squeak display item + squeak_display_entry = get_squeak_display( + other_admin_stub, saved_squeak_hash) + assert squeak_display_entry.squeak_hash == saved_squeak_hash def test_subscribe_squeaks( diff --git a/itests/tests/util.py b/itests/tests/util.py index 4cb96cbb..335ee740 100644 --- a/itests/tests/util.py +++ b/itests/tests/util.py @@ -242,9 +242,14 @@ def download_squeak(node_stub, squeak_hash): ) -def download_squeaks(node_stub): +def download_squeaks(node_stub, addresses, min_block, max_block, reply_to): node_stub.DownloadSqueaks( - squeak_admin_pb2.DownloadSqueaksRequest(), + squeak_admin_pb2.DownloadSqueaksRequest( + addreses=addresses, + min_block_height=min_block, + max_block_height=max_block, + replyto_squeak_hash=reply_to, + ), ) diff --git a/proto/squeak_admin.proto b/proto/squeak_admin.proto index 23900d2b..dd0b155c 100644 --- a/proto/squeak_admin.proto +++ b/proto/squeak_admin.proto @@ -736,6 +736,17 @@ message OfferDisplayEntry { } message DownloadSqueaksRequest { + /// The interested addresses + repeated string addreses = 1; + + /// Minimum block height + int32 min_block_height = 2; + + /// Maximum block height + int32 max_block_height = 3; + + /// The hash replied to + string replyto_squeak_hash = 4; } message DownloadSqueaksReply { diff --git a/squeaknode/admin/squeak_admin_server_handler.py b/squeaknode/admin/squeak_admin_server_handler.py index 961dc557..e783fc7a 100644 --- a/squeaknode/admin/squeak_admin_server_handler.py +++ b/squeaknode/admin/squeak_admin_server_handler.py @@ -531,8 +531,27 @@ class SqueakAdminServerHandler(object): ) def handle_download_squeaks(self, request): - logger.info("Handle download squeaks") - self.squeak_controller.download_squeaks() + addresses = request.addreses + min_block = request.min_block_height + max_block = request.max_block_height + replyto_hash = request.replyto_squeak_hash + logger.info("""Handle download squeaks for + addreses: {} + min_block: {} + max_block: {} + replyto_hash: {} + """.format( + addresses, + min_block, + max_block, + replyto_hash, + )) + self.squeak_controller.download_squeaks( + addresses, + min_block, + max_block, + replyto_hash, + ) return squeak_admin_pb2.DownloadSqueaksReply() def handle_download_squeak(self, request): diff --git a/squeaknode/node/squeak_controller.py b/squeaknode/node/squeak_controller.py index 74d2e89f..afaffee0 100644 --- a/squeaknode/node/squeak_controller.py +++ b/squeaknode/node/squeak_controller.py @@ -580,9 +580,29 @@ class SqueakController: vInterested=interests, ) - def download_squeaks(self): - # TODO: Don't use get interested locator, instead use params from request. - locator = self.get_interested_locator() + def download_squeaks( + self, + addresses: List[str], + min_block: int, + max_block: int, + replyto_hash: Optional[bytes], + ): + interest = CInterested( + addresses=[CSqueakAddress(address) + for address in addresses], + nMinBlockHeight=min_block, + nMaxBlockHeight=max_block, + replyto_squeak_hash=replyto_hash, + ) if replyto_hash else CInterested( + addresses=[CSqueakAddress(address) + for address in addresses], + nMinBlockHeight=min_block, + nMaxBlockHeight=max_block, + ) + self.temporary_interest_manager.add_range_interest(10, interest) + locator = CSqueakLocator( + vInterested=[interest], + ) getsqueaks_msg = msg_getsqueaks( locator=locator, )