1
0

Use attr.s classes for invoices and requests:

- storage upgrade
 - fixes #6192
 - add can_pay_invoice, can_receive_invoice to lnworker
This commit is contained in:
ThomasV
2020-05-31 12:49:49 +02:00
parent 5f527720cf
commit 6058829870
21 changed files with 490 additions and 371 deletions

View File

@@ -24,8 +24,8 @@ from aiorpcx import run_in_thread
from . import constants, util
from . import keystore
from .util import profiler
from .util import PR_UNPAID, PR_EXPIRED, PR_PAID, PR_INFLIGHT, PR_FAILED, PR_ROUTING
from .util import PR_TYPE_LN, NetworkRetryManager
from .invoices import PR_TYPE_LN, PR_UNPAID, PR_EXPIRED, PR_PAID, PR_INFLIGHT, PR_FAILED, PR_ROUTING, LNInvoice
from .util import NetworkRetryManager
from .lnutil import LN_MAX_FUNDING_SAT
from .keystore import BIP32_KeyStore
from .bitcoin import COIN
@@ -1102,15 +1102,7 @@ class LNWallet(LNWorker):
payment_secret=derive_payment_secret_from_payment_preimage(payment_preimage))
invoice = lnencode(lnaddr, self.node_keypair.privkey)
key = bh2u(lnaddr.paymenthash)
req = {
'type': PR_TYPE_LN,
'amount': amount_sat,
'time': lnaddr.date,
'exp': expiry,
'message': message,
'rhash': key,
'invoice': invoice
}
req = LNInvoice.from_bech32(invoice)
self.save_preimage(payment_hash, payment_preimage)
self.save_payment_info(info)
self.wallet.add_payment_request(req)
@@ -1145,7 +1137,8 @@ class LNWallet(LNWorker):
info = self.get_payment_info(payment_hash)
return info.status if info else PR_UNPAID
def get_invoice_status(self, key):
def get_invoice_status(self, invoice):
key = invoice.rhash
log = self.logs[key]
if key in self.is_routing:
return PR_ROUTING
@@ -1285,6 +1278,12 @@ class LNWallet(LNWorker):
return Decimal(max(chan.available_to_spend(REMOTE) if chan.is_open() else 0
for chan in self.channels.values()))/1000 if self.channels else 0
def can_pay_invoice(self, invoice):
return invoice.amount <= self.num_sats_can_send()
def can_receive_invoice(self, invoice):
return invoice.amount <= self.num_sats_can_receive()
async def close_channel(self, chan_id):
chan = self._channels[chan_id]
peer = self._peers[chan.node_id]