From 080ee1eac809cd4632790d17612f78ff02ab38d0 Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Sun, 10 Jan 2021 14:56:23 -0800 Subject: [PATCH] Remove received payments verifier (#591) * Got received payments working without verifier class * Process received payments works with better exception handling * Delete sent offers verifier --- squeaknode/core/squeak_controller.py | 28 ++++++++++++---- squeaknode/core/squeak_core.py | 44 ++++++++++--------------- squeaknode/node/sent_offers_verifier.py | 37 --------------------- 3 files changed, 38 insertions(+), 71 deletions(-) delete mode 100644 squeaknode/node/sent_offers_verifier.py diff --git a/squeaknode/core/squeak_controller.py b/squeaknode/core/squeak_controller.py index 721549e7..4992436c 100644 --- a/squeaknode/core/squeak_controller.py +++ b/squeaknode/core/squeak_controller.py @@ -1,4 +1,5 @@ import logging +import time from typing import List from squeak.core import CheckSqueak @@ -8,13 +9,13 @@ from squeak.core.signing import CSqueakAddress from proto import squeak_server_pb2 from squeaknode.core.offer import Offer +from squeaknode.core.sent_offer import SentOffer from squeaknode.core.squeak_address_validator import SqueakAddressValidator from squeaknode.core.squeak_peer import SqueakPeer from squeaknode.core.squeak_profile import SqueakProfile from squeaknode.node.received_payments_subscription_client import ( OpenReceivedPaymentsSubscriptionClient, ) -from squeaknode.node.sent_offers_verifier import SentOffersVerifier logger = logging.getLogger(__name__) @@ -32,10 +33,6 @@ class SqueakController: self.squeak_core = squeak_core self.squeak_store = squeak_store self.squeak_whitelist = squeak_whitelist - self.sent_offers_verifier = SentOffersVerifier( - self.squeak_db, - self.squeak_core, - ) self.config = config def save_uploaded_squeak(self, squeak: CSqueak): @@ -250,8 +247,25 @@ class SqueakController: num_expired_sent_offers) ) - def process_subscribed_invoices(self): - self.sent_offers_verifier.process_subscribed_invoices() + def process_subscribed_invoices(self, retry_s: int = 10): + def get_sent_offer_for_payment_hash(payment_hash: bytes) -> SentOffer: + return self.squeak_db.get_sent_offer_by_payment_hash( + payment_hash + ) + while True: + try: + latest_settle_index = self.squeak_db.get_latest_settle_index() or 0 + for received_payment in self.squeak_core.get_received_payments( + get_sent_offer_for_payment_hash, + latest_settle_index, + ): + self.squeak_db.insert_received_payment(received_payment) + except Exception: + logger.info( + "Unable to subscribe invoices from lnd. Retrying in " + "{} seconds.".format(retry_s), + ) + time.sleep(retry_s) def subscribe_received_payments(self, initial_index: int): with OpenReceivedPaymentsSubscriptionClient( diff --git a/squeaknode/core/squeak_core.py b/squeaknode/core/squeak_core.py index 3239a720..b0f26291 100644 --- a/squeaknode/core/squeak_core.py +++ b/squeaknode/core/squeak_core.py @@ -160,34 +160,24 @@ class SqueakCore: node_pubkey=offer.destination, ) - def get_received_payments(self, get_sent_offer_fn, latest_settle_index, retry_s) -> Iterator[ReceivedPayment]: - try: - for invoice in self.lightning_client.subscribe_invoices( + def get_received_payments(self, get_sent_offer_fn, latest_settle_index) -> Iterator[ReceivedPayment]: + for invoice in self.lightning_client.subscribe_invoices( settle_index=latest_settle_index, - ): - if invoice.settled: - payment_hash = invoice.r_hash - settle_index = invoice.settle_index - # sent_offer = self.squeak_db.get_sent_offer_by_payment_hash( - # payment_hash) - sent_offer = get_sent_offer_fn(payment_hash) - received_payment = ReceivedPayment( - received_payment_id=None, - created=None, - squeak_hash=sent_offer.squeak_hash, - payment_hash=sent_offer.payment_hash, - price_msat=sent_offer.price_msat, - settle_index=settle_index, - client_addr=sent_offer.client_addr, - ) - # self.squeak_db.insert_received_payment(received_payment) - yield received_payment - except Exception: - logger.info( - "Unable to subscribe invoices from lnd. Retrying in " - "{} seconds.".format(retry_s), - ) - time.sleep(retry_s) + ): + if invoice.settled: + payment_hash = invoice.r_hash + settle_index = invoice.settle_index + sent_offer = get_sent_offer_fn(payment_hash) + received_payment = ReceivedPayment( + received_payment_id=None, + created=None, + squeak_hash=sent_offer.squeak_hash, + payment_hash=sent_offer.payment_hash, + price_msat=sent_offer.price_msat, + settle_index=settle_index, + client_addr=sent_offer.client_addr, + ) + yield received_payment def get_offer(self, squeak: CSqueak, offer_msg: squeak_server_pb2.SqueakBuyOffer, peer: SqueakPeer) -> Offer: if peer.peer_id is None: diff --git a/squeaknode/node/sent_offers_verifier.py b/squeaknode/node/sent_offers_verifier.py deleted file mode 100644 index 68ef42a3..00000000 --- a/squeaknode/node/sent_offers_verifier.py +++ /dev/null @@ -1,37 +0,0 @@ -import logging - -from squeaknode.core.sent_offer import SentOffer - - -logger = logging.getLogger(__name__) - -LND_CONNECT_RETRY_S = 10 - - -class SentOffersVerifier: - def __init__(self, squeak_db, squeak_core): - self.squeak_db = squeak_db - self.squeak_core = squeak_core - - def process_subscribed_invoices(self): - while True: - self.try_processing() - - def try_processing(self): - latest_settle_index = self._get_latest_settle_index() or 0 - - def get_sent_offer_for_payment_hash(payment_hash: bytes) -> SentOffer: - return self.squeak_db.get_sent_offer_by_payment_hash( - payment_hash - ) - - for received_payment in self.squeak_core.get_received_payments( - get_sent_offer_for_payment_hash, - latest_settle_index, - LND_CONNECT_RETRY_S, - ): - self.squeak_db.insert_received_payment(received_payment) - - def _get_latest_settle_index(self): - logger.info("Getting latest settle index from db...") - return self.squeak_db.get_latest_settle_index()