From 80a484ef3bda05c111e49549829a3a22a225fd64 Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Thu, 14 Apr 2022 18:17:26 -0400 Subject: [PATCH] Simplify get payment summary in controller (#2159) --- squeaknode/admin/messages.py | 14 ++--- .../admin/squeak_admin_server_handler.py | 20 ++----- squeaknode/core/payment_summary.py | 31 +++++++++++ squeaknode/node/squeak_controller.py | 55 ++++++++++++++----- tests/admin/test_messages.py | 6 +- tests/conftest.py | 12 ++++ 6 files changed, 98 insertions(+), 40 deletions(-) create mode 100644 squeaknode/core/payment_summary.py diff --git a/squeaknode/admin/messages.py b/squeaknode/admin/messages.py index 848ea545..fc045081 100644 --- a/squeaknode/admin/messages.py +++ b/squeaknode/admin/messages.py @@ -28,14 +28,13 @@ from proto import squeak_admin_pb2 from squeaknode.admin.profile_image_util import bytes_to_base64_string from squeaknode.admin.profile_image_util import load_default_profile_image from squeaknode.core.download_result import DownloadResult +from squeaknode.core.payment_summary import PaymentSummary from squeaknode.core.peer_address import Network from squeaknode.core.peer_address import PeerAddress from squeaknode.core.received_offer import ReceivedOffer from squeaknode.core.received_payment import ReceivedPayment -from squeaknode.core.received_payment_summary import ReceivedPaymentSummary from squeaknode.core.sent_offer import SentOffer from squeaknode.core.sent_payment import SentPayment -from squeaknode.core.sent_payment_summary import SentPaymentSummary from squeaknode.core.squeak_entry import SqueakEntry from squeaknode.core.squeak_peer import SqueakPeer from squeaknode.core.squeak_profile import SqueakProfile @@ -149,14 +148,13 @@ def received_payment_to_message(received_payment: ReceivedPayment) -> squeak_adm def payment_summary_to_message( - received_payment_summary: ReceivedPaymentSummary, - sent_payment_summary: SentPaymentSummary, + payment_summary: PaymentSummary, ) -> squeak_admin_pb2.PaymentSummary: return squeak_admin_pb2.PaymentSummary( - num_received_payments=received_payment_summary.num_received_payments, - num_sent_payments=sent_payment_summary.num_sent_payments, - amount_earned_msat=received_payment_summary.total_amount_received_msat, - amount_spent_msat=sent_payment_summary.total_amount_sent_msat, + num_received_payments=payment_summary.received_payment_summary.num_received_payments, + num_sent_payments=payment_summary.sent_payment_summary.num_sent_payments, + amount_earned_msat=payment_summary.received_payment_summary.total_amount_received_msat, + amount_spent_msat=payment_summary.sent_payment_summary.total_amount_sent_msat, ) diff --git a/squeaknode/admin/squeak_admin_server_handler.py b/squeaknode/admin/squeak_admin_server_handler.py index 8b648248..b72296b6 100644 --- a/squeaknode/admin/squeak_admin_server_handler.py +++ b/squeaknode/admin/squeak_admin_server_handler.py @@ -942,11 +942,9 @@ class SqueakAdminServerHandler(object): def handle_get_payment_summary(self, request): logger.info("Handle get payment summary") - received_payment_summary = self.squeak_controller.get_received_payment_summary() - sent_payment_summary = self.squeak_controller.get_sent_payment_summary() + payment_summary = self.squeak_controller.get_payment_summary() payment_summary_msg = payment_summary_to_message( - received_payment_summary, - sent_payment_summary, + payment_summary, ) return squeak_admin_pb2.GetPaymentSummaryReply( payment_summary=payment_summary_msg, @@ -958,13 +956,10 @@ class SqueakAdminServerHandler(object): logger.info("Handle get payment summary for squeak hash: {}".format( squeak_hash_str, )) - received_payment_summary = self.squeak_controller.get_received_payment_summary_for_squeak( - squeak_hash) - sent_payment_summary = self.squeak_controller.get_sent_payment_summary_for_squeak( + payment_summary = self.squeak_controller.get_payment_summary_for_squeak( squeak_hash) payment_summary_msg = payment_summary_to_message( - received_payment_summary, - sent_payment_summary, + payment_summary, ) return squeak_admin_pb2.GetPaymentSummaryForSqueakReply( payment_summary=payment_summary_msg, @@ -976,13 +971,10 @@ class SqueakAdminServerHandler(object): public_key_hex, )) public_key = SqueakPublicKey.from_bytes(bytes.fromhex(public_key_hex)) - received_payment_summary = self.squeak_controller.get_received_payment_summary_for_pubkey( - public_key) - sent_payment_summary = self.squeak_controller.get_sent_payment_summary_for_pubkey( + payment_summary = self.squeak_controller.get_payment_summary_for_pubkey( public_key) payment_summary_msg = payment_summary_to_message( - received_payment_summary, - sent_payment_summary, + payment_summary, ) return squeak_admin_pb2.GetPaymentSummaryForPubkeyReply( payment_summary=payment_summary_msg, diff --git a/squeaknode/core/payment_summary.py b/squeaknode/core/payment_summary.py new file mode 100644 index 00000000..94bec1ea --- /dev/null +++ b/squeaknode/core/payment_summary.py @@ -0,0 +1,31 @@ +# MIT License +# +# Copyright (c) 2020 Jonathan Zernik +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +from typing import NamedTuple + +from squeaknode.core.received_payment_summary import ReceivedPaymentSummary +from squeaknode.core.sent_payment_summary import SentPaymentSummary + + +class PaymentSummary(NamedTuple): + """Represents information about payments sent and received.""" + sent_payment_summary: SentPaymentSummary + received_payment_summary: ReceivedPaymentSummary diff --git a/squeaknode/node/squeak_controller.py b/squeaknode/node/squeak_controller.py index d8ae2333..2229e724 100644 --- a/squeaknode/node/squeak_controller.py +++ b/squeaknode/node/squeak_controller.py @@ -31,13 +31,12 @@ from squeak.core.keys import SqueakPublicKey from squeaknode.core.download_result import DownloadResult from squeaknode.core.lightning_address import LightningAddressHostPort from squeaknode.core.offer import Offer +from squeaknode.core.payment_summary import PaymentSummary from squeaknode.core.peer_address import Network from squeaknode.core.peer_address import PeerAddress from squeaknode.core.received_offer import ReceivedOffer from squeaknode.core.received_payment import ReceivedPayment -from squeaknode.core.received_payment_summary import ReceivedPaymentSummary from squeaknode.core.sent_payment import SentPayment -from squeaknode.core.sent_payment_summary import SentPaymentSummary from squeaknode.core.squeak_entry import SqueakEntry from squeaknode.core.squeak_peer import SqueakPeer from squeaknode.core.squeak_profile import SqueakProfile @@ -370,23 +369,51 @@ class SqueakController: last_entry, ) - def get_received_payment_summary(self) -> ReceivedPaymentSummary: - return self.squeak_store.get_received_payment_summary() + def get_payment_summary(self) -> PaymentSummary: + received_payment_summary = self.squeak_store.get_received_payment_summary() + sent_payment_summary = self.squeak_store.get_sent_payment_summary() + return PaymentSummary( + sent_payment_summary=sent_payment_summary, + received_payment_summary=received_payment_summary, + ) - def get_received_payment_summary_for_squeak(self, squeak_hash: bytes) -> ReceivedPaymentSummary: - return self.squeak_store.get_received_payment_summary_for_squeak(squeak_hash) + def get_payment_summary_for_squeak(self, squeak_hash: bytes) -> PaymentSummary: + received_payment_summary = self.squeak_store.get_received_payment_summary_for_squeak( + squeak_hash) + sent_payment_summary = self.squeak_store.get_sent_payment_summary_for_squeak( + squeak_hash) + return PaymentSummary( + sent_payment_summary=sent_payment_summary, + received_payment_summary=received_payment_summary, + ) - def get_received_payment_summary_for_pubkey(self, pubkey: SqueakPublicKey) -> ReceivedPaymentSummary: - return self.squeak_store.get_received_payment_summary_for_pubkey(pubkey) + def get_payment_summary_for_pubkey(self, pubkey: SqueakPublicKey) -> PaymentSummary: + received_payment_summary = self.squeak_store.get_received_payment_summary_for_pubkey( + pubkey) + sent_payment_summary = self.squeak_store.get_sent_payment_summary_for_pubkey( + pubkey) + return PaymentSummary( + sent_payment_summary=sent_payment_summary, + received_payment_summary=received_payment_summary, + ) - def get_sent_payment_summary(self) -> SentPaymentSummary: - return self.squeak_store.get_sent_payment_summary() + # def get_received_payment_summary(self) -> ReceivedPaymentSummary: + # return self.squeak_store.get_received_payment_summary() - def get_sent_payment_summary_for_squeak(self, squeak_hash: bytes) -> SentPaymentSummary: - return self.squeak_store.get_sent_payment_summary_for_squeak(squeak_hash) + # def get_received_payment_summary_for_squeak(self, squeak_hash: bytes) -> ReceivedPaymentSummary: + # return self.squeak_store.get_received_payment_summary_for_squeak(squeak_hash) - def get_sent_payment_summary_for_pubkey(self, pubkey: SqueakPublicKey) -> SentPaymentSummary: - return self.squeak_store.get_sent_payment_summary_for_pubkey(pubkey) + # def get_received_payment_summary_for_pubkey(self, pubkey: SqueakPublicKey) -> ReceivedPaymentSummary: + # return self.squeak_store.get_received_payment_summary_for_pubkey(pubkey) + + # def get_sent_payment_summary(self) -> SentPaymentSummary: + # return self.squeak_store.get_sent_payment_summary() + + # def get_sent_payment_summary_for_squeak(self, squeak_hash: bytes) -> SentPaymentSummary: + # return self.squeak_store.get_sent_payment_summary_for_squeak(squeak_hash) + + # def get_sent_payment_summary_for_pubkey(self, pubkey: SqueakPublicKey) -> SentPaymentSummary: + # return self.squeak_store.get_sent_payment_summary_for_pubkey(pubkey) def reprocess_received_payments(self) -> None: self.squeak_store.clear_received_payment_settle_indices() diff --git a/tests/admin/test_messages.py b/tests/admin/test_messages.py index f7130e05..b3e0046b 100644 --- a/tests/admin/test_messages.py +++ b/tests/admin/test_messages.py @@ -131,13 +131,11 @@ def test_message_to_sent_payment(sent_payment, sent_payment_msg): def test_payment_summary_to_message( - received_payment_summary, - sent_payment_summary, + payment_summary, payment_summary_msg, ): msg = payment_summary_to_message( - received_payment_summary, - sent_payment_summary, + payment_summary, ) assert msg == payment_summary_msg diff --git a/tests/conftest.py b/tests/conftest.py index 95a71634..e5eaedd0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -28,6 +28,7 @@ from squeaknode.bitcoin.block_info import BlockInfo from squeaknode.core.download_result import DownloadResult from squeaknode.core.lightning_address import LightningAddressHostPort from squeaknode.core.offer import Offer +from squeaknode.core.payment_summary import PaymentSummary from squeaknode.core.peer_address import Network from squeaknode.core.peer_address import PeerAddress from squeaknode.core.received_offer import ReceivedOffer @@ -556,6 +557,17 @@ def sent_payment_summary( ) +@pytest.fixture +def payment_summary( + received_payment_summary, + sent_payment_summary, +): + yield PaymentSummary( + sent_payment_summary=sent_payment_summary, + received_payment_summary=received_payment_summary, + ) + + @pytest.fixture def download_result(): yield DownloadResult(