Browse Source

lnworker: fix rebalance_channels

fixes https://github.com/spesmilo/electrum/issues/8468
master
SomberNight 3 years ago
parent
commit
033ad0feb9
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 6
      electrum/commands.py
  2. 11
      electrum/lnworker.py
  3. 2
      electrum/submarine_swaps.py
  4. 4
      electrum/wallet.py

6
electrum/commands.py

@ -1269,7 +1269,11 @@ class Commands:
from_channel = wallet.lnworker.get_channel_by_scid(from_scid)
dest_channel = wallet.lnworker.get_channel_by_scid(dest_scid)
amount_sat = satoshis(amount)
success, log = await wallet.lnworker.rebalance_channels(from_channel, dest_channel, amount_sat * 1000)
success, log = await wallet.lnworker.rebalance_channels(
from_channel,
dest_channel,
amount_msat=amount_sat * 1000,
)
return {
'success': success,
'log': [x.formatted_tuple() for x in log]

11
electrum/lnworker.py

@ -1842,8 +1842,7 @@ class LNWallet(LNWorker):
self._bolt11_cache[payment_hash] = pair
return pair
def create_payment_info(self, amount_sat: Optional[int], write_to_disk=True) -> bytes:
amount_msat = amount_sat * 1000 if amount_sat else None
def create_payment_info(self, *, amount_msat: Optional[int], write_to_disk=True) -> bytes:
payment_preimage = os.urandom(32)
payment_hash = sha256(payment_preimage)
info = PaymentInfo(payment_hash, amount_msat, RECEIVED, PR_UNPAID)
@ -2289,17 +2288,19 @@ class LNWallet(LNWorker):
for chan, swap_recv_amount in suggestions:
return (chan, swap_recv_amount)
async def rebalance_channels(self, chan1, chan2, amount_msat):
async def rebalance_channels(self, chan1: Channel, chan2: Channel, *, amount_msat: int):
if chan1 == chan2:
raise Exception('Rebalance requires two different channels')
if self.uses_trampoline() and chan1.node_id == chan2.node_id:
raise Exception('Rebalance requires channels from different trampolines')
lnaddr, invoice = self.add_reqest(
payment_hash = self.create_payment_info(amount_msat=amount_msat)
lnaddr, invoice = self.get_bolt11_invoice(
payment_hash=payment_hash,
amount_msat=amount_msat,
message='rebalance',
expiry=3600,
fallback_address=None,
channels = [chan2]
channels=[chan2],
)
return await self.pay_invoice(
invoice, channels=[chan1])

2
electrum/submarine_swaps.py

@ -262,7 +262,7 @@ class SwapManager(Logger):
privkey = os.urandom(32)
pubkey = ECPrivkey(privkey).get_public_key_bytes(compressed=True)
amount_msat = lightning_amount_sat * 1000
payment_hash = self.lnworker.create_payment_info(lightning_amount_sat)
payment_hash = self.lnworker.create_payment_info(amount_msat=amount_msat)
lnaddr, invoice = self.lnworker.get_bolt11_invoice(
payment_hash=payment_hash,
amount_msat=amount_msat,

4
electrum/wallet.py

@ -2625,7 +2625,9 @@ class Abstract_Wallet(ABC, Logger, EventListener):
address = address or None # converts "" to None
exp_delay = exp_delay or 0
timestamp = int(Request._get_cur_time())
payment_hash = self.lnworker.create_payment_info(amount_sat, write_to_disk=False) if self.has_lightning() else None
payment_hash = None # type: Optional[bytes]
if self.has_lightning():
payment_hash = self.lnworker.create_payment_info(amount_msat=amount_sat * 1000, write_to_disk=False)
outputs = [ PartialTxOutput.from_address_and_value(address, amount_sat)] if address else []
height = self.adb.get_local_height()
req = Request(

Loading…
Cancel
Save