-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpayment_processor.py
More file actions
140 lines (123 loc) · 4.95 KB
/
Copy pathpayment_processor.py
File metadata and controls
140 lines (123 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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.
import logging
import threading
from typing import Optional
from squeaknode.core.exception import InvoiceSubscriptionError
from squeaknode.core.received_payment import ReceivedPayment
from squeaknode.core.sent_offer import SentOffer
logger = logging.getLogger(__name__)
class PaymentProcessor:
def __init__(
self,
squeak_db,
squeak_core,
retry_s: int,
):
self.squeak_db = squeak_db
self.squeak_core = squeak_core
self.retry_s = retry_s
self.lock = threading.Lock()
self.current_task = None
def start_processing(self):
with self.lock:
if self.current_task is not None:
self.current_task.stop_processing()
self.current_task = PaymentProcessorTask(
self.squeak_db,
self.squeak_core,
self.retry_s,
)
self.current_task.start_processing()
def stop_processing(self):
with self.lock:
if self.current_task is not None:
self.current_task.stop_processing()
class PaymentProcessorTask:
def __init__(
self,
squeak_db,
squeak_core,
retry_s: int,
):
self.squeak_db = squeak_db
self.squeak_core = squeak_core
self.retry_s = retry_s
self.stopped = threading.Event()
self.payments_result = None
def start_processing(self):
logger.info("Starting payment processor task.")
threading.Thread(
target=self.process_subscribed_invoices,
).start()
def stop_processing(self):
logger.info("Stopping payment processor task.")
self.stopped.set()
if self.payments_result is not None:
self.payments_result.cancel_fn()
def process_subscribed_invoices(self):
while not self.stopped.is_set():
try:
# Use zero as starting settle index, so that no invoices are skipped.
# latest_settle_index = self.get_latest_settle_index()
latest_settle_index = 0
logger.info("Starting payment subscription with settle index: {}".format(
latest_settle_index,
))
self.payments_result = self.squeak_core.get_received_payments(
latest_settle_index,
self.get_sent_offer_for_payment_hash,
)
if self.stopped.is_set():
self.payments_result.cancel_fn()
for received_payment in self.payments_result.result_stream:
self.handle_received_payment(received_payment)
except InvoiceSubscriptionError:
logger.error(
"Unable to subscribe invoices. Retrying in {} seconds...".format(
self.retry_s,
),
)
self.stopped.wait(self.retry_s)
def get_latest_settle_index(self) -> int:
return self.squeak_db.get_latest_settle_index() or 0
def get_sent_offer_for_payment_hash(self, payment_hash: bytes) -> Optional[SentOffer]:
return self.squeak_db.get_sent_offer_by_payment_hash(
payment_hash
)
def handle_received_payment(self, received_payment: ReceivedPayment):
logger.info(
"Got received payment: {}".format(received_payment))
received_payment_id = self.squeak_db.insert_received_payment(
received_payment,
)
if received_payment_id is not None:
logger.debug(
"Saved received payment: {}".format(received_payment))
# # TODO: Should not be deleting sent offer.
# self.squeak_db.delete_sent_offer(
# received_payment.payment_hash,
# )
self.squeak_db.set_sent_offer_paid(
received_payment.payment_hash,
paid=True,
)